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

Side by Side Diff: content/browser/background_sync/background_sync_manager_unittest.cc

Issue 2954433002: BackgroundSync: Convert to base::BindOnce/OnceCallback/OnceClosure (Closed)
Patch Set: rename local variable Created 3 years, 5 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "content/browser/background_sync/background_sync_manager.h" 5 #include "content/browser/background_sync/background_sync_manager.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 void TearDown() override { 154 void TearDown() override {
155 // Restore the network observer functionality for subsequent tests 155 // Restore the network observer functionality for subsequent tests
156 background_sync_test_util::SetIgnoreNetworkChangeNotifier(false); 156 background_sync_test_util::SetIgnoreNetworkChangeNotifier(false);
157 } 157 }
158 158
159 void RegisterServiceWorkers() { 159 void RegisterServiceWorkers() {
160 bool called_1 = false; 160 bool called_1 = false;
161 bool called_2 = false; 161 bool called_2 = false;
162 helper_->context()->RegisterServiceWorker( 162 helper_->context()->RegisterServiceWorker(
163 GURL(kPattern1), GURL(kScript1), NULL, 163 GURL(kPattern1), GURL(kScript1), NULL,
164 base::Bind(&RegisterServiceWorkerCallback, &called_1, 164 base::AdaptCallbackForRepeating(
165 &sw_registration_id_1_)); 165 base::BindOnce(&RegisterServiceWorkerCallback, &called_1,
166 &sw_registration_id_1_)));
166 167
167 helper_->context()->RegisterServiceWorker( 168 helper_->context()->RegisterServiceWorker(
168 GURL(kPattern2), GURL(kScript2), NULL, 169 GURL(kPattern2), GURL(kScript2), NULL,
169 base::Bind(&RegisterServiceWorkerCallback, &called_2, 170 base::AdaptCallbackForRepeating(
170 &sw_registration_id_2_)); 171 base::BindOnce(&RegisterServiceWorkerCallback, &called_2,
172 &sw_registration_id_2_)));
171 base::RunLoop().RunUntilIdle(); 173 base::RunLoop().RunUntilIdle();
172 EXPECT_TRUE(called_1); 174 EXPECT_TRUE(called_1);
173 EXPECT_TRUE(called_2); 175 EXPECT_TRUE(called_2);
174 176
175 // Hang onto the registrations as they need to be "live" when 177 // Hang onto the registrations as they need to be "live" when
176 // calling BackgroundSyncManager::Register. 178 // calling BackgroundSyncManager::Register.
177 helper_->context_wrapper()->FindReadyRegistrationForId( 179 helper_->context_wrapper()->FindReadyRegistrationForId(
178 sw_registration_id_1_, GURL(kPattern1).GetOrigin(), 180 sw_registration_id_1_, GURL(kPattern1).GetOrigin(),
179 base::Bind(FindServiceWorkerRegistrationCallback, &sw_registration_1_)); 181 base::AdaptCallbackForRepeating(base::BindOnce(
182 FindServiceWorkerRegistrationCallback, &sw_registration_1_)));
180 183
181 helper_->context_wrapper()->FindReadyRegistrationForId( 184 helper_->context_wrapper()->FindReadyRegistrationForId(
182 sw_registration_id_2_, GURL(kPattern1).GetOrigin(), 185 sw_registration_id_2_, GURL(kPattern1).GetOrigin(),
183 base::Bind(FindServiceWorkerRegistrationCallback, &sw_registration_2_)); 186 base::AdaptCallbackForRepeating(base::BindOnce(
187 FindServiceWorkerRegistrationCallback, &sw_registration_2_)));
184 base::RunLoop().RunUntilIdle(); 188 base::RunLoop().RunUntilIdle();
185 EXPECT_TRUE(sw_registration_1_); 189 EXPECT_TRUE(sw_registration_1_);
186 EXPECT_TRUE(sw_registration_2_); 190 EXPECT_TRUE(sw_registration_2_);
187 } 191 }
188 192
189 void SetNetwork(net::NetworkChangeNotifier::ConnectionType connection_type) { 193 void SetNetwork(net::NetworkChangeNotifier::ConnectionType connection_type) {
190 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( 194 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
191 connection_type); 195 connection_type);
192 if (test_background_sync_manager_) { 196 if (test_background_sync_manager_) {
193 BackgroundSyncNetworkObserver* network_observer = 197 BackgroundSyncNetworkObserver* network_observer =
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 bool Register(const BackgroundSyncRegistrationOptions& sync_options) { 273 bool Register(const BackgroundSyncRegistrationOptions& sync_options) {
270 return RegisterWithServiceWorkerId(sw_registration_id_1_, sync_options); 274 return RegisterWithServiceWorkerId(sw_registration_id_1_, sync_options);
271 } 275 }
272 276
273 bool RegisterWithServiceWorkerId( 277 bool RegisterWithServiceWorkerId(
274 int64_t sw_registration_id, 278 int64_t sw_registration_id,
275 const BackgroundSyncRegistrationOptions& options) { 279 const BackgroundSyncRegistrationOptions& options) {
276 bool was_called = false; 280 bool was_called = false;
277 background_sync_manager_->Register( 281 background_sync_manager_->Register(
278 sw_registration_id, options, 282 sw_registration_id, options,
279 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, 283 base::BindOnce(
280 base::Unretained(this), &was_called)); 284 &BackgroundSyncManagerTest::StatusAndRegistrationCallback,
285 base::Unretained(this), &was_called));
281 base::RunLoop().RunUntilIdle(); 286 base::RunLoop().RunUntilIdle();
282 EXPECT_TRUE(was_called); 287 EXPECT_TRUE(was_called);
283 return callback_status_ == BACKGROUND_SYNC_STATUS_OK; 288 return callback_status_ == BACKGROUND_SYNC_STATUS_OK;
284 } 289 }
285 290
286 MockPermissionManager* GetPermissionManager() { 291 MockPermissionManager* GetPermissionManager() {
287 return static_cast<MockPermissionManager*>( 292 return static_cast<MockPermissionManager*>(
288 helper_->browser_context()->GetPermissionManager()); 293 helper_->browser_context()->GetPermissionManager());
289 } 294 }
290 295
291 bool GetRegistration( 296 bool GetRegistration(
292 const BackgroundSyncRegistrationOptions& registration_options) { 297 const BackgroundSyncRegistrationOptions& registration_options) {
293 return GetRegistrationWithServiceWorkerId(sw_registration_id_1_, 298 return GetRegistrationWithServiceWorkerId(sw_registration_id_1_,
294 registration_options); 299 registration_options);
295 } 300 }
296 301
297 bool GetRegistrationWithServiceWorkerId( 302 bool GetRegistrationWithServiceWorkerId(
298 int64_t sw_registration_id, 303 int64_t sw_registration_id,
299 const BackgroundSyncRegistrationOptions& registration_options) { 304 const BackgroundSyncRegistrationOptions& registration_options) {
300 bool was_called = false; 305 bool was_called = false;
301 background_sync_manager_->GetRegistrations( 306 background_sync_manager_->GetRegistrations(
302 sw_registration_id, 307 sw_registration_id,
303 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationsCallback, 308 base::BindOnce(
304 base::Unretained(this), &was_called)); 309 &BackgroundSyncManagerTest::StatusAndRegistrationsCallback,
310 base::Unretained(this), &was_called));
305 base::RunLoop().RunUntilIdle(); 311 base::RunLoop().RunUntilIdle();
306 EXPECT_TRUE(was_called); 312 EXPECT_TRUE(was_called);
307 313
308 if (callback_status_ == BACKGROUND_SYNC_STATUS_OK) { 314 if (callback_status_ == BACKGROUND_SYNC_STATUS_OK) {
309 for (auto iter = callback_registrations_.begin(); 315 for (auto iter = callback_registrations_.begin();
310 iter < callback_registrations_.end(); ++iter) { 316 iter < callback_registrations_.end(); ++iter) {
311 if ((*iter)->options()->tag == registration_options.tag) { 317 if ((*iter)->options()->tag == registration_options.tag) {
312 // Transfer the matching registration out of the vector into 318 // Transfer the matching registration out of the vector into
313 // callback_registration_ for testing. 319 // callback_registration_ for testing.
314 callback_registration_ = std::move(*iter); 320 callback_registration_ = std::move(*iter);
315 callback_registrations_.erase(iter); 321 callback_registrations_.erase(iter);
316 return true; 322 return true;
317 } 323 }
318 } 324 }
319 } 325 }
320 return false; 326 return false;
321 } 327 }
322 328
323 bool GetRegistrations() { 329 bool GetRegistrations() {
324 return GetRegistrationsWithServiceWorkerId(sw_registration_id_1_); 330 return GetRegistrationsWithServiceWorkerId(sw_registration_id_1_);
325 } 331 }
326 332
327 bool GetRegistrationsWithServiceWorkerId(int64_t sw_registration_id) { 333 bool GetRegistrationsWithServiceWorkerId(int64_t sw_registration_id) {
328 bool was_called = false; 334 bool was_called = false;
329 background_sync_manager_->GetRegistrations( 335 background_sync_manager_->GetRegistrations(
330 sw_registration_id, 336 sw_registration_id,
331 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationsCallback, 337 base::BindOnce(
332 base::Unretained(this), &was_called)); 338 &BackgroundSyncManagerTest::StatusAndRegistrationsCallback,
339 base::Unretained(this), &was_called));
333 base::RunLoop().RunUntilIdle(); 340 base::RunLoop().RunUntilIdle();
334 EXPECT_TRUE(was_called); 341 EXPECT_TRUE(was_called);
335 342
336 return callback_status_ == BACKGROUND_SYNC_STATUS_OK; 343 return callback_status_ == BACKGROUND_SYNC_STATUS_OK;
337 } 344 }
338 345
339 MockBackgroundSyncController* GetController() { 346 MockBackgroundSyncController* GetController() {
340 return static_cast<MockBackgroundSyncController*>( 347 return static_cast<MockBackgroundSyncController*>(
341 helper_->browser_context()->GetBackgroundSyncController()); 348 helper_->browser_context()->GetBackgroundSyncController());
342 } 349 }
343 350
344 void StorageRegistrationCallback(ServiceWorkerStatusCode result) { 351 void StorageRegistrationCallback(ServiceWorkerStatusCode result) {
345 callback_sw_status_code_ = result; 352 callback_sw_status_code_ = result;
346 } 353 }
347 354
348 void UnregisterServiceWorker(uint64_t sw_registration_id) { 355 void UnregisterServiceWorker(uint64_t sw_registration_id) {
349 bool called = false; 356 bool called = false;
350 helper_->context()->UnregisterServiceWorker( 357 helper_->context()->UnregisterServiceWorker(
351 PatternForSWId(sw_registration_id), 358 PatternForSWId(sw_registration_id),
352 base::Bind(&UnregisterServiceWorkerCallback, &called)); 359 base::AdaptCallbackForRepeating(
360 base::BindOnce(&UnregisterServiceWorkerCallback, &called)));
353 base::RunLoop().RunUntilIdle(); 361 base::RunLoop().RunUntilIdle();
354 EXPECT_TRUE(called); 362 EXPECT_TRUE(called);
355 } 363 }
356 364
357 GURL PatternForSWId(int64_t sw_id) { 365 GURL PatternForSWId(int64_t sw_id) {
358 EXPECT_TRUE(sw_id == sw_registration_id_1_ || 366 EXPECT_TRUE(sw_id == sw_registration_id_1_ ||
359 sw_id == sw_registration_id_2_); 367 sw_id == sw_registration_id_2_);
360 return sw_id == sw_registration_id_1_ ? GURL(kPattern1) : GURL(kPattern2); 368 return sw_id == sw_registration_id_1_ ? GURL(kPattern1) : GURL(kPattern2);
361 } 369 }
362 370
363 void SetupForSyncEvent( 371 void SetupForSyncEvent(
364 const TestBackgroundSyncManager::DispatchSyncCallback& callback) { 372 const TestBackgroundSyncManager::DispatchSyncCallback& callback) {
365 test_background_sync_manager_->set_dispatch_sync_callback(callback); 373 test_background_sync_manager_->set_dispatch_sync_callback(callback);
366 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI); 374 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI);
367 } 375 }
368 376
369 void InitSyncEventTest() { 377 void InitSyncEventTest() {
370 SetupForSyncEvent( 378 SetupForSyncEvent(base::BindRepeating(DispatchSyncSuccessfulCallback,
371 base::Bind(DispatchSyncSuccessfulCallback, &sync_events_called_)); 379 &sync_events_called_));
372 } 380 }
373 381
374 void InitFailedSyncEventTest() { 382 void InitFailedSyncEventTest() {
375 SetupForSyncEvent( 383 SetupForSyncEvent(
376 base::Bind(DispatchSyncFailedCallback, &sync_events_called_)); 384 base::BindRepeating(DispatchSyncFailedCallback, &sync_events_called_));
377 } 385 }
378 386
379 void InitDelayedSyncEventTest() { 387 void InitDelayedSyncEventTest() {
380 SetupForSyncEvent(base::Bind(DispatchSyncDelayedCallback, 388 SetupForSyncEvent(base::BindRepeating(DispatchSyncDelayedCallback,
381 &sync_events_called_, &sync_fired_callback_)); 389 &sync_events_called_,
390 &sync_fired_callback_));
382 } 391 }
383 392
384 void RegisterAndVerifySyncEventDelayed( 393 void RegisterAndVerifySyncEventDelayed(
385 const BackgroundSyncRegistrationOptions& sync_options) { 394 const BackgroundSyncRegistrationOptions& sync_options) {
386 int sync_events_called = sync_events_called_; 395 int sync_events_called = sync_events_called_;
387 EXPECT_TRUE(sync_fired_callback_.is_null()); 396 EXPECT_TRUE(sync_fired_callback_.is_null());
388 397
389 EXPECT_TRUE(Register(sync_options)); 398 EXPECT_TRUE(Register(sync_options));
390 399
391 EXPECT_EQ(sync_events_called + 1, sync_events_called_); 400 EXPECT_EQ(sync_events_called + 1, sync_events_called_);
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
649 658
650 TEST_F(BackgroundSyncManagerTest, SequentialOperations) { 659 TEST_F(BackgroundSyncManagerTest, SequentialOperations) {
651 // Schedule Init and all of the operations on a delayed backend. Verify that 660 // Schedule Init and all of the operations on a delayed backend. Verify that
652 // the operations complete sequentially. 661 // the operations complete sequentially.
653 SetupDelayedBackgroundSyncManager(); 662 SetupDelayedBackgroundSyncManager();
654 663
655 bool register_called = false; 664 bool register_called = false;
656 bool get_registrations_called = false; 665 bool get_registrations_called = false;
657 test_background_sync_manager_->Register( 666 test_background_sync_manager_->Register(
658 sw_registration_id_1_, sync_options_1_, 667 sw_registration_id_1_, sync_options_1_,
659 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, 668 base::AdaptCallbackForRepeating(base::BindOnce(
660 base::Unretained(this), &register_called)); 669 &BackgroundSyncManagerTest::StatusAndRegistrationCallback,
670 base::Unretained(this), &register_called)));
661 test_background_sync_manager_->GetRegistrations( 671 test_background_sync_manager_->GetRegistrations(
662 sw_registration_id_1_, 672 sw_registration_id_1_,
663 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationsCallback, 673 base::AdaptCallbackForRepeating(base::BindOnce(
664 base::Unretained(this), &get_registrations_called)); 674 &BackgroundSyncManagerTest::StatusAndRegistrationsCallback,
675 base::Unretained(this), &get_registrations_called)));
665 676
666 base::RunLoop().RunUntilIdle(); 677 base::RunLoop().RunUntilIdle();
667 // Init should be blocked while loading from the backend. 678 // Init should be blocked while loading from the backend.
668 EXPECT_FALSE(register_called); 679 EXPECT_FALSE(register_called);
669 EXPECT_FALSE(get_registrations_called); 680 EXPECT_FALSE(get_registrations_called);
670 681
671 test_background_sync_manager_->ResumeBackendOperation(); 682 test_background_sync_manager_->ResumeBackendOperation();
672 base::RunLoop().RunUntilIdle(); 683 base::RunLoop().RunUntilIdle();
673 // Register should be blocked while storing to the backend. 684 // Register should be blocked while storing to the backend.
674 EXPECT_FALSE(register_called); 685 EXPECT_FALSE(register_called);
(...skipping 14 matching lines...) Expand all
689 } 700 }
690 701
691 TEST_F(BackgroundSyncManagerTest, 702 TEST_F(BackgroundSyncManagerTest,
692 UnregisterServiceWorkerDuringSyncRegistration) { 703 UnregisterServiceWorkerDuringSyncRegistration) {
693 EXPECT_TRUE(Register(sync_options_1_)); 704 EXPECT_TRUE(Register(sync_options_1_));
694 705
695 test_background_sync_manager_->set_delay_backend(true); 706 test_background_sync_manager_->set_delay_backend(true);
696 bool callback_called = false; 707 bool callback_called = false;
697 test_background_sync_manager_->Register( 708 test_background_sync_manager_->Register(
698 sw_registration_id_1_, sync_options_2_, 709 sw_registration_id_1_, sync_options_2_,
699 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, 710 base::AdaptCallbackForRepeating(base::BindOnce(
700 base::Unretained(this), &callback_called)); 711 &BackgroundSyncManagerTest::StatusAndRegistrationCallback,
712 base::Unretained(this), &callback_called)));
701 713
702 base::RunLoop().RunUntilIdle(); 714 base::RunLoop().RunUntilIdle();
703 EXPECT_FALSE(callback_called); 715 EXPECT_FALSE(callback_called);
704 UnregisterServiceWorker(sw_registration_id_1_); 716 UnregisterServiceWorker(sw_registration_id_1_);
705 717
706 test_background_sync_manager_->ResumeBackendOperation(); 718 test_background_sync_manager_->ResumeBackendOperation();
707 base::RunLoop().RunUntilIdle(); 719 base::RunLoop().RunUntilIdle();
708 EXPECT_TRUE(callback_called); 720 EXPECT_TRUE(callback_called);
709 EXPECT_EQ(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback_status_); 721 EXPECT_EQ(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback_status_);
710 722
(...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after
1164 EXPECT_FALSE(GetRegistration(sync_options_1_)); 1176 EXPECT_FALSE(GetRegistration(sync_options_1_));
1165 } 1177 }
1166 1178
1167 TEST_F(BackgroundSyncManagerTest, TwoAttempts) { 1179 TEST_F(BackgroundSyncManagerTest, TwoAttempts) {
1168 SetMaxSyncAttemptsAndRestartManager(2); 1180 SetMaxSyncAttemptsAndRestartManager(2);
1169 InitFailedSyncEventTest(); 1181 InitFailedSyncEventTest();
1170 1182
1171 // The first run will fail but it will setup a timer to try again. 1183 // The first run will fail but it will setup a timer to try again.
1172 EXPECT_TRUE(Register(sync_options_1_)); 1184 EXPECT_TRUE(Register(sync_options_1_));
1173 EXPECT_TRUE(GetRegistration(sync_options_1_)); 1185 EXPECT_TRUE(GetRegistration(sync_options_1_));
1174 EXPECT_FALSE(test_background_sync_manager_->delayed_task().is_null()); 1186 EXPECT_TRUE(test_background_sync_manager_->IsDelayedTaskScheduled());
1175 1187
1176 // Make sure the delay is reasonable. 1188 // Make sure the delay is reasonable.
1177 EXPECT_LT(base::TimeDelta::FromMinutes(1), 1189 EXPECT_LT(base::TimeDelta::FromMinutes(1),
1178 test_background_sync_manager_->delayed_task_delta()); 1190 test_background_sync_manager_->delayed_task_delta());
1179 EXPECT_GT(base::TimeDelta::FromHours(1), 1191 EXPECT_GT(base::TimeDelta::FromHours(1),
1180 test_background_sync_manager_->delayed_task_delta()); 1192 test_background_sync_manager_->delayed_task_delta());
1181 1193
1182 // Fire again and this time it should permanently fail. 1194 // Fire again and this time it should permanently fail.
1183 test_clock_->Advance(test_background_sync_manager_->delayed_task_delta()); 1195 test_clock_->Advance(test_background_sync_manager_->delayed_task_delta());
1184 test_background_sync_manager_->delayed_task().Run(); 1196 test_background_sync_manager_->RunDelayedTask();
1185 base::RunLoop().RunUntilIdle(); 1197 base::RunLoop().RunUntilIdle();
1186 EXPECT_FALSE(GetRegistration(sync_options_1_)); 1198 EXPECT_FALSE(GetRegistration(sync_options_1_));
1187 } 1199 }
1188 1200
1189 TEST_F(BackgroundSyncManagerTest, ThreeAttempts) { 1201 TEST_F(BackgroundSyncManagerTest, ThreeAttempts) {
1190 SetMaxSyncAttemptsAndRestartManager(3); 1202 SetMaxSyncAttemptsAndRestartManager(3);
1191 InitFailedSyncEventTest(); 1203 InitFailedSyncEventTest();
1192 1204
1193 // The first run will fail but it will setup a timer to try again. 1205 // The first run will fail but it will setup a timer to try again.
1194 EXPECT_TRUE(Register(sync_options_1_)); 1206 EXPECT_TRUE(Register(sync_options_1_));
1195 EXPECT_TRUE(GetRegistration(sync_options_1_)); 1207 EXPECT_TRUE(GetRegistration(sync_options_1_));
1196 EXPECT_FALSE(test_background_sync_manager_->delayed_task().is_null()); 1208 EXPECT_TRUE(test_background_sync_manager_->IsDelayedTaskScheduled());
1197 1209
1198 // The second run will fail but it will setup a timer to try again. 1210 // The second run will fail but it will setup a timer to try again.
1199 base::TimeDelta first_delta = 1211 base::TimeDelta first_delta =
1200 test_background_sync_manager_->delayed_task_delta(); 1212 test_background_sync_manager_->delayed_task_delta();
1201 test_clock_->Advance(test_background_sync_manager_->delayed_task_delta()); 1213 test_clock_->Advance(test_background_sync_manager_->delayed_task_delta());
1202 test_background_sync_manager_->delayed_task().Run(); 1214 test_background_sync_manager_->RunDelayedTask();
1203 base::RunLoop().RunUntilIdle(); 1215 base::RunLoop().RunUntilIdle();
1204 EXPECT_TRUE(GetRegistration(sync_options_1_)); 1216 EXPECT_TRUE(GetRegistration(sync_options_1_));
1205 1217
1206 // Verify that the delta grows for each attempt. 1218 // Verify that the delta grows for each attempt.
1207 EXPECT_LT(first_delta, test_background_sync_manager_->delayed_task_delta()); 1219 EXPECT_LT(first_delta, test_background_sync_manager_->delayed_task_delta());
1208 1220
1209 // The third run will permanently fail. 1221 // The third run will permanently fail.
1210 test_clock_->Advance(test_background_sync_manager_->delayed_task_delta()); 1222 test_clock_->Advance(test_background_sync_manager_->delayed_task_delta());
1211 test_background_sync_manager_->delayed_task().Run(); 1223 test_background_sync_manager_->RunDelayedTask();
1212 base::RunLoop().RunUntilIdle(); 1224 base::RunLoop().RunUntilIdle();
1213 EXPECT_FALSE(GetRegistration(sync_options_1_)); 1225 EXPECT_FALSE(GetRegistration(sync_options_1_));
1214 } 1226 }
1215 1227
1216 TEST_F(BackgroundSyncManagerTest, WaitsFullDelayTime) { 1228 TEST_F(BackgroundSyncManagerTest, WaitsFullDelayTime) {
1217 SetMaxSyncAttemptsAndRestartManager(2); 1229 SetMaxSyncAttemptsAndRestartManager(2);
1218 InitFailedSyncEventTest(); 1230 InitFailedSyncEventTest();
1219 1231
1220 // The first run will fail but it will setup a timer to try again. 1232 // The first run will fail but it will setup a timer to try again.
1221 EXPECT_TRUE(Register(sync_options_1_)); 1233 EXPECT_TRUE(Register(sync_options_1_));
1222 EXPECT_TRUE(GetRegistration(sync_options_1_)); 1234 EXPECT_TRUE(GetRegistration(sync_options_1_));
1223 EXPECT_FALSE(test_background_sync_manager_->delayed_task().is_null()); 1235 EXPECT_TRUE(test_background_sync_manager_->IsDelayedTaskScheduled());
1224 1236
1225 // Fire again one second before it's ready to retry. Expect it to reschedule 1237 // Fire again one second before it's ready to retry. Expect it to reschedule
1226 // the delay timer for one more second. 1238 // the delay timer for one more second.
1227 test_clock_->Advance(test_background_sync_manager_->delayed_task_delta() - 1239 test_clock_->Advance(test_background_sync_manager_->delayed_task_delta() -
1228 base::TimeDelta::FromSeconds(1)); 1240 base::TimeDelta::FromSeconds(1));
1229 test_background_sync_manager_->delayed_task().Run(); 1241 test_background_sync_manager_->RunDelayedTask();
1230 base::RunLoop().RunUntilIdle(); 1242 base::RunLoop().RunUntilIdle();
1231 EXPECT_TRUE(GetRegistration(sync_options_1_)); 1243 EXPECT_TRUE(GetRegistration(sync_options_1_));
1232 EXPECT_EQ(base::TimeDelta::FromSeconds(1), 1244 EXPECT_EQ(base::TimeDelta::FromSeconds(1),
1233 test_background_sync_manager_->delayed_task_delta()); 1245 test_background_sync_manager_->delayed_task_delta());
1234 1246
1235 // Fire one second later and it should fail permanently. 1247 // Fire one second later and it should fail permanently.
1236 test_clock_->Advance(base::TimeDelta::FromSeconds(1)); 1248 test_clock_->Advance(base::TimeDelta::FromSeconds(1));
1237 test_background_sync_manager_->delayed_task().Run(); 1249 test_background_sync_manager_->RunDelayedTask();
1238 base::RunLoop().RunUntilIdle(); 1250 base::RunLoop().RunUntilIdle();
1239 EXPECT_FALSE(GetRegistration(sync_options_1_)); 1251 EXPECT_FALSE(GetRegistration(sync_options_1_));
1240 } 1252 }
1241 1253
1242 TEST_F(BackgroundSyncManagerTest, RetryOnBrowserRestart) { 1254 TEST_F(BackgroundSyncManagerTest, RetryOnBrowserRestart) {
1243 SetMaxSyncAttemptsAndRestartManager(2); 1255 SetMaxSyncAttemptsAndRestartManager(2);
1244 InitFailedSyncEventTest(); 1256 InitFailedSyncEventTest();
1245 1257
1246 // The first run will fail but it will setup a timer to try again. 1258 // The first run will fail but it will setup a timer to try again.
1247 EXPECT_TRUE(Register(sync_options_1_)); 1259 EXPECT_TRUE(Register(sync_options_1_));
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
1296 1308
1297 TEST_F(BackgroundSyncManagerTest, AllTestsEventuallyFire) { 1309 TEST_F(BackgroundSyncManagerTest, AllTestsEventuallyFire) {
1298 SetMaxSyncAttemptsAndRestartManager(3); 1310 SetMaxSyncAttemptsAndRestartManager(3);
1299 InitFailedSyncEventTest(); 1311 InitFailedSyncEventTest();
1300 1312
1301 // The first run will fail but it will setup a timer to try again. 1313 // The first run will fail but it will setup a timer to try again.
1302 EXPECT_TRUE(Register(sync_options_1_)); 1314 EXPECT_TRUE(Register(sync_options_1_));
1303 1315
1304 // Run it a second time. 1316 // Run it a second time.
1305 test_clock_->Advance(test_background_sync_manager_->delayed_task_delta()); 1317 test_clock_->Advance(test_background_sync_manager_->delayed_task_delta());
1306 test_background_sync_manager_->delayed_task().Run(); 1318 test_background_sync_manager_->RunDelayedTask();
1307 base::RunLoop().RunUntilIdle(); 1319 base::RunLoop().RunUntilIdle();
1308 1320
1309 base::TimeDelta delay_delta = 1321 base::TimeDelta delay_delta =
1310 test_background_sync_manager_->delayed_task_delta(); 1322 test_background_sync_manager_->delayed_task_delta();
1311 1323
1312 // Create a second registration, which will fail and setup a timer. 1324 // Create a second registration, which will fail and setup a timer.
1313 EXPECT_TRUE(Register(sync_options_2_)); 1325 EXPECT_TRUE(Register(sync_options_2_));
1314 EXPECT_GT(delay_delta, test_background_sync_manager_->delayed_task_delta()); 1326 EXPECT_GT(delay_delta, test_background_sync_manager_->delayed_task_delta());
1315 1327
1316 while (!test_background_sync_manager_->delayed_task().is_null()) { 1328 while (test_background_sync_manager_->IsDelayedTaskScheduled()) {
1317 test_clock_->Advance(test_background_sync_manager_->delayed_task_delta()); 1329 test_clock_->Advance(test_background_sync_manager_->delayed_task_delta());
1318 test_background_sync_manager_->delayed_task().Run(); 1330 test_background_sync_manager_->RunDelayedTask();
1319 test_background_sync_manager_->ClearDelayedTask(); 1331 EXPECT_FALSE(test_background_sync_manager_->IsDelayedTaskScheduled());
1320 base::RunLoop().RunUntilIdle(); 1332 base::RunLoop().RunUntilIdle();
1321 } 1333 }
1322 1334
1323 EXPECT_FALSE(GetRegistration(sync_options_1_)); 1335 EXPECT_FALSE(GetRegistration(sync_options_1_));
1324 EXPECT_FALSE(GetRegistration(sync_options_2_)); 1336 EXPECT_FALSE(GetRegistration(sync_options_2_));
1325 } 1337 }
1326 1338
1327 TEST_F(BackgroundSyncManagerTest, LastChance) { 1339 TEST_F(BackgroundSyncManagerTest, LastChance) {
1328 SetMaxSyncAttemptsAndRestartManager(2); 1340 SetMaxSyncAttemptsAndRestartManager(2);
1329 InitFailedSyncEventTest(); 1341 InitFailedSyncEventTest();
1330 1342
1331 EXPECT_TRUE(Register(sync_options_1_)); 1343 EXPECT_TRUE(Register(sync_options_1_));
1332 EXPECT_EQ(blink::mojom::BackgroundSyncEventLastChance::IS_NOT_LAST_CHANCE, 1344 EXPECT_EQ(blink::mojom::BackgroundSyncEventLastChance::IS_NOT_LAST_CHANCE,
1333 test_background_sync_manager_->last_chance()); 1345 test_background_sync_manager_->last_chance());
1334 EXPECT_TRUE(GetRegistration(sync_options_1_)); 1346 EXPECT_TRUE(GetRegistration(sync_options_1_));
1335 1347
1336 // Run it again. 1348 // Run it again.
1337 test_clock_->Advance(test_background_sync_manager_->delayed_task_delta()); 1349 test_clock_->Advance(test_background_sync_manager_->delayed_task_delta());
1338 test_background_sync_manager_->delayed_task().Run(); 1350 test_background_sync_manager_->RunDelayedTask();
1339 base::RunLoop().RunUntilIdle(); 1351 base::RunLoop().RunUntilIdle();
1340 EXPECT_FALSE(GetRegistration(sync_options_1_)); 1352 EXPECT_FALSE(GetRegistration(sync_options_1_));
1341 EXPECT_EQ(blink::mojom::BackgroundSyncEventLastChance::IS_LAST_CHANCE, 1353 EXPECT_EQ(blink::mojom::BackgroundSyncEventLastChance::IS_LAST_CHANCE,
1342 test_background_sync_manager_->last_chance()); 1354 test_background_sync_manager_->last_chance());
1343 } 1355 }
1344 1356
1345 } // namespace content 1357 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698