| 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" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
| 13 #include "base/test/test_timeouts.h" | 13 #include "base/test/test_timeouts.h" |
| 14 #include "chrome/browser/chrome_notification_types.h" | 14 #include "chrome/browser/chrome_notification_types.h" |
| 15 #include "chrome/browser/invalidation/invalidator_storage.h" | 15 #include "chrome/browser/invalidation/invalidator_storage.h" |
| 16 #include "chrome/browser/prefs/pref_service_syncable.h" | 16 #include "chrome/browser/prefs/pref_service_syncable.h" |
| 17 #include "chrome/browser/sync/glue/device_info.h" | 17 #include "chrome/browser/sync/glue/device_info.h" |
| 18 #include "chrome/browser/sync/glue/synced_device_tracker.h" | 18 #include "chrome/browser/sync/glue/synced_device_tracker.h" |
| 19 #include "chrome/browser/sync/sync_prefs.h" | 19 #include "chrome/browser/sync/sync_prefs.h" |
| 20 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" |
| 21 #include "components/user_prefs/pref_registry_syncable.h" | 21 #include "components/user_prefs/pref_registry_syncable.h" |
| 22 #include "content/public/browser/notification_service.h" | 22 #include "content/public/browser/notification_service.h" |
| 23 #include "content/public/test/test_browser_thread.h" | 23 #include "content/public/test/test_browser_thread_bundle.h" |
| 24 #include "content/public/test/test_utils.h" |
| 24 #include "google/cacheinvalidation/include/types.h" | 25 #include "google/cacheinvalidation/include/types.h" |
| 25 #include "net/url_request/test_url_fetcher_factory.h" | 26 #include "net/url_request/test_url_fetcher_factory.h" |
| 26 #include "sync/internal_api/public/base/model_type.h" | 27 #include "sync/internal_api/public/base/model_type.h" |
| 27 #include "sync/internal_api/public/engine/model_safe_worker.h" | 28 #include "sync/internal_api/public/engine/model_safe_worker.h" |
| 28 #include "sync/internal_api/public/sync_manager_factory.h" | 29 #include "sync/internal_api/public/sync_manager_factory.h" |
| 29 #include "sync/internal_api/public/test/fake_sync_manager.h" | 30 #include "sync/internal_api/public/test/fake_sync_manager.h" |
| 30 #include "sync/internal_api/public/util/experiments.h" | 31 #include "sync/internal_api/public/util/experiments.h" |
| 31 #include "sync/notifier/invalidator_state.h" | 32 #include "sync/notifier/invalidator_state.h" |
| 32 #include "sync/notifier/object_id_invalidation_map_test_util.h" | 33 #include "sync/notifier/object_id_invalidation_map_test_util.h" |
| 33 #include "sync/protocol/encryption.pb.h" | 34 #include "sync/protocol/encryption.pb.h" |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 private: | 129 private: |
| 129 syncer::ModelTypeSet initial_sync_ended_types_; | 130 syncer::ModelTypeSet initial_sync_ended_types_; |
| 130 syncer::ModelTypeSet progress_marker_types_; | 131 syncer::ModelTypeSet progress_marker_types_; |
| 131 syncer::ModelTypeSet configure_fail_types_; | 132 syncer::ModelTypeSet configure_fail_types_; |
| 132 FakeSyncManager* fake_manager_; | 133 FakeSyncManager* fake_manager_; |
| 133 }; | 134 }; |
| 134 | 135 |
| 135 class SyncBackendHostTest : public testing::Test { | 136 class SyncBackendHostTest : public testing::Test { |
| 136 protected: | 137 protected: |
| 137 SyncBackendHostTest() | 138 SyncBackendHostTest() |
| 138 : ui_thread_(BrowserThread::UI, &ui_loop_), | 139 : thread_bundle_(content::TestBrowserThreadBundle::REAL_IO_THREAD), |
| 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(); | |
| 146 profile_.reset(new TestingProfile()); | 145 profile_.reset(new TestingProfile()); |
| 147 profile_->CreateRequestContext(); | |
| 148 sync_prefs_.reset(new SyncPrefs(profile_->GetPrefs())); | 146 sync_prefs_.reset(new SyncPrefs(profile_->GetPrefs())); |
| 149 backend_.reset(new SyncBackendHost( | 147 backend_.reset(new SyncBackendHost( |
| 150 profile_->GetDebugName(), | 148 profile_->GetDebugName(), |
| 151 profile_.get(), | 149 profile_.get(), |
| 152 sync_prefs_->AsWeakPtr())); | 150 sync_prefs_->AsWeakPtr())); |
| 153 credentials_.email = "user@example.com"; | 151 credentials_.email = "user@example.com"; |
| 154 credentials_.sync_token = "sync_token"; | 152 credentials_.sync_token = "sync_token"; |
| 155 | 153 |
| 156 // These types are always implicitly enabled. | 154 // These types are always implicitly enabled. |
| 157 enabled_types_.PutAll(syncer::ControlTypes()); | 155 enabled_types_.PutAll(syncer::ControlTypes()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 171 virtual void TearDown() OVERRIDE { | 169 virtual void TearDown() OVERRIDE { |
| 172 if (backend_) { | 170 if (backend_) { |
| 173 backend_->StopSyncingForShutdown(); | 171 backend_->StopSyncingForShutdown(); |
| 174 backend_->Shutdown(false); | 172 backend_->Shutdown(false); |
| 175 } | 173 } |
| 176 backend_.reset(); | 174 backend_.reset(); |
| 177 sync_prefs_.reset(); | 175 sync_prefs_.reset(); |
| 178 profile_.reset(); | 176 profile_.reset(); |
| 179 // Pump messages posted by the sync thread (which may end up | 177 // Pump messages posted by the sync thread (which may end up |
| 180 // posting on the IO thread). | 178 // posting on the IO thread). |
| 181 ui_loop_.RunUntilIdle(); | 179 base::RunLoop().RunUntilIdle(); |
| 182 io_thread_.Stop(); | 180 content::RunAllPendingInMessageLoop(BrowserThread::IO); |
| 183 // Pump any messages posted by the IO thread. | 181 // Pump any messages posted by the IO thread. |
| 184 ui_loop_.RunUntilIdle(); | 182 base::RunLoop().RunUntilIdle(); |
| 185 } | 183 } |
| 186 | 184 |
| 187 // Synchronously initializes the backend. | 185 // Synchronously initializes the backend. |
| 188 void InitializeBackend(bool expect_success) { | 186 void InitializeBackend(bool expect_success) { |
| 189 EXPECT_CALL(mock_frontend_, OnBackendInitialized(_, _, expect_success)). | 187 EXPECT_CALL(mock_frontend_, OnBackendInitialized(_, _, expect_success)). |
| 190 WillOnce(InvokeWithoutArgs(QuitMessageLoop)); | 188 WillOnce(InvokeWithoutArgs(QuitMessageLoop)); |
| 191 backend_->Initialize(&mock_frontend_, | 189 backend_->Initialize(&mock_frontend_, |
| 192 syncer::WeakHandle<syncer::JsEventHandler>(), | 190 syncer::WeakHandle<syncer::JsEventHandler>(), |
| 193 GURL(std::string()), | 191 GURL(std::string()), |
| 194 credentials_, | 192 credentials_, |
| 195 true, | 193 true, |
| 196 &fake_manager_factory_, | 194 &fake_manager_factory_, |
| 197 &handler_, | 195 &handler_, |
| 198 NULL); | 196 NULL); |
| 199 ui_loop_.PostDelayedTask(FROM_HERE, | 197 base::RunLoop run_loop; |
| 200 ui_loop_.QuitClosure(), TestTimeouts::action_timeout()); | 198 BrowserThread::PostDelayedTask(BrowserThread::UI, FROM_HERE, |
| 201 ui_loop_.Run(); | 199 run_loop.QuitClosure(), |
| 200 TestTimeouts::action_timeout()); |
| 201 run_loop.Run(); |
| 202 // |fake_manager_factory_|'s fake_manager() is set on the sync | 202 // |fake_manager_factory_|'s fake_manager() is set on the sync |
| 203 // thread, but we can rely on the message loop barriers to | 203 // thread, but we can rely on the message loop barriers to |
| 204 // guarantee that we see the updated value. | 204 // guarantee that we see the updated value. |
| 205 fake_manager_ = fake_manager_factory_.fake_manager(); | 205 fake_manager_ = fake_manager_factory_.fake_manager(); |
| 206 DCHECK(fake_manager_); | 206 DCHECK(fake_manager_); |
| 207 } | 207 } |
| 208 | 208 |
| 209 // Synchronously configures the backend's datatypes. | 209 // Synchronously configures the backend's datatypes. |
| 210 void ConfigureDataTypes(syncer::ModelTypeSet types_to_add, | 210 void ConfigureDataTypes(syncer::ModelTypeSet types_to_add, |
| 211 syncer::ModelTypeSet types_to_remove) { | 211 syncer::ModelTypeSet types_to_remove) { |
| 212 BackendDataTypeConfigurer::DataTypeConfigStateMap config_state_map; | 212 BackendDataTypeConfigurer::DataTypeConfigStateMap config_state_map; |
| 213 BackendDataTypeConfigurer::SetDataTypesState( | 213 BackendDataTypeConfigurer::SetDataTypesState( |
| 214 BackendDataTypeConfigurer::CONFIGURE_ACTIVE, | 214 BackendDataTypeConfigurer::CONFIGURE_ACTIVE, |
| 215 types_to_add, | 215 types_to_add, |
| 216 &config_state_map); | 216 &config_state_map); |
| 217 BackendDataTypeConfigurer::SetDataTypesState( | 217 BackendDataTypeConfigurer::SetDataTypesState( |
| 218 BackendDataTypeConfigurer::DISABLED, | 218 BackendDataTypeConfigurer::DISABLED, |
| 219 types_to_remove, &config_state_map); | 219 types_to_remove, &config_state_map); |
| 220 | 220 |
| 221 types_to_add.PutAll(syncer::ControlTypes()); | 221 types_to_add.PutAll(syncer::ControlTypes()); |
| 222 backend_->ConfigureDataTypes( | 222 backend_->ConfigureDataTypes( |
| 223 syncer::CONFIGURE_REASON_RECONFIGURATION, | 223 syncer::CONFIGURE_REASON_RECONFIGURATION, |
| 224 config_state_map, | 224 config_state_map, |
| 225 base::Bind(&SyncBackendHostTest::DownloadReady, | 225 base::Bind(&SyncBackendHostTest::DownloadReady, |
| 226 base::Unretained(this)), | 226 base::Unretained(this)), |
| 227 base::Bind(&SyncBackendHostTest::OnDownloadRetry, | 227 base::Bind(&SyncBackendHostTest::OnDownloadRetry, |
| 228 base::Unretained(this))); | 228 base::Unretained(this))); |
| 229 ui_loop_.PostDelayedTask(FROM_HERE, | 229 base::RunLoop run_loop; |
| 230 ui_loop_.QuitClosure(), TestTimeouts::action_timeout()); | 230 BrowserThread::PostDelayedTask(BrowserThread::UI, FROM_HERE, |
| 231 ui_loop_.Run(); | 231 run_loop.QuitClosure(), |
| 232 TestTimeouts::action_timeout()); |
| 233 run_loop.Run(); |
| 232 } | 234 } |
| 233 | 235 |
| 234 void IssueRefreshRequest(syncer::ModelTypeSet types) { | 236 void IssueRefreshRequest(syncer::ModelTypeSet types) { |
| 235 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 237 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 236 | 238 |
| 237 content::NotificationService::current()->Notify( | 239 content::NotificationService::current()->Notify( |
| 238 chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, | 240 chrome::NOTIFICATION_SYNC_REFRESH_LOCAL, |
| 239 content::Source<Profile>(profile_.get()), | 241 content::Source<Profile>(profile_.get()), |
| 240 content::Details<syncer::ModelTypeSet>(&types)); | 242 content::Details<syncer::ModelTypeSet>(&types)); |
| 241 } | 243 } |
| 242 | 244 |
| 243 protected: | 245 protected: |
| 244 void DownloadReady(syncer::ModelTypeSet succeeded_types, | 246 void DownloadReady(syncer::ModelTypeSet succeeded_types, |
| 245 syncer::ModelTypeSet failed_types) { | 247 syncer::ModelTypeSet failed_types) { |
| 246 base::MessageLoop::current()->Quit(); | 248 base::MessageLoop::current()->Quit(); |
| 247 } | 249 } |
| 248 | 250 |
| 249 void OnDownloadRetry() { | 251 void OnDownloadRetry() { |
| 250 NOTIMPLEMENTED(); | 252 NOTIMPLEMENTED(); |
| 251 } | 253 } |
| 252 | 254 |
| 253 base::MessageLoop ui_loop_; | 255 content::TestBrowserThreadBundle thread_bundle_; |
| 254 content::TestBrowserThread ui_thread_; | |
| 255 content::TestBrowserThread io_thread_; | |
| 256 StrictMock<MockSyncFrontend> mock_frontend_; | 256 StrictMock<MockSyncFrontend> mock_frontend_; |
| 257 syncer::SyncCredentials credentials_; | 257 syncer::SyncCredentials credentials_; |
| 258 syncer::TestUnrecoverableErrorHandler handler_; | 258 syncer::TestUnrecoverableErrorHandler handler_; |
| 259 scoped_ptr<TestingProfile> profile_; | 259 scoped_ptr<TestingProfile> profile_; |
| 260 scoped_ptr<SyncPrefs> sync_prefs_; | 260 scoped_ptr<SyncPrefs> sync_prefs_; |
| 261 scoped_ptr<SyncBackendHost> backend_; | 261 scoped_ptr<SyncBackendHost> backend_; |
| 262 FakeSyncManager* fake_manager_; | 262 FakeSyncManager* fake_manager_; |
| 263 FakeSyncManagerFactory fake_manager_factory_; | 263 FakeSyncManagerFactory fake_manager_factory_; |
| 264 syncer::ModelTypeSet enabled_types_; | 264 syncer::ModelTypeSet enabled_types_; |
| 265 }; | 265 }; |
| (...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 684 TEST_F(SyncBackendHostTest, DownloadControlTypesRestart) { | 684 TEST_F(SyncBackendHostTest, DownloadControlTypesRestart) { |
| 685 sync_prefs_->SetSyncSetupCompleted(); | 685 sync_prefs_->SetSyncSetupCompleted(); |
| 686 InitializeBackend(true); | 686 InitializeBackend(true); |
| 687 EXPECT_EQ(syncer::CONFIGURE_REASON_NEWLY_ENABLED_DATA_TYPE, | 687 EXPECT_EQ(syncer::CONFIGURE_REASON_NEWLY_ENABLED_DATA_TYPE, |
| 688 fake_manager_->GetAndResetConfigureReason()); | 688 fake_manager_->GetAndResetConfigureReason()); |
| 689 } | 689 } |
| 690 | 690 |
| 691 } // namespace | 691 } // namespace |
| 692 | 692 |
| 693 } // namespace browser_sync | 693 } // namespace browser_sync |
| OLD | NEW |