OLD | NEW |
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 "chrome/browser/sync/startup_controller.h" | 5 #include "chrome/browser/sync/startup_controller.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 signin_.reset(); | 79 signin_.reset(); |
80 token_service_.reset(); | 80 token_service_.reset(); |
81 sync_prefs_.reset(); | 81 sync_prefs_.reset(); |
82 started_ = false; | 82 started_ = false; |
83 } | 83 } |
84 | 84 |
85 void FakeStartBackend() { | 85 void FakeStartBackend() { |
86 started_ = true; | 86 started_ = true; |
87 } | 87 } |
88 | 88 |
89 void ForceDeferredStartup() { | |
90 if (!CommandLine::ForCurrentProcess()-> | |
91 HasSwitch(switches::kSyncEnableDeferredStartup)) { | |
92 CommandLine::ForCurrentProcess()-> | |
93 AppendSwitch(switches::kSyncEnableDeferredStartup); | |
94 controller_->Reset(syncer::UserTypes()); | |
95 } | |
96 } | |
97 | |
98 bool started() const { return started_; } | 89 bool started() const { return started_; } |
99 void clear_started() { started_ = false; } | 90 void clear_started() { started_ = false; } |
100 StartupController* controller() { return controller_.get(); } | 91 StartupController* controller() { return controller_.get(); } |
101 FakeManagedUserSigninManagerWrapper* signin() { return signin_.get(); } | 92 FakeManagedUserSigninManagerWrapper* signin() { return signin_.get(); } |
102 FakeProfileOAuth2TokenService* token_service() { | 93 FakeProfileOAuth2TokenService* token_service() { |
103 return static_cast<FakeProfileOAuth2TokenService*>( | 94 return static_cast<FakeProfileOAuth2TokenService*>( |
104 token_service_->GetProfileOAuth2TokenService()); | 95 token_service_->GetProfileOAuth2TokenService()); |
105 } | 96 } |
106 SyncPrefs* sync_prefs() { return sync_prefs_.get(); } | 97 SyncPrefs* sync_prefs() { return sync_prefs_.get(); } |
107 Profile* profile() { return profile_.get(); } | 98 Profile* profile() { return profile_.get(); } |
(...skipping 12 matching lines...) Expand all Loading... |
120 TEST_F(StartupControllerTest, Basic) { | 111 TEST_F(StartupControllerTest, Basic) { |
121 controller()->TryStart(); | 112 controller()->TryStart(); |
122 EXPECT_FALSE(started()); | 113 EXPECT_FALSE(started()); |
123 sync_prefs()->SetSyncSetupCompleted(); | 114 sync_prefs()->SetSyncSetupCompleted(); |
124 controller()->TryStart(); | 115 controller()->TryStart(); |
125 EXPECT_FALSE(started()); | 116 EXPECT_FALSE(started()); |
126 signin()->set_account(kTestUser); | 117 signin()->set_account(kTestUser); |
127 controller()->TryStart(); | 118 controller()->TryStart(); |
128 EXPECT_FALSE(started()); | 119 EXPECT_FALSE(started()); |
129 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); | 120 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); |
130 const bool deferred_start = CommandLine::ForCurrentProcess()-> | 121 const bool deferred_start = !CommandLine::ForCurrentProcess()-> |
131 HasSwitch(switches::kSyncEnableDeferredStartup); | 122 HasSwitch(switches::kSyncDisableDeferredStartup); |
132 controller()->TryStart(); | 123 controller()->TryStart(); |
133 EXPECT_EQ(!deferred_start, started()); | 124 EXPECT_EQ(!deferred_start, started()); |
134 std::string state(controller()->GetBackendInitializationStateString()); | 125 std::string state(controller()->GetBackendInitializationStateString()); |
135 EXPECT_TRUE(deferred_start ? state == kStateStringDeferred : | 126 EXPECT_TRUE(deferred_start ? state == kStateStringDeferred : |
136 state == kStateStringStarted); | 127 state == kStateStringStarted); |
137 } | 128 } |
138 | 129 |
139 // Test that sync doesn't when suppressed even if all other conditons are met. | 130 // Test that sync doesn't when suppressed even if all other conditons are met. |
140 TEST_F(StartupControllerTest, Suppressed) { | 131 TEST_F(StartupControllerTest, Suppressed) { |
141 sync_prefs()->SetSyncSetupCompleted(); | 132 sync_prefs()->SetSyncSetupCompleted(); |
(...skipping 14 matching lines...) Expand all Loading... |
156 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); | 147 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); |
157 controller()->TryStart(); | 148 controller()->TryStart(); |
158 EXPECT_FALSE(started()); | 149 EXPECT_FALSE(started()); |
159 EXPECT_EQ(kStateStringNotStarted, | 150 EXPECT_EQ(kStateStringNotStarted, |
160 controller()->GetBackendInitializationStateString()); | 151 controller()->GetBackendInitializationStateString()); |
161 } | 152 } |
162 | 153 |
163 // Test that sync doesn't start until all conditions are met and a | 154 // Test that sync doesn't start until all conditions are met and a |
164 // data type triggers sync startup. | 155 // data type triggers sync startup. |
165 TEST_F(StartupControllerTest, DataTypeTriggered) { | 156 TEST_F(StartupControllerTest, DataTypeTriggered) { |
166 ForceDeferredStartup(); | |
167 sync_prefs()->SetSyncSetupCompleted(); | 157 sync_prefs()->SetSyncSetupCompleted(); |
168 signin()->set_account(kTestUser); | 158 signin()->set_account(kTestUser); |
169 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); | 159 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); |
170 controller()->TryStart(); | 160 controller()->TryStart(); |
171 EXPECT_FALSE(started()); | 161 EXPECT_FALSE(started()); |
172 EXPECT_EQ(kStateStringDeferred, | 162 EXPECT_EQ(kStateStringDeferred, |
173 controller()->GetBackendInitializationStateString()); | 163 controller()->GetBackendInitializationStateString()); |
174 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS); | 164 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS); |
175 EXPECT_TRUE(started()); | 165 EXPECT_TRUE(started()); |
176 EXPECT_EQ(kStateStringStarted, | 166 EXPECT_EQ(kStateStringStarted, |
177 controller()->GetBackendInitializationStateString()); | 167 controller()->GetBackendInitializationStateString()); |
178 | 168 |
179 // 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 |
180 // we passed to the StartupController. | 170 // we passed to the StartupController. |
181 clear_started(); | 171 clear_started(); |
182 base::RunLoop().RunUntilIdle(); | 172 base::RunLoop().RunUntilIdle(); |
183 EXPECT_FALSE(started()); | 173 EXPECT_FALSE(started()); |
184 } | 174 } |
185 | 175 |
186 // Test that the fallback timer starts sync in the event all | 176 // Test that the fallback timer starts sync in the event all |
187 // conditions are met and no data type requests sync. | 177 // conditions are met and no data type requests sync. |
188 TEST_F(StartupControllerTest, FallbackTimer) { | 178 TEST_F(StartupControllerTest, FallbackTimer) { |
189 ForceDeferredStartup(); | |
190 sync_prefs()->SetSyncSetupCompleted(); | 179 sync_prefs()->SetSyncSetupCompleted(); |
191 signin()->set_account(kTestUser); | 180 signin()->set_account(kTestUser); |
192 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); | 181 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); |
193 controller()->TryStart(); | 182 controller()->TryStart(); |
194 EXPECT_FALSE(started()); | 183 EXPECT_FALSE(started()); |
195 base::RunLoop().RunUntilIdle(); | 184 base::RunLoop().RunUntilIdle(); |
196 EXPECT_TRUE(started()); | 185 EXPECT_TRUE(started()); |
197 } | 186 } |
198 | 187 |
199 // Test that we start immediately if sessions is disabled. | 188 // Test that we start immediately if sessions is disabled. |
200 TEST_F(StartupControllerTest, NoDeferralWithoutSessionsSync) { | 189 TEST_F(StartupControllerTest, NoDeferralWithoutSessionsSync) { |
201 syncer::ModelTypeSet types(syncer::UserTypes()); | 190 syncer::ModelTypeSet types(syncer::UserTypes()); |
202 // Disabling sessions means disabling 4 types due to groupings. | 191 // Disabling sessions means disabling 4 types due to groupings. |
203 types.Remove(syncer::SESSIONS); | 192 types.Remove(syncer::SESSIONS); |
204 types.Remove(syncer::PROXY_TABS); | 193 types.Remove(syncer::PROXY_TABS); |
205 types.Remove(syncer::TYPED_URLS); | 194 types.Remove(syncer::TYPED_URLS); |
206 types.Remove(syncer::MANAGED_USER_SETTINGS); | 195 types.Remove(syncer::MANAGED_USER_SETTINGS); |
207 sync_prefs()->SetKeepEverythingSynced(false); | 196 sync_prefs()->SetKeepEverythingSynced(false); |
208 sync_prefs()->SetPreferredDataTypes(syncer::UserTypes(), types); | 197 sync_prefs()->SetPreferredDataTypes(syncer::UserTypes(), types); |
209 CommandLine::ForCurrentProcess()->AppendSwitch( | |
210 switches::kSyncEnableDeferredStartup); | |
211 controller()->Reset(syncer::UserTypes()); | 198 controller()->Reset(syncer::UserTypes()); |
212 sync_prefs()->SetSyncSetupCompleted(); | 199 sync_prefs()->SetSyncSetupCompleted(); |
213 signin()->set_account(kTestUser); | 200 signin()->set_account(kTestUser); |
214 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); | 201 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); |
215 controller()->TryStart(); | 202 controller()->TryStart(); |
216 EXPECT_TRUE(started()); | 203 EXPECT_TRUE(started()); |
217 } | 204 } |
218 | 205 |
219 // Sanity check that the fallback timer doesn't fire before startup | 206 // Sanity check that the fallback timer doesn't fire before startup |
220 // conditions are met. | 207 // conditions are met. |
221 TEST_F(StartupControllerTest, FallbackTimerWaits) { | 208 TEST_F(StartupControllerTest, FallbackTimerWaits) { |
222 ForceDeferredStartup(); | |
223 controller()->TryStart(); | 209 controller()->TryStart(); |
224 EXPECT_FALSE(started()); | 210 EXPECT_FALSE(started()); |
225 base::RunLoop().RunUntilIdle(); | 211 base::RunLoop().RunUntilIdle(); |
226 EXPECT_FALSE(started()); | 212 EXPECT_FALSE(started()); |
227 } | 213 } |
228 | 214 |
229 // Test that sync starts when the user first asks to setup sync (which | 215 // Test that sync starts when the user first asks to setup sync (which |
230 // may be implicit due to the platform). | 216 // may be implicit due to the platform). |
231 TEST_F(StartupControllerTest, FirstSetup) { | 217 TEST_F(StartupControllerTest, FirstSetup) { |
232 signin()->set_account(kTestUser); | 218 signin()->set_account(kTestUser); |
(...skipping 14 matching lines...) Expand all Loading... |
247 sync_prefs()->SetSyncSetupCompleted(); | 233 sync_prefs()->SetSyncSetupCompleted(); |
248 signin()->set_account(kTestUser); | 234 signin()->set_account(kTestUser); |
249 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); | 235 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); |
250 controller()->TryStart(); | 236 controller()->TryStart(); |
251 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS); | 237 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS); |
252 EXPECT_TRUE(started()); | 238 EXPECT_TRUE(started()); |
253 clear_started(); | 239 clear_started(); |
254 controller()->Reset(syncer::UserTypes()); | 240 controller()->Reset(syncer::UserTypes()); |
255 base::RunLoop().RunUntilIdle(); | 241 base::RunLoop().RunUntilIdle(); |
256 EXPECT_FALSE(started()); | 242 EXPECT_FALSE(started()); |
257 const bool deferred_start = CommandLine::ForCurrentProcess()-> | 243 const bool deferred_start = !CommandLine::ForCurrentProcess()-> |
258 HasSwitch(switches::kSyncEnableDeferredStartup); | 244 HasSwitch(switches::kSyncDisableDeferredStartup); |
259 controller()->TryStart(); | 245 controller()->TryStart(); |
260 EXPECT_EQ(!deferred_start, started()); | 246 EXPECT_EQ(!deferred_start, started()); |
261 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS); | 247 controller()->OnDataTypeRequestsSyncStartup(syncer::SESSIONS); |
262 EXPECT_TRUE(started()); | 248 EXPECT_TRUE(started()); |
263 } | 249 } |
264 | 250 |
265 // Test that setup-in-progress tracking is reset properly on Reset. | 251 // Test that setup-in-progress tracking is reset properly on Reset. |
266 // This scenario doesn't affect auto-start platforms. | 252 // This scenario doesn't affect auto-start platforms. |
267 TEST_F(StartupControllerTest, ResetDuringSetup) { | 253 TEST_F(StartupControllerTest, ResetDuringSetup) { |
268 signin()->set_account(kTestUser); | 254 signin()->set_account(kTestUser); |
269 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); | 255 token_service()->IssueRefreshTokenForUser(kTestUser, kTestToken); |
270 controller()->set_setup_in_progress(true); | 256 controller()->set_setup_in_progress(true); |
271 controller()->Reset(syncer::UserTypes()); | 257 controller()->Reset(syncer::UserTypes()); |
272 controller()->TryStart(); | 258 controller()->TryStart(); |
273 | 259 |
274 if (!browser_defaults::kSyncAutoStarts) { | 260 if (!browser_defaults::kSyncAutoStarts) { |
275 EXPECT_FALSE(started()); | 261 EXPECT_FALSE(started()); |
276 EXPECT_EQ(kStateStringNotStarted, | 262 EXPECT_EQ(kStateStringNotStarted, |
277 controller()->GetBackendInitializationStateString()); | 263 controller()->GetBackendInitializationStateString()); |
278 } | 264 } |
279 } | 265 } |
280 | 266 |
281 } // namespace browser_sync | 267 } // namespace browser_sync |
OLD | NEW |