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

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: 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
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 void TearDown() override {
46 controller_.reset();
Nicolas Zea 2016/04/04 20:44:17 is it necessary to have these here? These will aut
maxbogue 2016/04/04 22:56:55 Good catch! They must have been explicitly called
47 sync_prefs_.reset();
48 started_ = false;
49 }
50
51 bool CanStart() { return can_start_; }
52
53 void SetCanStart(bool can_start) { can_start_ = can_start; }
54
81 void FakeStartBackend() { 55 void FakeStartBackend() {
82 started_ = true; 56 started_ = true;
83 sync_prefs()->SetFirstSetupComplete(); 57 sync_prefs()->SetFirstSetupComplete();
84 } 58 }
85 59
86 void ExpectStarted() { 60 void ExpectStarted() {
87 EXPECT_TRUE(started()); 61 EXPECT_TRUE(started());
88 EXPECT_EQ(kStateStringStarted, 62 EXPECT_EQ(kStateStringStarted,
89 controller()->GetBackendInitializationStateString()); 63 controller()->GetBackendInitializationStateString());
90 } 64 }
91 65
92 void ExpectStartDeferred() { 66 void ExpectStartDeferred() {
93 const bool deferred_start = 67 const bool deferred_start =
94 !base::CommandLine::ForCurrentProcess()->HasSwitch( 68 !base::CommandLine::ForCurrentProcess()->HasSwitch(
95 switches::kSyncDisableDeferredStartup); 69 switches::kSyncDisableDeferredStartup);
96 EXPECT_EQ(!deferred_start, started()); 70 EXPECT_EQ(!deferred_start, started());
97 EXPECT_EQ(deferred_start ? kStateStringDeferred : kStateStringStarted, 71 EXPECT_EQ(deferred_start ? kStateStringDeferred : kStateStringStarted,
98 controller()->GetBackendInitializationStateString()); 72 controller()->GetBackendInitializationStateString());
99 } 73 }
100 74
101 void ExpectNotStarted() { 75 void ExpectNotStarted() {
102 EXPECT_FALSE(started()); 76 EXPECT_FALSE(started());
103 EXPECT_EQ(kStateStringNotStarted, 77 EXPECT_EQ(kStateStringNotStarted,
104 controller()->GetBackendInitializationStateString()); 78 controller()->GetBackendInitializationStateString());
105 } 79 }
106 80
107 bool started() const { return started_; } 81 bool started() const { return started_; }
108 void clear_started() { started_ = false; } 82 void clear_started() { started_ = false; }
109 StartupController* controller() { return controller_.get(); } 83 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(); } 84 sync_driver::SyncPrefs* sync_prefs() { return sync_prefs_.get(); }
115 85
116 private: 86 private:
87 bool can_start_;
117 bool started_; 88 bool started_;
118 base::MessageLoop message_loop_; 89 base::MessageLoop message_loop_;
119 syncable_prefs::TestingPrefServiceSyncable pref_service_; 90 syncable_prefs::TestingPrefServiceSyncable pref_service_;
120 scoped_ptr<StartupController> controller_; 91 scoped_ptr<StartupController> controller_;
121 scoped_ptr<FakeSigninManagerWrapper> signin_;
122 scoped_ptr<FakeProfileOAuth2TokenService> token_service_;
123 scoped_ptr<sync_driver::SyncPrefs> sync_prefs_; 92 scoped_ptr<sync_driver::SyncPrefs> sync_prefs_;
124 }; 93 };
125 94
126 // Test that sync doesn't start until all conditions are met. 95 // Test that sync doesn't start until all conditions are met.
127 TEST_F(StartupControllerTest, Basic) { 96 TEST_F(StartupControllerTest, Basic) {
128 controller()->TryStart(); 97 controller()->TryStart();
129 ExpectNotStarted(); 98 ExpectNotStarted();
130 99
100 SetCanStart(true);
101 controller()->TryStart();
102 ExpectStarted();
103 }
104
105 // Test that sync defers if first setup is complete.
106 TEST_F(StartupControllerTest, DefersAfterFirstSetupComplete) {
131 sync_prefs()->SetFirstSetupComplete(); 107 sync_prefs()->SetFirstSetupComplete();
132 controller()->TryStart(); 108 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(); 109 controller()->TryStart();
141 ExpectStartDeferred(); 110 ExpectStartDeferred();
142 } 111 }
143 112
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. 113 // Test that a data type triggering startup starts sync immediately.
166 TEST_F(StartupControllerTest, NoDeferralDataTypeTrigger) { 114 TEST_F(StartupControllerTest, NoDeferralDataTypeTrigger) {
167 sync_prefs()->SetFirstSetupComplete(); 115 sync_prefs()->SetFirstSetupComplete();
168 signin()->set_account_id(kTestUser); 116 SetCanStart(true);
169 token_service()->UpdateCredentials(kTestUser, kTestToken);
170 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS); 117 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS);
171 ExpectStarted(); 118 ExpectStarted();
172 } 119 }
173 120
174 // Test that a data type trigger interrupts the deferral timer and starts 121 // Test that a data type trigger interrupts the deferral timer and starts
175 // sync immediately. 122 // sync immediately.
176 TEST_F(StartupControllerTest, DataTypeTriggerInterruptsDeferral) { 123 TEST_F(StartupControllerTest, DataTypeTriggerInterruptsDeferral) {
177 sync_prefs()->SetFirstSetupComplete(); 124 sync_prefs()->SetFirstSetupComplete();
178 signin()->set_account_id(kTestUser); 125 SetCanStart(true);
179 token_service()->UpdateCredentials(kTestUser, kTestToken);
180 controller()->TryStart(); 126 controller()->TryStart();
181 ExpectStartDeferred(); 127 ExpectStartDeferred();
182 128
183 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS); 129 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS);
184 ExpectStarted(); 130 ExpectStarted();
185 131
186 // The fallback timer shouldn't result in another invocation of the closure 132 // The fallback timer shouldn't result in another invocation of the closure
187 // we passed to the StartupController. 133 // we passed to the StartupController.
188 clear_started(); 134 clear_started();
189 base::RunLoop().RunUntilIdle(); 135 base::RunLoop().RunUntilIdle();
190 EXPECT_FALSE(started()); 136 EXPECT_FALSE(started());
191 } 137 }
192 138
193 // Test that the fallback timer starts sync in the event all 139 // Test that the fallback timer starts sync in the event all
194 // conditions are met and no data type requests sync. 140 // conditions are met and no data type requests sync.
195 TEST_F(StartupControllerTest, FallbackTimer) { 141 TEST_F(StartupControllerTest, FallbackTimer) {
196 sync_prefs()->SetFirstSetupComplete(); 142 sync_prefs()->SetFirstSetupComplete();
197 signin()->set_account_id(kTestUser); 143 SetCanStart(true);
198 token_service()->UpdateCredentials(kTestUser, kTestToken);
199 controller()->TryStart(); 144 controller()->TryStart();
200 ExpectStartDeferred(); 145 ExpectStartDeferred();
201 146
202 base::RunLoop().RunUntilIdle(); 147 base::RunLoop().RunUntilIdle();
203 ExpectStarted(); 148 ExpectStarted();
204 } 149 }
205 150
206 // Test that we start immediately if sessions is disabled. 151 // Test that we start immediately if sessions is disabled.
207 TEST_F(StartupControllerTest, NoDeferralWithoutSessionsSync) { 152 TEST_F(StartupControllerTest, NoDeferralWithoutSessionsSync) {
208 syncer::ModelTypeSet types(syncer::UserTypes()); 153 syncer::ModelTypeSet types(syncer::UserTypes());
209 // Disabling sessions means disabling 4 types due to groupings. 154 // Disabling sessions means disabling 4 types due to groupings.
210 types.Remove(syncer::SESSIONS); 155 types.Remove(syncer::SESSIONS);
211 types.Remove(syncer::PROXY_TABS); 156 types.Remove(syncer::PROXY_TABS);
212 types.Remove(syncer::TYPED_URLS); 157 types.Remove(syncer::TYPED_URLS);
213 types.Remove(syncer::SUPERVISED_USER_SETTINGS); 158 types.Remove(syncer::SUPERVISED_USER_SETTINGS);
214 sync_prefs()->SetKeepEverythingSynced(false); 159 sync_prefs()->SetKeepEverythingSynced(false);
215 sync_prefs()->SetPreferredDataTypes(syncer::UserTypes(), types); 160 sync_prefs()->SetPreferredDataTypes(syncer::UserTypes(), types);
216 controller()->Reset(syncer::UserTypes()); 161 controller()->Reset(syncer::UserTypes());
162
217 sync_prefs()->SetFirstSetupComplete(); 163 sync_prefs()->SetFirstSetupComplete();
218 164 SetCanStart(true);
219 signin()->set_account_id(kTestUser);
220 token_service()->UpdateCredentials(kTestUser, kTestToken);
221 controller()->TryStart(); 165 controller()->TryStart();
222 ExpectStarted(); 166 ExpectStarted();
223 } 167 }
224 168
225 // Sanity check that the fallback timer doesn't fire before startup 169 // Sanity check that the fallback timer doesn't fire before startup
226 // conditions are met. 170 // conditions are met.
227 TEST_F(StartupControllerTest, FallbackTimerWaits) { 171 TEST_F(StartupControllerTest, FallbackTimerWaits) {
228 controller()->TryStart(); 172 controller()->TryStart();
229 ExpectNotStarted(); 173 ExpectNotStarted();
230 base::RunLoop().RunUntilIdle(); 174 base::RunLoop().RunUntilIdle();
231 ExpectNotStarted(); 175 ExpectNotStarted();
232 } 176 }
233 177
234 // Test that sync starts immediately when setup in progress is true. 178 // Test that sync starts immediately when setup in progress is true.
235 TEST_F(StartupControllerTest, NoDeferralSetupInProgressTrigger) { 179 TEST_F(StartupControllerTest, NoDeferralSetupInProgressTrigger) {
236 sync_prefs()->SetFirstSetupComplete(); 180 sync_prefs()->SetFirstSetupComplete();
237 signin()->set_account_id(kTestUser); 181 SetCanStart(true);
238 token_service()->UpdateCredentials(kTestUser, kTestToken);
239
240 controller()->SetSetupInProgress(true); 182 controller()->SetSetupInProgress(true);
241 ExpectStarted(); 183 ExpectStarted();
242 } 184 }
243 185
244 // Test that setup in progress being set to true interrupts the deferral timer 186 // Test that setup in progress being set to true interrupts the deferral timer
245 // and starts sync immediately. 187 // and starts sync immediately.
246 TEST_F(StartupControllerTest, SetupInProgressTriggerInterruptsDeferral) { 188 TEST_F(StartupControllerTest, SetupInProgressTriggerInterruptsDeferral) {
247 sync_prefs()->SetFirstSetupComplete(); 189 sync_prefs()->SetFirstSetupComplete();
248 signin()->set_account_id(kTestUser); 190 SetCanStart(true);
249 token_service()->UpdateCredentials(kTestUser, kTestToken);
250 controller()->TryStart(); 191 controller()->TryStart();
251 ExpectStartDeferred(); 192 ExpectStartDeferred();
252 193
253 controller()->SetSetupInProgress(true); 194 controller()->SetSetupInProgress(true);
254 ExpectStarted(); 195 ExpectStarted();
255 } 196 }
256 197
257 // Test that start isn't deferred on the first start but is on restarts. 198 // Test that start isn't deferred on the first start but is on restarts.
258 TEST_F(StartupControllerTest, DeferralOnRestart) { 199 TEST_F(StartupControllerTest, DeferralOnRestart) {
259 signin()->set_account_id(kTestUser); 200 SetCanStart(true);
260 token_service()->UpdateCredentials(kTestUser, kTestToken);
261 controller()->TryStart(); 201 controller()->TryStart();
262 ExpectStarted(); 202 ExpectStarted();
263 203
264 clear_started(); 204 clear_started();
265 controller()->Reset(syncer::UserTypes()); 205 controller()->Reset(syncer::UserTypes());
266 ExpectNotStarted(); 206 ExpectNotStarted();
267 controller()->TryStart(); 207 controller()->TryStart();
268 ExpectStartDeferred(); 208 ExpectStartDeferred();
269 } 209 }
270 210
271 // Test that setup-in-progress tracking is persistent across a Reset. 211 // Test that setup-in-progress tracking is persistent across a Reset.
272 TEST_F(StartupControllerTest, ResetDuringSetup) { 212 TEST_F(StartupControllerTest, ResetDuringSetup) {
273 signin()->set_account_id(kTestUser); 213 SetCanStart(true);
274 token_service()->UpdateCredentials(kTestUser, kTestToken);
275 214
276 // Simulate UI telling us setup is in progress. 215 // Simulate UI telling us setup is in progress.
277 controller()->SetSetupInProgress(true); 216 controller()->SetSetupInProgress(true);
278 217
279 // This could happen if the UI triggers a stop-syncing permanently call. 218 // This could happen if the UI triggers a stop-syncing permanently call.
280 controller()->Reset(syncer::UserTypes()); 219 controller()->Reset(syncer::UserTypes());
281 220
282 // From the UI's point of view, setup is still in progress. 221 // From the UI's point of view, setup is still in progress.
283 EXPECT_TRUE(controller()->IsSetupInProgress()); 222 EXPECT_TRUE(controller()->IsSetupInProgress());
284 } 223 }
285 224
286 } // namespace browser_sync 225 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698