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