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

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

Issue 1575153004: [Sync] Simplify sync startup behavior. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@setup
Patch Set: Address more comments. Created 4 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
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"
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 class StartupControllerTest : public testing::Test { 48 class StartupControllerTest : public testing::Test {
49 public: 49 public:
50 StartupControllerTest() : started_(false) {} 50 StartupControllerTest() : started_(false) {}
51 51
52 void SetUp() override { 52 void SetUp() override {
53 sync_driver::SyncPrefs::RegisterProfilePrefs(pref_service_.registry()); 53 sync_driver::SyncPrefs::RegisterProfilePrefs(pref_service_.registry());
54 sync_prefs_.reset(new sync_driver::SyncPrefs(&pref_service_)); 54 sync_prefs_.reset(new sync_driver::SyncPrefs(&pref_service_));
55 token_service_.reset(new FakeProfileOAuth2TokenService()); 55 token_service_.reset(new FakeProfileOAuth2TokenService());
56 signin_.reset(new FakeSigninManagerWrapper()); 56 signin_.reset(new FakeSigninManagerWrapper());
57 57
58 SetUpController(AUTO_START); 58 SetUpController();
59 } 59 }
60 60
61 void TearDown() override { 61 void TearDown() override {
62 controller_.reset(); 62 controller_.reset();
63 signin_.reset(); 63 signin_.reset();
64 token_service_->Shutdown(); 64 token_service_->Shutdown();
65 token_service_.reset(); 65 token_service_.reset();
66 sync_prefs_.reset(); 66 sync_prefs_.reset();
67 started_ = false; 67 started_ = false;
68 } 68 }
69 69
70 void SetUpController(ProfileSyncServiceStartBehavior start_behavior) { 70 void SetUpController() {
71 started_ = false; 71 started_ = false;
72 base::Closure fake_start_backend = base::Bind( 72 base::Closure fake_start_backend = base::Bind(
73 &StartupControllerTest::FakeStartBackend, base::Unretained(this)); 73 &StartupControllerTest::FakeStartBackend, base::Unretained(this));
74 controller_.reset(new StartupController(start_behavior, token_service(), 74 controller_.reset(new StartupController(token_service(), sync_prefs_.get(),
75 sync_prefs_.get(), signin_.get(), 75 signin_.get(), fake_start_backend));
76 fake_start_backend));
77 controller_->Reset(syncer::UserTypes()); 76 controller_->Reset(syncer::UserTypes());
78 controller_->OverrideFallbackTimeoutForTest( 77 controller_->OverrideFallbackTimeoutForTest(
79 base::TimeDelta::FromSeconds(0)); 78 base::TimeDelta::FromSeconds(0));
80 } 79 }
81 80
82 void FakeStartBackend() { 81 void FakeStartBackend() {
83 started_ = true; 82 started_ = true;
84 } 83 }
85 84
85 void ExpectStarted() {
86 EXPECT_TRUE(started());
87 EXPECT_EQ(kStateStringStarted,
88 controller()->GetBackendInitializationStateString());
89 }
90
91 void ExpectStartDeferred() {
92 const bool deferred_start =
93 !base::CommandLine::ForCurrentProcess()->HasSwitch(
94 switches::kSyncDisableDeferredStartup);
95 EXPECT_EQ(!deferred_start, started());
96 EXPECT_EQ(deferred_start ? kStateStringDeferred : kStateStringStarted,
97 controller()->GetBackendInitializationStateString());
98 }
99
100 void ExpectNotStarted() {
101 EXPECT_FALSE(started());
102 EXPECT_EQ(kStateStringNotStarted,
103 controller()->GetBackendInitializationStateString());
104 }
105
86 bool started() const { return started_; } 106 bool started() const { return started_; }
87 void clear_started() { started_ = false; } 107 void clear_started() { started_ = false; }
88 StartupController* controller() { return controller_.get(); } 108 StartupController* controller() { return controller_.get(); }
89 FakeSigninManagerWrapper* signin() { return signin_.get(); } 109 FakeSigninManagerWrapper* signin() { return signin_.get(); }
90 FakeProfileOAuth2TokenService* token_service() { 110 FakeProfileOAuth2TokenService* token_service() {
91 return token_service_.get(); 111 return token_service_.get();
92 } 112 }
93 sync_driver::SyncPrefs* sync_prefs() { return sync_prefs_.get(); } 113 sync_driver::SyncPrefs* sync_prefs() { return sync_prefs_.get(); }
94 114
95 private: 115 private:
96 bool started_; 116 bool started_;
97 base::MessageLoop message_loop_; 117 base::MessageLoop message_loop_;
98 syncable_prefs::TestingPrefServiceSyncable pref_service_; 118 syncable_prefs::TestingPrefServiceSyncable pref_service_;
99 scoped_ptr<StartupController> controller_; 119 scoped_ptr<StartupController> controller_;
100 scoped_ptr<FakeSigninManagerWrapper> signin_; 120 scoped_ptr<FakeSigninManagerWrapper> signin_;
101 scoped_ptr<FakeProfileOAuth2TokenService> token_service_; 121 scoped_ptr<FakeProfileOAuth2TokenService> token_service_;
102 scoped_ptr<sync_driver::SyncPrefs> sync_prefs_; 122 scoped_ptr<sync_driver::SyncPrefs> sync_prefs_;
103 }; 123 };
104 124
105 // Test that sync doesn't start until all conditions are met. 125 // Test that sync doesn't start until all conditions are met.
106 TEST_F(StartupControllerTest, Basic) { 126 TEST_F(StartupControllerTest, Basic) {
107 controller()->TryStart(); 127 controller()->TryStart();
108 EXPECT_FALSE(started()); 128 ExpectNotStarted();
109 sync_prefs()->SetFirstSetupComplete(); 129
110 controller()->TryStart();
111 EXPECT_FALSE(started());
112 signin()->set_account_id(kTestUser); 130 signin()->set_account_id(kTestUser);
113 controller()->TryStart(); 131 controller()->TryStart();
114 EXPECT_FALSE(started()); 132 ExpectNotStarted();
133
115 token_service()->UpdateCredentials(kTestUser, kTestToken); 134 token_service()->UpdateCredentials(kTestUser, kTestToken);
116 const bool deferred_start =
117 !base::CommandLine::ForCurrentProcess()->HasSwitch(
118 switches::kSyncDisableDeferredStartup);
119 controller()->TryStart(); 135 controller()->TryStart();
120 EXPECT_EQ(!deferred_start, started()); 136 ExpectStartDeferred();
121 std::string state(controller()->GetBackendInitializationStateString());
122 EXPECT_TRUE(deferred_start ? state == kStateStringDeferred :
123 state == kStateStringStarted);
124 } 137 }
125 138
126 // Test that sync doesn't start when not requested even if all other 139 // Test that sync doesn't start when not requested even if all other
127 // conditons are met. 140 // conditons are met.
128 TEST_F(StartupControllerTest, NotRequested) { 141 TEST_F(StartupControllerTest, NotRequested) {
129 sync_prefs()->SetFirstSetupComplete();
130 sync_prefs()->SetSyncRequested(false); 142 sync_prefs()->SetSyncRequested(false);
131 signin()->set_account_id(kTestUser); 143 signin()->set_account_id(kTestUser);
132 token_service()->UpdateCredentials(kTestUser, kTestToken); 144 token_service()->UpdateCredentials(kTestUser, kTestToken);
133 controller()->TryStart(); 145 controller()->TryStart();
134 EXPECT_FALSE(started()); 146 ExpectNotStarted();
135 EXPECT_EQ(kStateStringNotStarted,
136 controller()->GetBackendInitializationStateString());
137 } 147 }
138 148
139 // Test that sync doesn't when managed even if all other conditons are met. 149 // Test that sync doesn't when managed even if all other conditons are met.
140 TEST_F(StartupControllerTest, Managed) { 150 TEST_F(StartupControllerTest, Managed) {
141 sync_prefs()->SetFirstSetupComplete();
142 sync_prefs()->SetManagedForTest(true); 151 sync_prefs()->SetManagedForTest(true);
143 signin()->set_account_id(kTestUser); 152 signin()->set_account_id(kTestUser);
144 token_service()->UpdateCredentials(kTestUser, kTestToken); 153 token_service()->UpdateCredentials(kTestUser, kTestToken);
145 controller()->TryStart(); 154 controller()->TryStart();
146 EXPECT_FALSE(started()); 155 ExpectNotStarted();
147 EXPECT_EQ(kStateStringNotStarted,
148 controller()->GetBackendInitializationStateString());
149 } 156 }
150 157
151 // Test that sync doesn't start until all conditions are met and a 158 // Test that sync doesn't start until all conditions are met and a
152 // data type triggers sync startup. 159 // data type triggers sync startup.
153 TEST_F(StartupControllerTest, DataTypeTriggered) { 160 TEST_F(StartupControllerTest, DataTypeTriggered) {
154 sync_prefs()->SetFirstSetupComplete();
155 signin()->set_account_id(kTestUser); 161 signin()->set_account_id(kTestUser);
156 token_service()->UpdateCredentials(kTestUser, kTestToken); 162 token_service()->UpdateCredentials(kTestUser, kTestToken);
157 controller()->TryStart(); 163 controller()->TryStart();
158 EXPECT_FALSE(started()); 164 ExpectStartDeferred();
159 EXPECT_EQ(kStateStringDeferred, 165
160 controller()->GetBackendInitializationStateString());
161 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS); 166 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS);
162 EXPECT_TRUE(started()); 167 ExpectStarted();
163 EXPECT_EQ(kStateStringStarted,
164 controller()->GetBackendInitializationStateString());
165 168
166 // The fallback timer shouldn't result in another invocation of the closure 169 // The fallback timer shouldn't result in another invocation of the closure
167 // we passed to the StartupController. 170 // we passed to the StartupController.
168 clear_started(); 171 clear_started();
169 base::RunLoop().RunUntilIdle(); 172 base::RunLoop().RunUntilIdle();
170 EXPECT_FALSE(started()); 173 EXPECT_FALSE(started());
171 } 174 }
172 175
173 // Test that the fallback timer starts sync in the event all 176 // Test that the fallback timer starts sync in the event all
174 // conditions are met and no data type requests sync. 177 // conditions are met and no data type requests sync.
175 TEST_F(StartupControllerTest, FallbackTimer) { 178 TEST_F(StartupControllerTest, FallbackTimer) {
176 sync_prefs()->SetFirstSetupComplete();
177 signin()->set_account_id(kTestUser); 179 signin()->set_account_id(kTestUser);
178 token_service()->UpdateCredentials(kTestUser, kTestToken); 180 token_service()->UpdateCredentials(kTestUser, kTestToken);
179 controller()->TryStart(); 181 controller()->TryStart();
180 EXPECT_FALSE(started()); 182 ExpectStartDeferred();
183
181 base::RunLoop().RunUntilIdle(); 184 base::RunLoop().RunUntilIdle();
182 EXPECT_TRUE(started()); 185 ExpectStarted();
183 } 186 }
184 187
185 // Test that we start immediately if sessions is disabled. 188 // Test that we start immediately if sessions is disabled.
186 TEST_F(StartupControllerTest, NoDeferralWithoutSessionsSync) { 189 TEST_F(StartupControllerTest, NoDeferralWithoutSessionsSync) {
187 syncer::ModelTypeSet types(syncer::UserTypes()); 190 syncer::ModelTypeSet types(syncer::UserTypes());
188 // Disabling sessions means disabling 4 types due to groupings. 191 // Disabling sessions means disabling 4 types due to groupings.
189 types.Remove(syncer::SESSIONS); 192 types.Remove(syncer::SESSIONS);
190 types.Remove(syncer::PROXY_TABS); 193 types.Remove(syncer::PROXY_TABS);
191 types.Remove(syncer::TYPED_URLS); 194 types.Remove(syncer::TYPED_URLS);
192 types.Remove(syncer::SUPERVISED_USER_SETTINGS); 195 types.Remove(syncer::SUPERVISED_USER_SETTINGS);
193 sync_prefs()->SetKeepEverythingSynced(false); 196 sync_prefs()->SetKeepEverythingSynced(false);
194 sync_prefs()->SetPreferredDataTypes(syncer::UserTypes(), types); 197 sync_prefs()->SetPreferredDataTypes(syncer::UserTypes(), types);
195 controller()->Reset(syncer::UserTypes()); 198 controller()->Reset(syncer::UserTypes());
196 sync_prefs()->SetFirstSetupComplete(); 199
197 signin()->set_account_id(kTestUser); 200 signin()->set_account_id(kTestUser);
198 token_service()->UpdateCredentials(kTestUser, kTestToken); 201 token_service()->UpdateCredentials(kTestUser, kTestToken);
199 controller()->TryStart(); 202 controller()->TryStart();
200 EXPECT_TRUE(started()); 203 ExpectStarted();
201 } 204 }
202 205
203 // Sanity check that the fallback timer doesn't fire before startup 206 // Sanity check that the fallback timer doesn't fire before startup
204 // conditions are met. 207 // conditions are met.
205 TEST_F(StartupControllerTest, FallbackTimerWaits) { 208 TEST_F(StartupControllerTest, FallbackTimerWaits) {
206 controller()->TryStart(); 209 controller()->TryStart();
207 EXPECT_FALSE(started()); 210 ExpectNotStarted();
208 base::RunLoop().RunUntilIdle(); 211 base::RunLoop().RunUntilIdle();
209 EXPECT_FALSE(started()); 212 ExpectNotStarted();
210 } 213 }
211 214
212 // Test that sync starts without the user having to explicitly ask for 215 TEST_F(StartupControllerTest, NoDeferralWithSetupInProgress) {
Nicolas Zea 2016/03/10 21:21:39 nit: would be good to keep the comments that give
maxbogue 2016/03/11 00:35:34 Done.
213 // setup when AUTO_START is the startup behavior requested. 216 signin()->set_account_id(kTestUser);
214 TEST_F(StartupControllerTest, FirstSetupWithAutoStart) { 217 token_service()->UpdateCredentials(kTestUser, kTestToken);
218 controller()->set_setup_in_progress(true);
219 controller()->TryStart();
220 ExpectStarted();
221 }
222
223 TEST_F(StartupControllerTest, NoDeferralOnRestart) {
Nicolas Zea 2016/03/10 21:21:39 nit: Shouldn't the name of the test be the opposit
maxbogue 2016/03/11 00:35:34 Done.
215 signin()->set_account_id(kTestUser); 224 signin()->set_account_id(kTestUser);
216 token_service()->UpdateCredentials(kTestUser, kTestToken); 225 token_service()->UpdateCredentials(kTestUser, kTestToken);
217 controller()->TryStart(); 226 controller()->TryStart();
218 EXPECT_TRUE(started()); 227 ExpectStartDeferred();
219 } 228 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS);
229 ExpectStarted();
220 230
221 // Test that sync starts only after user explicitly asks for setup when
222 // MANUAL_START is the startup behavior requested.
223 TEST_F(StartupControllerTest, FirstSetupWithManualStart) {
224 signin()->set_account_id(kTestUser);
225 token_service()->UpdateCredentials(kTestUser, kTestToken);
226 SetUpController(MANUAL_START);
227 controller()->TryStart();
228 EXPECT_FALSE(started());
229 controller()->set_setup_in_progress(true);
230 controller()->TryStart();
231 EXPECT_TRUE(started());
232 }
233
234 TEST_F(StartupControllerTest, Reset) {
235 sync_prefs()->SetFirstSetupComplete();
236 signin()->set_account_id(kTestUser);
237 token_service()->UpdateCredentials(kTestUser, kTestToken);
238 controller()->TryStart();
239 const bool deferred_start =
240 !base::CommandLine::ForCurrentProcess()->HasSwitch(
241 switches::kSyncDisableDeferredStartup);
242 EXPECT_EQ(!deferred_start, started());
243 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS);
244 EXPECT_TRUE(started());
245 clear_started(); 231 clear_started();
246 controller()->Reset(syncer::UserTypes()); 232 controller()->Reset(syncer::UserTypes());
247 EXPECT_FALSE(started()); 233 ExpectNotStarted();
248 controller()->TryStart(); 234 controller()->TryStart();
249 // Restart is not deferred. 235 // Restart is not deferred.
250 EXPECT_TRUE(started()); 236 ExpectStarted();
Nicolas Zea 2016/03/10 21:21:39 I think this should be deferred. I'm pretty sure t
maxbogue 2016/03/11 00:35:34 It's not passing in this patch, no. I was letting
251 } 237 }
252 238
253 // Test that setup-in-progress tracking is persistent across a Reset. 239 // Test that setup-in-progress tracking is persistent across a Reset.
254 TEST_F(StartupControllerTest, ResetDuringSetup) { 240 TEST_F(StartupControllerTest, ResetDuringSetup) {
255 signin()->set_account_id(kTestUser); 241 signin()->set_account_id(kTestUser);
256 token_service()->UpdateCredentials(kTestUser, kTestToken); 242 token_service()->UpdateCredentials(kTestUser, kTestToken);
257 243
258 // Simulate UI telling us setup is in progress. 244 // Simulate UI telling us setup is in progress.
259 controller()->set_setup_in_progress(true); 245 controller()->set_setup_in_progress(true);
260 246
261 // This could happen if the UI triggers a stop-syncing permanently call. 247 // This could happen if the UI triggers a stop-syncing permanently call.
262 controller()->Reset(syncer::UserTypes()); 248 controller()->Reset(syncer::UserTypes());
263 249
264 // From the UI's point of view, setup is still in progress. 250 // From the UI's point of view, setup is still in progress.
265 EXPECT_TRUE(controller()->IsSetupInProgress()); 251 EXPECT_TRUE(controller()->IsSetupInProgress());
266 } 252 }
267 253
268 } // namespace browser_sync 254 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698