| 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/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/prefs/pref_service_syncable.h" | 9 #include "chrome/browser/prefs/pref_service_syncable.h" |
| 10 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" | 10 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 using browser_sync::SyncBackendHostMock; | 38 using browser_sync::SyncBackendHostMock; |
| 39 using content::BrowserThread; | 39 using content::BrowserThread; |
| 40 using sync_driver::DataTypeManager; | 40 using sync_driver::DataTypeManager; |
| 41 using sync_driver::DataTypeManagerMock; | 41 using sync_driver::DataTypeManagerMock; |
| 42 using testing::_; | 42 using testing::_; |
| 43 using testing::AnyNumber; | 43 using testing::AnyNumber; |
| 44 using testing::DoAll; | 44 using testing::DoAll; |
| 45 using testing::InvokeArgument; | 45 using testing::InvokeArgument; |
| 46 using testing::Mock; | 46 using testing::Mock; |
| 47 using testing::Return; | 47 using testing::Return; |
| 48 using testing::SaveArg; | |
| 49 | 48 |
| 50 ACTION_P(InvokeOnConfigureStart, pss) { | 49 ACTION_P(InvokeOnConfigureStart, pss) { |
| 51 ProfileSyncService* service = | 50 ProfileSyncService* service = |
| 52 static_cast<ProfileSyncService*>(pss); | 51 static_cast<ProfileSyncService*>(pss); |
| 53 service->OnConfigureStart(); | 52 service->OnConfigureStart(); |
| 54 } | 53 } |
| 55 | 54 |
| 56 ACTION_P3(InvokeOnConfigureDone, pss, error_callback, result) { | 55 ACTION_P2(InvokeOnConfigureDone, pss, result) { |
| 57 ProfileSyncService* service = | 56 ProfileSyncService* service = |
| 58 static_cast<ProfileSyncService*>(pss); | 57 static_cast<ProfileSyncService*>(pss); |
| 59 DataTypeManager::ConfigureResult configure_result = | 58 DataTypeManager::ConfigureResult configure_result = |
| 60 static_cast<DataTypeManager::ConfigureResult>(result); | 59 static_cast<DataTypeManager::ConfigureResult>(result); |
| 61 if (result.status == sync_driver::DataTypeManager::ABORTED) | |
| 62 error_callback.Run(); | |
| 63 service->OnConfigureDone(configure_result); | 60 service->OnConfigureDone(configure_result); |
| 64 } | 61 } |
| 65 | 62 |
| 66 class ProfileSyncServiceStartupTest : public testing::Test { | 63 class ProfileSyncServiceStartupTest : public testing::Test { |
| 67 public: | 64 public: |
| 68 ProfileSyncServiceStartupTest() | 65 ProfileSyncServiceStartupTest() |
| 69 : thread_bundle_(content::TestBrowserThreadBundle::REAL_DB_THREAD | | 66 : thread_bundle_(content::TestBrowserThreadBundle::REAL_DB_THREAD | |
| 70 content::TestBrowserThreadBundle::REAL_FILE_THREAD | | 67 content::TestBrowserThreadBundle::REAL_FILE_THREAD | |
| 71 content::TestBrowserThreadBundle::REAL_IO_THREAD), | 68 content::TestBrowserThreadBundle::REAL_IO_THREAD), |
| 72 profile_manager_(TestingBrowserProcess::GetGlobal()), | 69 profile_manager_(TestingBrowserProcess::GetGlobal()), |
| 73 sync_(NULL), | 70 sync_(NULL) {} |
| 74 failed_data_types_handler_(NULL) {} | |
| 75 | 71 |
| 76 virtual ~ProfileSyncServiceStartupTest() { | 72 virtual ~ProfileSyncServiceStartupTest() { |
| 77 } | 73 } |
| 78 | 74 |
| 79 virtual void SetUp() { | 75 virtual void SetUp() { |
| 80 CHECK(profile_manager_.SetUp()); | 76 CHECK(profile_manager_.SetUp()); |
| 81 | 77 |
| 82 TestingProfile::TestingFactories testing_facotries; | 78 TestingProfile::TestingFactories testing_facotries; |
| 83 testing_facotries.push_back( | 79 testing_facotries.push_back( |
| 84 std::make_pair(SigninManagerFactory::GetInstance(), | 80 std::make_pair(SigninManagerFactory::GetInstance(), |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 } | 120 } |
| 125 | 121 |
| 126 ProfileSyncComponentsFactoryMock* components_factory_mock() { | 122 ProfileSyncComponentsFactoryMock* components_factory_mock() { |
| 127 return static_cast<ProfileSyncComponentsFactoryMock*>(sync_->factory()); | 123 return static_cast<ProfileSyncComponentsFactoryMock*>(sync_->factory()); |
| 128 } | 124 } |
| 129 | 125 |
| 130 FakeSigninManagerForTesting* fake_signin() { | 126 FakeSigninManagerForTesting* fake_signin() { |
| 131 return static_cast<FakeSigninManagerForTesting*>(sync_->signin()); | 127 return static_cast<FakeSigninManagerForTesting*>(sync_->signin()); |
| 132 } | 128 } |
| 133 | 129 |
| 134 void SetError() { | |
| 135 sync_driver::FailedDataTypesHandler::TypeErrorMap errors; | |
| 136 errors[syncer::BOOKMARKS] = | |
| 137 syncer::SyncError(FROM_HERE, | |
| 138 syncer::SyncError::UNRECOVERABLE_ERROR, | |
| 139 "Error", | |
| 140 syncer::BOOKMARKS); | |
| 141 failed_data_types_handler_->UpdateFailedDataTypes(errors); | |
| 142 } | |
| 143 | |
| 144 protected: | 130 protected: |
| 145 void SimulateTestUserSignin() { | 131 void SimulateTestUserSignin() { |
| 146 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, | 132 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, |
| 147 "test_user@gmail.com"); | 133 "test_user@gmail.com"); |
| 148 #if !defined(OS_CHROMEOS) | 134 #if !defined(OS_CHROMEOS) |
| 149 fake_signin()->SignIn("test_user@gmail.com", ""); | 135 fake_signin()->SignIn("test_user@gmail.com", ""); |
| 150 #else | 136 #else |
| 151 fake_signin()->SetAuthenticatedUsername("test_user@gmail.com"); | 137 fake_signin()->SetAuthenticatedUsername("test_user@gmail.com"); |
| 152 sync_->GoogleSigninSucceeded("test_user@gmail.com", ""); | 138 sync_->GoogleSigninSucceeded("test_user@gmail.com", ""); |
| 153 #endif | 139 #endif |
| 154 } | 140 } |
| 155 | 141 |
| 156 DataTypeManagerMock* SetUpDataTypeManager() { | 142 DataTypeManagerMock* SetUpDataTypeManager() { |
| 157 DataTypeManagerMock* data_type_manager = new DataTypeManagerMock(); | 143 DataTypeManagerMock* data_type_manager = new DataTypeManagerMock(); |
| 158 EXPECT_CALL(*components_factory_mock(), | 144 EXPECT_CALL(*components_factory_mock(), |
| 159 CreateDataTypeManager(_, _, _, _, _, _)). | 145 CreateDataTypeManager(_, _, _, _, _, _)). |
| 160 WillOnce(DoAll(SaveArg<5>(&failed_data_types_handler_), | 146 WillOnce(Return(data_type_manager)); |
| 161 Return(data_type_manager))); | |
| 162 return data_type_manager; | 147 return data_type_manager; |
| 163 } | 148 } |
| 164 | 149 |
| 165 browser_sync::SyncBackendHostMock* SetUpSyncBackendHost() { | 150 browser_sync::SyncBackendHostMock* SetUpSyncBackendHost() { |
| 166 browser_sync::SyncBackendHostMock* sync_backend_host = | 151 browser_sync::SyncBackendHostMock* sync_backend_host = |
| 167 new browser_sync::SyncBackendHostMock(); | 152 new browser_sync::SyncBackendHostMock(); |
| 168 EXPECT_CALL(*components_factory_mock(), | 153 EXPECT_CALL(*components_factory_mock(), |
| 169 CreateSyncBackendHost(_, _, _, _, _)). | 154 CreateSyncBackendHost(_, _, _, _, _)). |
| 170 WillOnce(Return(sync_backend_host)); | 155 WillOnce(Return(sync_backend_host)); |
| 171 return sync_backend_host; | 156 return sync_backend_host; |
| 172 } | 157 } |
| 173 | 158 |
| 174 content::TestBrowserThreadBundle thread_bundle_; | 159 content::TestBrowserThreadBundle thread_bundle_; |
| 175 TestingProfileManager profile_manager_; | 160 TestingProfileManager profile_manager_; |
| 176 TestingProfile* profile_; | 161 TestingProfile* profile_; |
| 177 ProfileSyncService* sync_; | 162 ProfileSyncService* sync_; |
| 178 ProfileSyncServiceObserverMock observer_; | 163 ProfileSyncServiceObserverMock observer_; |
| 179 sync_driver::FailedDataTypesHandler* failed_data_types_handler_; | |
| 180 }; | 164 }; |
| 181 | 165 |
| 182 class ProfileSyncServiceStartupCrosTest : public ProfileSyncServiceStartupTest { | 166 class ProfileSyncServiceStartupCrosTest : public ProfileSyncServiceStartupTest { |
| 183 public: | 167 public: |
| 184 virtual void SetUp() { | 168 virtual void SetUp() { |
| 185 ProfileSyncServiceStartupTest::SetUp(); | 169 ProfileSyncServiceStartupTest::SetUp(); |
| 186 sync_ = static_cast<ProfileSyncService*>( | 170 sync_ = static_cast<ProfileSyncService*>( |
| 187 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 171 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 188 profile_, BuildCrosService)); | 172 profile_, BuildCrosService)); |
| 189 sync_->AddObserver(&observer_); | 173 sync_->AddObserver(&observer_); |
| (...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 TEST_F(ProfileSyncServiceStartupTest, StartFailure) { | 501 TEST_F(ProfileSyncServiceStartupTest, StartFailure) { |
| 518 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, | 502 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, |
| 519 "test_user@gmail.com"); | 503 "test_user@gmail.com"); |
| 520 SigninManagerFactory::GetForProfile(profile_) | 504 SigninManagerFactory::GetForProfile(profile_) |
| 521 ->SetAuthenticatedUsername("test_user@gmail.com"); | 505 ->SetAuthenticatedUsername("test_user@gmail.com"); |
| 522 CreateSyncService(); | 506 CreateSyncService(); |
| 523 sync_->SetSyncSetupCompleted(); | 507 sync_->SetSyncSetupCompleted(); |
| 524 SetUpSyncBackendHost(); | 508 SetUpSyncBackendHost(); |
| 525 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager(); | 509 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager(); |
| 526 DataTypeManager::ConfigureStatus status = DataTypeManager::ABORTED; | 510 DataTypeManager::ConfigureStatus status = DataTypeManager::ABORTED; |
| 511 syncer::SyncError error( |
| 512 FROM_HERE, |
| 513 syncer::SyncError::DATATYPE_ERROR, |
| 514 "Association failed.", |
| 515 syncer::BOOKMARKS); |
| 516 std::map<syncer::ModelType, syncer::SyncError> errors; |
| 517 errors[syncer::BOOKMARKS] = error; |
| 527 DataTypeManager::ConfigureResult result( | 518 DataTypeManager::ConfigureResult result( |
| 528 status, | 519 status, |
| 520 syncer::ModelTypeSet(), |
| 521 errors, |
| 522 syncer::ModelTypeSet(), |
| 529 syncer::ModelTypeSet()); | 523 syncer::ModelTypeSet()); |
| 530 EXPECT_CALL(*data_type_manager, Configure(_, _)).WillRepeatedly( | 524 EXPECT_CALL(*data_type_manager, Configure(_, _)). |
| 531 DoAll(InvokeOnConfigureStart(sync_), | 525 WillRepeatedly( |
| 532 InvokeOnConfigureDone( | 526 DoAll(InvokeOnConfigureStart(sync_), |
| 533 sync_, | 527 InvokeOnConfigureDone(sync_, result))); |
| 534 base::Bind(&ProfileSyncServiceStartupTest::SetError, | |
| 535 base::Unretained(this)), | |
| 536 result))); | |
| 537 EXPECT_CALL(*data_type_manager, state()). | 528 EXPECT_CALL(*data_type_manager, state()). |
| 538 WillOnce(Return(DataTypeManager::STOPPED)); | 529 WillOnce(Return(DataTypeManager::STOPPED)); |
| 539 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); | 530 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); |
| 540 IssueTestTokens(); | 531 IssueTestTokens(); |
| 541 sync_->Initialize(); | 532 sync_->Initialize(); |
| 542 EXPECT_TRUE(sync_->HasUnrecoverableError()); | 533 EXPECT_TRUE(sync_->HasUnrecoverableError()); |
| 543 } | 534 } |
| 544 | 535 |
| 545 TEST_F(ProfileSyncServiceStartupTest, StartDownloadFailed) { | 536 TEST_F(ProfileSyncServiceStartupTest, StartDownloadFailed) { |
| 546 // Pre load the tokens | 537 // Pre load the tokens |
| 547 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, | 538 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, |
| 548 "test_user@gmail.com"); | 539 "test_user@gmail.com"); |
| 549 SigninManagerFactory::GetForProfile(profile_) | 540 SigninManagerFactory::GetForProfile(profile_) |
| 550 ->SetAuthenticatedUsername("test_user@gmail.com"); | 541 ->SetAuthenticatedUsername("test_user@gmail.com"); |
| 551 CreateSyncService(); | 542 CreateSyncService(); |
| 552 SyncBackendHostMock* mock_sbh = SetUpSyncBackendHost(); | 543 SyncBackendHostMock* mock_sbh = SetUpSyncBackendHost(); |
| 553 mock_sbh->set_fail_initial_download(true); | 544 mock_sbh->set_fail_initial_download(true); |
| 554 | 545 |
| 555 profile_->GetPrefs()->ClearPref(sync_driver::prefs::kSyncHasSetupCompleted); | 546 profile_->GetPrefs()->ClearPref(sync_driver::prefs::kSyncHasSetupCompleted); |
| 556 | 547 |
| 557 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); | 548 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); |
| 558 sync_->Initialize(); | 549 sync_->Initialize(); |
| 559 | 550 |
| 560 sync_->SetSetupInProgress(true); | 551 sync_->SetSetupInProgress(true); |
| 561 IssueTestTokens(); | 552 IssueTestTokens(); |
| 562 sync_->SetSetupInProgress(false); | 553 sync_->SetSetupInProgress(false); |
| 563 EXPECT_FALSE(sync_->sync_initialized()); | 554 EXPECT_FALSE(sync_->sync_initialized()); |
| 564 } | 555 } |
| OLD | NEW |