Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: chrome/browser/sync/profile_sync_service_startup_unittest.cc

Issue 15421011: Use OAuth2 token for sync (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.h" 8 #include "base/message_loop.h"
9 #include "base/prefs/pref_service.h" 9 #include "base/prefs/pref_service.h"
10 #include "chrome/browser/signin/fake_signin_manager.h" 10 #include "chrome/browser/signin/fake_signin_manager.h"
11 #include "chrome/browser/signin/oauth2_token_service.h"
12 #include "chrome/browser/signin/profile_oauth2_token_service.h"
13 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
11 #include "chrome/browser/signin/signin_manager.h" 14 #include "chrome/browser/signin/signin_manager.h"
12 #include "chrome/browser/signin/signin_manager_factory.h" 15 #include "chrome/browser/signin/signin_manager_factory.h"
13 #include "chrome/browser/signin/token_service.h" 16 #include "chrome/browser/signin/token_service.h"
14 #include "chrome/browser/signin/token_service_factory.h" 17 #include "chrome/browser/signin/token_service_factory.h"
15 #include "chrome/browser/sync/glue/data_type_manager.h" 18 #include "chrome/browser/sync/glue/data_type_manager.h"
16 #include "chrome/browser/sync/glue/data_type_manager_mock.h" 19 #include "chrome/browser/sync/glue/data_type_manager_mock.h"
17 #include "chrome/browser/sync/profile_sync_components_factory_mock.h" 20 #include "chrome/browser/sync/profile_sync_components_factory_mock.h"
18 #include "chrome/browser/sync/profile_sync_service_factory.h" 21 #include "chrome/browser/sync/profile_sync_service_factory.h"
19 #include "chrome/browser/sync/profile_sync_test_util.h" 22 #include "chrome/browser/sync/profile_sync_test_util.h"
20 #include "chrome/browser/sync/sync_prefs.h" 23 #include "chrome/browser/sync/sync_prefs.h"
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 #else 91 #else
89 SigninManagerFactory::GetInstance()->SetTestingFactory( 92 SigninManagerFactory::GetInstance()->SetTestingFactory(
90 profile_.get(), FakeSigninManager::Build); 93 profile_.get(), FakeSigninManager::Build);
91 #endif 94 #endif
92 } 95 }
93 96
94 virtual void TearDown() { 97 virtual void TearDown() {
95 sync_->RemoveObserver(&observer_); 98 sync_->RemoveObserver(&observer_);
96 ProfileSyncServiceFactory::GetInstance()->SetTestingFactory( 99 ProfileSyncServiceFactory::GetInstance()->SetTestingFactory(
97 profile_.get(), NULL); 100 profile_.get(), NULL);
101 ProfileOAuth2TokenServiceFactory::GetInstance()->SetTestingFactory(
102 profile_.get(), NULL);
98 profile_.reset(); 103 profile_.reset();
99 104
100 // Pump messages posted by the sync core thread (which may end up 105 // Pump messages posted by the sync core thread (which may end up
101 // posting on the IO thread). 106 // posting on the IO thread).
102 ui_loop_.RunUntilIdle(); 107 ui_loop_.RunUntilIdle();
103 io_thread_.Stop(); 108 io_thread_.Stop();
104 file_thread_.Stop(); 109 file_thread_.Stop();
105 ui_loop_.RunUntilIdle(); 110 ui_loop_.RunUntilIdle();
106 } 111 }
107 112
108 static BrowserContextKeyedService* BuildService( 113 static BrowserContextKeyedService* BuildService(
109 content::BrowserContext* profile) { 114 content::BrowserContext* profile) {
110 return new TestProfileSyncService( 115 return new TestProfileSyncService(
111 new ProfileSyncComponentsFactoryMock(), 116 new ProfileSyncComponentsFactoryMock(),
112 static_cast<Profile*>(profile), 117 static_cast<Profile*>(profile),
113 SigninManagerFactory::GetForProfile(static_cast<Profile*>(profile)), 118 SigninManagerFactory::GetForProfile(static_cast<Profile*>(profile)),
114 ProfileSyncService::MANUAL_START, 119 ProfileSyncService::MANUAL_START,
115 true); 120 true);
116 } 121 }
117 122
118 void CreateSyncService() { 123 void CreateSyncService() {
124 ProfileOAuth2TokenServiceFactory::GetInstance()->SetTestingFactory(
125 profile_.get(), FakeOAuth2TokenService::BuildTokenService);
119 sync_ = static_cast<TestProfileSyncService*>( 126 sync_ = static_cast<TestProfileSyncService*>(
120 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( 127 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
121 profile_.get(), BuildService)); 128 profile_.get(), BuildService));
122 sync_->AddObserver(&observer_); 129 sync_->AddObserver(&observer_);
123 sync_->set_synchronous_sync_configuration(); 130 sync_->set_synchronous_sync_configuration();
124 } 131 }
125 132
126 protected: 133 protected:
127 DataTypeManagerMock* SetUpDataTypeManager() { 134 DataTypeManagerMock* SetUpDataTypeManager() {
128 DataTypeManagerMock* data_type_manager = new DataTypeManagerMock(); 135 DataTypeManagerMock* data_type_manager = new DataTypeManagerMock();
(...skipping 10 matching lines...) Expand all
139 content::TestBrowserThread io_thread_; 146 content::TestBrowserThread io_thread_;
140 scoped_ptr<TestingProfile> profile_; 147 scoped_ptr<TestingProfile> profile_;
141 TestProfileSyncService* sync_; 148 TestProfileSyncService* sync_;
142 ProfileSyncServiceObserverMock observer_; 149 ProfileSyncServiceObserverMock observer_;
143 }; 150 };
144 151
145 class ProfileSyncServiceStartupCrosTest : public ProfileSyncServiceStartupTest { 152 class ProfileSyncServiceStartupCrosTest : public ProfileSyncServiceStartupTest {
146 public: 153 public:
147 virtual void SetUp() { 154 virtual void SetUp() {
148 ProfileSyncServiceStartupTest::SetUp(); 155 ProfileSyncServiceStartupTest::SetUp();
156 ProfileOAuth2TokenServiceFactory::GetInstance()->SetTestingFactory(
157 profile_.get(), FakeOAuth2TokenService::BuildTokenService);
149 sync_ = static_cast<TestProfileSyncService*>( 158 sync_ = static_cast<TestProfileSyncService*>(
150 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse( 159 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
151 profile_.get(), BuildCrosService)); 160 profile_.get(), BuildCrosService));
152 sync_->AddObserver(&observer_); 161 sync_->AddObserver(&observer_);
153 sync_->set_synchronous_sync_configuration(); 162 sync_->set_synchronous_sync_configuration();
154 } 163 }
155 164
156 static BrowserContextKeyedService* BuildCrosService( 165 static BrowserContextKeyedService* BuildCrosService(
157 content::BrowserContext* context) { 166 content::BrowserContext* context) {
158 Profile* profile = static_cast<Profile*>(context); 167 Profile* profile = static_cast<Profile*>(context);
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 "test_user"); 219 "test_user");
211 sync_->signin()->SetAuthenticatedUsername("test_user"); 220 sync_->signin()->SetAuthenticatedUsername("test_user");
212 GoogleServiceSigninSuccessDetails details("test_user", ""); 221 GoogleServiceSigninSuccessDetails details("test_user", "");
213 content::NotificationService::current()->Notify( 222 content::NotificationService::current()->Notify(
214 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, 223 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL,
215 content::Source<Profile>(profile_.get()), 224 content::Source<Profile>(profile_.get()),
216 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); 225 content::Details<const GoogleServiceSigninSuccessDetails>(&details));
217 226
218 // Create some tokens in the token service. 227 // Create some tokens in the token service.
219 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( 228 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest(
220 GaiaConstants::kSyncService, "sync_token"); 229 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth2_login_token");
221 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( 230 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest(
222 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth2_login_token"); 231 GaiaConstants::kSyncService, "token");
223 232
224 // Simulate the UI telling sync it has finished setting up. 233 // Simulate the UI telling sync it has finished setting up.
225 sync_->SetSetupInProgress(false); 234 sync_->SetSetupInProgress(false);
226 EXPECT_TRUE(sync_->ShouldPushChanges()); 235 EXPECT_TRUE(sync_->ShouldPushChanges());
227 } 236 }
228 237
229 TEST_F(ProfileSyncServiceStartupTest, StartNoCredentials) { 238 // TODO(pavely): Reenable test once android is switched to oauth2.
230 // We've never completed startup. 239 TEST_F(ProfileSyncServiceStartupTest, DISABLED_StartNoCredentials) {
240 // We've never completed startup.
231 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted); 241 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted);
232 SigninManagerFactory::GetForProfile( 242 SigninManagerFactory::GetForProfile(
233 profile_.get())->Initialize(profile_.get(), NULL); 243 profile_.get())->Initialize(profile_.get(), NULL);
234 TokenService* token_service = static_cast<TokenService*>( 244 TokenService* token_service = static_cast<TokenService*>(
235 TokenServiceFactory::GetInstance()->SetTestingFactoryAndUse( 245 TokenServiceFactory::GetInstance()->SetTestingFactoryAndUse(
236 profile_.get(), BuildFakeTokenService)); 246 profile_.get(), BuildFakeTokenService));
237 CreateSyncService(); 247 CreateSyncService();
238 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
239 EXPECT_CALL(*data_type_manager, Configure(_, _)).Times(0);
240 248
241 // Should not actually start, rather just clean things up and wait 249 // Should not actually start, rather just clean things up and wait
242 // to be enabled. 250 // to be enabled.
251 EXPECT_CALL(*sync_->components_factory_mock(),
252 CreateDataTypeManager(_, _, _, _, _, _)).Times(0);
243 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); 253 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
244 sync_->Initialize(); 254 sync_->Initialize();
245 EXPECT_FALSE(sync_->GetBackendForTest()); 255 EXPECT_FALSE(sync_->GetBackendForTest());
246 256
247 // Preferences should be back to defaults. 257 // Preferences should be back to defaults.
248 EXPECT_EQ(0, profile_->GetPrefs()->GetInt64(prefs::kSyncLastSyncedTime)); 258 EXPECT_EQ(0, profile_->GetPrefs()->GetInt64(prefs::kSyncLastSyncedTime));
249 EXPECT_FALSE(profile_->GetPrefs()->GetBoolean(prefs::kSyncHasSetupCompleted)); 259 EXPECT_FALSE(profile_->GetPrefs()->GetBoolean(prefs::kSyncHasSetupCompleted));
250 Mock::VerifyAndClearExpectations(data_type_manager);
251 260
252 // Then start things up. 261 // Then start things up.
253 EXPECT_CALL(*data_type_manager, Configure(_, _)).Times(1);
254 EXPECT_CALL(*data_type_manager, state()).
255 WillOnce(Return(DataTypeManager::CONFIGURED)).
256 WillOnce(Return(DataTypeManager::CONFIGURED));
257 EXPECT_CALL(*data_type_manager, Stop()).Times(1);
258 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
259
260 sync_->SetSetupInProgress(true); 262 sync_->SetSetupInProgress(true);
261 263
262 // Simulate successful signin as test_user. 264 // Simulate successful signin as test_user.
263 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, 265 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
264 "test_user"); 266 "test_user");
265 sync_->signin()->SetAuthenticatedUsername("test_user"); 267 sync_->signin()->SetAuthenticatedUsername("test_user");
266 GoogleServiceSigninSuccessDetails details("test_user", ""); 268 GoogleServiceSigninSuccessDetails details("test_user", "");
267 content::NotificationService::current()->Notify( 269 content::NotificationService::current()->Notify(
268 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, 270 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL,
269 content::Source<Profile>(profile_.get()), 271 content::Source<Profile>(profile_.get()),
270 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); 272 content::Details<const GoogleServiceSigninSuccessDetails>(&details));
271 // NOTE: Unlike StartFirstTime, this test does not issue any auth tokens. 273 // NOTE: Unlike StartFirstTime, this test does not issue any auth tokens.
272 token_service->LoadTokensFromDB(); 274 token_service->LoadTokensFromDB();
273 275
274 sync_->SetSetupInProgress(false); 276 sync_->SetSetupInProgress(false);
275 // Backend should initialize using a bogus GAIA token for credentials. 277 // ProfileSyncService should try to start by requesting access token.
276 EXPECT_TRUE(sync_->ShouldPushChanges()); 278 // This request should fail as login token was not issued to TokenService.
279 EXPECT_FALSE(sync_->ShouldPushChanges());
280 EXPECT_EQ(GoogleServiceAuthError::USER_NOT_SIGNED_UP,
281 sync_->GetAuthError().state());
277 } 282 }
278 283
279 TEST_F(ProfileSyncServiceStartupTest, StartInvalidCredentials) { 284 // TODO(pavely): Reenable test once android is switched to oauth2.
285 TEST_F(ProfileSyncServiceStartupTest, DISABLED_StartInvalidCredentials) {
280 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, 286 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
281 "test_user"); 287 "test_user");
282 SigninManagerFactory::GetForProfile( 288 SigninManagerFactory::GetForProfile(
283 profile_.get())->Initialize(profile_.get(), NULL); 289 profile_.get())->Initialize(profile_.get(), NULL);
284 CreateSyncService(); 290 CreateSyncService();
285 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager(); 291 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
286 EXPECT_CALL(*data_type_manager, Configure(_, _)).Times(0); 292 EXPECT_CALL(*data_type_manager, Configure(_, _)).Times(0);
287 TokenService* token_service = static_cast<TokenService*>( 293 // Issue login token so that ProfileSyncServer tries to initialize backend.
288 TokenServiceFactory::GetInstance()->SetTestingFactoryAndUse( 294 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest(
289 profile_.get(), BuildFakeTokenService)); 295 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth2_login_token");
290 token_service->LoadTokensFromDB();
291 296
292 // Tell the backend to stall while downloading control types (simulating an 297 // Tell the backend to stall while downloading control types (simulating an
293 // auth error). 298 // auth error).
294 sync_->fail_initial_download(); 299 sync_->fail_initial_download();
295 300
296 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); 301 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
297 sync_->Initialize(); 302 sync_->Initialize();
298 EXPECT_TRUE(sync_->GetBackendForTest()); 303 EXPECT_TRUE(sync_->GetBackendForTest());
299 EXPECT_FALSE(sync_->sync_initialized()); 304 EXPECT_FALSE(sync_->sync_initialized());
300 EXPECT_FALSE(sync_->ShouldPushChanges());
301 Mock::VerifyAndClearExpectations(data_type_manager); 305 Mock::VerifyAndClearExpectations(data_type_manager);
302 306
303 // Update the credentials, unstalling the backend. 307 // Update the credentials, unstalling the backend.
304 EXPECT_CALL(*data_type_manager, Configure(_, _)); 308 EXPECT_CALL(*data_type_manager, Configure(_, _));
305 EXPECT_CALL(*data_type_manager, state()). 309 EXPECT_CALL(*data_type_manager, state()).
306 WillRepeatedly(Return(DataTypeManager::CONFIGURED)); 310 WillRepeatedly(Return(DataTypeManager::CONFIGURED));
307 EXPECT_CALL(*data_type_manager, Stop()).Times(1); 311 EXPECT_CALL(*data_type_manager, Stop()).Times(1);
308 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); 312 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
309 sync_->SetSetupInProgress(true); 313 sync_->SetSetupInProgress(true);
310 314
311 // Simulate successful signin. 315 // Simulate successful signin.
312 GoogleServiceSigninSuccessDetails details("test_user", std::string()); 316 GoogleServiceSigninSuccessDetails details("test_user", std::string());
313 content::NotificationService::current()->Notify( 317 content::NotificationService::current()->Notify(
314 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, 318 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL,
315 content::Source<Profile>(profile_.get()), 319 content::Source<Profile>(profile_.get()),
316 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); 320 content::Details<const GoogleServiceSigninSuccessDetails>(&details));
317 321
318 token_service->IssueAuthTokenForTest( 322 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest(
319 GaiaConstants::kSyncService, "sync_token"); 323 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth2_login_token");
320 sync_->SetSetupInProgress(false); 324 sync_->SetSetupInProgress(false);
321 base::MessageLoop::current()->Run();
322 325
323 // Verify we successfully finish startup and configuration. 326 // Verify we successfully finish startup and configuration.
324 EXPECT_TRUE(sync_->ShouldPushChanges()); 327 EXPECT_TRUE(sync_->ShouldPushChanges());
325 } 328 }
326 329
327 TEST_F(ProfileSyncServiceStartupCrosTest, StartCrosNoCredentials) { 330 TEST_F(ProfileSyncServiceStartupCrosTest, StartCrosNoCredentials) {
328 EXPECT_CALL(*sync_->components_factory_mock(), 331 EXPECT_CALL(*sync_->components_factory_mock(),
329 CreateDataTypeManager(_, _, _, _, _, _)).Times(0); 332 CreateDataTypeManager(_, _, _, _, _, _)).Times(0);
330 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted); 333 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted);
331 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); 334 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
(...skipping 16 matching lines...) Expand all
348 TEST_F(ProfileSyncServiceStartupCrosTest, StartFirstTime) { 351 TEST_F(ProfileSyncServiceStartupCrosTest, StartFirstTime) {
349 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager(); 352 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
350 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted); 353 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted);
351 EXPECT_CALL(*data_type_manager, Configure(_, _)); 354 EXPECT_CALL(*data_type_manager, Configure(_, _));
352 EXPECT_CALL(*data_type_manager, state()). 355 EXPECT_CALL(*data_type_manager, state()).
353 WillRepeatedly(Return(DataTypeManager::CONFIGURED)); 356 WillRepeatedly(Return(DataTypeManager::CONFIGURED));
354 EXPECT_CALL(*data_type_manager, Stop()).Times(1); 357 EXPECT_CALL(*data_type_manager, Stop()).Times(1);
355 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); 358 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
356 359
357 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( 360 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest(
361 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth2_login_token");
362 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest(
358 GaiaConstants::kSyncService, "sync_token"); 363 GaiaConstants::kSyncService, "sync_token");
359 sync_->Initialize(); 364 sync_->Initialize();
360 EXPECT_TRUE(sync_->ShouldPushChanges()); 365 EXPECT_TRUE(sync_->ShouldPushChanges());
361 } 366 }
362 367
363 TEST_F(ProfileSyncServiceStartupTest, StartNormal) { 368 TEST_F(ProfileSyncServiceStartupTest, StartNormal) {
364 // Pre load the tokens 369 // Pre load the tokens
365 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, "test_user"); 370 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, "test_user");
366 SigninManagerFactory::GetForProfile(profile_.get())->Initialize( 371 SigninManagerFactory::GetForProfile(profile_.get())->Initialize(
367 profile_.get(), NULL); 372 profile_.get(), NULL);
368 CreateSyncService(); 373 CreateSyncService();
369 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager(); 374 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
370 EXPECT_CALL(*data_type_manager, Configure(_, _)); 375 EXPECT_CALL(*data_type_manager, Configure(_, _));
371 EXPECT_CALL(*data_type_manager, state()). 376 EXPECT_CALL(*data_type_manager, state()).
372 WillRepeatedly(Return(DataTypeManager::CONFIGURED)); 377 WillRepeatedly(Return(DataTypeManager::CONFIGURED));
373 EXPECT_CALL(*data_type_manager, Stop()).Times(1); 378 EXPECT_CALL(*data_type_manager, Stop()).Times(1);
374 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); 379 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
375 380
376 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( 381 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest(
382 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth2_login_token");
383 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest(
377 GaiaConstants::kSyncService, "sync_token"); 384 GaiaConstants::kSyncService, "sync_token");
378 385
379 sync_->Initialize(); 386 sync_->Initialize();
380 } 387 }
381 388
382 // Test that we can recover from a case where a bug in the code resulted in 389 // Test that we can recover from a case where a bug in the code resulted in
383 // OnUserChoseDatatypes not being properly called and datatype preferences 390 // OnUserChoseDatatypes not being properly called and datatype preferences
384 // therefore being left unset. 391 // therefore being left unset.
385 TEST_F(ProfileSyncServiceStartupTest, StartRecoverDatatypePrefs) { 392 TEST_F(ProfileSyncServiceStartupTest, StartRecoverDatatypePrefs) {
386 // Clear the datatype preference fields (simulating bug 154940). 393 // Clear the datatype preference fields (simulating bug 154940).
(...skipping 11 matching lines...) Expand all
398 profile_.get(), NULL); 405 profile_.get(), NULL);
399 CreateSyncService(); 406 CreateSyncService();
400 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager(); 407 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
401 EXPECT_CALL(*data_type_manager, Configure(_, _)); 408 EXPECT_CALL(*data_type_manager, Configure(_, _));
402 EXPECT_CALL(*data_type_manager, state()). 409 EXPECT_CALL(*data_type_manager, state()).
403 WillRepeatedly(Return(DataTypeManager::CONFIGURED)); 410 WillRepeatedly(Return(DataTypeManager::CONFIGURED));
404 EXPECT_CALL(*data_type_manager, Stop()).Times(1); 411 EXPECT_CALL(*data_type_manager, Stop()).Times(1);
405 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); 412 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
406 413
407 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( 414 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest(
415 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth2_login_token");
416 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest(
408 GaiaConstants::kSyncService, "sync_token"); 417 GaiaConstants::kSyncService, "sync_token");
409 sync_->Initialize(); 418 sync_->Initialize();
410 419
411 EXPECT_TRUE(profile_->GetPrefs()->GetBoolean( 420 EXPECT_TRUE(profile_->GetPrefs()->GetBoolean(
412 prefs::kSyncKeepEverythingSynced)); 421 prefs::kSyncKeepEverythingSynced));
413 } 422 }
414 423
415 // Verify that the recovery of datatype preferences doesn't overwrite a valid 424 // Verify that the recovery of datatype preferences doesn't overwrite a valid
416 // case where only bookmarks are enabled. 425 // case where only bookmarks are enabled.
417 TEST_F(ProfileSyncServiceStartupTest, StartDontRecoverDatatypePrefs) { 426 TEST_F(ProfileSyncServiceStartupTest, StartDontRecoverDatatypePrefs) {
418 // Explicitly set Keep Everything Synced to false and have only bookmarks 427 // Explicitly set Keep Everything Synced to false and have only bookmarks
419 // enabled. 428 // enabled.
420 profile_->GetPrefs()->SetBoolean(prefs::kSyncKeepEverythingSynced, false); 429 profile_->GetPrefs()->SetBoolean(prefs::kSyncKeepEverythingSynced, false);
421 430
422 // Pre load the tokens 431 // Pre load the tokens
423 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, "test_user"); 432 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, "test_user");
424 SigninManagerFactory::GetForProfile(profile_.get())->Initialize( 433 SigninManagerFactory::GetForProfile(profile_.get())->Initialize(
425 profile_.get(), NULL); 434 profile_.get(), NULL);
426 CreateSyncService(); 435 CreateSyncService();
427 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager(); 436 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
428 EXPECT_CALL(*data_type_manager, Configure(_, _)); 437 EXPECT_CALL(*data_type_manager, Configure(_, _));
429 EXPECT_CALL(*data_type_manager, state()). 438 EXPECT_CALL(*data_type_manager, state()).
430 WillRepeatedly(Return(DataTypeManager::CONFIGURED)); 439 WillRepeatedly(Return(DataTypeManager::CONFIGURED));
431 EXPECT_CALL(*data_type_manager, Stop()).Times(1); 440 EXPECT_CALL(*data_type_manager, Stop()).Times(1);
432 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); 441 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
433 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( 442 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest(
443 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth2_login_token");
444 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest(
434 GaiaConstants::kSyncService, "sync_token"); 445 GaiaConstants::kSyncService, "sync_token");
435 sync_->Initialize(); 446 sync_->Initialize();
436 447
437 EXPECT_FALSE(profile_->GetPrefs()->GetBoolean( 448 EXPECT_FALSE(profile_->GetPrefs()->GetBoolean(
438 prefs::kSyncKeepEverythingSynced)); 449 prefs::kSyncKeepEverythingSynced));
439 } 450 }
440 451
441 TEST_F(ProfileSyncServiceStartupTest, ManagedStartup) { 452 TEST_F(ProfileSyncServiceStartupTest, ManagedStartup) {
442 // Service should not be started by Initialize() since it's managed. 453 // Service should not be started by Initialize() since it's managed.
443 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, "test_user"); 454 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, "test_user");
444 SigninManagerFactory::GetForProfile(profile_.get())->Initialize( 455 SigninManagerFactory::GetForProfile(profile_.get())->Initialize(
445 profile_.get(), NULL); 456 profile_.get(), NULL);
446 CreateSyncService(); 457 CreateSyncService();
447 458
448 // Disable sync through policy. 459 // Disable sync through policy.
449 profile_->GetPrefs()->SetBoolean(prefs::kSyncManaged, true); 460 profile_->GetPrefs()->SetBoolean(prefs::kSyncManaged, true);
450 EXPECT_CALL(*sync_->components_factory_mock(), 461 EXPECT_CALL(*sync_->components_factory_mock(),
451 CreateDataTypeManager(_, _, _, _, _, _)).Times(0); 462 CreateDataTypeManager(_, _, _, _, _, _)).Times(0);
452 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); 463 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
453 464
454 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( 465 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest(
455 GaiaConstants::kSyncService, "sync_token"); 466 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth2_login_token");
456 sync_->Initialize(); 467 sync_->Initialize();
457 } 468 }
458 469
459 TEST_F(ProfileSyncServiceStartupTest, SwitchManaged) { 470 TEST_F(ProfileSyncServiceStartupTest, SwitchManaged) {
460 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, "test_user"); 471 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, "test_user");
461 SigninManagerFactory::GetForProfile(profile_.get())->Initialize( 472 SigninManagerFactory::GetForProfile(profile_.get())->Initialize(
462 profile_.get(), NULL); 473 profile_.get(), NULL);
463 CreateSyncService(); 474 CreateSyncService();
464 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager(); 475 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
465 EXPECT_CALL(*data_type_manager, Configure(_, _)); 476 EXPECT_CALL(*data_type_manager, Configure(_, _));
466 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); 477 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
467 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( 478 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest(
468 GaiaConstants::kSyncService, "sync_token"); 479 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth2_login_token");
480 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest(
481 GaiaConstants::kSyncService, "token");
469 sync_->Initialize(); 482 sync_->Initialize();
470 483
471 // The service should stop when switching to managed mode. 484 // The service should stop when switching to managed mode.
472 Mock::VerifyAndClearExpectations(data_type_manager); 485 Mock::VerifyAndClearExpectations(data_type_manager);
473 EXPECT_CALL(*data_type_manager, state()). 486 EXPECT_CALL(*data_type_manager, state()).
474 WillOnce(Return(DataTypeManager::CONFIGURED)); 487 WillOnce(Return(DataTypeManager::CONFIGURED));
475 EXPECT_CALL(*data_type_manager, Stop()).Times(1); 488 EXPECT_CALL(*data_type_manager, Stop()).Times(1);
476 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); 489 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
477 profile_->GetPrefs()->SetBoolean(prefs::kSyncManaged, true); 490 profile_->GetPrefs()->SetBoolean(prefs::kSyncManaged, true);
478 491
(...skipping 24 matching lines...) Expand all
503 syncer::ModelTypeSet(), 516 syncer::ModelTypeSet(),
504 syncer::ModelTypeSet()); 517 syncer::ModelTypeSet());
505 EXPECT_CALL(*data_type_manager, Configure(_, _)). 518 EXPECT_CALL(*data_type_manager, Configure(_, _)).
506 WillRepeatedly( 519 WillRepeatedly(
507 DoAll(InvokeOnConfigureStart(sync_), 520 DoAll(InvokeOnConfigureStart(sync_),
508 InvokeOnConfigureDone(sync_, result))); 521 InvokeOnConfigureDone(sync_, result)));
509 EXPECT_CALL(*data_type_manager, state()). 522 EXPECT_CALL(*data_type_manager, state()).
510 WillOnce(Return(DataTypeManager::STOPPED)); 523 WillOnce(Return(DataTypeManager::STOPPED));
511 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); 524 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
512 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( 525 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest(
513 GaiaConstants::kSyncService, "sync_token"); 526 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth2_login_token");
527 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest(
528 GaiaConstants::kSyncService, "token");
514 sync_->Initialize(); 529 sync_->Initialize();
515 EXPECT_TRUE(sync_->HasUnrecoverableError()); 530 EXPECT_TRUE(sync_->HasUnrecoverableError());
516 } 531 }
517 532
518 TEST_F(ProfileSyncServiceStartupTest, StartDownloadFailed) { 533 TEST_F(ProfileSyncServiceStartupTest, StartDownloadFailed) {
519 // Pre load the tokens 534 // Pre load the tokens
520 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, "test_user"); 535 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, "test_user");
521 SigninManagerFactory::GetForProfile(profile_.get())->Initialize( 536 SigninManagerFactory::GetForProfile(profile_.get())->Initialize(
522 profile_.get(), NULL); 537 profile_.get(), NULL);
523 CreateSyncService(); 538 CreateSyncService();
524 539
525 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted); 540 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted);
526 541
527 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); 542 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
528 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest( 543 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest(
529 GaiaConstants::kSyncService, "sync_token"); 544 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth2_login_token");
545 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest(
546 GaiaConstants::kSyncService, "token");
530 sync_->fail_initial_download(); 547 sync_->fail_initial_download();
531 548
549 sync_->SetSetupInProgress(true);
532 sync_->Initialize(); 550 sync_->Initialize();
551 sync_->SetSetupInProgress(false);
533 EXPECT_FALSE(sync_->sync_initialized()); 552 EXPECT_FALSE(sync_->sync_initialized());
534 EXPECT_FALSE(sync_->GetBackendForTest()); 553 EXPECT_TRUE(sync_->GetBackendForTest());
535 } 554 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698