Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / sync / profile_sync_service_startup_unittest.cc
blob2d710968d6f62e850784068f91ab09066a57b0d1
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
3 // found in the LICENSE file.
5 #include "base/files/file_util.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/prefs/pref_service_syncable.h"
10 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
11 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
12 #include "chrome/browser/signin/fake_signin_manager.h"
13 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
14 #include "chrome/browser/signin/signin_manager_factory.h"
15 #include "chrome/browser/sync/glue/sync_backend_host_mock.h"
16 #include "chrome/browser/sync/profile_sync_components_factory_mock.h"
17 #include "chrome/browser/sync/profile_sync_service_factory.h"
18 #include "chrome/browser/sync/profile_sync_test_util.h"
19 #include "chrome/browser/sync/supervised_user_signin_manager_wrapper.h"
20 #include "chrome/common/pref_names.h"
21 #include "chrome/test/base/testing_browser_process.h"
22 #include "chrome/test/base/testing_profile.h"
23 #include "chrome/test/base/testing_profile_manager.h"
24 #include "components/signin/core/browser/profile_oauth2_token_service.h"
25 #include "components/signin/core/browser/signin_manager.h"
26 #include "components/sync_driver/data_type_manager.h"
27 #include "components/sync_driver/data_type_manager_mock.h"
28 #include "components/sync_driver/pref_names.h"
29 #include "components/sync_driver/sync_prefs.h"
30 #include "content/public/test/test_browser_thread_bundle.h"
31 #include "content/public/test/test_utils.h"
32 #include "google_apis/gaia/gaia_auth_consumer.h"
33 #include "google_apis/gaia/gaia_constants.h"
34 #include "google_apis/gaia/oauth2_token_service.h"
35 #include "testing/gmock/include/gmock/gmock.h"
36 #include "testing/gtest/include/gtest/gtest.h"
38 using browser_sync::SyncBackendHostMock;
39 using content::BrowserThread;
40 using sync_driver::DataTypeManager;
41 using sync_driver::DataTypeManagerMock;
42 using testing::_;
43 using testing::AnyNumber;
44 using testing::DoAll;
45 using testing::InvokeArgument;
46 using testing::Mock;
47 using testing::Return;
48 using testing::SaveArg;
50 ACTION_P(InvokeOnConfigureStart, pss) {
51 ProfileSyncService* service =
52 static_cast<ProfileSyncService*>(pss);
53 service->OnConfigureStart();
56 ACTION_P3(InvokeOnConfigureDone, pss, error_callback, result) {
57 ProfileSyncService* service =
58 static_cast<ProfileSyncService*>(pss);
59 DataTypeManager::ConfigureResult configure_result =
60 static_cast<DataTypeManager::ConfigureResult>(result);
61 if (result.status == sync_driver::DataTypeManager::ABORTED)
62 error_callback.Run(&configure_result);
63 service->OnConfigureDone(configure_result);
66 class TestProfileSyncServiceNoBackup : public ProfileSyncService {
67 public:
68 TestProfileSyncServiceNoBackup(
69 scoped_ptr<ProfileSyncComponentsFactory> factory,
70 Profile* profile,
71 scoped_ptr<SupervisedUserSigninManagerWrapper> signin_wrapper,
72 ProfileOAuth2TokenService* oauth2_token_service,
73 browser_sync::ProfileSyncServiceStartBehavior start_behavior)
74 : ProfileSyncService(factory.Pass(), profile, signin_wrapper.Pass(),
75 oauth2_token_service, start_behavior) {}
77 protected:
78 virtual bool NeedBackup() const override {
79 return false;
83 class ProfileSyncServiceStartupTest : public testing::Test {
84 public:
85 ProfileSyncServiceStartupTest()
86 : thread_bundle_(content::TestBrowserThreadBundle::REAL_DB_THREAD |
87 content::TestBrowserThreadBundle::REAL_FILE_THREAD |
88 content::TestBrowserThreadBundle::REAL_IO_THREAD),
89 profile_manager_(TestingBrowserProcess::GetGlobal()),
90 sync_(NULL) {}
92 virtual ~ProfileSyncServiceStartupTest() {
95 virtual void SetUp() {
96 CHECK(profile_manager_.SetUp());
98 TestingProfile::TestingFactories testing_facotries;
99 testing_facotries.push_back(
100 std::make_pair(SigninManagerFactory::GetInstance(),
101 FakeSigninManagerBase::Build));
102 testing_facotries.push_back(
103 std::make_pair(ProfileOAuth2TokenServiceFactory::GetInstance(),
104 BuildAutoIssuingFakeProfileOAuth2TokenService));
105 testing_facotries.push_back(
106 std::make_pair(ProfileSyncServiceFactory::GetInstance(),
107 BuildService));
109 profile_ = profile_manager_.CreateTestingProfile(
110 "sync-startup-test", scoped_ptr<PrefServiceSyncable>(),
111 base::UTF8ToUTF16("sync-startup-test"), 0, std::string(),
112 testing_facotries);
115 virtual void TearDown() {
116 sync_->RemoveObserver(&observer_);
119 static KeyedService* BuildService(content::BrowserContext* browser_context) {
120 Profile* profile = static_cast<Profile*>(browser_context);
121 return new TestProfileSyncServiceNoBackup(
122 scoped_ptr<ProfileSyncComponentsFactory>(
123 new ProfileSyncComponentsFactoryMock()),
124 profile,
125 make_scoped_ptr(new SupervisedUserSigninManagerWrapper(
126 profile, SigninManagerFactory::GetForProfile(profile))),
127 ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
128 browser_sync::MANUAL_START);
131 void CreateSyncService() {
132 sync_ = static_cast<ProfileSyncService*>(
133 ProfileSyncServiceFactory::GetForProfile(profile_));
134 sync_->AddObserver(&observer_);
137 void IssueTestTokens() {
138 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)
139 ->UpdateCredentials("test_user@gmail.com", "oauth2_login_token");
142 ProfileSyncComponentsFactoryMock* components_factory_mock() {
143 return static_cast<ProfileSyncComponentsFactoryMock*>(sync_->factory());
146 FakeSigninManagerForTesting* fake_signin() {
147 return static_cast<FakeSigninManagerForTesting*>(sync_->signin());
150 void SetError(DataTypeManager::ConfigureResult* result) {
151 sync_driver::DataTypeStatusTable::TypeErrorMap errors;
152 errors[syncer::BOOKMARKS] =
153 syncer::SyncError(FROM_HERE,
154 syncer::SyncError::UNRECOVERABLE_ERROR,
155 "Error",
156 syncer::BOOKMARKS);
157 result->data_type_status_table.UpdateFailedDataTypes(errors);
160 protected:
161 void SimulateTestUserSignin() {
162 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
163 "test_user@gmail.com");
164 #if !defined(OS_CHROMEOS)
165 fake_signin()->SignIn("test_user@gmail.com", "");
166 #else
167 fake_signin()->SetAuthenticatedUsername("test_user@gmail.com");
168 sync_->GoogleSigninSucceeded("test_user@gmail.com",
169 "test_user@gmail.com",
170 "");
171 #endif
174 DataTypeManagerMock* SetUpDataTypeManager() {
175 DataTypeManagerMock* data_type_manager = new DataTypeManagerMock();
176 EXPECT_CALL(*components_factory_mock(),
177 CreateDataTypeManager(_, _, _, _, _)).
178 WillOnce(Return(data_type_manager));
179 return data_type_manager;
182 browser_sync::SyncBackendHostMock* SetUpSyncBackendHost() {
183 browser_sync::SyncBackendHostMock* sync_backend_host =
184 new browser_sync::SyncBackendHostMock();
185 EXPECT_CALL(*components_factory_mock(),
186 CreateSyncBackendHost(_, _, _, _, _)).
187 WillOnce(Return(sync_backend_host));
188 return sync_backend_host;
191 content::TestBrowserThreadBundle thread_bundle_;
192 TestingProfileManager profile_manager_;
193 TestingProfile* profile_;
194 ProfileSyncService* sync_;
195 ProfileSyncServiceObserverMock observer_;
196 sync_driver::DataTypeStatusTable data_type_status_table_;
199 class ProfileSyncServiceStartupCrosTest : public ProfileSyncServiceStartupTest {
200 public:
201 virtual void SetUp() {
202 ProfileSyncServiceStartupTest::SetUp();
203 sync_ = static_cast<ProfileSyncService*>(
204 ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
205 profile_, BuildCrosService));
206 sync_->AddObserver(&observer_);
209 static KeyedService* BuildCrosService(content::BrowserContext* context) {
210 Profile* profile = static_cast<Profile*>(context);
211 SigninManagerBase* signin =
212 SigninManagerFactory::GetForProfile(profile);
213 profile->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
214 "test_user@gmail.com");
215 signin->SetAuthenticatedUsername("test_user@gmail.com");
216 ProfileOAuth2TokenService* oauth2_token_service =
217 ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
218 EXPECT_TRUE(signin->IsAuthenticated());
219 return new TestProfileSyncServiceNoBackup(
220 scoped_ptr<ProfileSyncComponentsFactory>(
221 new ProfileSyncComponentsFactoryMock()),
222 profile,
223 make_scoped_ptr(new SupervisedUserSigninManagerWrapper(profile,
224 signin)),
225 oauth2_token_service,
226 browser_sync::AUTO_START);
230 TEST_F(ProfileSyncServiceStartupTest, StartFirstTime) {
231 // We've never completed startup.
232 profile_->GetPrefs()->ClearPref(sync_driver::prefs::kSyncHasSetupCompleted);
233 CreateSyncService();
234 SetUpSyncBackendHost();
235 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
236 EXPECT_CALL(*data_type_manager, Configure(_, _)).Times(0);
238 // Should not actually start, rather just clean things up and wait
239 // to be enabled.
240 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
241 sync_->Initialize();
243 // Preferences should be back to defaults.
244 EXPECT_EQ(
246 profile_->GetPrefs()->GetInt64(sync_driver::prefs::kSyncLastSyncedTime));
247 EXPECT_FALSE(profile_->GetPrefs()->GetBoolean(
248 sync_driver::prefs::kSyncHasSetupCompleted));
249 Mock::VerifyAndClearExpectations(data_type_manager);
251 // Then start things up.
252 EXPECT_CALL(*data_type_manager, Configure(_, _)).Times(1);
253 EXPECT_CALL(*data_type_manager, state()).
254 WillOnce(Return(DataTypeManager::CONFIGURED)).
255 WillOnce(Return(DataTypeManager::CONFIGURED));
256 EXPECT_CALL(*data_type_manager, Stop()).Times(1);
257 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
259 sync_->SetSetupInProgress(true);
261 // Simulate successful signin as test_user.
262 SimulateTestUserSignin();
263 // Create some tokens in the token service.
264 IssueTestTokens();
266 // Simulate the UI telling sync it has finished setting up.
267 sync_->SetSetupInProgress(false);
268 EXPECT_TRUE(sync_->SyncActive());
271 // TODO(pavely): Reenable test once android is switched to oauth2.
272 TEST_F(ProfileSyncServiceStartupTest, DISABLED_StartNoCredentials) {
273 // We've never completed startup.
274 profile_->GetPrefs()->ClearPref(sync_driver::prefs::kSyncHasSetupCompleted);
275 CreateSyncService();
277 // Should not actually start, rather just clean things up and wait
278 // to be enabled.
279 EXPECT_CALL(*components_factory_mock(),
280 CreateDataTypeManager(_, _, _, _, _)).Times(0);
281 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
282 sync_->Initialize();
284 // Preferences should be back to defaults.
285 EXPECT_EQ(
287 profile_->GetPrefs()->GetInt64(sync_driver::prefs::kSyncLastSyncedTime));
288 EXPECT_FALSE(profile_->GetPrefs()->GetBoolean(
289 sync_driver::prefs::kSyncHasSetupCompleted));
291 // Then start things up.
292 sync_->SetSetupInProgress(true);
294 // Simulate successful signin as test_user.
295 SimulateTestUserSignin();
297 ProfileOAuth2TokenService* token_service =
298 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
299 token_service->LoadCredentials("test_user@gmail.com");
301 sync_->SetSetupInProgress(false);
302 // ProfileSyncService should try to start by requesting access token.
303 // This request should fail as login token was not issued.
304 EXPECT_FALSE(sync_->SyncActive());
305 EXPECT_EQ(GoogleServiceAuthError::USER_NOT_SIGNED_UP,
306 sync_->GetAuthError().state());
309 // TODO(pavely): Reenable test once android is switched to oauth2.
310 TEST_F(ProfileSyncServiceStartupTest, DISABLED_StartInvalidCredentials) {
311 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
312 "test_user@gmail.com");
313 sync_->signin()->SetAuthenticatedUsername("test_user@gmail.com");
314 CreateSyncService();
315 SyncBackendHostMock* mock_sbh = SetUpSyncBackendHost();
317 // Tell the backend to stall while downloading control types (simulating an
318 // auth error).
319 mock_sbh->set_fail_initial_download(true);
321 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
322 EXPECT_CALL(*data_type_manager, Configure(_, _)).Times(0);
324 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
325 sync_->Initialize();
326 EXPECT_FALSE(sync_->SyncActive());
327 Mock::VerifyAndClearExpectations(data_type_manager);
329 // Update the credentials, unstalling the backend.
330 EXPECT_CALL(*data_type_manager, Configure(_, _));
331 EXPECT_CALL(*data_type_manager, state()).
332 WillRepeatedly(Return(DataTypeManager::CONFIGURED));
333 EXPECT_CALL(*data_type_manager, Stop()).Times(1);
334 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
335 sync_->SetSetupInProgress(true);
337 // Simulate successful signin.
338 SimulateTestUserSignin();
340 sync_->SetSetupInProgress(false);
342 // Verify we successfully finish startup and configuration.
343 EXPECT_TRUE(sync_->SyncActive());
346 #if defined(OS_WIN)
347 // http://crbug.com/396402
348 #define MAYBE_StartCrosNoCredentials DISABLED_StartCrosNoCredentials
349 #else
350 #define MAYBE_StartCrosNoCredentials StartCrosNoCredentials
351 #endif
352 TEST_F(ProfileSyncServiceStartupCrosTest, MAYBE_StartCrosNoCredentials) {
353 EXPECT_CALL(*components_factory_mock(),
354 CreateDataTypeManager(_, _, _, _, _)).Times(0);
355 EXPECT_CALL(*components_factory_mock(),
356 CreateSyncBackendHost(_, _, _, _, _)).Times(0);
357 profile_->GetPrefs()->ClearPref(sync_driver::prefs::kSyncHasSetupCompleted);
358 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
360 sync_->Initialize();
361 // Sync should not start because there are no tokens yet.
362 EXPECT_FALSE(sync_->SyncActive());
363 sync_->SetSetupInProgress(false);
365 // Sync should not start because there are still no tokens.
366 EXPECT_FALSE(sync_->SyncActive());
369 TEST_F(ProfileSyncServiceStartupCrosTest, StartFirstTime) {
370 SetUpSyncBackendHost();
371 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
372 profile_->GetPrefs()->ClearPref(sync_driver::prefs::kSyncHasSetupCompleted);
373 EXPECT_CALL(*data_type_manager, Configure(_, _));
374 EXPECT_CALL(*data_type_manager, state()).
375 WillRepeatedly(Return(DataTypeManager::CONFIGURED));
376 EXPECT_CALL(*data_type_manager, Stop()).Times(1);
377 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
379 IssueTestTokens();
380 sync_->Initialize();
381 EXPECT_TRUE(sync_->SyncActive());
384 #if defined(OS_WIN)
385 // http://crbug.com/396402
386 #define MAYBE_StartNormal DISABLED_StartNormal
387 #else
388 #define MAYBE_StartNormal StartNormal
389 #endif
390 TEST_F(ProfileSyncServiceStartupTest, MAYBE_StartNormal) {
391 // Pre load the tokens
392 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
393 "test_user@gmail.com");
394 SigninManagerFactory::GetForProfile(profile_)
395 ->SetAuthenticatedUsername("test_user@gmail.com");
396 CreateSyncService();
397 sync_->SetSyncSetupCompleted();
398 SetUpSyncBackendHost();
399 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
400 EXPECT_CALL(*data_type_manager, Configure(_, _));
401 EXPECT_CALL(*data_type_manager, state()).
402 WillRepeatedly(Return(DataTypeManager::CONFIGURED));
403 EXPECT_CALL(*data_type_manager, Stop()).Times(1);
404 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
406 IssueTestTokens();
408 sync_->Initialize();
411 // Test that we can recover from a case where a bug in the code resulted in
412 // OnUserChoseDatatypes not being properly called and datatype preferences
413 // therefore being left unset.
414 TEST_F(ProfileSyncServiceStartupTest, StartRecoverDatatypePrefs) {
415 // Clear the datatype preference fields (simulating bug 154940).
416 profile_->GetPrefs()->ClearPref(
417 sync_driver::prefs::kSyncKeepEverythingSynced);
418 syncer::ModelTypeSet user_types = syncer::UserTypes();
419 for (syncer::ModelTypeSet::Iterator iter = user_types.First();
420 iter.Good(); iter.Inc()) {
421 profile_->GetPrefs()->ClearPref(
422 sync_driver::SyncPrefs::GetPrefNameForDataType(iter.Get()));
425 // Pre load the tokens
426 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
427 "test_user@gmail.com");
428 SigninManagerFactory::GetForProfile(profile_)
429 ->SetAuthenticatedUsername("test_user@gmail.com");
430 CreateSyncService();
431 sync_->SetSyncSetupCompleted();
432 SetUpSyncBackendHost();
433 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
434 EXPECT_CALL(*data_type_manager, Configure(_, _));
435 EXPECT_CALL(*data_type_manager, state()).
436 WillRepeatedly(Return(DataTypeManager::CONFIGURED));
437 EXPECT_CALL(*data_type_manager, Stop()).Times(1);
438 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
440 IssueTestTokens();
441 sync_->Initialize();
443 EXPECT_TRUE(profile_->GetPrefs()->GetBoolean(
444 sync_driver::prefs::kSyncKeepEverythingSynced));
447 // Verify that the recovery of datatype preferences doesn't overwrite a valid
448 // case where only bookmarks are enabled.
449 #if defined(OS_WIN)
450 // http://crbug.com/396402
451 #define MAYBE_StartDontRecoverDatatypePrefs DISABLED_StartDontRecoverDatatypePrefs
452 #else
453 #define MAYBE_StartDontRecoverDatatypePrefs StartDontRecoverDatatypePrefs
454 #endif
455 TEST_F(ProfileSyncServiceStartupTest, MAYBE_StartDontRecoverDatatypePrefs) {
456 // Explicitly set Keep Everything Synced to false and have only bookmarks
457 // enabled.
458 profile_->GetPrefs()->SetBoolean(
459 sync_driver::prefs::kSyncKeepEverythingSynced, false);
461 // Pre load the tokens
462 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
463 "test_user@gmail.com");
464 SigninManagerFactory::GetForProfile(profile_)
465 ->SetAuthenticatedUsername("test_user@gmail.com");
466 CreateSyncService();
467 sync_->SetSyncSetupCompleted();
468 SetUpSyncBackendHost();
469 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
470 EXPECT_CALL(*data_type_manager, Configure(_, _));
471 EXPECT_CALL(*data_type_manager, state()).
472 WillRepeatedly(Return(DataTypeManager::CONFIGURED));
473 EXPECT_CALL(*data_type_manager, Stop()).Times(1);
474 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
475 IssueTestTokens();
476 sync_->Initialize();
478 EXPECT_FALSE(profile_->GetPrefs()->GetBoolean(
479 sync_driver::prefs::kSyncKeepEverythingSynced));
482 #if defined(OS_WIN)
483 // http://crbug.com/396402
484 #define MAYBE_ManagedStartup DISABLED_ManagedStartup
485 #else
486 #define MAYBE_ManagedStartup ManagedStartup
487 #endif
488 TEST_F(ProfileSyncServiceStartupTest, MAYBE_ManagedStartup) {
489 // Service should not be started by Initialize() since it's managed.
490 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
491 "test_user@gmail.com");
492 CreateSyncService();
494 // Disable sync through policy.
495 profile_->GetPrefs()->SetBoolean(sync_driver::prefs::kSyncManaged, true);
496 EXPECT_CALL(*components_factory_mock(),
497 CreateDataTypeManager(_, _, _, _, _)).Times(0);
498 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
500 sync_->Initialize();
503 TEST_F(ProfileSyncServiceStartupTest, SwitchManaged) {
504 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
505 "test_user@gmail.com");
506 SigninManagerFactory::GetForProfile(profile_)
507 ->SetAuthenticatedUsername("test_user@gmail.com");
508 CreateSyncService();
509 sync_->SetSyncSetupCompleted();
510 SetUpSyncBackendHost();
511 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
512 EXPECT_CALL(*data_type_manager, Configure(_, _));
513 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
514 IssueTestTokens();
515 sync_->Initialize();
517 // The service should stop when switching to managed mode.
518 Mock::VerifyAndClearExpectations(data_type_manager);
519 EXPECT_CALL(*data_type_manager, state()).
520 WillOnce(Return(DataTypeManager::CONFIGURED));
521 EXPECT_CALL(*data_type_manager, Stop()).Times(1);
522 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
523 profile_->GetPrefs()->SetBoolean(sync_driver::prefs::kSyncManaged, true);
525 // When switching back to unmanaged, the state should change, but the service
526 // should not start up automatically (kSyncSetupCompleted will be false).
527 Mock::VerifyAndClearExpectations(data_type_manager);
528 EXPECT_CALL(*components_factory_mock(),
529 CreateDataTypeManager(_, _, _, _, _)).Times(0);
530 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
531 profile_->GetPrefs()->ClearPref(sync_driver::prefs::kSyncManaged);
534 TEST_F(ProfileSyncServiceStartupTest, StartFailure) {
535 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
536 "test_user@gmail.com");
537 SigninManagerFactory::GetForProfile(profile_)
538 ->SetAuthenticatedUsername("test_user@gmail.com");
539 CreateSyncService();
540 sync_->SetSyncSetupCompleted();
541 SetUpSyncBackendHost();
542 DataTypeManagerMock* data_type_manager = SetUpDataTypeManager();
543 DataTypeManager::ConfigureStatus status = DataTypeManager::ABORTED;
544 DataTypeManager::ConfigureResult result(
545 status,
546 syncer::ModelTypeSet());
547 EXPECT_CALL(*data_type_manager, Configure(_, _)).WillRepeatedly(
548 DoAll(InvokeOnConfigureStart(sync_),
549 InvokeOnConfigureDone(
550 sync_,
551 base::Bind(&ProfileSyncServiceStartupTest::SetError,
552 base::Unretained(this)),
553 result)));
554 EXPECT_CALL(*data_type_manager, state()).
555 WillOnce(Return(DataTypeManager::STOPPED));
556 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
557 IssueTestTokens();
558 sync_->Initialize();
559 EXPECT_TRUE(sync_->HasUnrecoverableError());
562 TEST_F(ProfileSyncServiceStartupTest, StartDownloadFailed) {
563 // Pre load the tokens
564 profile_->GetPrefs()->SetString(prefs::kGoogleServicesUsername,
565 "test_user@gmail.com");
566 SigninManagerFactory::GetForProfile(profile_)
567 ->SetAuthenticatedUsername("test_user@gmail.com");
568 CreateSyncService();
569 SyncBackendHostMock* mock_sbh = SetUpSyncBackendHost();
570 mock_sbh->set_fail_initial_download(true);
572 profile_->GetPrefs()->ClearPref(sync_driver::prefs::kSyncHasSetupCompleted);
574 EXPECT_CALL(observer_, OnStateChanged()).Times(AnyNumber());
575 sync_->Initialize();
577 sync_->SetSetupInProgress(true);
578 IssueTestTokens();
579 sync_->SetSetupInProgress(false);
580 EXPECT_FALSE(sync_->SyncActive());