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/basictypes.h"
6 #include "base/command_line.h"
7 #include "base/compiler_specific.h"
8 #include "base/location.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/run_loop.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "base/values.h"
16 #include "chrome/browser/invalidation/fake_invalidation_service.h"
17 #include "chrome/browser/invalidation/profile_invalidation_provider_factory.h"
18 #include "chrome/browser/signin/account_tracker_service_factory.h"
19 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
20 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
21 #include "chrome/browser/signin/signin_manager_factory.h"
22 #include "chrome/browser/sync/glue/sync_backend_host_mock.h"
23 #include "chrome/browser/sync/profile_sync_components_factory_mock.h"
24 #include "chrome/browser/sync/supervised_user_signin_manager_wrapper.h"
25 #include "chrome/common/chrome_switches.h"
26 #include "chrome/common/pref_names.h"
27 #include "chrome/common/sync_util.h"
28 #include "chrome/grit/generated_resources.h"
29 #include "chrome/test/base/testing_browser_process.h"
30 #include "chrome/test/base/testing_pref_service_syncable.h"
31 #include "chrome/test/base/testing_profile.h"
32 #include "chrome/test/base/testing_profile_manager.h"
33 #include "components/invalidation/impl/profile_invalidation_provider.h"
34 #include "components/invalidation/public/invalidation_service.h"
35 #include "components/signin/core/browser/account_tracker_service.h"
36 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
37 #include "components/signin/core/browser/signin_manager.h"
38 #include "components/signin/core/browser/signin_manager.h"
39 #include "components/sync_driver/data_type_manager.h"
40 #include "components/sync_driver/pref_names.h"
41 #include "components/sync_driver/sync_prefs.h"
42 #include "content/public/test/test_browser_thread_bundle.h"
43 #include "google_apis/gaia/gaia_constants.h"
44 #include "testing/gmock/include/gmock/gmock.h"
45 #include "testing/gtest/include/gtest/gtest.h"
46 #include "ui/base/l10n/l10n_util.h"
52 namespace browser_sync
{
56 const char kGaiaId
[] = "12345";
57 const char kEmail
[] = "test_user@gmail.com";
59 class FakeDataTypeManager
: public sync_driver::DataTypeManager
{
61 explicit FakeDataTypeManager(sync_driver::DataTypeManagerObserver
* observer
)
62 : observer_(observer
) {}
63 ~FakeDataTypeManager() override
{};
65 void Configure(syncer::ModelTypeSet desired_types
,
66 syncer::ConfigureReason reason
) override
{
67 sync_driver::DataTypeManager::ConfigureResult result
;
68 result
.status
= sync_driver::DataTypeManager::OK
;
69 observer_
->OnConfigureDone(result
);
72 void ReenableType(syncer::ModelType type
) override
{}
73 void ResetDataTypeErrors() override
{}
74 void PurgeForMigration(syncer::ModelTypeSet undesired_types
,
75 syncer::ConfigureReason reason
) override
{}
76 void Stop() override
{};
77 State
state() const override
{
78 return sync_driver::DataTypeManager::CONFIGURED
;
82 sync_driver::DataTypeManagerObserver
* observer_
;
85 ACTION(ReturnNewDataTypeManager
) {
86 return new FakeDataTypeManager(arg4
);
89 using testing::Return
;
90 using testing::StrictMock
;
93 class TestSyncServiceObserver
: public sync_driver::SyncServiceObserver
{
95 explicit TestSyncServiceObserver(ProfileSyncService
* service
)
96 : service_(service
), first_setup_in_progress_(false) {}
97 void OnStateChanged() override
{
98 first_setup_in_progress_
= service_
->FirstSetupInProgress();
100 bool first_setup_in_progress() const { return first_setup_in_progress_
; }
102 ProfileSyncService
* service_
;
103 bool first_setup_in_progress_
;
106 // A variant of the SyncBackendHostMock that won't automatically
107 // call back when asked to initialized. Allows us to test things
108 // that could happen while backend init is in progress.
109 class SyncBackendHostNoReturn
: public SyncBackendHostMock
{
111 sync_driver::SyncFrontend
* frontend
,
112 scoped_ptr
<base::Thread
> sync_thread
,
113 const syncer::WeakHandle
<syncer::JsEventHandler
>& event_handler
,
114 const GURL
& service_url
,
115 const syncer::SyncCredentials
& credentials
,
116 bool delete_sync_data_folder
,
117 scoped_ptr
<syncer::SyncManagerFactory
> sync_manager_factory
,
118 scoped_ptr
<syncer::UnrecoverableErrorHandler
> unrecoverable_error_handler
,
119 const base::Closure
& report_unrecoverable_error_function
,
120 syncer::NetworkResources
* network_resources
,
121 scoped_ptr
<syncer::SyncEncryptionHandler::NigoriState
> saved_nigori_state
)
125 class SyncBackendHostMockCollectDeleteDirParam
: public SyncBackendHostMock
{
127 explicit SyncBackendHostMockCollectDeleteDirParam(
128 std::vector
<bool>* delete_dir_param
)
129 : delete_dir_param_(delete_dir_param
) {}
132 sync_driver::SyncFrontend
* frontend
,
133 scoped_ptr
<base::Thread
> sync_thread
,
134 const syncer::WeakHandle
<syncer::JsEventHandler
>& event_handler
,
135 const GURL
& service_url
,
136 const syncer::SyncCredentials
& credentials
,
137 bool delete_sync_data_folder
,
138 scoped_ptr
<syncer::SyncManagerFactory
> sync_manager_factory
,
139 scoped_ptr
<syncer::UnrecoverableErrorHandler
> unrecoverable_error_handler
,
140 const base::Closure
& report_unrecoverable_error_function
,
141 syncer::NetworkResources
* network_resources
,
142 scoped_ptr
<syncer::SyncEncryptionHandler::NigoriState
> saved_nigori_state
)
144 delete_dir_param_
->push_back(delete_sync_data_folder
);
145 SyncBackendHostMock::Initialize(frontend
, sync_thread
.Pass(),
146 event_handler
, service_url
, credentials
,
147 delete_sync_data_folder
,
148 sync_manager_factory
.Pass(),
149 unrecoverable_error_handler
.Pass(),
150 report_unrecoverable_error_function
,
152 saved_nigori_state
.Pass());
156 std::vector
<bool>* delete_dir_param_
;
159 ACTION(ReturnNewSyncBackendHostMock
) {
160 return new browser_sync::SyncBackendHostMock();
163 ACTION(ReturnNewSyncBackendHostNoReturn
) {
164 return new browser_sync::SyncBackendHostNoReturn();
167 ACTION_P(ReturnNewMockHostCollectDeleteDirParam
, delete_dir_param
) {
168 return new browser_sync::SyncBackendHostMockCollectDeleteDirParam(
172 scoped_ptr
<KeyedService
> BuildFakeProfileInvalidationProvider(
173 content::BrowserContext
* context
) {
174 return make_scoped_ptr(new invalidation::ProfileInvalidationProvider(
175 scoped_ptr
<invalidation::InvalidationService
>(
176 new invalidation::FakeInvalidationService
)));
179 // A test harness that uses a real ProfileSyncService and in most cases a
180 // MockSyncBackendHost.
182 // This is useful if we want to test the ProfileSyncService and don't care about
183 // testing the SyncBackendHost.
184 class ProfileSyncServiceTest
: public ::testing::Test
{
186 ProfileSyncServiceTest()
187 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
),
188 profile_manager_(TestingBrowserProcess::GetGlobal()) {}
189 ~ProfileSyncServiceTest() override
{}
191 void SetUp() override
{
192 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
193 switches::kSyncDeferredStartupTimeoutSeconds
, "0");
195 CHECK(profile_manager_
.SetUp());
197 TestingProfile::TestingFactories testing_facotries
;
198 testing_facotries
.push_back(
199 std::make_pair(ProfileOAuth2TokenServiceFactory::GetInstance(),
200 BuildAutoIssuingFakeProfileOAuth2TokenService
));
201 testing_facotries
.push_back(
203 invalidation::ProfileInvalidationProviderFactory::GetInstance(),
204 BuildFakeProfileInvalidationProvider
));
206 profile_
= profile_manager_
.CreateTestingProfile(
207 "sync-service-test", scoped_ptr
<PrefServiceSyncable
>(),
208 base::UTF8ToUTF16("sync-service-test"), 0, std::string(),
212 void TearDown() override
{
213 // Kill the service before the profile.
215 service_
->Shutdown();
220 void IssueTestTokens() {
221 std::string account_id
=
222 AccountTrackerServiceFactory::GetForProfile(profile_
)
223 ->SeedAccountInfo(kGaiaId
, kEmail
);
224 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_
)
225 ->UpdateCredentials(account_id
, "oauth2_login_token");
228 void CreateService(ProfileSyncServiceStartBehavior behavior
) {
229 SigninManagerBase
* signin
=
230 SigninManagerFactory::GetForProfile(profile_
);
231 signin
->SetAuthenticatedAccountInfo(kGaiaId
, kEmail
);
232 ProfileOAuth2TokenService
* oauth2_token_service
=
233 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_
);
234 components_factory_
= new ProfileSyncComponentsFactoryMock();
235 service_
.reset(new ProfileSyncService(
236 scoped_ptr
<ProfileSyncComponentsFactory
>(components_factory_
),
238 make_scoped_ptr(new SupervisedUserSigninManagerWrapper(profile_
,
240 oauth2_token_service
,
242 service_
->SetClearingBrowseringDataForTesting(
243 base::Bind(&ProfileSyncServiceTest::ClearBrowsingDataCallback
,
244 base::Unretained(this)));
247 #if defined(OS_WIN) || defined(OS_MACOSX) || (defined(OS_LINUX) && !defined(OS_CHROMEOS))
248 void CreateServiceWithoutSignIn() {
249 CreateService(browser_sync::AUTO_START
);
250 SigninManagerFactory::GetForProfile(profile())->SignOut(
251 signin_metrics::SIGNOUT_TEST
);
255 void ShutdownAndDeleteService() {
257 service_
->Shutdown();
261 void InitializeForNthSync() {
262 // Set first sync time before initialize to disable backup.
263 sync_driver::SyncPrefs
sync_prefs(service()->profile()->GetPrefs());
264 sync_prefs
.SetFirstSyncTime(base::Time::Now());
265 service_
->Initialize();
268 void InitializeForFirstSync() {
269 service_
->Initialize();
272 void ExpectDataTypeManagerCreation(int times
) {
273 EXPECT_CALL(*components_factory_
, CreateDataTypeManager(_
, _
, _
, _
, _
))
275 .WillRepeatedly(ReturnNewDataTypeManager());
278 void ExpectSyncBackendHostCreation(int times
) {
279 EXPECT_CALL(*components_factory_
, CreateSyncBackendHost(_
, _
, _
, _
, _
))
281 .WillRepeatedly(ReturnNewSyncBackendHostMock());
284 void ExpectSyncBackendHostCreationCollectDeleteDir(
285 int times
, std::vector
<bool> *delete_dir_param
) {
286 EXPECT_CALL(*components_factory_
, CreateSyncBackendHost(_
, _
, _
, _
, _
))
288 .WillRepeatedly(ReturnNewMockHostCollectDeleteDirParam(
292 void PrepareDelayedInitSyncBackendHost() {
293 EXPECT_CALL(*components_factory_
, CreateSyncBackendHost(_
, _
, _
, _
, _
)).
294 WillOnce(ReturnNewSyncBackendHostNoReturn());
297 TestingProfile
* profile() {
301 ProfileSyncService
* service() {
302 return service_
.get();
305 ProfileSyncComponentsFactoryMock
* components_factory() {
306 return components_factory_
;
309 void ClearBrowsingDataCallback(BrowsingDataRemover::Observer
* observer
,
313 EXPECT_EQ(profile_
, profile
);
314 clear_browsing_date_start_
= start
;
319 base::RunLoop run_loop
;
320 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
,
321 run_loop
.QuitClosure());
325 // The requested start time when ClearBrowsingDataCallback is called.
326 base::Time clear_browsing_date_start_
;
329 content::TestBrowserThreadBundle thread_bundle_
;
330 TestingProfileManager profile_manager_
;
331 TestingProfile
* profile_
;
332 scoped_ptr
<ProfileSyncService
> service_
;
334 // Pointer to the components factory. Not owned. May be null.
335 ProfileSyncComponentsFactoryMock
* components_factory_
;
338 // Verify that the server URLs are sane.
339 TEST_F(ProfileSyncServiceTest
, InitialState
) {
340 CreateService(browser_sync::AUTO_START
);
341 InitializeForNthSync();
342 const std::string
& url
= service()->sync_service_url().spec();
343 EXPECT_TRUE(url
== internal::kSyncServerUrl
||
344 url
== internal::kSyncDevServerUrl
);
347 // Verify a successful initialization.
348 TEST_F(ProfileSyncServiceTest
, SuccessfulInitialization
) {
349 profile()->GetTestingPrefService()->SetManagedPref(
350 sync_driver::prefs::kSyncManaged
, new base::FundamentalValue(false));
352 CreateService(browser_sync::AUTO_START
);
353 ExpectDataTypeManagerCreation(1);
354 ExpectSyncBackendHostCreation(1);
355 InitializeForNthSync();
356 EXPECT_FALSE(service()->IsManaged());
357 EXPECT_TRUE(service()->IsSyncActive());
358 EXPECT_EQ(ProfileSyncService::SYNC
, service()->backend_mode());
362 // Verify that the SetSetupInProgress function call updates state
363 // and notifies observers.
364 TEST_F(ProfileSyncServiceTest
, SetupInProgress
) {
365 CreateService(browser_sync::AUTO_START
);
366 InitializeForNthSync();
368 TestSyncServiceObserver
observer(service());
369 service()->AddObserver(&observer
);
371 service()->SetSetupInProgress(true);
372 EXPECT_TRUE(observer
.first_setup_in_progress());
373 service()->SetSetupInProgress(false);
374 EXPECT_FALSE(observer
.first_setup_in_progress());
376 service()->RemoveObserver(&observer
);
379 // Verify that disable by enterprise policy works.
380 TEST_F(ProfileSyncServiceTest
, DisabledByPolicyBeforeInit
) {
381 profile()->GetTestingPrefService()->SetManagedPref(
382 sync_driver::prefs::kSyncManaged
, new base::FundamentalValue(true));
384 CreateService(browser_sync::AUTO_START
);
385 InitializeForNthSync();
386 EXPECT_TRUE(service()->IsManaged());
387 EXPECT_FALSE(service()->IsSyncActive());
390 // Verify that disable by enterprise policy works even after the backend has
392 TEST_F(ProfileSyncServiceTest
, DisabledByPolicyAfterInit
) {
394 CreateService(browser_sync::AUTO_START
);
395 ExpectDataTypeManagerCreation(1);
396 ExpectSyncBackendHostCreation(1);
397 InitializeForNthSync();
399 EXPECT_FALSE(service()->IsManaged());
400 EXPECT_TRUE(service()->IsSyncActive());
402 profile()->GetTestingPrefService()->SetManagedPref(
403 sync_driver::prefs::kSyncManaged
, new base::FundamentalValue(true));
405 EXPECT_TRUE(service()->IsManaged());
406 EXPECT_FALSE(service()->IsSyncActive());
409 // Exercies the ProfileSyncService's code paths related to getting shut down
410 // before the backend initialize call returns.
411 TEST_F(ProfileSyncServiceTest
, AbortedByShutdown
) {
412 CreateService(browser_sync::AUTO_START
);
413 PrepareDelayedInitSyncBackendHost();
416 InitializeForNthSync();
417 EXPECT_FALSE(service()->IsSyncActive());
419 ShutdownAndDeleteService();
422 // Test RequestStop() before we've initialized the backend.
423 TEST_F(ProfileSyncServiceTest
, EarlyRequestStop
) {
424 CreateService(browser_sync::AUTO_START
);
427 service()->RequestStop(ProfileSyncService::KEEP_DATA
);
428 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
429 sync_driver::prefs::kSyncSuppressStart
));
431 // Because of supression, this should fail.
432 InitializeForNthSync();
433 EXPECT_FALSE(service()->IsSyncActive());
435 // Remove suppression. This should be enough to allow init to happen.
436 ExpectDataTypeManagerCreation(1);
437 ExpectSyncBackendHostCreation(1);
438 service()->RequestStart();
439 EXPECT_TRUE(service()->IsSyncActive());
440 EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(
441 sync_driver::prefs::kSyncSuppressStart
));
444 // Test RequestStop() after we've initialized the backend.
445 TEST_F(ProfileSyncServiceTest
, DisableAndEnableSyncTemporarily
) {
446 CreateService(browser_sync::AUTO_START
);
448 ExpectDataTypeManagerCreation(1);
449 ExpectSyncBackendHostCreation(1);
450 InitializeForNthSync();
452 EXPECT_TRUE(service()->IsSyncActive());
453 EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(
454 sync_driver::prefs::kSyncSuppressStart
));
456 testing::Mock::VerifyAndClearExpectations(components_factory());
458 service()->RequestStop(ProfileSyncService::KEEP_DATA
);
459 EXPECT_FALSE(service()->IsSyncActive());
460 EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
461 sync_driver::prefs::kSyncSuppressStart
));
463 ExpectDataTypeManagerCreation(1);
464 ExpectSyncBackendHostCreation(1);
466 service()->RequestStart();
467 EXPECT_TRUE(service()->IsSyncActive());
468 EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(
469 sync_driver::prefs::kSyncSuppressStart
));
472 // Certain ProfileSyncService tests don't apply to Chrome OS, for example
473 // things that deal with concepts like "signing out" and policy.
474 #if !defined (OS_CHROMEOS)
475 TEST_F(ProfileSyncServiceTest
, EnableSyncAndSignOut
) {
476 CreateService(browser_sync::AUTO_START
);
477 ExpectDataTypeManagerCreation(1);
478 ExpectSyncBackendHostCreation(1);
480 InitializeForNthSync();
482 EXPECT_TRUE(service()->IsSyncActive());
483 EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(
484 sync_driver::prefs::kSyncSuppressStart
));
486 SigninManagerFactory::GetForProfile(profile())->SignOut(
487 signin_metrics::SIGNOUT_TEST
);
488 EXPECT_FALSE(service()->IsSyncActive());
490 #endif // !defined(OS_CHROMEOS)
492 TEST_F(ProfileSyncServiceTest
, GetSyncTokenStatus
) {
493 CreateService(browser_sync::AUTO_START
);
495 ExpectDataTypeManagerCreation(1);
496 ExpectSyncBackendHostCreation(1);
497 InitializeForNthSync();
500 ProfileSyncService::SyncTokenStatus token_status
=
501 service()->GetSyncTokenStatus();
502 EXPECT_EQ(syncer::CONNECTION_NOT_ATTEMPTED
, token_status
.connection_status
);
503 EXPECT_TRUE(token_status
.connection_status_update_time
.is_null());
504 EXPECT_TRUE(token_status
.token_request_time
.is_null());
505 EXPECT_TRUE(token_status
.token_receive_time
.is_null());
507 // Simulate an auth error.
508 service()->OnConnectionStatusChange(syncer::CONNECTION_AUTH_ERROR
);
510 // The token request will take the form of a posted task. Run it.
514 token_status
= service()->GetSyncTokenStatus();
515 EXPECT_EQ(syncer::CONNECTION_AUTH_ERROR
, token_status
.connection_status
);
516 EXPECT_FALSE(token_status
.connection_status_update_time
.is_null());
517 EXPECT_FALSE(token_status
.token_request_time
.is_null());
518 EXPECT_FALSE(token_status
.token_receive_time
.is_null());
519 EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(),
520 token_status
.last_get_token_error
);
521 EXPECT_TRUE(token_status
.next_token_request_time
.is_null());
523 // Simulate successful connection.
524 service()->OnConnectionStatusChange(syncer::CONNECTION_OK
);
525 token_status
= service()->GetSyncTokenStatus();
526 EXPECT_EQ(syncer::CONNECTION_OK
, token_status
.connection_status
);
529 #if defined(ENABLE_PRE_SYNC_BACKUP)
530 TEST_F(ProfileSyncServiceTest
, DontStartBackupOnBrowserStart
) {
531 CreateServiceWithoutSignIn();
532 InitializeForFirstSync();
534 EXPECT_EQ(ProfileSyncService::IDLE
, service()->backend_mode());
537 TEST_F(ProfileSyncServiceTest
, BackupBeforeFirstSync
) {
538 CreateServiceWithoutSignIn();
539 ExpectDataTypeManagerCreation(2);
540 std::vector
<bool> delete_dir_param
;
541 ExpectSyncBackendHostCreationCollectDeleteDir(2, &delete_dir_param
);
542 InitializeForFirstSync();
544 SigninManagerFactory::GetForProfile(profile())
545 ->SetAuthenticatedAccountInfo(kGaiaId
, kEmail
);
549 // At this time, backup is finished. Task is posted to start sync again.
550 EXPECT_EQ(ProfileSyncService::BACKUP
, service()->backend_mode());
551 EXPECT_FALSE(service()->IsSyncActive());
552 EXPECT_EQ(1u, delete_dir_param
.size());
553 EXPECT_TRUE(delete_dir_param
[0]);
555 // Pump loop to start sync.
557 EXPECT_EQ(ProfileSyncService::SYNC
, service()->backend_mode());
558 EXPECT_TRUE(service()->IsSyncActive());
559 EXPECT_EQ(2u, delete_dir_param
.size());
560 EXPECT_TRUE(delete_dir_param
[0]);
563 // Test backup is done again on browser start if user signed in last session
564 // but backup didn't finish when last session was closed.
565 TEST_F(ProfileSyncServiceTest
, ResumeBackupIfAborted
) {
567 CreateService(AUTO_START
);
568 ExpectDataTypeManagerCreation(2);
569 std::vector
<bool> delete_dir_param
;
570 ExpectSyncBackendHostCreationCollectDeleteDir(2, &delete_dir_param
);
571 InitializeForFirstSync();
574 // At this time, backup is finished. Task is posted to start sync again.
575 EXPECT_EQ(ProfileSyncService::BACKUP
, service()->backend_mode());
576 EXPECT_FALSE(service()->IsSyncActive());
577 EXPECT_EQ(1u, delete_dir_param
.size());
578 EXPECT_TRUE(delete_dir_param
[0]);
580 // Pump loop to start sync.
582 EXPECT_EQ(ProfileSyncService::SYNC
, service()->backend_mode());
583 EXPECT_TRUE(service()->IsSyncActive());
584 EXPECT_EQ(2u, delete_dir_param
.size());
585 EXPECT_TRUE(delete_dir_param
[0]);
588 TEST_F(ProfileSyncServiceTest
, Rollback
) {
589 CreateService(browser_sync::MANUAL_START
);
590 service()->SetSyncSetupCompleted();
591 ExpectDataTypeManagerCreation(2);
592 std::vector
<bool> delete_dir_param
;
593 ExpectSyncBackendHostCreationCollectDeleteDir(2, &delete_dir_param
);
595 InitializeForNthSync();
596 EXPECT_TRUE(service()->IsSyncActive());
597 EXPECT_EQ(ProfileSyncService::SYNC
, service()->backend_mode());
599 // First sync time should be recorded.
600 sync_driver::SyncPrefs
sync_prefs(service()->profile()->GetPrefs());
601 base::Time first_sync_time
= sync_prefs
.GetFirstSyncTime();
602 EXPECT_FALSE(first_sync_time
.is_null());
604 syncer::SyncProtocolError client_cmd
;
605 client_cmd
.action
= syncer::DISABLE_SYNC_AND_ROLLBACK
;
606 service()->OnActionableError(client_cmd
);
607 EXPECT_EQ(ProfileSyncService::IDLE
, service()->backend_mode());
609 // Pump loop to run rollback.
611 EXPECT_EQ(ProfileSyncService::ROLLBACK
, service()->backend_mode());
613 // Browser data should be cleared during rollback.
614 EXPECT_EQ(first_sync_time
, clear_browsing_date_start_
);
616 client_cmd
.action
= syncer::ROLLBACK_DONE
;
617 service()->OnActionableError(client_cmd
);
618 EXPECT_EQ(ProfileSyncService::IDLE
, service()->backend_mode());
620 // First sync time is erased after rollback is done.
621 EXPECT_TRUE(sync_prefs
.GetFirstSyncTime().is_null());
623 EXPECT_EQ(2u, delete_dir_param
.size());
624 EXPECT_FALSE(delete_dir_param
[0]);
625 EXPECT_FALSE(delete_dir_param
[1]);
630 // Verify that LastSyncedTime is cleared when the user signs out.
631 TEST_F(ProfileSyncServiceTest
, ClearLastSyncedTimeOnSignOut
) {
633 CreateService(AUTO_START
);
634 ExpectDataTypeManagerCreation(1);
635 ExpectSyncBackendHostCreation(1);
636 InitializeForNthSync();
637 EXPECT_TRUE(service()->IsSyncActive());
638 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_SYNC_TIME_JUST_NOW
),
639 service()->GetLastSyncedTimeString());
642 service()->RequestStop(ProfileSyncService::CLEAR_DATA
);
645 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_SYNC_TIME_NEVER
),
646 service()->GetLastSyncedTimeString());
649 // Verify that the disable sync flag disables sync.
650 TEST_F(ProfileSyncServiceTest
, DisableSyncFlag
) {
651 base::CommandLine::ForCurrentProcess()->AppendSwitch(switches::kDisableSync
);
652 EXPECT_FALSE(ProfileSyncService::IsSyncAllowedByFlag());
655 // Verify that no disable sync flag enables sync.
656 TEST_F(ProfileSyncServiceTest
, NoDisableSyncFlag
) {
657 EXPECT_TRUE(ProfileSyncService::IsSyncAllowedByFlag());
660 // Test Sync will stop after receive memory pressure
661 TEST_F(ProfileSyncServiceTest
, MemoryPressureRecording
) {
662 CreateService(browser_sync::AUTO_START
);
664 ExpectDataTypeManagerCreation(1);
665 ExpectSyncBackendHostCreation(1);
666 InitializeForNthSync();
668 EXPECT_TRUE(service()->IsSyncActive());
669 EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(
670 sync_driver::prefs::kSyncSuppressStart
));
672 testing::Mock::VerifyAndClearExpectations(components_factory());
674 sync_driver::SyncPrefs
sync_prefs(service()->profile()->GetPrefs());
676 EXPECT_EQ(profile()->GetPrefs()->GetInteger(
677 sync_driver::prefs::kSyncMemoryPressureWarningCount
),
679 EXPECT_FALSE(sync_prefs
.DidSyncShutdownCleanly());
681 // Simulate memory pressure notification.
682 base::MemoryPressureListener::NotifyMemoryPressure(
683 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL
);
684 base::RunLoop().RunUntilIdle();
686 // Verify memory pressure recorded.
687 EXPECT_EQ(profile()->GetPrefs()->GetInteger(
688 sync_driver::prefs::kSyncMemoryPressureWarningCount
),
690 EXPECT_FALSE(sync_prefs
.DidSyncShutdownCleanly());
692 // Simulate memory pressure notification.
693 base::MemoryPressureListener::NotifyMemoryPressure(
694 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL
);
695 base::RunLoop().RunUntilIdle();
696 ShutdownAndDeleteService();
698 // Verify memory pressure and shutdown recorded.
699 EXPECT_EQ(profile()->GetPrefs()->GetInteger(
700 sync_driver::prefs::kSyncMemoryPressureWarningCount
),
702 EXPECT_TRUE(sync_prefs
.DidSyncShutdownCleanly());
705 // Verify that OnLocalSetPassphraseEncryption shuts down and restarts sync.
706 TEST_F(ProfileSyncServiceTest
, OnLocalSetPassphraseEncryption
) {
707 base::CommandLine::ForCurrentProcess()->AppendSwitch(
708 switches::kSyncEnableClearDataOnPassphraseEncryption
);
710 CreateService(browser_sync::AUTO_START
);
711 ExpectDataTypeManagerCreation(1);
712 ExpectSyncBackendHostCreation(1);
713 InitializeForNthSync();
714 EXPECT_TRUE(service()->IsSyncActive());
715 EXPECT_EQ(ProfileSyncService::SYNC
, service()->backend_mode());
716 testing::Mock::VerifyAndClearExpectations(components_factory());
718 ExpectDataTypeManagerCreation(1);
719 ExpectSyncBackendHostCreation(1);
720 const syncer::SyncEncryptionHandler::NigoriState nigori_state
;
721 service()->OnLocalSetPassphraseEncryption(nigori_state
);
723 testing::Mock::VerifyAndClearExpectations(components_factory());
727 } // namespace browser_sync