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

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

Issue 162443004: sync: final pieces to sync deferred initialization (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase again Created 6 years, 9 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 | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/sync/startup_controller.h"
6
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/managed_mode/managed_user_signin_manager_wrapper.h"
13 #include "chrome/browser/signin/fake_profile_oauth2_token_service.h"
14 #include "chrome/browser/signin/fake_profile_oauth2_token_service_wrapper.h"
15 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
16 #include "chrome/browser/sync/sync_prefs.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20
21 namespace browser_sync {
22
23 static const char kTestUser[] = "test@gmail.com";
24 static const char kTestToken[] = "testToken";
25
26 // These are coupled to the implementation of StartupController's
27 // GetBackendInitializationStateString which is used by about:sync. We use it
28 // as a convenient way to verify internal state and that the class is
29 // outputting the correct values for the debug string.
30 static const char kStateStringStarted[] = "Started";
31 static const char kStateStringDeferred[] = "Deferred";
32 static const char kStateStringNotStarted[] = "Not started";
33
34 class FakeManagedUserSigninManagerWrapper
35 : public ManagedUserSigninManagerWrapper {
36 public:
37 FakeManagedUserSigninManagerWrapper()
38 : ManagedUserSigninManagerWrapper(NULL) {}
39 virtual std::string GetEffectiveUsername() const OVERRIDE {
40 return account_;
41 }
42
43 virtual std::string GetAccountIdToUse() const OVERRIDE {
44 return account_;
45 }
46
47 void set_account(const std::string& account) { account_ = account; }
48
49 private:
50 std::string account_;
51 };
52
53 class StartupControllerTest : public testing::Test {
54 public:
55 StartupControllerTest() : started_(false) {}
56
57 virtual void SetUp() OVERRIDE {
58 profile_.reset(new TestingProfile());
59 sync_prefs_.reset(new SyncPrefs(profile_->GetPrefs()));
60 token_service_.reset(
61 static_cast<FakeProfileOAuth2TokenServiceWrapper*>(
62 FakeProfileOAuth2TokenServiceWrapper::Build(profile_.get())));
63 signin_.reset(new FakeManagedUserSigninManagerWrapper());
64
65 ProfileSyncServiceStartBehavior behavior =
66 browser_defaults::kSyncAutoStarts ? AUTO_START : MANUAL_START;
67 base::Closure fake_start_backend = base::Bind(
68 &StartupControllerTest::FakeStartBackend, base::Unretained(this));
69 controller_.reset(new StartupController(behavior, token_service(),
70 sync_prefs_.get(), signin_.get(),
71 fake_start_backend));
72 controller_->OverrideFallbackTimeoutForTest(
73 base::TimeDelta::FromSeconds(0));
74 }
75
76 virtual void TearDown() OVERRIDE {
77 controller_.reset();
78 signin_.reset();
79 token_service_.reset();
80 sync_prefs_.reset();
81 started_ = false;
82 }
83
84 void FakeStartBackend() {
85 started_ = true;
86 }
87
88 void ForceDeferredStartup() {
89 if (!CommandLine::ForCurrentProcess()->
90 HasSwitch(switches::kSyncEnableDeferredStartup)) {
91 CommandLine::ForCurrentProcess()->
92 AppendSwitch(switches::kSyncEnableDeferredStartup);
93 }
94 }
95
96 bool started() const { return started_; }
97 void clear_started() { started_ = false; }
98 StartupController* controller() { return controller_.get(); }
99 FakeManagedUserSigninManagerWrapper* signin() { return signin_.get(); }
100 FakeProfileOAuth2TokenService* token_service() {
101 return static_cast<FakeProfileOAuth2TokenService*>(
102 token_service_->GetProfileOAuth2TokenService());
103 }
104 SyncPrefs* sync_prefs() { return sync_prefs_.get(); }
105 Profile* profile() { return profile_.get(); }
106
107 private:
108 bool started_;
109 base::MessageLoop message_loop_;
110 scoped_ptr<StartupController> controller_;
111 scoped_ptr<FakeManagedUserSigninManagerWrapper> signin_;
112 scoped_ptr<FakeProfileOAuth2TokenServiceWrapper> token_service_;
113 scoped_ptr<SyncPrefs> sync_prefs_;
114 scoped_ptr<TestingProfile> profile_;
115 };
116
117 // Test that sync doesn't start until all conditions are met.
118 TEST_F(StartupControllerTest, Basic) {
119 controller()->TryStart();
120 EXPECT_FALSE(started());
121 sync_prefs()->SetSyncSetupCompleted();
122 controller()->TryStart();
123 EXPECT_FALSE(started());
124 signin()->set_account(kTestUser);
125 controller()->TryStart();
126 EXPECT_FALSE(started());
127 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken);
128 const bool deferred_start = CommandLine::ForCurrentProcess()->
129 HasSwitch(switches::kSyncEnableDeferredStartup);
130 controller()->TryStart();
131 EXPECT_EQ(!deferred_start, started());
132 std::string state(controller()->GetBackendInitializationStateString());
133 EXPECT_TRUE(deferred_start ? state == kStateStringDeferred :
134 state == kStateStringStarted);
135 }
136
137 // Test that sync doesn't when suppressed even if all other conditons are met.
138 TEST_F(StartupControllerTest, Suppressed) {
139 sync_prefs()->SetSyncSetupCompleted();
140 sync_prefs()->SetStartSuppressed(true);
141 signin()->set_account(kTestUser);
142 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken);
143 controller()->TryStart();
144 EXPECT_FALSE(started());
145 EXPECT_EQ(kStateStringNotStarted,
146 controller()->GetBackendInitializationStateString());
147 }
148
149 // Test that sync doesn't when managed even if all other conditons are met.
150 TEST_F(StartupControllerTest, Managed) {
151 sync_prefs()->SetSyncSetupCompleted();
152 sync_prefs()->SetManagedForTest(true);
153 signin()->set_account(kTestUser);
154 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken);
155 controller()->TryStart();
156 EXPECT_FALSE(started());
157 EXPECT_EQ(kStateStringNotStarted,
158 controller()->GetBackendInitializationStateString());
159 }
160
161 // Test that sync doesn't start until all conditions are met and a
162 // data type triggers sync startup.
163 TEST_F(StartupControllerTest, DataTypeTriggered) {
164 ForceDeferredStartup();
165 sync_prefs()->SetSyncSetupCompleted();
166 signin()->set_account(kTestUser);
167 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken);
168 controller()->TryStart();
169 EXPECT_FALSE(started());
170 EXPECT_EQ(kStateStringDeferred,
171 controller()->GetBackendInitializationStateString());
172 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS);
173 EXPECT_TRUE(started());
174 EXPECT_EQ(kStateStringStarted,
175 controller()->GetBackendInitializationStateString());
176
177 // The fallback timer shouldn't result in another invocation of the closure
178 // we passed to the StartupController.
179 clear_started();
180 base::RunLoop().RunUntilIdle();
181 EXPECT_FALSE(started());
182 }
183
184 // Test that the fallback timer starts sync in the event all
185 // conditions are met and no data type requests sync.
186 TEST_F(StartupControllerTest, FallbackTimer) {
187 ForceDeferredStartup();
188 sync_prefs()->SetSyncSetupCompleted();
189 signin()->set_account(kTestUser);
190 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken);
191 controller()->TryStart();
192 EXPECT_FALSE(started());
193 base::RunLoop().RunUntilIdle();
194 EXPECT_TRUE(started());
195 }
196
197 // Sanity check that the fallback timer doesn't fire before startup
198 // conditions are met.
199 TEST_F(StartupControllerTest, FallbackTimerWaits) {
200 ForceDeferredStartup();
201 controller()->TryStart();
202 EXPECT_FALSE(started());
203 base::RunLoop().RunUntilIdle();
204 EXPECT_FALSE(started());
205 }
206
207 // Test that sync starts when the user first asks to setup sync (which
208 // may be implicit due to the platform).
209 TEST_F(StartupControllerTest, FirstSetup) {
210 signin()->set_account(kTestUser);
211 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken);
212 controller()->TryStart();
213
214 if (browser_defaults::kSyncAutoStarts) {
215 EXPECT_TRUE(started());
216 } else {
217 controller()->set_setup_in_progress(true);
218 controller()->TryStart();
219 EXPECT_TRUE(started());
220 }
221 }
222
223 // Test that the controller "forgets" that preconditions were met on reset.
224 TEST_F(StartupControllerTest, Reset) {
225 sync_prefs()->SetSyncSetupCompleted();
226 signin()->set_account(kTestUser);
227 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken);
228 controller()->TryStart();
229 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS);
230 EXPECT_TRUE(started());
231 clear_started();
232 controller()->Reset();
233 base::RunLoop().RunUntilIdle();
234 EXPECT_FALSE(started());
235 const bool deferred_start = CommandLine::ForCurrentProcess()->
236 HasSwitch(switches::kSyncEnableDeferredStartup);
237 controller()->TryStart();
238 EXPECT_EQ(!deferred_start, started());
239 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS);
240 EXPECT_TRUE(started());
241 }
242
243 // Test that setup-in-progress tracking is reset properly on Reset.
244 // This scenario doesn't affect auto-start platforms.
245 TEST_F(StartupControllerTest, ResetDuringSetup) {
246 signin()->set_account(kTestUser);
247 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken);
248 controller()->set_setup_in_progress(true);
249 controller()->Reset();
250 controller()->TryStart();
251
252 if (!browser_defaults::kSyncAutoStarts) {
253 EXPECT_FALSE(started());
254 EXPECT_EQ(kStateStringNotStarted,
255 controller()->GetBackendInitializationStateString());
256 }
257 }
258
259 } // namespace browser_sync
OLDNEW
« no previous file with comments | « chrome/browser/sync/startup_controller.cc ('k') | chrome/browser/sync/test_profile_sync_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698