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

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

Issue 60733019: This is the third CL of several that will eventually replace TokenService with (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased Created 7 years, 1 month 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 | Annotate | Revision Log
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/prefs/pref_service.h" 8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/chrome_notification_types.h" 9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/signin/fake_signin_manager.h" 10 #include "chrome/browser/signin/fake_signin_manager.h"
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 } 51 }
52 52
53 ACTION_P2(InvokeOnConfigureDone, pss, result) { 53 ACTION_P2(InvokeOnConfigureDone, pss, result) {
54 ProfileSyncService* service = 54 ProfileSyncService* service =
55 static_cast<ProfileSyncService*>(pss); 55 static_cast<ProfileSyncService*>(pss);
56 DataTypeManager::ConfigureResult configure_result = 56 DataTypeManager::ConfigureResult configure_result =
57 static_cast<DataTypeManager::ConfigureResult>(result); 57 static_cast<DataTypeManager::ConfigureResult>(result);
58 service->OnConfigureDone(configure_result); 58 service->OnConfigureDone(configure_result);
59 } 59 }
60 60
61 class FakeTokenService : public TokenService {
62 public:
63 FakeTokenService() {}
64 virtual ~FakeTokenService() {}
65
66 virtual void LoadTokensFromDB() OVERRIDE {
67 set_tokens_loaded(true);
68 content::NotificationService::current()->Notify(
69 chrome::NOTIFICATION_TOKEN_LOADING_FINISHED,
70 content::Source<TokenService>(this),
71 content::NotificationService::NoDetails());
72 }
73
74 static BrowserContextKeyedService* BuildFakeTokenService(
75 content::BrowserContext* profile) {
76 return new FakeTokenService();
77 }
78 };
79
80 class ProfileSyncServiceStartupTest : public testing::Test { 61 class ProfileSyncServiceStartupTest : public testing::Test {
81 public: 62 public:
82 ProfileSyncServiceStartupTest() 63 ProfileSyncServiceStartupTest()
83 : thread_bundle_(content::TestBrowserThreadBundle::REAL_DB_THREAD | 64 : thread_bundle_(content::TestBrowserThreadBundle::REAL_DB_THREAD |
84 content::TestBrowserThreadBundle::REAL_FILE_THREAD | 65 content::TestBrowserThreadBundle::REAL_FILE_THREAD |
85 content::TestBrowserThreadBundle::REAL_IO_THREAD), 66 content::TestBrowserThreadBundle::REAL_IO_THREAD),
86 sync_(NULL) {} 67 sync_(NULL) {}
87 68
88 virtual ~ProfileSyncServiceStartupTest() { 69 virtual ~ProfileSyncServiceStartupTest() {
89 } 70 }
90 71
91 virtual void SetUp() { 72 virtual void SetUp() {
92 profile_ = CreateProfile(); 73 profile_ = CreateProfile();
93 } 74 }
94 75
95 virtual scoped_ptr<TestingProfile> CreateProfile() { 76 virtual scoped_ptr<TestingProfile> CreateProfile() {
96 TestingProfile::Builder builder; 77 TestingProfile::Builder builder;
97 #if defined(OS_CHROMEOS) 78 #if defined(OS_CHROMEOS)
98 builder.AddTestingFactory(SigninManagerFactory::GetInstance(), 79 builder.AddTestingFactory(SigninManagerFactory::GetInstance(),
99 FakeSigninManagerBase::Build); 80 FakeSigninManagerBase::Build);
100 #else 81 #else
101 builder.AddTestingFactory(SigninManagerFactory::GetInstance(), 82 builder.AddTestingFactory(SigninManagerFactory::GetInstance(),
102 FakeSigninManager::Build); 83 FakeSigninManager::Build);
103 #endif 84 #endif
104 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(), 85 builder.AddTestingFactory(ProfileOAuth2TokenServiceFactory::GetInstance(),
105 FakeOAuth2TokenService::BuildTokenService); 86 FakeOAuth2TokenService::BuildTokenService);
106 builder.AddTestingFactory(ProfileSyncServiceFactory::GetInstance(), 87 builder.AddTestingFactory(ProfileSyncServiceFactory::GetInstance(),
107 ProfileSyncServiceStartupTest::BuildService); 88 BuildService);
108 builder.AddTestingFactory(TokenServiceFactory::GetInstance(),
109 FakeTokenService::BuildFakeTokenService);
110 return builder.Build(); 89 return builder.Build();
111 } 90 }
112 91
113 virtual void TearDown() { 92 virtual void TearDown() {
114 sync_->RemoveObserver(&observer_); 93 sync_->RemoveObserver(&observer_);
115 profile_.reset(); 94 profile_.reset();
116 } 95 }
117 96
118 static BrowserContextKeyedService* BuildService( 97 static BrowserContextKeyedService* BuildService(
119 content::BrowserContext* browser_context) { 98 content::BrowserContext* browser_context) {
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 sync_->SetSetupInProgress(false); 222 sync_->SetSetupInProgress(false);
244 EXPECT_TRUE(sync_->ShouldPushChanges()); 223 EXPECT_TRUE(sync_->ShouldPushChanges());
245 } 224 }
246 225
247 // TODO(pavely): Reenable test once android is switched to oauth2. 226 // TODO(pavely): Reenable test once android is switched to oauth2.
248 TEST_F(ProfileSyncServiceStartupTest, DISABLED_StartNoCredentials) { 227 TEST_F(ProfileSyncServiceStartupTest, DISABLED_StartNoCredentials) {
249 // We've never completed startup. 228 // We've never completed startup.
250 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted); 229 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted);
251 SigninManagerFactory::GetForProfile( 230 SigninManagerFactory::GetForProfile(
252 profile_.get())->Initialize(profile_.get(), NULL); 231 profile_.get())->Initialize(profile_.get(), NULL);
253 TokenService* token_service = static_cast<TokenService*>(
254 TokenServiceFactory::GetForProfile(profile_.get()));
255 CreateSyncService(); 232 CreateSyncService();
256 233
257 // Should not actually start, rather just clean things up and wait 234 // Should not actually start, rather just clean things up and wait
258 // to be enabled. 235 // to be enabled.
259 EXPECT_CALL(*components_factory_mock(), 236 EXPECT_CALL(*components_factory_mock(),
260 CreateDataTypeManager(_, _, _, _, _, _)).Times(0); 237 CreateDataTypeManager(_, _, _, _, _, _)).Times(0);
261 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); 238 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
262 sync_->Initialize(); 239 sync_->Initialize();
263 240
264 // Preferences should be back to defaults. 241 // Preferences should be back to defaults.
265 EXPECT_EQ(0, profile_->GetPrefs()->GetInt64(prefs::kSyncLastSyncedTime)); 242 EXPECT_EQ(0, profile_->GetPrefs()->GetInt64(prefs::kSyncLastSyncedTime));
266 EXPECT_FALSE(profile_->GetPrefs()->GetBoolean(prefs::kSyncHasSetupCompleted)); 243 EXPECT_FALSE(profile_->GetPrefs()->GetBoolean(prefs::kSyncHasSetupCompleted));
267 244
268 // Then start things up. 245 // Then start things up.
269 sync_->SetSetupInProgress(true); 246 sync_->SetSetupInProgress(true);
270 247
271 // Simulate successful signin as test_user. 248 // Simulate successful signin as test_user.
272 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, 249 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
273 "test_user@gmail.com"); 250 "test_user@gmail.com");
274 sync_->signin()->SetAuthenticatedUsername("test_user@gmail.com"); 251 sync_->signin()->SetAuthenticatedUsername("test_user@gmail.com");
275 GoogleServiceSigninSuccessDetails details("test_user@gmail.com", ""); 252 GoogleServiceSigninSuccessDetails details("test_user@gmail.com", "");
276 content::NotificationService::current()->Notify( 253 content::NotificationService::current()->Notify(
277 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, 254 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL,
278 content::Source<Profile>(profile_.get()), 255 content::Source<Profile>(profile_.get()),
279 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); 256 content::Details<const GoogleServiceSigninSuccessDetails>(&details));
280 // NOTE: Unlike StartFirstTime, this test does not issue any auth tokens. 257 ProfileOAuth2TokenService* token_service =
281 token_service->LoadTokensFromDB(); 258 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get());
259 token_service->LoadCredentials();
282 260
283 sync_->SetSetupInProgress(false); 261 sync_->SetSetupInProgress(false);
284 // ProfileSyncService should try to start by requesting access token. 262 // ProfileSyncService should try to start by requesting access token.
285 // This request should fail as login token was not issued to TokenService. 263 // This request should fail as login token was not issued.
286 EXPECT_FALSE(sync_->ShouldPushChanges()); 264 EXPECT_FALSE(sync_->ShouldPushChanges());
287 EXPECT_EQ(GoogleServiceAuthError::USER_NOT_SIGNED_UP, 265 EXPECT_EQ(GoogleServiceAuthError::USER_NOT_SIGNED_UP,
288 sync_->GetAuthError().state()); 266 sync_->GetAuthError().state());
289 } 267 }
290 268
291 // TODO(pavely): Reenable test once android is switched to oauth2. 269 // TODO(pavely): Reenable test once android is switched to oauth2.
292 TEST_F(ProfileSyncServiceStartupTest, DISABLED_StartInvalidCredentials) { 270 TEST_F(ProfileSyncServiceStartupTest, DISABLED_StartInvalidCredentials) {
293 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, 271 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
294 "test_user@gmail.com"); 272 "test_user@gmail.com");
295 SigninManagerFactory::GetForProfile( 273 SigninManagerFactory::GetForProfile(
296 profile_.get())->Initialize(profile_.get(), NULL); 274 profile_.get())->Initialize(profile_.get(), NULL);
297 CreateSyncService(); 275 CreateSyncService();
298 SyncBackendHostMock* mock_sbh = SetUpSyncBackendHost(); 276 SyncBackendHostMock* mock_sbh = SetUpSyncBackendHost();
299 277
300 // Tell the backend to stall while downloading control types (simulating an 278 // Tell the backend to stall while downloading control types (simulating an
301 // auth error). 279 // auth error).
302 mock_sbh->set_fail_initial_download(true); 280 mock_sbh->set_fail_initial_download(true);
303 281
304 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager(); 282 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
305 EXPECT_CALL(*data_type_manager, Configure(_, _)).Times(0); 283 EXPECT_CALL(*data_type_manager, Configure(_, _)).Times(0);
306 // Issue login token so that ProfileSyncServer tries to initialize backend.
307 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest(
308 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth2_login_token");
309 284
310 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); 285 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
311 sync_->Initialize(); 286 sync_->Initialize();
312 EXPECT_FALSE(sync_->sync_initialized()); 287 EXPECT_FALSE(sync_->sync_initialized());
313 Mock::VerifyAndClearExpectations(data_type_manager); 288 Mock::VerifyAndClearExpectations(data_type_manager);
314 289
315 // Update the credentials, unstalling the backend. 290 // Update the credentials, unstalling the backend.
316 EXPECT_CALL(*data_type_manager, Configure(_, _)); 291 EXPECT_CALL(*data_type_manager, Configure(_, _));
317 EXPECT_CALL(*data_type_manager, state()). 292 EXPECT_CALL(*data_type_manager, state()).
318 WillRepeatedly(Return(DataTypeManager::CONFIGURED)); 293 WillRepeatedly(Return(DataTypeManager::CONFIGURED));
319 EXPECT_CALL(*data_type_manager, Stop()).Times(1); 294 EXPECT_CALL(*data_type_manager, Stop()).Times(1);
320 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); 295 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
321 sync_->SetSetupInProgress(true); 296 sync_->SetSetupInProgress(true);
322 297
323 // Simulate successful signin. 298 // Simulate successful signin.
324 GoogleServiceSigninSuccessDetails details("test_user@gmail.com", 299 GoogleServiceSigninSuccessDetails details("test_user@gmail.com",
325 std::string()); 300 std::string());
326 content::NotificationService::current()->Notify( 301 content::NotificationService::current()->Notify(
327 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL, 302 chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL,
328 content::Source<Profile>(profile_.get()), 303 content::Source<Profile>(profile_.get()),
329 content::Details<const GoogleServiceSigninSuccessDetails>(&details)); 304 content::Details<const GoogleServiceSigninSuccessDetails>(&details));
330 305
331 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest(
332 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth2_login_token");
333 sync_->SetSetupInProgress(false); 306 sync_->SetSetupInProgress(false);
334 307
335 // Verify we successfully finish startup and configuration. 308 // Verify we successfully finish startup and configuration.
336 EXPECT_TRUE(sync_->ShouldPushChanges()); 309 EXPECT_TRUE(sync_->ShouldPushChanges());
337 } 310 }
338 311
339 TEST_F(ProfileSyncServiceStartupCrosTest, StartCrosNoCredentials) { 312 TEST_F(ProfileSyncServiceStartupCrosTest, StartCrosNoCredentials) {
340 EXPECT_CALL(*components_factory_mock(), 313 EXPECT_CALL(*components_factory_mock(),
341 CreateDataTypeManager(_, _, _, _, _, _)).Times(0); 314 CreateDataTypeManager(_, _, _, _, _, _)).Times(0);
342 EXPECT_CALL(*components_factory_mock(), 315 EXPECT_CALL(*components_factory_mock(),
343 CreateSyncBackendHost(_, _, _)).Times(0); 316 CreateSyncBackendHost(_, _, _)).Times(0);
344 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted); 317 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted);
345 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); 318 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
346 TokenService* token_service = static_cast<TokenService*>( 319 ProfileOAuth2TokenService* token_service =
347 TokenServiceFactory::GetForProfile(profile_.get())); 320 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_.get());
348 321
349 sync_->Initialize(); 322 sync_->Initialize();
350 // Sync should not start because there are no tokens yet. 323 // Sync should not start because there are no tokens yet.
351 EXPECT_FALSE(sync_->ShouldPushChanges()); 324 EXPECT_FALSE(sync_->ShouldPushChanges());
352 token_service->LoadTokensFromDB(); 325 token_service->LoadCredentials();
353 sync_->SetSetupInProgress(false); 326 sync_->SetSetupInProgress(false);
354 327
355 // Sync should not start because there are still no tokens. 328 // Sync should not start because there are still no tokens.
356 EXPECT_FALSE(sync_->ShouldPushChanges()); 329 EXPECT_FALSE(sync_->ShouldPushChanges());
357 } 330 }
358 331
359 TEST_F(ProfileSyncServiceStartupCrosTest, StartFirstTime) { 332 TEST_F(ProfileSyncServiceStartupCrosTest, StartFirstTime) {
360 SetUpSyncBackendHost(); 333 SetUpSyncBackendHost();
361 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager(); 334 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
362 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted); 335 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted);
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 SigninManagerFactory::GetForProfile(profile_.get())->Initialize( 435 SigninManagerFactory::GetForProfile(profile_.get())->Initialize(
463 profile_.get(), NULL); 436 profile_.get(), NULL);
464 CreateSyncService(); 437 CreateSyncService();
465 438
466 // Disable sync through policy. 439 // Disable sync through policy.
467 profile_->GetPrefs()->SetBoolean(prefs::kSyncManaged, true); 440 profile_->GetPrefs()->SetBoolean(prefs::kSyncManaged, true);
468 EXPECT_CALL(*components_factory_mock(), 441 EXPECT_CALL(*components_factory_mock(),
469 CreateDataTypeManager(_, _, _, _, _, _)).Times(0); 442 CreateDataTypeManager(_, _, _, _, _, _)).Times(0);
470 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); 443 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
471 444
472 TokenServiceFactory::GetForProfile(profile_.get())->IssueAuthTokenForTest(
473 GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth2_login_token");
474 sync_->Initialize(); 445 sync_->Initialize();
475 } 446 }
476 447
477 TEST_F(ProfileSyncServiceStartupTest, SwitchManaged) { 448 TEST_F(ProfileSyncServiceStartupTest, SwitchManaged) {
478 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername, 449 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
479 "test_user@gmail.com"); 450 "test_user@gmail.com");
480 SigninManagerFactory::GetForProfile(profile_.get())->Initialize( 451 SigninManagerFactory::GetForProfile(profile_.get())->Initialize(
481 profile_.get(), NULL); 452 profile_.get(), NULL);
482 CreateSyncService(); 453 CreateSyncService();
483 sync_->SetSyncSetupCompleted(); 454 sync_->SetSyncSetupCompleted();
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
553 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted); 524 profile_->GetPrefs()->ClearPref(prefs::kSyncHasSetupCompleted);
554 525
555 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber()); 526 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
556 IssueTestTokens(); 527 IssueTestTokens();
557 528
558 sync_->SetSetupInProgress(true); 529 sync_->SetSetupInProgress(true);
559 sync_->Initialize(); 530 sync_->Initialize();
560 sync_->SetSetupInProgress(false); 531 sync_->SetSetupInProgress(false);
561 EXPECT_FALSE(sync_->sync_initialized()); 532 EXPECT_FALSE(sync_->sync_initialized());
562 } 533 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698