| 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 "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 FakeTokenService() {} | 61 FakeTokenService() {} |
| 62 virtual ~FakeTokenService() {} | 62 virtual ~FakeTokenService() {} |
| 63 | 63 |
| 64 virtual void LoadTokensFromDB() OVERRIDE { | 64 virtual void LoadTokensFromDB() OVERRIDE { |
| 65 set_tokens_loaded(true); | 65 set_tokens_loaded(true); |
| 66 content::NotificationService::current()->Notify( | 66 content::NotificationService::current()->Notify( |
| 67 chrome::NOTIFICATION_TOKEN_LOADING_FINISHED, | 67 chrome::NOTIFICATION_TOKEN_LOADING_FINISHED, |
| 68 content::Source<TokenService>(this), | 68 content::Source<TokenService>(this), |
| 69 content::NotificationService::NoDetails()); | 69 content::NotificationService::NoDetails()); |
| 70 } | 70 } |
| 71 |
| 72 static BrowserContextKeyedService* BuildFakeTokenService( |
| 73 content::BrowserContext* profile) { |
| 74 return new FakeTokenService(); |
| 75 } |
| 71 }; | 76 }; |
| 72 | 77 |
| 73 class ProfileSyncServiceStartupTest : public testing::Test { | 78 class ProfileSyncServiceStartupTest : public testing::Test { |
| 74 public: | 79 public: |
| 75 ProfileSyncServiceStartupTest() | 80 ProfileSyncServiceStartupTest() |
| 76 : thread_bundle_(content::TestBrowserThreadBundle::REAL_DB_THREAD | | 81 : thread_bundle_(content::TestBrowserThreadBundle::REAL_DB_THREAD | |
| 77 content::TestBrowserThreadBundle::REAL_FILE_THREAD | | 82 content::TestBrowserThreadBundle::REAL_FILE_THREAD | |
| 78 content::TestBrowserThreadBundle::REAL_IO_THREAD), | 83 content::TestBrowserThreadBundle::REAL_IO_THREAD), |
| 79 profile_(new TestingProfile), | |
| 80 sync_(NULL) {} | 84 sync_(NULL) {} |
| 81 | 85 |
| 82 virtual ~ProfileSyncServiceStartupTest() { | 86 virtual ~ProfileSyncServiceStartupTest() { |
| 83 } | 87 } |
| 84 | 88 |
| 85 virtual void SetUp() { | 89 virtual void SetUp() { |
| 90 profile_ = CreateProfile(); |
| 91 } |
| 92 |
| 93 virtual scoped_ptr<TestingProfile> CreateProfile() { |
| 94 TestingProfile::Builder builder; |
| 86 #if defined(OS_CHROMEOS) | 95 #if defined(OS_CHROMEOS) |
| 87 SigninManagerFactory::GetInstance()->SetTestingFactory( | 96 builder.AddTestingFactory(SigninManagerFactory::GetInstance(), |
| 88 profile_.get(), FakeSigninManagerBase::Build); | 97 FakeSigninManagerBase::Build); |
| 89 #else | 98 #else |
| 90 SigninManagerFactory::GetInstance()->SetTestingFactory( | 99 builder.AddTestingFactory(SigninManagerFactory::GetInstance(), |
| 91 profile_.get(), FakeSigninManager::Build); | 100 FakeSigninManager::Build); |
| 92 #endif | 101 #endif |
| 102 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), |
| 103 FakeOAuth2TokenService::BuildTokenService); |
| 104 builder.AddTestingFactory(ProfileSyncServiceFactory::GetInstance(), |
| 105 BuildService); |
| 106 builder.AddTestingFactory(TokenServiceFactory::GetInstance(), |
| 107 FakeTokenService::BuildFakeTokenService); |
| 108 return builder.Build(); |
| 93 } | 109 } |
| 94 | 110 |
| 95 virtual void TearDown() { | 111 virtual void TearDown() { |
| 96 sync_->RemoveObserver(&observer_); | 112 sync_->RemoveObserver(&observer_); |
| 97 ProfileSyncServiceFactory::GetInstance()->SetTestingFactory( | |
| 98 profile_.get(), NULL); | |
| 99 ProfileOAuth2TokenServiceFactory::GetInstance()->SetTestingFactory( | |
| 100 profile_.get(), NULL); | |
| 101 profile_.reset(); | 113 profile_.reset(); |
| 102 | 114 |
| 103 // Pump messages posted by the sync core thread (which may end up | 115 // Pump messages posted by the sync core thread (which may end up |
| 104 // posting on the IO thread). | 116 // posting on the IO thread). |
| 105 base::RunLoop().RunUntilIdle(); | 117 base::RunLoop().RunUntilIdle(); |
| 106 content::RunAllPendingInMessageLoop(content::BrowserThread::IO); | 118 content::RunAllPendingInMessageLoop(content::BrowserThread::IO); |
| 107 base::RunLoop().RunUntilIdle(); | 119 base::RunLoop().RunUntilIdle(); |
| 108 } | 120 } |
| 109 | 121 |
| 110 static BrowserContextKeyedService* BuildService( | 122 static BrowserContextKeyedService* BuildService( |
| 111 content::BrowserContext* profile) { | 123 content::BrowserContext* profile) { |
| 112 return new TestProfileSyncService( | 124 return new TestProfileSyncService( |
| 113 new ProfileSyncComponentsFactoryMock(), | 125 new ProfileSyncComponentsFactoryMock(), |
| 114 static_cast<Profile*>(profile), | 126 static_cast<Profile*>(profile), |
| 115 SigninManagerFactory::GetForProfile(static_cast<Profile*>(profile)), | 127 SigninManagerFactory::GetForProfile(static_cast<Profile*>(profile)), |
| 116 ProfileSyncService::MANUAL_START, | 128 ProfileSyncService::MANUAL_START, |
| 117 true); | 129 true); |
| 118 } | 130 } |
| 119 | 131 |
| 120 void CreateSyncService() { | 132 void CreateSyncService() { |
| 121 ProfileOAuth2TokenServiceFactory::GetInstance()->SetTestingFactory( | |
| 122 profile_.get(), FakeOAuth2TokenService::BuildTokenService); | |
| 123 sync_ = static_cast<TestProfileSyncService*>( | 133 sync_ = static_cast<TestProfileSyncService*>( |
| 124 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 134 ProfileSyncServiceFactory::GetForProfile(profile_.get())); |
| 125 profile_.get(), BuildService)); | |
| 126 sync_->AddObserver(&observer_); | 135 sync_->AddObserver(&observer_); |
| 127 sync_->set_synchronous_sync_configuration(); | 136 sync_->set_synchronous_sync_configuration(); |
| 128 } | 137 } |
| 129 | 138 |
| 130 protected: | 139 protected: |
| 131 DataTypeManagerMock* SetUpDataTypeManager() { | 140 DataTypeManagerMock* SetUpDataTypeManager() { |
| 132 DataTypeManagerMock* data_type_manager = new DataTypeManagerMock(); | 141 DataTypeManagerMock* data_type_manager = new DataTypeManagerMock(); |
| 133 EXPECT_CALL(*sync_->components_factory_mock(), | 142 EXPECT_CALL(*sync_->components_factory_mock(), |
| 134 CreateDataTypeManager(_, _, _, _, _, _)). | 143 CreateDataTypeManager(_, _, _, _, _, _)). |
| 135 WillOnce(Return(data_type_manager)); | 144 WillOnce(Return(data_type_manager)); |
| 136 return data_type_manager; | 145 return data_type_manager; |
| 137 } | 146 } |
| 138 | 147 |
| 139 content::TestBrowserThreadBundle thread_bundle_; | 148 content::TestBrowserThreadBundle thread_bundle_; |
| 140 scoped_ptr<TestingProfile> profile_; | 149 scoped_ptr<TestingProfile> profile_; |
| 141 TestProfileSyncService* sync_; | 150 TestProfileSyncService* sync_; |
| 142 ProfileSyncServiceObserverMock observer_; | 151 ProfileSyncServiceObserverMock observer_; |
| 143 }; | 152 }; |
| 144 | 153 |
| 145 class ProfileSyncServiceStartupCrosTest : public ProfileSyncServiceStartupTest { | 154 class ProfileSyncServiceStartupCrosTest : public ProfileSyncServiceStartupTest { |
| 146 public: | 155 public: |
| 147 virtual void SetUp() { | 156 virtual void SetUp() { |
| 148 ProfileSyncServiceStartupTest::SetUp(); | 157 ProfileSyncServiceStartupTest::SetUp(); |
| 149 ProfileOAuth2TokenServiceFactory::GetInstance()->SetTestingFactory( | |
| 150 profile_.get(), FakeOAuth2TokenService::BuildTokenService); | |
| 151 sync_ = static_cast<TestProfileSyncService*>( | 158 sync_ = static_cast<TestProfileSyncService*>( |
| 152 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 159 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 153 profile_.get(), BuildCrosService)); | 160 profile_.get(), BuildCrosService)); |
| 154 sync_->AddObserver(&observer_); | 161 sync_->AddObserver(&observer_); |
| 155 sync_->set_synchronous_sync_configuration(); | 162 sync_->set_synchronous_sync_configuration(); |
| 156 } | 163 } |
| 157 | 164 |
| 158 static BrowserContextKeyedService* BuildCrosService( | 165 static BrowserContextKeyedService* BuildCrosService( |
| 159 content::BrowserContext* context) { | 166 content::BrowserContext* context) { |
| 160 Profile* profile = static_cast<Profile*>(context); | 167 Profile* profile = static_cast<Profile*>(context); |
| 161 SigninManagerBase* signin = | 168 SigninManagerBase* signin = |
| 162 SigninManagerFactory::GetForProfile(profile); | 169 SigninManagerFactory::GetForProfile(profile); |
| 163 profile->GetPrefs()->SetString(prefs::kGoogleServicesUsername, | 170 profile->GetPrefs()->SetString(prefs::kGoogleServicesUsername, |
| 164 "test_user@gmail.com"); | 171 "test_user@gmail.com"); |
| 165 signin->Initialize(profile, NULL); | 172 signin->Initialize(profile, NULL); |
| 166 EXPECT_FALSE(signin->GetAuthenticatedUsername().empty()); | 173 EXPECT_FALSE(signin->GetAuthenticatedUsername().empty()); |
| 167 return new TestProfileSyncService( | 174 return new TestProfileSyncService( |
| 168 new ProfileSyncComponentsFactoryMock(), | 175 new ProfileSyncComponentsFactoryMock(), |
| 169 profile, | 176 profile, |
| 170 signin, | 177 signin, |
| 171 ProfileSyncService::AUTO_START, | 178 ProfileSyncService::AUTO_START, |
| 172 true); | 179 true); |
| 173 } | 180 } |
| 174 }; | 181 }; |
| 175 | 182 |
| 176 BrowserContextKeyedService* BuildFakeTokenService( | |
| 177 content::BrowserContext* profile) { | |
| 178 return new FakeTokenService(); | |
| 179 } | |
| 180 | |
| 181 TEST_F(ProfileSyncServiceStartupTest, StartFirstTime) { | 183 TEST_F(ProfileSyncServiceStartupTest, StartFirstTime) { |
| 182 // We've never completed startup. | 184 // We've never completed startup. |
| 183 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted); | 185 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted); |
| 184 SigninManagerFactory::GetForProfile( | 186 SigninManagerFactory::GetForProfile( |
| 185 profile_.get())->Initialize(profile_.get(), NULL); | 187 profile_.get())->Initialize(profile_.get(), NULL); |
| 186 CreateSyncService(); | 188 CreateSyncService(); |
| 187 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager(); | 189 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager(); |
| 188 EXPECT_CALL(*data_type_manager, Configure(_, _)).Times(0); | 190 EXPECT_CALL(*data_type_manager, Configure(_, _)).Times(0); |
| 189 | 191 |
| 190 // Should not actually start, rather just clean things up and wait | 192 // Should not actually start, rather just clean things up and wait |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 EXPECT_TRUE(sync_->ShouldPushChanges()); | 230 EXPECT_TRUE(sync_->ShouldPushChanges()); |
| 229 } | 231 } |
| 230 | 232 |
| 231 // TODO(pavely): Reenable test once android is switched to oauth2. | 233 // TODO(pavely): Reenable test once android is switched to oauth2. |
| 232 TEST_F(ProfileSyncServiceStartupTest, DISABLED_StartNoCredentials) { | 234 TEST_F(ProfileSyncServiceStartupTest, DISABLED_StartNoCredentials) { |
| 233 // We've never completed startup. | 235 // We've never completed startup. |
| 234 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted); | 236 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted); |
| 235 SigninManagerFactory::GetForProfile( | 237 SigninManagerFactory::GetForProfile( |
| 236 profile_.get())->Initialize(profile_.get(), NULL); | 238 profile_.get())->Initialize(profile_.get(), NULL); |
| 237 TokenService* token_service = static_cast<TokenService*>( | 239 TokenService* token_service = static_cast<TokenService*>( |
| 238 TokenServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 240 TokenServiceFactory::GetForProfile(profile_.get())); |
| 239 profile_.get(), BuildFakeTokenService)); | |
| 240 CreateSyncService(); | 241 CreateSyncService(); |
| 241 | 242 |
| 242 // Should not actually start, rather just clean things up and wait | 243 // Should not actually start, rather just clean things up and wait |
| 243 // to be enabled. | 244 // to be enabled. |
| 244 EXPECT_CALL(*sync_->components_factory_mock(), | 245 EXPECT_CALL(*sync_->components_factory_mock(), |
| 245 CreateDataTypeManager(_, _, _, _, _, _)).Times(0); | 246 CreateDataTypeManager(_, _, _, _, _, _)).Times(0); |
| 246 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); | 247 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); |
| 247 sync_->Initialize(); | 248 sync_->Initialize(); |
| 248 EXPECT_FALSE(sync_->GetBackendForTest()); | 249 EXPECT_FALSE(sync_->GetBackendForTest()); |
| 249 | 250 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 // Verify we successfully finish startup and configuration. | 321 // Verify we successfully finish startup and configuration. |
| 321 EXPECT_TRUE(sync_->ShouldPushChanges()); | 322 EXPECT_TRUE(sync_->ShouldPushChanges()); |
| 322 } | 323 } |
| 323 | 324 |
| 324 TEST_F(ProfileSyncServiceStartupCrosTest, StartCrosNoCredentials) { | 325 TEST_F(ProfileSyncServiceStartupCrosTest, StartCrosNoCredentials) { |
| 325 EXPECT_CALL(*sync_->components_factory_mock(), | 326 EXPECT_CALL(*sync_->components_factory_mock(), |
| 326 CreateDataTypeManager(_, _, _, _, _, _)).Times(0); | 327 CreateDataTypeManager(_, _, _, _, _, _)).Times(0); |
| 327 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted); | 328 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted); |
| 328 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); | 329 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); |
| 329 TokenService* token_service = static_cast<TokenService*>( | 330 TokenService* token_service = static_cast<TokenService*>( |
| 330 TokenServiceFactory::GetInstance()->SetTestingFactoryAndUse( | 331 TokenServiceFactory::GetForProfile(profile_.get())); |
| 331 profile_.get(), BuildFakeTokenService)); | |
| 332 | 332 |
| 333 sync_->Initialize(); | 333 sync_->Initialize(); |
| 334 // Sync should not start because there are no tokens yet. | 334 // Sync should not start because there are no tokens yet. |
| 335 EXPECT_FALSE(sync_->ShouldPushChanges()); | 335 EXPECT_FALSE(sync_->ShouldPushChanges()); |
| 336 EXPECT_FALSE(sync_->GetBackendForTest()); | 336 EXPECT_FALSE(sync_->GetBackendForTest()); |
| 337 token_service->LoadTokensFromDB(); | 337 token_service->LoadTokensFromDB(); |
| 338 sync_->SetSetupInProgress(false); | 338 sync_->SetSetupInProgress(false); |
| 339 | 339 |
| 340 // Sync should not start because there are still no tokens. | 340 // Sync should not start because there are still no tokens. |
| 341 EXPECT_FALSE(sync_->ShouldPushChanges()); | 341 EXPECT_FALSE(sync_->ShouldPushChanges()); |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 549 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( | 549 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( |
| 550 GaiaConstants::kSyncService, "token"); | 550 GaiaConstants::kSyncService, "token"); |
| 551 sync_->fail_initial_download(); | 551 sync_->fail_initial_download(); |
| 552 | 552 |
| 553 sync_->SetSetupInProgress(true); | 553 sync_->SetSetupInProgress(true); |
| 554 sync_->Initialize(); | 554 sync_->Initialize(); |
| 555 sync_->SetSetupInProgress(false); | 555 sync_->SetSetupInProgress(false); |
| 556 EXPECT_FALSE(sync_->sync_initialized()); | 556 EXPECT_FALSE(sync_->sync_initialized()); |
| 557 EXPECT_TRUE(sync_->GetBackendForTest()); | 557 EXPECT_TRUE(sync_->GetBackendForTest()); |
| 558 } | 558 } |
| OLD | NEW |