OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/sync/glue/sync_backend_host.h" | 5 #include "chrome/browser/sync/glue/sync_backend_host.h" |
6 | 6 |
7 #include <cstddef> | 7 #include <cstddef> |
8 | 8 |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 io_thread_(BrowserThread::IO), | 139 io_thread_(BrowserThread::IO), |
140 fake_manager_(NULL) {} | 140 fake_manager_(NULL) {} |
141 | 141 |
142 virtual ~SyncBackendHostTest() {} | 142 virtual ~SyncBackendHostTest() {} |
143 | 143 |
144 virtual void SetUp() OVERRIDE { | 144 virtual void SetUp() OVERRIDE { |
145 io_thread_.StartIOThread(); | 145 io_thread_.StartIOThread(); |
146 profile_.reset(new TestingProfile()); | 146 profile_.reset(new TestingProfile()); |
147 profile_->CreateRequestContext(); | 147 profile_->CreateRequestContext(); |
148 sync_prefs_.reset(new SyncPrefs(profile_->GetPrefs())); | 148 sync_prefs_.reset(new SyncPrefs(profile_->GetPrefs())); |
149 invalidator_storage_.reset(new invalidation::InvalidatorStorage( | |
150 profile_->GetPrefs())); | |
151 backend_.reset(new SyncBackendHost( | 149 backend_.reset(new SyncBackendHost( |
152 profile_->GetDebugName(), | 150 profile_->GetDebugName(), |
153 profile_.get(), | 151 profile_.get(), |
154 sync_prefs_->AsWeakPtr(), | 152 sync_prefs_->AsWeakPtr())); |
155 invalidator_storage_->AsWeakPtr())); | |
156 credentials_.email = "user@example.com"; | 153 credentials_.email = "user@example.com"; |
157 credentials_.sync_token = "sync_token"; | 154 credentials_.sync_token = "sync_token"; |
158 | 155 |
159 // These types are always implicitly enabled. | 156 // These types are always implicitly enabled. |
160 enabled_types_.PutAll(syncer::ControlTypes()); | 157 enabled_types_.PutAll(syncer::ControlTypes()); |
161 | 158 |
162 // NOTE: We can't include Passwords or Typed URLs due to the Sync Backend | 159 // NOTE: We can't include Passwords or Typed URLs due to the Sync Backend |
163 // Registrar removing them if it can't find their model workers. | 160 // Registrar removing them if it can't find their model workers. |
164 enabled_types_.Put(syncer::BOOKMARKS); | 161 enabled_types_.Put(syncer::BOOKMARKS); |
165 enabled_types_.Put(syncer::NIGORI); | 162 enabled_types_.Put(syncer::NIGORI); |
166 enabled_types_.Put(syncer::DEVICE_INFO); | 163 enabled_types_.Put(syncer::DEVICE_INFO); |
167 enabled_types_.Put(syncer::PREFERENCES); | 164 enabled_types_.Put(syncer::PREFERENCES); |
168 enabled_types_.Put(syncer::SESSIONS); | 165 enabled_types_.Put(syncer::SESSIONS); |
169 enabled_types_.Put(syncer::SEARCH_ENGINES); | 166 enabled_types_.Put(syncer::SEARCH_ENGINES); |
170 enabled_types_.Put(syncer::AUTOFILL); | 167 enabled_types_.Put(syncer::AUTOFILL); |
171 enabled_types_.Put(syncer::EXPERIMENTS); | 168 enabled_types_.Put(syncer::EXPERIMENTS); |
172 } | 169 } |
173 | 170 |
174 virtual void TearDown() OVERRIDE { | 171 virtual void TearDown() OVERRIDE { |
175 if (backend_) { | 172 if (backend_) { |
176 backend_->StopSyncingForShutdown(); | 173 backend_->StopSyncingForShutdown(); |
177 backend_->Shutdown(false); | 174 backend_->Shutdown(false); |
178 } | 175 } |
179 backend_.reset(); | 176 backend_.reset(); |
180 sync_prefs_.reset(); | 177 sync_prefs_.reset(); |
181 invalidator_storage_.reset(); | |
182 profile_.reset(); | 178 profile_.reset(); |
183 // Pump messages posted by the sync thread (which may end up | 179 // Pump messages posted by the sync thread (which may end up |
184 // posting on the IO thread). | 180 // posting on the IO thread). |
185 ui_loop_.RunUntilIdle(); | 181 ui_loop_.RunUntilIdle(); |
186 io_thread_.Stop(); | 182 io_thread_.Stop(); |
187 // Pump any messages posted by the IO thread. | 183 // Pump any messages posted by the IO thread. |
188 ui_loop_.RunUntilIdle(); | 184 ui_loop_.RunUntilIdle(); |
189 } | 185 } |
190 | 186 |
191 // Synchronously initializes the backend. | 187 // Synchronously initializes the backend. |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 } | 253 } |
258 | 254 |
259 MessageLoop ui_loop_; | 255 MessageLoop ui_loop_; |
260 content::TestBrowserThread ui_thread_; | 256 content::TestBrowserThread ui_thread_; |
261 content::TestBrowserThread io_thread_; | 257 content::TestBrowserThread io_thread_; |
262 StrictMock<MockSyncFrontend> mock_frontend_; | 258 StrictMock<MockSyncFrontend> mock_frontend_; |
263 syncer::SyncCredentials credentials_; | 259 syncer::SyncCredentials credentials_; |
264 syncer::TestUnrecoverableErrorHandler handler_; | 260 syncer::TestUnrecoverableErrorHandler handler_; |
265 scoped_ptr<TestingProfile> profile_; | 261 scoped_ptr<TestingProfile> profile_; |
266 scoped_ptr<SyncPrefs> sync_prefs_; | 262 scoped_ptr<SyncPrefs> sync_prefs_; |
267 scoped_ptr<invalidation::InvalidatorStorage> invalidator_storage_; | |
268 scoped_ptr<SyncBackendHost> backend_; | 263 scoped_ptr<SyncBackendHost> backend_; |
269 FakeSyncManager* fake_manager_; | 264 FakeSyncManager* fake_manager_; |
270 FakeSyncManagerFactory fake_manager_factory_; | 265 FakeSyncManagerFactory fake_manager_factory_; |
271 syncer::ModelTypeSet enabled_types_; | 266 syncer::ModelTypeSet enabled_types_; |
272 }; | 267 }; |
273 | 268 |
274 // Test basic initialization with no initial types (first time initialization). | 269 // Test basic initialization with no initial types (first time initialization). |
275 // Only the nigori should be configured. | 270 // Only the nigori should be configured. |
276 TEST_F(SyncBackendHostTest, InitShutdown) { | 271 TEST_F(SyncBackendHostTest, InitShutdown) { |
277 InitializeBackend(true); | 272 InitializeBackend(true); |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 EXPECT_TRUE(fake_manager_->GetAndResetDownloadedTypes().Equals( | 580 EXPECT_TRUE(fake_manager_->GetAndResetDownloadedTypes().Equals( |
586 Union(new_types, partial_types))); | 581 Union(new_types, partial_types))); |
587 EXPECT_TRUE(Intersection(fake_manager_->GetAndResetCleanedTypes(), | 582 EXPECT_TRUE(Intersection(fake_manager_->GetAndResetCleanedTypes(), |
588 enabled_types_).Empty()); | 583 enabled_types_).Empty()); |
589 EXPECT_TRUE(fake_manager_->InitialSyncEndedTypes().Equals(enabled_types_)); | 584 EXPECT_TRUE(fake_manager_->InitialSyncEndedTypes().Equals(enabled_types_)); |
590 EXPECT_TRUE(fake_manager_->GetAndResetEnabledTypes().Equals(enabled_types_)); | 585 EXPECT_TRUE(fake_manager_->GetAndResetEnabledTypes().Equals(enabled_types_)); |
591 EXPECT_TRUE(fake_manager_->GetTypesWithEmptyProgressMarkerToken( | 586 EXPECT_TRUE(fake_manager_->GetTypesWithEmptyProgressMarkerToken( |
592 enabled_types_).Empty()); | 587 enabled_types_).Empty()); |
593 } | 588 } |
594 | 589 |
595 // Register for some IDs and trigger an invalidation. This should | |
596 // propagate all the way to the frontend. | |
597 TEST_F(SyncBackendHostTest, Invalidate) { | |
598 InitializeBackend(true); | |
599 | |
600 syncer::ObjectIdSet ids; | |
601 ids.insert(invalidation::ObjectId(1, "id1")); | |
602 ids.insert(invalidation::ObjectId(2, "id2")); | |
603 const syncer::ObjectIdInvalidationMap& invalidation_map = | |
604 syncer::ObjectIdSetToInvalidationMap(ids, "payload"); | |
605 | |
606 EXPECT_CALL( | |
607 mock_frontend_, | |
608 OnIncomingInvalidation(invalidation_map)) | |
609 .WillOnce(InvokeWithoutArgs(QuitMessageLoop)); | |
610 | |
611 backend_->UpdateRegisteredInvalidationIds(ids); | |
612 fake_manager_->Invalidate(invalidation_map); | |
613 ui_loop_.PostDelayedTask( | |
614 FROM_HERE, ui_loop_.QuitClosure(), TestTimeouts::action_timeout()); | |
615 ui_loop_.Run(); | |
616 } | |
617 | |
618 // Register for some IDs and update the invalidator state. This | |
619 // should propagate all the way to the frontend. | |
620 TEST_F(SyncBackendHostTest, UpdateInvalidatorState) { | |
621 InitializeBackend(true); | |
622 | |
623 EXPECT_CALL(mock_frontend_, | |
624 OnInvalidatorStateChange(syncer::INVALIDATIONS_ENABLED)) | |
625 .WillOnce(InvokeWithoutArgs(QuitMessageLoop)); | |
626 | |
627 syncer::ObjectIdSet ids; | |
628 ids.insert(invalidation::ObjectId(3, "id3")); | |
629 backend_->UpdateRegisteredInvalidationIds(ids); | |
630 fake_manager_->UpdateInvalidatorState(syncer::INVALIDATIONS_ENABLED); | |
631 ui_loop_.PostDelayedTask( | |
632 FROM_HERE, ui_loop_.QuitClosure(), TestTimeouts::action_timeout()); | |
633 ui_loop_.Run(); | |
634 } | |
635 | |
636 // Call StopSyncingForShutdown() on the backend and fire some invalidations | |
637 // before calling Shutdown(). Then start up and shut down the backend again. | |
638 // Those notifications shouldn't propagate to the frontend. | |
639 TEST_F(SyncBackendHostTest, InvalidationsAfterStopSyncingForShutdown) { | |
640 InitializeBackend(true); | |
641 | |
642 syncer::ObjectIdSet ids; | |
643 ids.insert(invalidation::ObjectId(5, "id5")); | |
644 backend_->UpdateRegisteredInvalidationIds(ids); | |
645 | |
646 backend_->StopSyncingForShutdown(); | |
647 | |
648 // Should not trigger anything. | |
649 fake_manager_->UpdateInvalidatorState(syncer::TRANSIENT_INVALIDATION_ERROR); | |
650 fake_manager_->UpdateInvalidatorState(syncer::INVALIDATIONS_ENABLED); | |
651 const syncer::ObjectIdInvalidationMap& invalidation_map = | |
652 syncer::ObjectIdSetToInvalidationMap(ids, "payload"); | |
653 fake_manager_->Invalidate(invalidation_map); | |
654 | |
655 // Make sure the above calls take effect before we continue. | |
656 fake_manager_->WaitForSyncThread(); | |
657 | |
658 backend_->Shutdown(false); | |
659 backend_.reset(); | |
660 | |
661 TearDown(); | |
662 SetUp(); | |
663 } | |
664 | |
665 // Ensure the device info tracker is initialized properly on startup. | 590 // Ensure the device info tracker is initialized properly on startup. |
666 TEST_F(SyncBackendHostTest, InitializeDeviceInfo) { | 591 TEST_F(SyncBackendHostTest, InitializeDeviceInfo) { |
667 ASSERT_EQ(NULL, backend_->GetSyncedDeviceTracker()); | 592 ASSERT_EQ(NULL, backend_->GetSyncedDeviceTracker()); |
668 | 593 |
669 InitializeBackend(true); | 594 InitializeBackend(true); |
670 const SyncedDeviceTracker* device_tracker = | 595 const SyncedDeviceTracker* device_tracker = |
671 backend_->GetSyncedDeviceTracker(); | 596 backend_->GetSyncedDeviceTracker(); |
672 ASSERT_TRUE(device_tracker->ReadLocalDeviceInfo()); | 597 ASSERT_TRUE(device_tracker->ReadLocalDeviceInfo()); |
673 } | 598 } |
674 | 599 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
760 TEST_F(SyncBackendHostTest, DownloadControlTypesRestart) { | 685 TEST_F(SyncBackendHostTest, DownloadControlTypesRestart) { |
761 sync_prefs_->SetSyncSetupCompleted(); | 686 sync_prefs_->SetSyncSetupCompleted(); |
762 InitializeBackend(true); | 687 InitializeBackend(true); |
763 EXPECT_EQ(syncer::CONFIGURE_REASON_NEWLY_ENABLED_DATA_TYPE, | 688 EXPECT_EQ(syncer::CONFIGURE_REASON_NEWLY_ENABLED_DATA_TYPE, |
764 fake_manager_->GetAndResetConfigureReason()); | 689 fake_manager_->GetAndResetConfigureReason()); |
765 } | 690 } |
766 | 691 |
767 } // namespace | 692 } // namespace |
768 | 693 |
769 } // namespace browser_sync | 694 } // namespace browser_sync |
OLD | NEW |