1 // Copyright 2014 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 "chrome/browser/sync/startup_controller.h"
7 #include "base/command_line.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "base/time/time.h"
11 #include "chrome/browser/defaults.h"
12 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
13 #include "chrome/browser/signin/fake_profile_oauth2_token_service_builder.h"
14 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
15 #include "chrome/browser/sync/supervised_user_signin_manager_wrapper.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/test/base/testing_profile.h"
18 #include "components/sync_driver/sync_prefs.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 namespace browser_sync
{
24 static const char kTestUser
[] = "test@gmail.com";
25 static const char kTestToken
[] = "testToken";
27 // These are coupled to the implementation of StartupController's
28 // GetBackendInitializationStateString which is used by about:sync. We use it
29 // as a convenient way to verify internal state and that the class is
30 // outputting the correct values for the debug string.
31 static const char kStateStringStarted
[] = "Started";
32 static const char kStateStringDeferred
[] = "Deferred";
33 static const char kStateStringNotStarted
[] = "Not started";
35 class FakeSupervisedUserSigninManagerWrapper
36 : public SupervisedUserSigninManagerWrapper
{
38 FakeSupervisedUserSigninManagerWrapper()
39 : SupervisedUserSigninManagerWrapper(NULL
, NULL
) {}
40 std::string
GetEffectiveUsername() const override
{ return account_
; }
42 std::string
GetAccountIdToUse() const override
{ return account_
; }
44 void set_account(const std::string
& account
) { account_
= account
; }
50 class StartupControllerTest
: public testing::Test
{
52 StartupControllerTest() : started_(false) {}
54 void SetUp() override
{
55 profile_
.reset(new TestingProfile());
56 sync_prefs_
.reset(new sync_driver::SyncPrefs(profile_
->GetPrefs()));
57 token_service_
.reset(static_cast<FakeProfileOAuth2TokenService
*>(
58 BuildFakeProfileOAuth2TokenService(profile_
.get()).release()));
59 signin_
.reset(new FakeSupervisedUserSigninManagerWrapper());
61 ProfileSyncServiceStartBehavior behavior
=
62 browser_defaults::kSyncAutoStarts
? AUTO_START
: MANUAL_START
;
63 base::Closure fake_start_backend
= base::Bind(
64 &StartupControllerTest::FakeStartBackend
, base::Unretained(this));
65 controller_
.reset(new StartupController(behavior
, token_service(),
66 sync_prefs_
.get(), signin_
.get(),
68 controller_
->Reset(syncer::UserTypes());
69 controller_
->OverrideFallbackTimeoutForTest(
70 base::TimeDelta::FromSeconds(0));
73 void TearDown() override
{
76 token_service_
->Shutdown();
77 token_service_
.reset();
82 void FakeStartBackend() {
86 bool started() const { return started_
; }
87 void clear_started() { started_
= false; }
88 StartupController
* controller() { return controller_
.get(); }
89 FakeSupervisedUserSigninManagerWrapper
* signin() { return signin_
.get(); }
90 FakeProfileOAuth2TokenService
* token_service() {
91 return token_service_
.get();
93 sync_driver::SyncPrefs
* sync_prefs() { return sync_prefs_
.get(); }
94 Profile
* profile() { return profile_
.get(); }
98 content::TestBrowserThreadBundle thread_bundle_
;
99 scoped_ptr
<StartupController
> controller_
;
100 scoped_ptr
<FakeSupervisedUserSigninManagerWrapper
> signin_
;
101 scoped_ptr
<FakeProfileOAuth2TokenService
> token_service_
;
102 scoped_ptr
<sync_driver::SyncPrefs
> sync_prefs_
;
103 scoped_ptr
<TestingProfile
> profile_
;
106 // Test that sync doesn't start until all conditions are met.
107 TEST_F(StartupControllerTest
, Basic
) {
108 controller()->TryStart();
109 EXPECT_FALSE(started());
110 sync_prefs()->SetSyncSetupCompleted();
111 controller()->TryStart();
112 EXPECT_FALSE(started());
113 signin()->set_account(kTestUser
);
114 controller()->TryStart();
115 EXPECT_FALSE(started());
116 token_service()->IssueRefreshTokenForUser(kTestUser
, kTestToken
);
117 const bool deferred_start
=
118 !base::CommandLine::ForCurrentProcess()->HasSwitch(
119 switches::kSyncDisableDeferredStartup
);
120 controller()->TryStart();
121 EXPECT_EQ(!deferred_start
, started());
122 std::string
state(controller()->GetBackendInitializationStateString());
123 EXPECT_TRUE(deferred_start
? state
== kStateStringDeferred
:
124 state
== kStateStringStarted
);
127 // Test that sync doesn't start when not requested even if all other
128 // conditons are met.
129 TEST_F(StartupControllerTest
, NotRequested
) {
130 sync_prefs()->SetSyncSetupCompleted();
131 sync_prefs()->SetSyncRequested(false);
132 signin()->set_account(kTestUser
);
133 token_service()->IssueRefreshTokenForUser(kTestUser
, kTestToken
);
134 controller()->TryStart();
135 EXPECT_FALSE(started());
136 EXPECT_EQ(kStateStringNotStarted
,
137 controller()->GetBackendInitializationStateString());
140 // Test that sync doesn't when managed even if all other conditons are met.
141 TEST_F(StartupControllerTest
, Managed
) {
142 sync_prefs()->SetSyncSetupCompleted();
143 sync_prefs()->SetManagedForTest(true);
144 signin()->set_account(kTestUser
);
145 token_service()->IssueRefreshTokenForUser(kTestUser
, kTestToken
);
146 controller()->TryStart();
147 EXPECT_FALSE(started());
148 EXPECT_EQ(kStateStringNotStarted
,
149 controller()->GetBackendInitializationStateString());
152 // Test that sync doesn't start until all conditions are met and a
153 // data type triggers sync startup.
154 TEST_F(StartupControllerTest
, DataTypeTriggered
) {
155 sync_prefs()->SetSyncSetupCompleted();
156 signin()->set_account(kTestUser
);
157 token_service()->IssueRefreshTokenForUser(kTestUser
, kTestToken
);
158 controller()->TryStart();
159 EXPECT_FALSE(started());
160 EXPECT_EQ(kStateStringDeferred
,
161 controller()->GetBackendInitializationStateString());
162 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS
);
163 EXPECT_TRUE(started());
164 EXPECT_EQ(kStateStringStarted
,
165 controller()->GetBackendInitializationStateString());
167 // The fallback timer shouldn't result in another invocation of the closure
168 // we passed to the StartupController.
170 base::RunLoop().RunUntilIdle();
171 EXPECT_FALSE(started());
174 // Test that the fallback timer starts sync in the event all
175 // conditions are met and no data type requests sync.
176 TEST_F(StartupControllerTest
, FallbackTimer
) {
177 sync_prefs()->SetSyncSetupCompleted();
178 signin()->set_account(kTestUser
);
179 token_service()->IssueRefreshTokenForUser(kTestUser
, kTestToken
);
180 controller()->TryStart();
181 EXPECT_FALSE(started());
182 base::RunLoop().RunUntilIdle();
183 EXPECT_TRUE(started());
186 // Test that we start immediately if sessions is disabled.
187 TEST_F(StartupControllerTest
, NoDeferralWithoutSessionsSync
) {
188 syncer::ModelTypeSet
types(syncer::UserTypes());
189 // Disabling sessions means disabling 4 types due to groupings.
190 types
.Remove(syncer::SESSIONS
);
191 types
.Remove(syncer::PROXY_TABS
);
192 types
.Remove(syncer::TYPED_URLS
);
193 types
.Remove(syncer::SUPERVISED_USER_SETTINGS
);
194 sync_prefs()->SetKeepEverythingSynced(false);
195 sync_prefs()->SetPreferredDataTypes(syncer::UserTypes(), types
);
196 controller()->Reset(syncer::UserTypes());
197 sync_prefs()->SetSyncSetupCompleted();
198 signin()->set_account(kTestUser
);
199 token_service()->IssueRefreshTokenForUser(kTestUser
, kTestToken
);
200 controller()->TryStart();
201 EXPECT_TRUE(started());
204 // Sanity check that the fallback timer doesn't fire before startup
205 // conditions are met.
206 TEST_F(StartupControllerTest
, FallbackTimerWaits
) {
207 controller()->TryStart();
208 EXPECT_FALSE(started());
209 base::RunLoop().RunUntilIdle();
210 EXPECT_FALSE(started());
213 // Test that sync starts when the user first asks to setup sync (which
214 // may be implicit due to the platform).
215 TEST_F(StartupControllerTest
, FirstSetup
) {
216 signin()->set_account(kTestUser
);
217 token_service()->IssueRefreshTokenForUser(kTestUser
, kTestToken
);
218 controller()->TryStart();
220 if (browser_defaults::kSyncAutoStarts
) {
221 EXPECT_TRUE(started());
223 controller()->set_setup_in_progress(true);
224 controller()->TryStart();
225 EXPECT_TRUE(started());
229 TEST_F(StartupControllerTest
, Reset
) {
230 sync_prefs()->SetSyncSetupCompleted();
231 signin()->set_account(kTestUser
);
232 token_service()->IssueRefreshTokenForUser(kTestUser
, kTestToken
);
233 controller()->TryStart();
234 const bool deferred_start
=
235 !base::CommandLine::ForCurrentProcess()->HasSwitch(
236 switches::kSyncDisableDeferredStartup
);
237 EXPECT_EQ(!deferred_start
, started());
238 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS
);
239 EXPECT_TRUE(started());
241 controller()->Reset(syncer::UserTypes());
242 EXPECT_FALSE(started());
243 controller()->TryStart();
244 // Restart is not deferred.
245 EXPECT_TRUE(started());
248 // Test that setup-in-progress tracking is persistent across a Reset.
249 TEST_F(StartupControllerTest
, ResetDuringSetup
) {
250 signin()->set_account(kTestUser
);
251 token_service()->IssueRefreshTokenForUser(kTestUser
, kTestToken
);
253 // Simulate UI telling us setup is in progress.
254 controller()->set_setup_in_progress(true);
256 // This could happen if the UI triggers a stop-syncing permanently call.
257 controller()->Reset(syncer::UserTypes());
259 // From the UI's point of view, setup is still in progress.
260 EXPECT_TRUE(controller()->setup_in_progress());
263 } // namespace browser_sync