| 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" | |
| 12 #include "chrome/browser/signin/signin_manager.h" | 11 #include "chrome/browser/signin/signin_manager.h" |
| 13 #include "chrome/browser/signin/signin_manager_factory.h" | 12 #include "chrome/browser/signin/signin_manager_factory.h" |
| 14 #include "chrome/browser/signin/token_service.h" | 13 #include "chrome/browser/signin/token_service.h" |
| 15 #include "chrome/browser/signin/token_service_factory.h" | 14 #include "chrome/browser/signin/token_service_factory.h" |
| 16 #include "chrome/browser/sync/fake_oauth2_token_service.h" | 15 #include "chrome/browser/sync/fake_oauth2_token_service.h" |
| 17 #include "chrome/browser/sync/glue/data_type_manager_impl.h" | 16 #include "chrome/browser/sync/glue/bookmark_data_type_controller.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" |
| 20 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
| 21 #include "chrome/test/base/testing_pref_service_syncable.h" | 23 #include "chrome/test/base/testing_pref_service_syncable.h" |
| 22 #include "chrome/test/base/testing_profile.h" | 24 #include "chrome/test/base/testing_profile.h" |
| 23 #include "content/public/test/test_browser_thread_bundle.h" | 25 #include "content/public/test/test_browser_thread_bundle.h" |
| 26 #include "content/public/test/test_utils.h" |
| 27 #include "google/cacheinvalidation/include/types.h" |
| 24 #include "google_apis/gaia/gaia_constants.h" | 28 #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" |
| 25 #include "testing/gmock/include/gmock/gmock.h" | 32 #include "testing/gmock/include/gmock/gmock.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 34 |
| 35 // TODO(akalin): Add tests here that exercise the whole |
| 36 // ProfileSyncService/SyncBackendHost stack while mocking out as |
| 37 // little as possible. |
| 38 |
| 28 namespace browser_sync { | 39 namespace browser_sync { |
| 29 | 40 |
| 30 namespace { | 41 namespace { |
| 31 | 42 |
| 32 ACTION(ReturnNewDataTypeManager) { | 43 using testing::_; |
| 33 return new browser_sync::DataTypeManagerImpl(arg0, | 44 using testing::AtLeast; |
| 34 arg1, | 45 using testing::AtMost; |
| 35 arg2, | 46 using testing::Mock; |
| 36 arg3, | 47 using testing::Return; |
| 37 arg4, | 48 using testing::StrictMock; |
| 38 arg5); | 49 |
| 50 void SignalDone(base::WaitableEvent* done) { |
| 51 done->Signal(); |
| 39 } | 52 } |
| 40 | 53 |
| 41 using testing::_; | 54 class ProfileSyncServiceTest : public testing::Test { |
| 42 using testing::StrictMock; | 55 protected: |
| 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 }; |
| 43 | 140 |
| 44 class TestProfileSyncServiceObserver : public ProfileSyncServiceObserver { | 141 class TestProfileSyncServiceObserver : public ProfileSyncServiceObserver { |
| 45 public: | 142 public: |
| 46 explicit TestProfileSyncServiceObserver(ProfileSyncService* service) | 143 explicit TestProfileSyncServiceObserver(ProfileSyncService* service) |
| 47 : service_(service), first_setup_in_progress_(false) {} | 144 : service_(service), first_setup_in_progress_(false) {} |
| 48 virtual void OnStateChanged() OVERRIDE { | 145 virtual void OnStateChanged() OVERRIDE { |
| 49 first_setup_in_progress_ = service_->FirstSetupInProgress(); | 146 first_setup_in_progress_ = service_->FirstSetupInProgress(); |
| 50 } | 147 } |
| 51 bool first_setup_in_progress() const { return first_setup_in_progress_; } | 148 bool first_setup_in_progress() const { return first_setup_in_progress_; } |
| 52 private: | 149 private: |
| (...skipping 23 matching lines...) Expand all Loading... |
| 76 } | 173 } |
| 77 | 174 |
| 78 ACTION(ReturnNewSyncBackendHostNoReturn) { | 175 ACTION(ReturnNewSyncBackendHostNoReturn) { |
| 79 return new browser_sync::SyncBackendHostNoReturn(); | 176 return new browser_sync::SyncBackendHostNoReturn(); |
| 80 } | 177 } |
| 81 | 178 |
| 82 // A test harness that uses a real ProfileSyncService and in most cases a | 179 // A test harness that uses a real ProfileSyncService and in most cases a |
| 83 // MockSyncBackendHost. | 180 // MockSyncBackendHost. |
| 84 // | 181 // |
| 85 // This is useful if we want to test the ProfileSyncService and don't care about | 182 // This is useful if we want to test the ProfileSyncService and don't care about |
| 86 // testing the SyncBackendHost. | 183 // testing the SyncBackendHost. It's easier to use than the other tests, since |
| 87 class ProfileSyncServiceTest : public ::testing::Test { | 184 // it doesn't involve any threads. |
| 185 class ProfileSyncServiceSimpleTest : public ::testing::Test { |
| 88 protected: | 186 protected: |
| 89 ProfileSyncServiceTest() | 187 ProfileSyncServiceSimpleTest() |
| 90 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} | 188 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 91 virtual ~ProfileSyncServiceTest() {} | 189 virtual ~ProfileSyncServiceSimpleTest() {} |
| 92 | 190 |
| 93 virtual void SetUp() OVERRIDE { | 191 virtual void SetUp() OVERRIDE { |
| 94 TestingProfile::Builder builder; | 192 TestingProfile::Builder builder; |
| 95 | 193 |
| 96 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), | 194 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), |
| 97 FakeOAuth2TokenService::BuildTokenService); | 195 FakeOAuth2TokenService::BuildTokenService); |
| 98 invalidation::InvalidationServiceFactory::GetInstance()-> | 196 invalidation::InvalidationServiceFactory::GetInstance()-> |
| 99 SetBuildOnlyFakeInvalidatorsForTest(true); | 197 SetBuildOnlyFakeInvalidatorsForTest(true); |
| 100 | 198 |
| 101 profile_ = builder.Build().Pass(); | 199 profile_ = builder.Build().Pass(); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 scoped_ptr<TestingProfile> profile_; | 269 scoped_ptr<TestingProfile> profile_; |
| 172 scoped_ptr<ProfileSyncService> service_; | 270 scoped_ptr<ProfileSyncService> service_; |
| 173 | 271 |
| 174 // Pointer to the components factory. Not owned. May be null. | 272 // Pointer to the components factory. Not owned. May be null. |
| 175 ProfileSyncComponentsFactoryMock* components_factory_; | 273 ProfileSyncComponentsFactoryMock* components_factory_; |
| 176 | 274 |
| 177 content::TestBrowserThreadBundle thread_bundle_; | 275 content::TestBrowserThreadBundle thread_bundle_; |
| 178 }; | 276 }; |
| 179 | 277 |
| 180 // Verify that the server URLs are sane. | 278 // Verify that the server URLs are sane. |
| 181 TEST_F(ProfileSyncServiceTest, InitialState) { | 279 TEST_F(ProfileSyncServiceSimpleTest, InitialState) { |
| 182 CreateService(ProfileSyncService::AUTO_START); | 280 CreateService(ProfileSyncService::AUTO_START); |
| 183 Initialize(); | 281 Initialize(); |
| 184 const std::string& url = service()->sync_service_url().spec(); | 282 const std::string& url = service()->sync_service_url().spec(); |
| 185 EXPECT_TRUE(url == ProfileSyncService::kSyncServerUrl || | 283 EXPECT_TRUE(url == ProfileSyncService::kSyncServerUrl || |
| 186 url == ProfileSyncService::kDevServerUrl); | 284 url == ProfileSyncService::kDevServerUrl); |
| 187 } | 285 } |
| 188 | 286 |
| 189 // Verify a successful initialization. | 287 // Verify a successful initialization. |
| 190 TEST_F(ProfileSyncServiceTest, SuccessfulInitialization) { | 288 TEST_F(ProfileSyncServiceSimpleTest, SuccessfulInitialization) { |
| 191 profile()->GetTestingPrefService()->SetManagedPref( | 289 profile()->GetTestingPrefService()->SetManagedPref( |
| 192 prefs::kSyncManaged, | 290 prefs::kSyncManaged, |
| 193 Value::CreateBooleanValue(false)); | 291 Value::CreateBooleanValue(false)); |
| 194 IssueTestTokens(); | 292 IssueTestTokens(); |
| 195 CreateService(ProfileSyncService::AUTO_START); | 293 CreateService(ProfileSyncService::AUTO_START); |
| 196 ExpectDataTypeManagerCreation(); | 294 ExpectDataTypeManagerCreation(); |
| 197 ExpectSyncBackendHostCreation(); | 295 ExpectSyncBackendHostCreation(); |
| 198 Initialize(); | 296 Initialize(); |
| 199 EXPECT_FALSE(service()->IsManaged()); | 297 EXPECT_FALSE(service()->IsManaged()); |
| 200 EXPECT_TRUE(service()->sync_initialized()); | 298 EXPECT_TRUE(service()->sync_initialized()); |
| 201 } | 299 } |
| 202 | 300 |
| 203 | 301 |
| 204 // Verify that the SetSetupInProgress function call updates state | 302 // Verify that the SetSetupInProgress function call updates state |
| 205 // and notifies observers. | 303 // and notifies observers. |
| 206 TEST_F(ProfileSyncServiceTest, SetupInProgress) { | 304 TEST_F(ProfileSyncServiceSimpleTest, SetupInProgress) { |
| 207 CreateService(ProfileSyncService::MANUAL_START); | 305 CreateService(ProfileSyncService::MANUAL_START); |
| 208 Initialize(); | 306 Initialize(); |
| 209 | 307 |
| 210 TestProfileSyncServiceObserver observer(service()); | 308 TestProfileSyncServiceObserver observer(service()); |
| 211 service()->AddObserver(&observer); | 309 service()->AddObserver(&observer); |
| 212 | 310 |
| 213 service()->SetSetupInProgress(true); | 311 service()->SetSetupInProgress(true); |
| 214 EXPECT_TRUE(observer.first_setup_in_progress()); | 312 EXPECT_TRUE(observer.first_setup_in_progress()); |
| 215 service()->SetSetupInProgress(false); | 313 service()->SetSetupInProgress(false); |
| 216 EXPECT_FALSE(observer.first_setup_in_progress()); | 314 EXPECT_FALSE(observer.first_setup_in_progress()); |
| 217 | 315 |
| 218 service()->RemoveObserver(&observer); | 316 service()->RemoveObserver(&observer); |
| 219 } | 317 } |
| 220 | 318 |
| 221 // Verify that disable by enterprise policy works. | 319 // Verify that disable by enterprise policy works. |
| 222 TEST_F(ProfileSyncServiceTest, DisabledByPolicyBeforeInit) { | 320 TEST_F(ProfileSyncServiceSimpleTest, DisabledByPolicyBeforeInit) { |
| 223 profile()->GetTestingPrefService()->SetManagedPref( | 321 profile()->GetTestingPrefService()->SetManagedPref( |
| 224 prefs::kSyncManaged, | 322 prefs::kSyncManaged, |
| 225 Value::CreateBooleanValue(true)); | 323 Value::CreateBooleanValue(true)); |
| 226 IssueTestTokens(); | 324 IssueTestTokens(); |
| 227 CreateService(ProfileSyncService::AUTO_START); | 325 CreateService(ProfileSyncService::AUTO_START); |
| 228 Initialize(); | 326 Initialize(); |
| 229 EXPECT_TRUE(service()->IsManaged()); | 327 EXPECT_TRUE(service()->IsManaged()); |
| 230 EXPECT_FALSE(service()->sync_initialized()); | 328 EXPECT_FALSE(service()->sync_initialized()); |
| 231 } | 329 } |
| 232 | 330 |
| 233 // Verify that disable by enterprise policy works even after the backend has | 331 // Verify that disable by enterprise policy works even after the backend has |
| 234 // been initialized. | 332 // been initialized. |
| 235 TEST_F(ProfileSyncServiceTest, DisabledByPolicyAfterInit) { | 333 TEST_F(ProfileSyncServiceSimpleTest, DisabledByPolicyAfterInit) { |
| 236 IssueTestTokens(); | 334 IssueTestTokens(); |
| 237 CreateService(ProfileSyncService::AUTO_START); | 335 CreateService(ProfileSyncService::AUTO_START); |
| 238 ExpectDataTypeManagerCreation(); | 336 ExpectDataTypeManagerCreation(); |
| 239 ExpectSyncBackendHostCreation(); | 337 ExpectSyncBackendHostCreation(); |
| 240 Initialize(); | 338 Initialize(); |
| 241 | 339 |
| 242 EXPECT_FALSE(service()->IsManaged()); | 340 EXPECT_FALSE(service()->IsManaged()); |
| 243 EXPECT_TRUE(service()->sync_initialized()); | 341 EXPECT_TRUE(service()->sync_initialized()); |
| 244 | 342 |
| 245 profile()->GetTestingPrefService()->SetManagedPref( | 343 profile()->GetTestingPrefService()->SetManagedPref( |
| 246 prefs::kSyncManaged, | 344 prefs::kSyncManaged, |
| 247 Value::CreateBooleanValue(true)); | 345 Value::CreateBooleanValue(true)); |
| 248 | 346 |
| 249 EXPECT_TRUE(service()->IsManaged()); | 347 EXPECT_TRUE(service()->IsManaged()); |
| 250 EXPECT_FALSE(service()->sync_initialized()); | 348 EXPECT_FALSE(service()->sync_initialized()); |
| 251 } | 349 } |
| 252 | 350 |
| 253 // Exercies the ProfileSyncService's code paths related to getting shut down | 351 // Exercies the ProfileSyncService's code paths related to getting shut down |
| 254 // before the backend initialize call returns. | 352 // before the backend initialize call returns. |
| 255 TEST_F(ProfileSyncServiceTest, AbortedByShutdown) { | 353 TEST_F(ProfileSyncServiceSimpleTest, AbortedByShutdown) { |
| 256 CreateService(ProfileSyncService::AUTO_START); | 354 CreateService(ProfileSyncService::AUTO_START); |
| 257 PrepareDelayedInitSyncBackendHost(); | 355 PrepareDelayedInitSyncBackendHost(); |
| 258 | 356 |
| 259 IssueTestTokens(); | 357 IssueTestTokens(); |
| 260 Initialize(); | 358 Initialize(); |
| 261 EXPECT_FALSE(service()->sync_initialized()); | 359 EXPECT_FALSE(service()->sync_initialized()); |
| 262 | 360 |
| 263 ShutdownAndDeleteService(); | 361 ShutdownAndDeleteService(); |
| 264 } | 362 } |
| 265 | 363 |
| 266 // Test StopAndSuppress() before we've initialized the backend. | 364 // Test StopAndSuppress() before we've initialized the backend. |
| 267 TEST_F(ProfileSyncServiceTest, EarlyStopAndSuppress) { | 365 TEST_F(ProfileSyncServiceSimpleTest, EarlyStopAndSuppress) { |
| 268 CreateService(ProfileSyncService::AUTO_START); | 366 CreateService(ProfileSyncService::AUTO_START); |
| 269 IssueTestTokens(); | 367 IssueTestTokens(); |
| 270 | 368 |
| 271 service()->StopAndSuppress(); | 369 service()->StopAndSuppress(); |
| 272 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); | 370 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); |
| 273 | 371 |
| 274 // Because of supression, this should fail. | 372 // Because of supression, this should fail. |
| 275 Initialize(); | 373 Initialize(); |
| 276 EXPECT_FALSE(service()->sync_initialized()); | 374 EXPECT_FALSE(service()->sync_initialized()); |
| 277 | 375 |
| 278 // Remove suppression. This should be enough to allow init to happen. | 376 // Remove suppression. This should be enough to allow init to happen. |
| 279 ExpectDataTypeManagerCreation(); | 377 ExpectDataTypeManagerCreation(); |
| 280 ExpectSyncBackendHostCreation(); | 378 ExpectSyncBackendHostCreation(); |
| 281 service()->UnsuppressAndStart(); | 379 service()->UnsuppressAndStart(); |
| 282 EXPECT_TRUE(service()->sync_initialized()); | 380 EXPECT_TRUE(service()->sync_initialized()); |
| 283 EXPECT_FALSE( | 381 EXPECT_FALSE( |
| 284 profile()->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); | 382 profile()->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); |
| 285 } | 383 } |
| 286 | 384 |
| 287 // Test StopAndSuppress() after we've initialized the backend. | 385 // Test StopAndSuppress() after we've initialized the backend. |
| 288 TEST_F(ProfileSyncServiceTest, DisableAndEnableSyncTemporarily) { | 386 TEST_F(ProfileSyncServiceSimpleTest, DisableAndEnableSyncTemporarily) { |
| 289 CreateService(ProfileSyncService::AUTO_START); | 387 CreateService(ProfileSyncService::AUTO_START); |
| 290 IssueTestTokens(); | 388 IssueTestTokens(); |
| 291 ExpectDataTypeManagerCreation(); | 389 ExpectDataTypeManagerCreation(); |
| 292 ExpectSyncBackendHostCreation(); | 390 ExpectSyncBackendHostCreation(); |
| 293 Initialize(); | 391 Initialize(); |
| 294 | 392 |
| 295 EXPECT_TRUE(service()->sync_initialized()); | 393 EXPECT_TRUE(service()->sync_initialized()); |
| 296 EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); | 394 EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); |
| 297 | 395 |
| 298 testing::Mock::VerifyAndClearExpectations(components_factory()); | 396 testing::Mock::VerifyAndClearExpectations(components_factory()); |
| 299 | 397 |
| 300 service()->StopAndSuppress(); | 398 service()->StopAndSuppress(); |
| 301 EXPECT_FALSE(service()->sync_initialized()); | 399 EXPECT_FALSE(service()->sync_initialized()); |
| 302 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); | 400 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); |
| 303 | 401 |
| 304 ExpectDataTypeManagerCreation(); | 402 ExpectDataTypeManagerCreation(); |
| 305 ExpectSyncBackendHostCreation(); | 403 ExpectSyncBackendHostCreation(); |
| 306 | 404 |
| 307 service()->UnsuppressAndStart(); | 405 service()->UnsuppressAndStart(); |
| 308 EXPECT_TRUE(service()->sync_initialized()); | 406 EXPECT_TRUE(service()->sync_initialized()); |
| 309 EXPECT_FALSE( | 407 EXPECT_FALSE( |
| 310 profile()->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); | 408 profile()->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); |
| 311 } | 409 } |
| 312 | 410 |
| 313 // Certain ProfileSyncService tests don't apply to Chrome OS, for example | 411 // Certain ProfileSyncService tests don't apply to Chrome OS, for example |
| 314 // things that deal with concepts like "signing out" and policy. | 412 // things that deal with concepts like "signing out" and policy. |
| 315 #if !defined (OS_CHROMEOS) | 413 #if !defined (OS_CHROMEOS) |
| 316 | 414 |
| 317 TEST_F(ProfileSyncServiceTest, EnableSyncAndSignOut) { | 415 TEST_F(ProfileSyncServiceSimpleTest, EnableSyncAndSignOut) { |
| 318 CreateService(ProfileSyncService::AUTO_START); | 416 CreateService(ProfileSyncService::AUTO_START); |
| 319 ExpectDataTypeManagerCreation(); | 417 ExpectDataTypeManagerCreation(); |
| 320 ExpectSyncBackendHostCreation(); | 418 ExpectSyncBackendHostCreation(); |
| 321 IssueTestTokens(); | 419 IssueTestTokens(); |
| 322 Initialize(); | 420 Initialize(); |
| 323 | 421 |
| 324 EXPECT_TRUE(service()->sync_initialized()); | 422 EXPECT_TRUE(service()->sync_initialized()); |
| 325 EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); | 423 EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(prefs::kSyncSuppressStart)); |
| 326 | 424 |
| 327 SigninManagerFactory::GetForProfile(profile())->SignOut(); | 425 SigninManagerFactory::GetForProfile(profile())->SignOut(); |
| 328 EXPECT_FALSE(service()->sync_initialized()); | 426 EXPECT_FALSE(service()->sync_initialized()); |
| 329 } | 427 } |
| 330 | 428 |
| 331 #endif // !defined(OS_CHROMEOS) | 429 #endif // !defined(OS_CHROMEOS) |
| 332 | 430 |
| 431 TEST_F(ProfileSyncServiceTest, JsControllerHandlersBasic) { |
| 432 StartSyncServiceAndSetInitialSyncEnded(); |
| 433 IssueTestTokens(); |
| 434 EXPECT_TRUE(service_->sync_initialized()); |
| 435 EXPECT_TRUE(service_->GetBackendForTest() != NULL); |
| 436 |
| 437 base::WeakPtr<syncer::JsController> js_controller = |
| 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 |
| 333 TEST_F(ProfileSyncServiceTest, GetSyncTokenStatus) { | 527 TEST_F(ProfileSyncServiceTest, GetSyncTokenStatus) { |
| 334 CreateService(ProfileSyncService::AUTO_START); | 528 StartSyncServiceAndSetInitialSyncEnded(); |
| 335 IssueTestTokens(); | 529 IssueTestTokens(); |
| 336 ExpectDataTypeManagerCreation(); | |
| 337 ExpectSyncBackendHostCreation(); | |
| 338 Initialize(); | |
| 339 | |
| 340 // Initial status. | |
| 341 ProfileSyncService::SyncTokenStatus token_status = | 530 ProfileSyncService::SyncTokenStatus token_status = |
| 342 service()->GetSyncTokenStatus(); | 531 service_->GetSyncTokenStatus(); |
| 343 EXPECT_EQ(syncer::CONNECTION_NOT_ATTEMPTED, token_status.connection_status); | 532 EXPECT_EQ(syncer::CONNECTION_NOT_ATTEMPTED, token_status.connection_status); |
| 344 EXPECT_TRUE(token_status.connection_status_update_time.is_null()); | 533 EXPECT_TRUE(token_status.connection_status_update_time.is_null()); |
| 345 EXPECT_TRUE(token_status.token_request_time.is_null()); | 534 EXPECT_TRUE(token_status.token_request_time.is_null()); |
| 346 EXPECT_TRUE(token_status.token_receive_time.is_null()); | 535 EXPECT_TRUE(token_status.token_receive_time.is_null()); |
| 347 | 536 |
| 348 // Simulate an auth error. | 537 // Sync engine is given invalid token at initialization and will report |
| 349 service()->OnConnectionStatusChange(syncer::CONNECTION_AUTH_ERROR); | 538 // auth error when trying to connect. |
| 350 | 539 service_->OnConnectionStatusChange(syncer::CONNECTION_AUTH_ERROR); |
| 351 // The token request will take the form of a posted task. Run it. | 540 token_status = service_->GetSyncTokenStatus(); |
| 352 base::RunLoop loop; | |
| 353 loop.RunUntilIdle(); | |
| 354 | |
| 355 token_status = service()->GetSyncTokenStatus(); | |
| 356 EXPECT_EQ(syncer::CONNECTION_AUTH_ERROR, token_status.connection_status); | 541 EXPECT_EQ(syncer::CONNECTION_AUTH_ERROR, token_status.connection_status); |
| 357 EXPECT_FALSE(token_status.connection_status_update_time.is_null()); | 542 EXPECT_FALSE(token_status.connection_status_update_time.is_null()); |
| 358 EXPECT_FALSE(token_status.token_request_time.is_null()); | 543 EXPECT_FALSE(token_status.token_request_time.is_null()); |
| 359 EXPECT_FALSE(token_status.token_receive_time.is_null()); | 544 EXPECT_FALSE(token_status.token_receive_time.is_null()); |
| 360 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), | 545 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(), |
| 361 token_status.last_get_token_error); | 546 token_status.last_get_token_error); |
| 362 EXPECT_TRUE(token_status.next_token_request_time.is_null()); | 547 EXPECT_TRUE(token_status.next_token_request_time.is_null()); |
| 363 | 548 |
| 364 // Simulate successful connection. | 549 // Simulate successful connection. |
| 365 service()->OnConnectionStatusChange(syncer::CONNECTION_OK); | 550 service_->OnConnectionStatusChange(syncer::CONNECTION_OK); |
| 366 token_status = service()->GetSyncTokenStatus(); | 551 token_status = service_->GetSyncTokenStatus(); |
| 367 EXPECT_EQ(syncer::CONNECTION_OK, token_status.connection_status); | 552 EXPECT_EQ(syncer::CONNECTION_OK, token_status.connection_status); |
| 368 } | 553 } |
| 369 | 554 |
| 370 } // namespace | 555 } // namespace |
| 371 } // namespace browser_sync | 556 } // namespace browser_sync |
| OLD | NEW |