Chromium Code Reviews| 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/compiler_specific.h" | 6 #include "base/compiler_specific.h" |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/invalidation/invalidation_service_factory.h" | 10 #include "chrome/browser/invalidation/invalidation_service_factory.h" |
| 11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
| 11 #include "chrome/browser/signin/signin_manager.h" | 12 #include "chrome/browser/signin/signin_manager.h" |
| 12 #include "chrome/browser/signin/signin_manager_factory.h" | 13 #include "chrome/browser/signin/signin_manager_factory.h" |
| 13 #include "chrome/browser/signin/token_service.h" | 14 #include "chrome/browser/signin/token_service.h" |
| 14 #include "chrome/browser/signin/token_service_factory.h" | 15 #include "chrome/browser/signin/token_service_factory.h" |
| 15 #include "chrome/browser/sync/fake_oauth2_token_service.h" | 16 #include "chrome/browser/sync/fake_oauth2_token_service.h" |
| 16 #include "chrome/browser/sync/glue/bookmark_data_type_controller.h" | 17 #include "chrome/browser/sync/glue/data_type_manager_impl.h" |
| 17 #include "chrome/browser/sync/glue/data_type_controller.h" | |
| 18 #include "chrome/browser/sync/glue/sync_backend_host_mock.h" | 18 #include "chrome/browser/sync/glue/sync_backend_host_mock.h" |
| 19 #include "chrome/browser/sync/profile_sync_components_factory_mock.h" | 19 #include "chrome/browser/sync/profile_sync_components_factory_mock.h" |
| 20 #include "chrome/browser/sync/test_profile_sync_service.h" | |
| 21 #include "chrome/common/chrome_version_info.h" | |
| 22 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 23 #include "chrome/test/base/testing_pref_service_syncable.h" | 21 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 24 #include "chrome/test/base/testing_profile.h" | 22 #include "chrome/test/base/testing_profile.h" |
| 25 #include "content/public/test/test_browser_thread_bundle.h" | 23 #include "content/public/test/test_browser_thread_bundle.h" |
| 26 #include "content/public/test/test_utils.h" | |
| 27 #include "google/cacheinvalidation/include/types.h" | |
| 28 #include "google_apis/gaia/gaia_constants.h" | 24 #include "google_apis/gaia/gaia_constants.h" |
| 29 #include "sync/js/js_arg_list.h" | |
| 30 #include "sync/js/js_event_details.h" | |
| 31 #include "sync/js/js_test_util.h" | |
| 32 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
| 33 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 34 | 27 |
| 35 // TODO(akalin): Add tests here that exercise the whole | |
| 36 // ProfileSyncService/SyncBackendHost stack while mocking out as | |
| 37 // little as possible. | |
| 38 | |
| 39 namespace browser_sync { | 28 namespace browser_sync { |
| 40 | 29 |
| 41 namespace { | 30 namespace { |
| 42 | 31 |
| 43 using testing::_; | 32 ACTION(ReturnNewDataTypeManager) { |
|
pavely
2013/12/02 19:00:55
I don't think you are using ReturnNewDataTypeManag
rlarocque
2013/12/02 21:08:59
It's used in ExpectDataTypeManagerCreation() on li
| |
| 44 using testing::AtLeast; | 33 return new browser_sync::DataTypeManagerImpl(arg0, |
| 45 using testing::AtMost; | 34 arg1, |
| 46 using testing::Mock; | 35 arg2, |
| 47 using testing::Return; | 36 arg3, |
| 48 using testing::StrictMock; | 37 arg4, |
| 49 | 38 arg5); |
| 50 void SignalDone(base::WaitableEvent* done) { | |
| 51 done->Signal(); | |
| 52 } | 39 } |
| 53 | 40 |
| 54 class ProfileSyncServiceTest : public testing::Test { | 41 using testing::_; |
| 55 protected: | 42 using testing::StrictMock; |
| 56 ProfileSyncServiceTest() | |
| 57 : thread_bundle_(content::TestBrowserThreadBundle::REAL_DB_THREAD | | |
| 58 content::TestBrowserThreadBundle::REAL_FILE_THREAD | | |
| 59 content::TestBrowserThreadBundle::REAL_IO_THREAD) { | |
| 60 } | |
| 61 | |
| 62 virtual ~ProfileSyncServiceTest() {} | |
| 63 | |
| 64 virtual void SetUp() OVERRIDE { | |
| 65 TestingProfile::Builder builder; | |
| 66 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), | |
| 67 FakeOAuth2TokenService::BuildTokenService); | |
| 68 profile_ = builder.Build().Pass(); | |
| 69 invalidation::InvalidationServiceFactory::GetInstance()-> | |
| 70 SetBuildOnlyFakeInvalidatorsForTest(true); | |
| 71 } | |
| 72 | |
| 73 virtual void TearDown() OVERRIDE { | |
| 74 // Kill the service before the profile. | |
| 75 if (service_) | |
| 76 service_->Shutdown(); | |
| 77 | |
| 78 service_.reset(); | |
| 79 profile_.reset(); | |
| 80 | |
| 81 // Pump messages posted by the sync thread (which may end up | |
| 82 // posting on the IO thread). | |
| 83 base::RunLoop().RunUntilIdle(); | |
| 84 content::RunAllPendingInMessageLoop(content::BrowserThread::IO); | |
| 85 base::RunLoop().RunUntilIdle(); | |
| 86 } | |
| 87 | |
| 88 void StartSyncServiceAndSetInitialSyncEnded() { | |
| 89 if (service_) | |
| 90 return; | |
| 91 | |
| 92 SigninManagerBase* signin = | |
| 93 SigninManagerFactory::GetForProfile(profile_.get()); | |
| 94 signin->SetAuthenticatedUsername("test"); | |
| 95 ProfileOAuth2TokenService* oauth2_token_service = | |
| 96 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get()); | |
| 97 ProfileSyncComponentsFactoryMock* factory = | |
| 98 new ProfileSyncComponentsFactoryMock(); | |
| 99 service_.reset(new TestProfileSyncService( | |
| 100 factory, | |
| 101 profile_.get(), | |
| 102 signin, | |
| 103 oauth2_token_service, | |
| 104 ProfileSyncService::AUTO_START, | |
| 105 true)); | |
| 106 | |
| 107 | |
| 108 // Register the bookmark data type. | |
| 109 ON_CALL(*factory, CreateDataTypeManager(_, _, _, _, _, _)). | |
| 110 WillByDefault(ReturnNewDataTypeManager()); | |
| 111 | |
| 112 service_->Initialize(); | |
| 113 } | |
| 114 | |
| 115 void WaitForBackendInitDone() { | |
| 116 for (int i = 0; i < 5; ++i) { | |
| 117 base::WaitableEvent done(false, false); | |
| 118 service_->GetBackendForTest()->GetSyncLoopForTesting() | |
| 119 ->PostTask(FROM_HERE, base::Bind(&SignalDone, &done)); | |
| 120 done.Wait(); | |
| 121 base::RunLoop().RunUntilIdle(); | |
| 122 if (service_->sync_initialized()) { | |
| 123 return; | |
| 124 } | |
| 125 } | |
| 126 LOG(ERROR) << "Backend not initialized."; | |
| 127 } | |
| 128 | |
| 129 void IssueTestTokens() { | |
| 130 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get()) | |
| 131 ->UpdateCredentials("test", "oauth2_login_token"); | |
| 132 } | |
| 133 | |
| 134 scoped_ptr<TestProfileSyncService> service_; | |
| 135 scoped_ptr<TestingProfile> profile_; | |
| 136 | |
| 137 private: | |
| 138 content::TestBrowserThreadBundle thread_bundle_; | |
| 139 }; | |
| 140 | 43 |
| 141 class TestProfileSyncServiceObserver : public ProfileSyncServiceObserver { | 44 class TestProfileSyncServiceObserver : public ProfileSyncServiceObserver { |
| 142 public: | 45 public: |
| 143 explicit TestProfileSyncServiceObserver(ProfileSyncService* service) | 46 explicit TestProfileSyncServiceObserver(ProfileSyncService* service) |
| 144 : service_(service), first_setup_in_progress_(false) {} | 47 : service_(service), first_setup_in_progress_(false) {} |
| 145 virtual void OnStateChanged() OVERRIDE { | 48 virtual void OnStateChanged() OVERRIDE { |
| 146 first_setup_in_progress_ = service_->FirstSetupInProgress(); | 49 first_setup_in_progress_ = service_->FirstSetupInProgress(); |
| 147 } | 50 } |
| 148 bool first_setup_in_progress() const { return first_setup_in_progress_; } | 51 bool first_setup_in_progress() const { return first_setup_in_progress_; } |
| 149 private: | 52 private: |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 173 } | 76 } |
| 174 | 77 |
| 175 ACTION(ReturnNewSyncBackendHostNoReturn) { | 78 ACTION(ReturnNewSyncBackendHostNoReturn) { |
| 176 return new browser_sync::SyncBackendHostNoReturn(); | 79 return new browser_sync::SyncBackendHostNoReturn(); |
| 177 } | 80 } |
| 178 | 81 |
| 179 // A test harness that uses a real ProfileSyncService and in most cases a | 82 // A test harness that uses a real ProfileSyncService and in most cases a |
| 180 // MockSyncBackendHost. | 83 // MockSyncBackendHost. |
| 181 // | 84 // |
| 182 // This is useful if we want to test the ProfileSyncService and don't care about | 85 // This is useful if we want to test the ProfileSyncService and don't care about |
| 183 // testing the SyncBackendHost. It's easier to use than the other tests, since | 86 // testing the SyncBackendHost. |
| 184 // it doesn't involve any threads. | 87 class ProfileSyncServiceTest : public ::testing::Test { |
|
rlarocque
2013/11/19 20:41:09
The diffs here may be a bit confusing.
In a previ
| |
| 185 class ProfileSyncServiceSimpleTest : public ::testing::Test { | |
| 186 protected: | 88 protected: |
| 187 ProfileSyncServiceSimpleTest() | 89 ProfileSyncServiceTest() |
| 188 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} | 90 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 189 virtual ~ProfileSyncServiceSimpleTest() {} | 91 virtual ~ProfileSyncServiceTest() {} |
| 190 | 92 |
| 191 virtual void SetUp() OVERRIDE { | 93 virtual void SetUp() OVERRIDE { |
| 192 TestingProfile::Builder builder; | 94 TestingProfile::Builder builder; |
| 193 | 95 |
| 194 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), | 96 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), |
| 195 FakeOAuth2TokenService::BuildTokenService); | 97 FakeOAuth2TokenService::BuildTokenService); |
| 196 invalidation::InvalidationServiceFactory::GetInstance()-> | 98 invalidation::InvalidationServiceFactory::GetInstance()-> |
| 197 SetBuildOnlyFakeInvalidatorsForTest(true); | 99 SetBuildOnlyFakeInvalidatorsForTest(true); |
| 198 | 100 |
| 199 profile_ = builder.Build().Pass(); | 101 profile_ = builder.Build().Pass(); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 269 scoped_ptr<TestingProfile> profile_; | 171 scoped_ptr<TestingProfile> profile_; |
| 270 scoped_ptr<ProfileSyncService> service_; | 172 scoped_ptr<ProfileSyncService> service_; |
| 271 | 173 |
| 272 // Pointer to the components factory. Not owned. May be null. | 174 // Pointer to the components factory. Not owned. May be null. |
| 273 ProfileSyncComponentsFactoryMock* components_factory_; | 175 ProfileSyncComponentsFactoryMock* components_factory_; |
| 274 | 176 |
| 275 content::TestBrowserThreadBundle thread_bundle_; | 177 content::TestBrowserThreadBundle thread_bundle_; |
| 276 }; | 178 }; |
| 277 | 179 |
| 278 // Verify that the server URLs are sane. | 180 // Verify that the server URLs are sane. |
| 279 TEST_F(ProfileSyncServiceSimpleTest, InitialState) { | 181 TEST_F(ProfileSyncServiceTest, InitialState) { |
| 280 CreateService(ProfileSyncService::AUTO_START); | 182 CreateService(ProfileSyncService::AUTO_START); |
| 281 Initialize(); | 183 Initialize(); |
| 282 const std::string& url = service()->sync_service_url().spec(); | 184 const std::string& url = service()->sync_service_url().spec(); |
| 283 EXPECT_TRUE(url == ProfileSyncService::kSyncServerUrl || | 185 EXPECT_TRUE(url == ProfileSyncService::kSyncServerUrl || |
| 284 url == ProfileSyncService::kDevServerUrl); | 186 url == ProfileSyncService::kDevServerUrl); |
| 285 } | 187 } |
| 286 | 188 |
| 287 // Verify a successful initialization. | 189 // Verify a successful initialization. |
| 288 TEST_F(ProfileSyncServiceSimpleTest, SuccessfulInitialization) { | 190 TEST_F(ProfileSyncServiceTest, SuccessfulInitialization) { |
| 289 profile()->GetTestingPrefService()->SetManagedPref( | 191 profile()->GetTestingPrefService()->SetManagedPref( |
| 290 prefs::kSyncManaged, | 192 prefs::kSyncManaged, |
| 291 Value::CreateBooleanValue(false)); | 193 Value::CreateBooleanValue(false)); |
| 292 IssueTestTokens(); | 194 IssueTestTokens(); |
| 293 CreateService(ProfileSyncService::AUTO_START); | 195 CreateService(ProfileSyncService::AUTO_START); |
| 294 ExpectDataTypeManagerCreation(); | 196 ExpectDataTypeManagerCreation(); |
| 295 ExpectSyncBackendHostCreation(); | 197 ExpectSyncBackendHostCreation(); |
| 296 Initialize(); | 198 Initialize(); |
| 297 EXPECT_FALSE(service()->IsManaged()); | 199 EXPECT_FALSE(service()->IsManaged()); |
| 298 EXPECT_TRUE(service()->sync_initialized()); | 200 EXPECT_TRUE(service()->sync_initialized()); |
| 299 } | 201 } |
| 300 | 202 |
| 301 | 203 |
| 302 // Verify that the SetSetupInProgress function call updates state | 204 // Verify that the SetSetupInProgress function call updates state |
| 303 // and notifies observers. | 205 // and notifies observers. |
| 304 TEST_F(ProfileSyncServiceSimpleTest, SetupInProgress) { | 206 TEST_F(ProfileSyncServiceTest, SetupInProgress) { |
| 305 CreateService(ProfileSyncService::MANUAL_START); | 207 CreateService(ProfileSyncService::MANUAL_START); |
| 306 Initialize(); | 208 Initialize(); |
| 307 | 209 |
| 308 TestProfileSyncServiceObserver observer(service()); | 210 TestProfileSyncServiceObserver observer(service()); |
| 309 service()->AddObserver(&observer); | 211 service()->AddObserver(&observer); |
| 310 | 212 |
| 311 service()->SetSetupInProgress(true); | 213 service()->SetSetupInProgress(true); |
| 312 EXPECT_TRUE(observer.first_setup_in_progress()); | 214 EXPECT_TRUE(observer.first_setup_in_progress()); |
| 313 service()->SetSetupInProgress(false); | 215 service()->SetSetupInProgress(false); |
| 314 EXPECT_FALSE(observer.first_setup_in_progress()); | 216 EXPECT_FALSE(observer.first_setup_in_progress()); |
| 315 | 217 |
| 316 service()->RemoveObserver(&observer); | 218 service()->RemoveObserver(&observer); |
| 317 } | 219 } |
| 318 | 220 |
| 319 // Verify that disable by enterprise policy works. | 221 // Verify that disable by enterprise policy works. |
| 320 TEST_F(ProfileSyncServiceSimpleTest, DisabledByPolicyBeforeInit) { | 222 TEST_F(ProfileSyncServiceTest, DisabledByPolicyBeforeInit) { |
| 321 profile()->GetTestingPrefService()->SetManagedPref( | 223 profile()->GetTestingPrefService()->SetManagedPref( |
| 322 prefs::kSyncManaged, | 224 prefs::kSyncManaged, |
| 323 Value::CreateBooleanValue(true)); | 225 Value::CreateBooleanValue(true)); |
| 324 IssueTestTokens(); | 226 IssueTestTokens(); |
| 325 CreateService(ProfileSyncService::AUTO_START); | 227 CreateService(ProfileSyncService::AUTO_START); |
| 326 Initialize(); | 228 Initialize(); |
| 327 EXPECT_TRUE(service()->IsManaged()); | 229 EXPECT_TRUE(service()->IsManaged()); |
| 328 EXPECT_FALSE(service()->sync_initialized()); | 230 EXPECT_FALSE(service()->sync_initialized()); |
| 329 } | 231 } |
| 330 | 232 |
| 331 // Verify that disable by enterprise policy works even after the backend has | 233 // Verify that disable by enterprise policy works even after the backend has |
| 332 // been initialized. | 234 // been initialized. |
| 333 TEST_F(ProfileSyncServiceSimpleTest, DisabledByPolicyAfterInit) { | 235 TEST_F(ProfileSyncServiceTest, DisabledByPolicyAfterInit) { |
| 334 IssueTestTokens(); | 236 IssueTestTokens(); |
| 335 CreateService(ProfileSyncService::AUTO_START); | 237 CreateService(ProfileSyncService::AUTO_START); |
| 336 ExpectDataTypeManagerCreation(); | 238 ExpectDataTypeManagerCreation(); |
| 337 ExpectSyncBackendHostCreation(); | 239 ExpectSyncBackendHostCreation(); |
| 338 Initialize(); | 240 Initialize(); |
| 339 | 241 |
| 340 EXPECT_FALSE(service()->IsManaged()); | 242 EXPECT_FALSE(service()->IsManaged()); |
| 341 EXPECT_TRUE(service()->sync_initialized()); | 243 EXPECT_TRUE(service()->sync_initialized()); |
| 342 | 244 |
| 343 profile()->GetTestingPrefService()->SetManagedPref( | 245 profile()->GetTestingPrefService()->SetManagedPref( |
| 344 prefs::kSyncManaged, | 246 prefs::kSyncManaged, |
| 345 Value::CreateBooleanValue(true)); | 247 Value::CreateBooleanValue(true)); |
| 346 | 248 |
| 347 EXPECT_TRUE(service()->IsManaged()); | 249 EXPECT_TRUE(service()->IsManaged()); |
| 348 EXPECT_FALSE(service()->sync_initialized()); | 250 EXPECT_FALSE(service()->sync_initialized()); |
| 349 } | 251 } |
| 350 | 252 |
| 351 // Exercies the ProfileSyncService's code paths related to getting shut down | 253 // Exercies the ProfileSyncService's code paths related to getting shut down |
| 352 // before the backend initialize call returns. | 254 // before the backend initialize call returns. |
| 353 TEST_F(ProfileSyncServiceSimpleTest, AbortedByShutdown) { | 255 TEST_F(ProfileSyncServiceTest, AbortedByShutdown) { |
| 354 CreateService(ProfileSyncService::AUTO_START); | 256 CreateService(ProfileSyncService::AUTO_START); |
| 355 PrepareDelayedInitSyncBackendHost(); | 257 PrepareDelayedInitSyncBackendHost(); |
| 356 | 258 |
| 357 IssueTestTokens(); | 259 IssueTestTokens(); |
| 358 Initialize(); | 260 Initialize(); |
| 359 EXPECT_FALSE(service()->sync_initialized()); | 261 EXPECT_FALSE(service()->sync_initialized()); |
| 360 | 262 |
| 361 ShutdownAndDeleteService(); | 263 ShutdownAndDeleteService(); |
| 362 } | 264 } |
| 363 | 265 |
| 364 // Test StopAndSuppress() before we've initialized the backend. | 266 // Test StopAndSuppress() before we've initialized the backend. |
| 365 TEST_F(ProfileSyncServiceSimpleTest, EarlyStopAndSuppress) { | 267 TEST_F(ProfileSyncServiceTest, EarlyStopAndSuppress) { |
| 366 CreateService(ProfileSyncService::AUTO_START); | 268 CreateService(ProfileSyncService::AUTO_START); |
| 367 IssueTestTokens(); | 269 IssueTestTokens(); |
| 368 | 270 |
| 369 service()->StopAndSuppress(); | 271 service()->StopAndSuppress(); |
| 370 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); | 272 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); |
| 371 | 273 |
| 372 // Because of supression, this should fail. | 274 // Because of supression, this should fail. |
| 373 Initialize(); | 275 Initialize(); |
| 374 EXPECT_FALSE(service()->sync_initialized()); | 276 EXPECT_FALSE(service()->sync_initialized()); |
| 375 | 277 |
| 376 // Remove suppression. This should be enough to allow init to happen. | 278 // Remove suppression. This should be enough to allow init to happen. |
| 377 ExpectDataTypeManagerCreation(); | 279 ExpectDataTypeManagerCreation(); |
| 378 ExpectSyncBackendHostCreation(); | 280 ExpectSyncBackendHostCreation(); |
| 379 service()->UnsuppressAndStart(); | 281 service()->UnsuppressAndStart(); |
| 380 EXPECT_TRUE(service()->sync_initialized()); | 282 EXPECT_TRUE(service()->sync_initialized()); |
| 381 EXPECT_FALSE( | 283 EXPECT_FALSE( |
| 382 profile()->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); | 284 profile()->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); |
| 383 } | 285 } |
| 384 | 286 |
| 385 // Test StopAndSuppress() after we've initialized the backend. | 287 // Test StopAndSuppress() after we've initialized the backend. |
| 386 TEST_F(ProfileSyncServiceSimpleTest, DisableAndEnableSyncTemporarily) { | 288 TEST_F(ProfileSyncServiceTest, DisableAndEnableSyncTemporarily) { |
| 387 CreateService(ProfileSyncService::AUTO_START); | 289 CreateService(ProfileSyncService::AUTO_START); |
| 388 IssueTestTokens(); | 290 IssueTestTokens(); |
| 389 ExpectDataTypeManagerCreation(); | 291 ExpectDataTypeManagerCreation(); |
| 390 ExpectSyncBackendHostCreation(); | 292 ExpectSyncBackendHostCreation(); |
| 391 Initialize(); | 293 Initialize(); |
| 392 | 294 |
| 393 EXPECT_TRUE(service()->sync_initialized()); | 295 EXPECT_TRUE(service()->sync_initialized()); |
| 394 EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); | 296 EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); |
| 395 | 297 |
| 396 testing::Mock::VerifyAndClearExpectations(components_factory()); | 298 testing::Mock::VerifyAndClearExpectations(components_factory()); |
| 397 | 299 |
| 398 service()->StopAndSuppress(); | 300 service()->StopAndSuppress(); |
| 399 EXPECT_FALSE(service()->sync_initialized()); | 301 EXPECT_FALSE(service()->sync_initialized()); |
| 400 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); | 302 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); |
| 401 | 303 |
| 402 ExpectDataTypeManagerCreation(); | 304 ExpectDataTypeManagerCreation(); |
| 403 ExpectSyncBackendHostCreation(); | 305 ExpectSyncBackendHostCreation(); |
| 404 | 306 |
| 405 service()->UnsuppressAndStart(); | 307 service()->UnsuppressAndStart(); |
| 406 EXPECT_TRUE(service()->sync_initialized()); | 308 EXPECT_TRUE(service()->sync_initialized()); |
| 407 EXPECT_FALSE( | 309 EXPECT_FALSE( |
| 408 profile()->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); | 310 profile()->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); |
| 409 } | 311 } |
| 410 | 312 |
| 411 // Certain ProfileSyncService tests don't apply to Chrome OS, for example | 313 // Certain ProfileSyncService tests don't apply to Chrome OS, for example |
| 412 // things that deal with concepts like "signing out" and policy. | 314 // things that deal with concepts like "signing out" and policy. |
| 413 #if !defined (OS_CHROMEOS) | 315 #if !defined (OS_CHROMEOS) |
| 414 | 316 |
| 415 TEST_F(ProfileSyncServiceSimpleTest, EnableSyncAndSignOut) { | 317 TEST_F(ProfileSyncServiceTest, EnableSyncAndSignOut) { |
| 416 CreateService(ProfileSyncService::AUTO_START); | 318 CreateService(ProfileSyncService::AUTO_START); |
| 417 ExpectDataTypeManagerCreation(); | 319 ExpectDataTypeManagerCreation(); |
| 418 ExpectSyncBackendHostCreation(); | 320 ExpectSyncBackendHostCreation(); |
| 419 IssueTestTokens(); | 321 IssueTestTokens(); |
| 420 Initialize(); | 322 Initialize(); |
| 421 | 323 |
| 422 EXPECT_TRUE(service()->sync_initialized()); | 324 EXPECT_TRUE(service()->sync_initialized()); |
| 423 EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); | 325 EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); |
| 424 | 326 |
| 425 SigninManagerFactory::GetForProfile(profile())->SignOut(); | 327 SigninManagerFactory::GetForProfile(profile())->SignOut(); |
| 426 EXPECT_FALSE(service()->sync_initialized()); | 328 EXPECT_FALSE(service()->sync_initialized()); |
| 427 } | 329 } |
| 428 | 330 |
| 429 #endif // !defined(OS_CHROMEOS) | 331 #endif // !defined(OS_CHROMEOS) |
| 430 | 332 |
| 431 TEST_F(ProfileSyncServiceTest, JsControllerHandlersBasic) { | 333 TEST_F(ProfileSyncServiceTest, GetSyncTokenStatus) { |
| 432 StartSyncServiceAndSetInitialSyncEnded(); | 334 CreateService(ProfileSyncService::AUTO_START); |
| 433 IssueTestTokens(); | 335 IssueTestTokens(); |
| 434 EXPECT_TRUE(service_->sync_initialized()); | 336 ExpectDataTypeManagerCreation(); |
| 435 EXPECT_TRUE(service_->GetBackendForTest() != NULL); | 337 ExpectSyncBackendHostCreation(); |
| 338 Initialize(); | |
| 436 | 339 |
| 437 base::WeakPtr<syncer::JsController> js_controller = | 340 // Initial status. |
| 438 service_->GetJsController(); | |
| 439 StrictMock<syncer::MockJsEventHandler> event_handler; | |
| 440 js_controller->AddJsEventHandler(&event_handler); | |
| 441 js_controller->RemoveJsEventHandler(&event_handler); | |
| 442 } | |
| 443 | |
| 444 TEST_F(ProfileSyncServiceTest, | |
| 445 JsControllerHandlersDelayedBackendInitialization) { | |
| 446 StartSyncServiceAndSetInitialSyncEnded(); | |
| 447 | |
| 448 StrictMock<syncer::MockJsEventHandler> event_handler; | |
| 449 EXPECT_CALL(event_handler, HandleJsEvent(_, _)).Times(AtLeast(1)); | |
| 450 | |
| 451 EXPECT_EQ(NULL, service_->GetBackendForTest()); | |
| 452 EXPECT_FALSE(service_->sync_initialized()); | |
| 453 | |
| 454 base::WeakPtr<syncer::JsController> js_controller = | |
| 455 service_->GetJsController(); | |
| 456 js_controller->AddJsEventHandler(&event_handler); | |
| 457 // Since we're doing synchronous initialization, backend should be | |
| 458 // initialized by this call. | |
| 459 IssueTestTokens(); | |
| 460 EXPECT_TRUE(service_->sync_initialized()); | |
| 461 js_controller->RemoveJsEventHandler(&event_handler); | |
| 462 } | |
| 463 | |
| 464 TEST_F(ProfileSyncServiceTest, JsControllerProcessJsMessageBasic) { | |
| 465 StartSyncServiceAndSetInitialSyncEnded(); | |
| 466 IssueTestTokens(); | |
| 467 WaitForBackendInitDone(); | |
| 468 | |
| 469 StrictMock<syncer::MockJsReplyHandler> reply_handler; | |
| 470 | |
| 471 ListValue arg_list1; | |
| 472 arg_list1.Append(Value::CreateStringValue("INVALIDATIONS_ENABLED")); | |
| 473 syncer::JsArgList args1(&arg_list1); | |
| 474 EXPECT_CALL(reply_handler, | |
| 475 HandleJsReply("getNotificationState", HasArgs(args1))); | |
| 476 | |
| 477 { | |
| 478 base::WeakPtr<syncer::JsController> js_controller = | |
| 479 service_->GetJsController(); | |
| 480 js_controller->ProcessJsMessage("getNotificationState", args1, | |
| 481 reply_handler.AsWeakHandle()); | |
| 482 } | |
| 483 | |
| 484 // This forces the sync thread to process the message and reply. | |
| 485 base::WaitableEvent done(false, false); | |
| 486 service_->GetBackendForTest()->GetSyncLoopForTesting() | |
| 487 ->PostTask(FROM_HERE, | |
| 488 base::Bind(&SignalDone, &done)); | |
| 489 done.Wait(); | |
| 490 | |
| 491 // Call TearDown() to flush the message loops before the mock is destroyed. | |
| 492 // TearDown() is idempotent, so it's not a problem that it gets called by the | |
| 493 // test fixture again later. | |
| 494 TearDown(); | |
| 495 } | |
| 496 | |
| 497 TEST_F(ProfileSyncServiceTest, | |
| 498 JsControllerProcessJsMessageBasicDelayedBackendInitialization) { | |
| 499 StartSyncServiceAndSetInitialSyncEnded(); | |
| 500 | |
| 501 StrictMock<syncer::MockJsReplyHandler> reply_handler; | |
| 502 | |
| 503 ListValue arg_list1; | |
| 504 arg_list1.Append(Value::CreateStringValue("INVALIDATIONS_ENABLED")); | |
| 505 syncer::JsArgList args1(&arg_list1); | |
| 506 EXPECT_CALL(reply_handler, | |
| 507 HandleJsReply("getNotificationState", HasArgs(args1))); | |
| 508 | |
| 509 { | |
| 510 base::WeakPtr<syncer::JsController> js_controller = | |
| 511 service_->GetJsController(); | |
| 512 js_controller->ProcessJsMessage("getNotificationState", | |
| 513 args1, reply_handler.AsWeakHandle()); | |
| 514 } | |
| 515 | |
| 516 IssueTestTokens(); | |
| 517 WaitForBackendInitDone(); | |
| 518 | |
| 519 // This forces the sync thread to process the message and reply. | |
| 520 base::WaitableEvent done(false, false); | |
| 521 service_->GetBackendForTest()->GetSyncLoopForTesting() | |
| 522 ->PostTask(FROM_HERE, | |
| 523 base::Bind(&SignalDone, &done)); | |
| 524 done.Wait(); | |
| 525 } | |
| 526 | |
| 527 TEST_F(ProfileSyncServiceTest, GetSyncTokenStatus) { | |
| 528 StartSyncServiceAndSetInitialSyncEnded(); | |
| 529 IssueTestTokens(); | |
| 530 ProfileSyncService::SyncTokenStatus token_status = | 341 ProfileSyncService::SyncTokenStatus token_status = |
| 531 service_->GetSyncTokenStatus(); | 342 service()->GetSyncTokenStatus(); |
| 532 EXPECT_EQ(syncer::CONNECTION_NOT_ATTEMPTED, token_status.connection_status); | 343 EXPECT_EQ(syncer::CONNECTION_NOT_ATTEMPTED, token_status.connection_status); |
| 533 EXPECT_TRUE(token_status.connection_status_update_time.is_null()); | 344 EXPECT_TRUE(token_status.connection_status_update_time.is_null()); |
| 534 EXPECT_TRUE(token_status.token_request_time.is_null()); | 345 EXPECT_TRUE(token_status.token_request_time.is_null()); |
| 535 EXPECT_TRUE(token_status.token_receive_time.is_null()); | 346 EXPECT_TRUE(token_status.token_receive_time.is_null()); |
| 536 | 347 |
| 537 // Sync engine is given invalid token at initialization and will report | 348 // Simulate an auth error. |
| 538 // auth error when trying to connect. | 349 service()->OnConnectionStatusChange(syncer::CONNECTION_AUTH_ERROR); |
| 539 service_->OnConnectionStatusChange(syncer::CONNECTION_AUTH_ERROR); | 350 |
| 540 token_status = service_->GetSyncTokenStatus(); | 351 // The token request will take the form of a posted task. Run it. |
| 352 base::RunLoop loop; | |
| 353 loop.RunUntilIdle(); | |
| 354 | |
| 355 token_status = service()->GetSyncTokenStatus(); | |
| 541 EXPECT_EQ(syncer::CONNECTION_AUTH_ERROR, token_status.connection_status); | 356 EXPECT_EQ(syncer::CONNECTION_AUTH_ERROR, token_status.connection_status); |
| 542 EXPECT_FALSE(token_status.connection_status_update_time.is_null()); | 357 EXPECT_FALSE(token_status.connection_status_update_time.is_null()); |
| 543 EXPECT_FALSE(token_status.token_request_time.is_null()); | 358 EXPECT_FALSE(token_status.token_request_time.is_null()); |
| 544 EXPECT_FALSE(token_status.token_receive_time.is_null()); | 359 EXPECT_FALSE(token_status.token_receive_time.is_null()); |
| 545 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), | 360 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), |
| 546 token_status.last_get_token_error); | 361 token_status.last_get_token_error); |
| 547 EXPECT_TRUE(token_status.next_token_request_time.is_null()); | 362 EXPECT_TRUE(token_status.next_token_request_time.is_null()); |
| 548 | 363 |
| 549 // Simulate successful connection. | 364 // Simulate successful connection. |
| 550 service_->OnConnectionStatusChange(syncer::CONNECTION_OK); | 365 service()->OnConnectionStatusChange(syncer::CONNECTION_OK); |
| 551 token_status = service_->GetSyncTokenStatus(); | 366 token_status = service()->GetSyncTokenStatus(); |
| 552 EXPECT_EQ(syncer::CONNECTION_OK, token_status.connection_status); | 367 EXPECT_EQ(syncer::CONNECTION_OK, token_status.connection_status); |
| 553 } | 368 } |
| 554 | 369 |
| 555 } // namespace | 370 } // namespace |
| 556 } // namespace browser_sync | 371 } // namespace browser_sync |
| OLD | NEW |