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

Side by Side Diff: components/sync_driver/startup_controller_unittest.cc

Issue 1858673002: [Sync] Inject startup dependencies into StartupController. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments. Created 4 years, 8 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
« no previous file with comments | « components/sync_driver/startup_controller.cc ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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 "components/sync_driver/startup_controller.h" 5 #include "components/sync_driver/startup_controller.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h" 11 #include "base/run_loop.h"
12 #include "base/time/time.h" 12 #include "base/time/time.h"
13 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
14 #include "components/sync_driver/signin_manager_wrapper.h"
15 #include "components/sync_driver/sync_driver_switches.h" 13 #include "components/sync_driver/sync_driver_switches.h"
16 #include "components/sync_driver/sync_prefs.h" 14 #include "components/sync_driver/sync_prefs.h"
17 #include "components/syncable_prefs/testing_pref_service_syncable.h" 15 #include "components/syncable_prefs/testing_pref_service_syncable.h"
18 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
19 17
20 namespace browser_sync { 18 namespace browser_sync {
21 19
22 static const char kTestUser[] = "test@gmail.com";
23 static const char kTestToken[] = "testToken";
24
25 // These are coupled to the implementation of StartupController's 20 // These are coupled to the implementation of StartupController's
26 // GetBackendInitializationStateString which is used by about:sync. We use it 21 // GetBackendInitializationStateString which is used by about:sync. We use it
27 // as a convenient way to verify internal state and that the class is 22 // as a convenient way to verify internal state and that the class is
28 // outputting the correct values for the debug string. 23 // outputting the correct values for the debug string.
29 static const char kStateStringStarted[] = "Started"; 24 static const char kStateStringStarted[] = "Started";
30 static const char kStateStringDeferred[] = "Deferred"; 25 static const char kStateStringDeferred[] = "Deferred";
31 static const char kStateStringNotStarted[] = "Not started"; 26 static const char kStateStringNotStarted[] = "Not started";
32 27
33 class FakeSigninManagerWrapper : public SigninManagerWrapper {
34 public:
35 FakeSigninManagerWrapper() : SigninManagerWrapper(NULL) {}
36 std::string GetEffectiveUsername() const override { return std::string(); }
37
38 std::string GetAccountIdToUse() const override { return account_id_; }
39
40 void set_account_id(const std::string& account_id) {
41 account_id_ = account_id;
42 }
43
44 private:
45 std::string account_id_;
46 };
47
48 class StartupControllerTest : public testing::Test { 28 class StartupControllerTest : public testing::Test {
49 public: 29 public:
50 StartupControllerTest() : started_(false) {} 30 StartupControllerTest() : can_start_(false), started_(false) {}
51 31
52 void SetUp() override { 32 void SetUp() override {
53 sync_driver::SyncPrefs::RegisterProfilePrefs(pref_service_.registry()); 33 sync_driver::SyncPrefs::RegisterProfilePrefs(pref_service_.registry());
54 sync_prefs_.reset(new sync_driver::SyncPrefs(&pref_service_)); 34 sync_prefs_.reset(new sync_driver::SyncPrefs(&pref_service_));
55 token_service_.reset(new FakeProfileOAuth2TokenService()); 35 controller_.reset(new StartupController(
56 signin_.reset(new FakeSigninManagerWrapper()); 36 sync_prefs_.get(),
57 37 base::Bind(&StartupControllerTest::CanStart, base::Unretained(this)),
58 SetUpController(); 38 base::Bind(&StartupControllerTest::FakeStartBackend,
59 } 39 base::Unretained(this))));
60
61 void TearDown() override {
62 controller_.reset();
63 signin_.reset();
64 token_service_->Shutdown();
65 token_service_.reset();
66 sync_prefs_.reset();
67 started_ = false;
68 }
69
70 void SetUpController() {
71 started_ = false;
72 base::Closure fake_start_backend = base::Bind(
73 &StartupControllerTest::FakeStartBackend, base::Unretained(this));
74 controller_.reset(new StartupController(token_service(), sync_prefs_.get(),
75 signin_.get(), fake_start_backend));
76 controller_->Reset(syncer::UserTypes()); 40 controller_->Reset(syncer::UserTypes());
77 controller_->OverrideFallbackTimeoutForTest( 41 controller_->OverrideFallbackTimeoutForTest(
78 base::TimeDelta::FromSeconds(0)); 42 base::TimeDelta::FromSeconds(0));
79 } 43 }
80 44
45 bool CanStart() { return can_start_; }
46
47 void SetCanStart(bool can_start) { can_start_ = can_start; }
48
81 void FakeStartBackend() { 49 void FakeStartBackend() {
82 started_ = true; 50 started_ = true;
83 sync_prefs()->SetFirstSetupComplete(); 51 sync_prefs()->SetFirstSetupComplete();
84 } 52 }
85 53
86 void ExpectStarted() { 54 void ExpectStarted() {
87 EXPECT_TRUE(started()); 55 EXPECT_TRUE(started());
88 EXPECT_EQ(kStateStringStarted, 56 EXPECT_EQ(kStateStringStarted,
89 controller()->GetBackendInitializationStateString()); 57 controller()->GetBackendInitializationStateString());
90 } 58 }
91 59
92 void ExpectStartDeferred() { 60 void ExpectStartDeferred() {
93 const bool deferred_start = 61 const bool deferred_start =
94 !base::CommandLine::ForCurrentProcess()->HasSwitch( 62 !base::CommandLine::ForCurrentProcess()->HasSwitch(
95 switches::kSyncDisableDeferredStartup); 63 switches::kSyncDisableDeferredStartup);
96 EXPECT_EQ(!deferred_start, started()); 64 EXPECT_EQ(!deferred_start, started());
97 EXPECT_EQ(deferred_start ? kStateStringDeferred : kStateStringStarted, 65 EXPECT_EQ(deferred_start ? kStateStringDeferred : kStateStringStarted,
98 controller()->GetBackendInitializationStateString()); 66 controller()->GetBackendInitializationStateString());
99 } 67 }
100 68
101 void ExpectNotStarted() { 69 void ExpectNotStarted() {
102 EXPECT_FALSE(started()); 70 EXPECT_FALSE(started());
103 EXPECT_EQ(kStateStringNotStarted, 71 EXPECT_EQ(kStateStringNotStarted,
104 controller()->GetBackendInitializationStateString()); 72 controller()->GetBackendInitializationStateString());
105 } 73 }
106 74
107 bool started() const { return started_; } 75 bool started() const { return started_; }
108 void clear_started() { started_ = false; } 76 void clear_started() { started_ = false; }
109 StartupController* controller() { return controller_.get(); } 77 StartupController* controller() { return controller_.get(); }
110 FakeSigninManagerWrapper* signin() { return signin_.get(); }
111 FakeProfileOAuth2TokenService* token_service() {
112 return token_service_.get();
113 }
114 sync_driver::SyncPrefs* sync_prefs() { return sync_prefs_.get(); } 78 sync_driver::SyncPrefs* sync_prefs() { return sync_prefs_.get(); }
115 79
116 private: 80 private:
81 bool can_start_;
117 bool started_; 82 bool started_;
118 base::MessageLoop message_loop_; 83 base::MessageLoop message_loop_;
119 syncable_prefs::TestingPrefServiceSyncable pref_service_; 84 syncable_prefs::TestingPrefServiceSyncable pref_service_;
85 scoped_ptr<sync_driver::SyncPrefs> sync_prefs_;
120 scoped_ptr<StartupController> controller_; 86 scoped_ptr<StartupController> controller_;
121 scoped_ptr<FakeSigninManagerWrapper> signin_;
122 scoped_ptr<FakeProfileOAuth2TokenService> token_service_;
123 scoped_ptr<sync_driver::SyncPrefs> sync_prefs_;
124 }; 87 };
125 88
126 // Test that sync doesn't start until all conditions are met. 89 // Test that sync doesn't start until all conditions are met.
127 TEST_F(StartupControllerTest, Basic) { 90 TEST_F(StartupControllerTest, Basic) {
128 controller()->TryStart(); 91 controller()->TryStart();
129 ExpectNotStarted(); 92 ExpectNotStarted();
130 93
94 SetCanStart(true);
95 controller()->TryStart();
96 ExpectStarted();
97 }
98
99 // Test that sync defers if first setup is complete.
100 TEST_F(StartupControllerTest, DefersAfterFirstSetupComplete) {
131 sync_prefs()->SetFirstSetupComplete(); 101 sync_prefs()->SetFirstSetupComplete();
132 controller()->TryStart(); 102 SetCanStart(true);
133 ExpectNotStarted();
134
135 signin()->set_account_id(kTestUser);
136 controller()->TryStart();
137 ExpectNotStarted();
138
139 token_service()->UpdateCredentials(kTestUser, kTestToken);
140 controller()->TryStart(); 103 controller()->TryStart();
141 ExpectStartDeferred(); 104 ExpectStartDeferred();
142 } 105 }
143 106
144 // Test that sync doesn't start when not requested even if all other
145 // conditons are met.
146 TEST_F(StartupControllerTest, NotRequested) {
147 sync_prefs()->SetFirstSetupComplete();
148 sync_prefs()->SetSyncRequested(false);
149 signin()->set_account_id(kTestUser);
150 token_service()->UpdateCredentials(kTestUser, kTestToken);
151 controller()->TryStart();
152 ExpectNotStarted();
153 }
154
155 // Test that sync doesn't when managed even if all other conditons are met.
156 TEST_F(StartupControllerTest, Managed) {
157 sync_prefs()->SetFirstSetupComplete();
158 sync_prefs()->SetManagedForTest(true);
159 signin()->set_account_id(kTestUser);
160 token_service()->UpdateCredentials(kTestUser, kTestToken);
161 controller()->TryStart();
162 ExpectNotStarted();
163 }
164
165 // Test that a data type triggering startup starts sync immediately. 107 // Test that a data type triggering startup starts sync immediately.
166 TEST_F(StartupControllerTest, NoDeferralDataTypeTrigger) { 108 TEST_F(StartupControllerTest, NoDeferralDataTypeTrigger) {
167 sync_prefs()->SetFirstSetupComplete(); 109 sync_prefs()->SetFirstSetupComplete();
168 signin()->set_account_id(kTestUser); 110 SetCanStart(true);
169 token_service()->UpdateCredentials(kTestUser, kTestToken);
170 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS); 111 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS);
171 ExpectStarted(); 112 ExpectStarted();
172 } 113 }
173 114
174 // Test that a data type trigger interrupts the deferral timer and starts 115 // Test that a data type trigger interrupts the deferral timer and starts
175 // sync immediately. 116 // sync immediately.
176 TEST_F(StartupControllerTest, DataTypeTriggerInterruptsDeferral) { 117 TEST_F(StartupControllerTest, DataTypeTriggerInterruptsDeferral) {
177 sync_prefs()->SetFirstSetupComplete(); 118 sync_prefs()->SetFirstSetupComplete();
178 signin()->set_account_id(kTestUser); 119 SetCanStart(true);
179 token_service()->UpdateCredentials(kTestUser, kTestToken);
180 controller()->TryStart(); 120 controller()->TryStart();
181 ExpectStartDeferred(); 121 ExpectStartDeferred();
182 122
183 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS); 123 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS);
184 ExpectStarted(); 124 ExpectStarted();
185 125
186 // The fallback timer shouldn't result in another invocation of the closure 126 // The fallback timer shouldn't result in another invocation of the closure
187 // we passed to the StartupController. 127 // we passed to the StartupController.
188 clear_started(); 128 clear_started();
189 base::RunLoop().RunUntilIdle(); 129 base::RunLoop().RunUntilIdle();
190 EXPECT_FALSE(started()); 130 EXPECT_FALSE(started());
191 } 131 }
192 132
193 // Test that the fallback timer starts sync in the event all 133 // Test that the fallback timer starts sync in the event all
194 // conditions are met and no data type requests sync. 134 // conditions are met and no data type requests sync.
195 TEST_F(StartupControllerTest, FallbackTimer) { 135 TEST_F(StartupControllerTest, FallbackTimer) {
196 sync_prefs()->SetFirstSetupComplete(); 136 sync_prefs()->SetFirstSetupComplete();
197 signin()->set_account_id(kTestUser); 137 SetCanStart(true);
198 token_service()->UpdateCredentials(kTestUser, kTestToken);
199 controller()->TryStart(); 138 controller()->TryStart();
200 ExpectStartDeferred(); 139 ExpectStartDeferred();
201 140
202 base::RunLoop().RunUntilIdle(); 141 base::RunLoop().RunUntilIdle();
203 ExpectStarted(); 142 ExpectStarted();
204 } 143 }
205 144
206 // Test that we start immediately if sessions is disabled. 145 // Test that we start immediately if sessions is disabled.
207 TEST_F(StartupControllerTest, NoDeferralWithoutSessionsSync) { 146 TEST_F(StartupControllerTest, NoDeferralWithoutSessionsSync) {
208 syncer::ModelTypeSet types(syncer::UserTypes()); 147 syncer::ModelTypeSet types(syncer::UserTypes());
209 // Disabling sessions means disabling 4 types due to groupings. 148 // Disabling sessions means disabling 4 types due to groupings.
210 types.Remove(syncer::SESSIONS); 149 types.Remove(syncer::SESSIONS);
211 types.Remove(syncer::PROXY_TABS); 150 types.Remove(syncer::PROXY_TABS);
212 types.Remove(syncer::TYPED_URLS); 151 types.Remove(syncer::TYPED_URLS);
213 types.Remove(syncer::SUPERVISED_USER_SETTINGS); 152 types.Remove(syncer::SUPERVISED_USER_SETTINGS);
214 sync_prefs()->SetKeepEverythingSynced(false); 153 sync_prefs()->SetKeepEverythingSynced(false);
215 sync_prefs()->SetPreferredDataTypes(syncer::UserTypes(), types); 154 sync_prefs()->SetPreferredDataTypes(syncer::UserTypes(), types);
216 controller()->Reset(syncer::UserTypes()); 155 controller()->Reset(syncer::UserTypes());
156
217 sync_prefs()->SetFirstSetupComplete(); 157 sync_prefs()->SetFirstSetupComplete();
218 158 SetCanStart(true);
219 signin()->set_account_id(kTestUser);
220 token_service()->UpdateCredentials(kTestUser, kTestToken);
221 controller()->TryStart(); 159 controller()->TryStart();
222 ExpectStarted(); 160 ExpectStarted();
223 } 161 }
224 162
225 // Sanity check that the fallback timer doesn't fire before startup 163 // Sanity check that the fallback timer doesn't fire before startup
226 // conditions are met. 164 // conditions are met.
227 TEST_F(StartupControllerTest, FallbackTimerWaits) { 165 TEST_F(StartupControllerTest, FallbackTimerWaits) {
228 controller()->TryStart(); 166 controller()->TryStart();
229 ExpectNotStarted(); 167 ExpectNotStarted();
230 base::RunLoop().RunUntilIdle(); 168 base::RunLoop().RunUntilIdle();
231 ExpectNotStarted(); 169 ExpectNotStarted();
232 } 170 }
233 171
234 // Test that sync starts immediately when setup in progress is true. 172 // Test that sync starts immediately when setup in progress is true.
235 TEST_F(StartupControllerTest, NoDeferralSetupInProgressTrigger) { 173 TEST_F(StartupControllerTest, NoDeferralSetupInProgressTrigger) {
236 sync_prefs()->SetFirstSetupComplete(); 174 sync_prefs()->SetFirstSetupComplete();
237 signin()->set_account_id(kTestUser); 175 SetCanStart(true);
238 token_service()->UpdateCredentials(kTestUser, kTestToken);
239
240 controller()->SetSetupInProgress(true); 176 controller()->SetSetupInProgress(true);
241 ExpectStarted(); 177 ExpectStarted();
242 } 178 }
243 179
244 // Test that setup in progress being set to true interrupts the deferral timer 180 // Test that setup in progress being set to true interrupts the deferral timer
245 // and starts sync immediately. 181 // and starts sync immediately.
246 TEST_F(StartupControllerTest, SetupInProgressTriggerInterruptsDeferral) { 182 TEST_F(StartupControllerTest, SetupInProgressTriggerInterruptsDeferral) {
247 sync_prefs()->SetFirstSetupComplete(); 183 sync_prefs()->SetFirstSetupComplete();
248 signin()->set_account_id(kTestUser); 184 SetCanStart(true);
249 token_service()->UpdateCredentials(kTestUser, kTestToken);
250 controller()->TryStart(); 185 controller()->TryStart();
251 ExpectStartDeferred(); 186 ExpectStartDeferred();
252 187
253 controller()->SetSetupInProgress(true); 188 controller()->SetSetupInProgress(true);
254 ExpectStarted(); 189 ExpectStarted();
255 } 190 }
256 191
257 // Test that start isn't deferred on the first start but is on restarts. 192 // Test that start isn't deferred on the first start but is on restarts.
258 TEST_F(StartupControllerTest, DeferralOnRestart) { 193 TEST_F(StartupControllerTest, DeferralOnRestart) {
259 signin()->set_account_id(kTestUser); 194 SetCanStart(true);
260 token_service()->UpdateCredentials(kTestUser, kTestToken);
261 controller()->TryStart(); 195 controller()->TryStart();
262 ExpectStarted(); 196 ExpectStarted();
263 197
264 clear_started(); 198 clear_started();
265 controller()->Reset(syncer::UserTypes()); 199 controller()->Reset(syncer::UserTypes());
266 ExpectNotStarted(); 200 ExpectNotStarted();
267 controller()->TryStart(); 201 controller()->TryStart();
268 ExpectStartDeferred(); 202 ExpectStartDeferred();
269 } 203 }
270 204
271 // Test that setup-in-progress tracking is persistent across a Reset. 205 // Test that setup-in-progress tracking is persistent across a Reset.
272 TEST_F(StartupControllerTest, ResetDuringSetup) { 206 TEST_F(StartupControllerTest, ResetDuringSetup) {
273 signin()->set_account_id(kTestUser); 207 SetCanStart(true);
274 token_service()->UpdateCredentials(kTestUser, kTestToken);
275 208
276 // Simulate UI telling us setup is in progress. 209 // Simulate UI telling us setup is in progress.
277 controller()->SetSetupInProgress(true); 210 controller()->SetSetupInProgress(true);
278 211
279 // This could happen if the UI triggers a stop-syncing permanently call. 212 // This could happen if the UI triggers a stop-syncing permanently call.
280 controller()->Reset(syncer::UserTypes()); 213 controller()->Reset(syncer::UserTypes());
281 214
282 // From the UI's point of view, setup is still in progress. 215 // From the UI's point of view, setup is still in progress.
283 EXPECT_TRUE(controller()->IsSetupInProgress()); 216 EXPECT_TRUE(controller()->IsSetupInProgress());
284 } 217 }
285 218
286 } // namespace browser_sync 219 } // namespace browser_sync
OLDNEW
« no previous file with comments | « components/sync_driver/startup_controller.cc ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698