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