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
3 // found in the LICENSE file.
5 #include "content/browser/background_sync/background_sync_manager.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/logging.h"
9 #include "base/power_monitor/power_monitor.h"
10 #include "base/power_monitor/power_monitor_source.h"
11 #include "base/run_loop.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "content/browser/browser_thread_impl.h"
14 #include "content/browser/service_worker/embedded_worker_test_helper.h"
15 #include "content/browser/service_worker/service_worker_context_core.h"
16 #include "content/browser/service_worker/service_worker_context_wrapper.h"
17 #include "content/browser/service_worker/service_worker_storage.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "net/base/network_change_notifier.h"
20 #include "testing/gtest/include/gtest/gtest.h"
26 const char kPattern1
[] = "https://example.com/a";
27 const char kPattern2
[] = "https://example.com/b";
28 const char kScript1
[] = "https://example.com/a/script.js";
29 const char kScript2
[] = "https://example.com/b/script.js";
30 const int kRenderProcessId
= 99;
32 void RegisterServiceWorkerCallback(bool* called
,
33 int64
* store_registration_id
,
34 ServiceWorkerStatusCode status
,
35 const std::string
& status_message
,
36 int64 registration_id
) {
37 EXPECT_EQ(SERVICE_WORKER_OK
, status
) << ServiceWorkerStatusToString(status
);
39 *store_registration_id
= registration_id
;
42 void FindServiceWorkerRegistrationCallback(
43 scoped_refptr
<ServiceWorkerRegistration
>* out_registration
,
44 ServiceWorkerStatusCode status
,
45 const scoped_refptr
<ServiceWorkerRegistration
>& registration
) {
46 EXPECT_EQ(SERVICE_WORKER_OK
, status
) << ServiceWorkerStatusToString(status
);
47 *out_registration
= registration
;
50 void UnregisterServiceWorkerCallback(bool* called
,
51 ServiceWorkerStatusCode code
) {
52 EXPECT_EQ(SERVICE_WORKER_OK
, code
);
56 void OneShotSuccessfulCallback(
58 const scoped_refptr
<ServiceWorkerVersion
>& active_version
,
59 const ServiceWorkerVersion::StatusCallback
& callback
) {
61 callback
.Run(SERVICE_WORKER_OK
);
64 void OneShotFailedCallback(
66 const scoped_refptr
<ServiceWorkerVersion
>& active_version
,
67 const ServiceWorkerVersion::StatusCallback
& callback
) {
69 callback
.Run(SERVICE_WORKER_ERROR_FAILED
);
72 void OneShotDelayedCallback(
74 ServiceWorkerVersion::StatusCallback
* out_callback
,
75 const scoped_refptr
<ServiceWorkerVersion
>& active_version
,
76 const ServiceWorkerVersion::StatusCallback
& callback
) {
78 *out_callback
= callback
;
81 class TestPowerSource
: public base::PowerMonitorSource
{
83 void GeneratePowerStateEvent(bool on_battery_power
) {
84 test_on_battery_power_
= on_battery_power
;
85 ProcessPowerEvent(POWER_STATE_EVENT
);
89 bool IsOnBatteryPowerImpl() final
{ return test_on_battery_power_
; }
90 bool test_on_battery_power_
= false;
95 // A BackgroundSyncManager that can simulate delaying and corrupting the backend
96 // storage and service worker onsync events.
97 class TestBackgroundSyncManager
: public BackgroundSyncManager
{
99 using OneShotCallback
=
100 base::Callback
<void(const scoped_refptr
<ServiceWorkerVersion
>&,
101 const ServiceWorkerVersion::StatusCallback
&)>;
103 explicit TestBackgroundSyncManager(
104 const scoped_refptr
<ServiceWorkerContextWrapper
>& service_worker_context
)
105 : BackgroundSyncManager(service_worker_context
) {}
107 void DoInit() { Init(); }
109 void StoreDataInBackendContinue(
110 int64 sw_registration_id
,
112 const std::string
& key
,
113 const std::string
& data
,
114 const ServiceWorkerStorage::StatusCallback
& callback
) {
115 BackgroundSyncManager::StoreDataInBackend(sw_registration_id
, origin
, key
,
119 void GetDataFromBackendContinue(
120 const std::string
& key
,
121 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback
&
123 BackgroundSyncManager::GetDataFromBackend(key
, callback
);
128 continuation_
.Reset();
131 void set_corrupt_backend(bool corrupt_backend
) {
132 corrupt_backend_
= corrupt_backend
;
134 void set_delay_backend(bool delay_backend
) { delay_backend_
= delay_backend
; }
135 void set_one_shot_callback(const OneShotCallback
& callback
) {
136 one_shot_callback_
= callback
;
140 void StoreDataInBackend(
141 int64 sw_registration_id
,
143 const std::string
& key
,
144 const std::string
& data
,
145 const ServiceWorkerStorage::StatusCallback
& callback
) override
{
146 EXPECT_TRUE(continuation_
.is_null());
147 if (corrupt_backend_
) {
148 base::MessageLoop::current()->PostTask(
149 FROM_HERE
, base::Bind(callback
, SERVICE_WORKER_ERROR_FAILED
));
153 base::Bind(&TestBackgroundSyncManager::StoreDataInBackendContinue
,
154 base::Unretained(this), sw_registration_id
, origin
, key
,
162 void GetDataFromBackend(
163 const std::string
& key
,
164 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback
&
166 EXPECT_TRUE(continuation_
.is_null());
167 if (corrupt_backend_
) {
168 base::MessageLoop::current()->PostTask(
170 base::Bind(callback
, std::vector
<std::pair
<int64
, std::string
>>(),
171 SERVICE_WORKER_ERROR_FAILED
));
175 base::Bind(&TestBackgroundSyncManager::GetDataFromBackendContinue
,
176 base::Unretained(this), key
, callback
);
183 void FireOneShotSync(
184 const scoped_refptr
<ServiceWorkerVersion
>& active_version
,
185 const ServiceWorkerVersion::StatusCallback
& callback
) override
{
186 if (one_shot_callback_
.is_null()) {
187 BackgroundSyncManager::FireOneShotSync(active_version
, callback
);
189 one_shot_callback_
.Run(active_version
, callback
);
194 bool corrupt_backend_
= false;
195 bool delay_backend_
= false;
196 base::Closure continuation_
;
197 OneShotCallback one_shot_callback_
;
200 class BackgroundSyncManagerTest
: public testing::Test
{
202 BackgroundSyncManagerTest()
203 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP
),
204 network_change_notifier_(net::NetworkChangeNotifier::CreateMock()),
205 test_background_sync_manager_(nullptr),
206 sync_reg_1_(BackgroundSyncManager::BackgroundSyncRegistration()),
207 sync_reg_2_(BackgroundSyncManager::BackgroundSyncRegistration()),
208 callback_error_(BackgroundSyncManager::ERROR_TYPE_OK
),
209 callback_sw_status_code_(SERVICE_WORKER_OK
),
210 sync_events_called_(0) {
211 sync_reg_1_
.tag
= "foo";
212 sync_reg_1_
.periodicity
= SYNC_ONE_SHOT
;
213 sync_reg_1_
.network_state
= NETWORK_STATE_ONLINE
;
214 sync_reg_1_
.power_state
= POWER_STATE_AUTO
;
216 sync_reg_2_
.tag
= "bar";
217 sync_reg_2_
.periodicity
= SYNC_ONE_SHOT
;
218 sync_reg_2_
.network_state
= NETWORK_STATE_ONLINE
;
219 sync_reg_2_
.power_state
= POWER_STATE_AUTO
;
222 void SetUp() override
{
223 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI
);
226 new EmbeddedWorkerTestHelper(base::FilePath(), kRenderProcessId
));
228 power_monitor_source_
= new TestPowerSource();
229 // power_monitor_ takes ownership of power_monitor_source.
230 power_monitor_
.reset(new base::PowerMonitor(
231 scoped_ptr
<base::PowerMonitorSource
>(power_monitor_source_
)));
233 SetOnBatteryPower(false);
235 background_sync_manager_
=
236 BackgroundSyncManager::Create(helper_
->context_wrapper());
238 // Wait for storage to finish initializing before registering service
240 base::RunLoop().RunUntilIdle();
241 RegisterServiceWorkers();
244 void RegisterServiceWorkers() {
245 bool called_1
= false;
246 bool called_2
= false;
247 helper_
->context()->RegisterServiceWorker(
248 GURL(kPattern1
), GURL(kScript1
), NULL
,
249 base::Bind(&RegisterServiceWorkerCallback
, &called_1
,
250 &sw_registration_id_1_
));
252 helper_
->context()->RegisterServiceWorker(
253 GURL(kPattern2
), GURL(kScript2
), NULL
,
254 base::Bind(&RegisterServiceWorkerCallback
, &called_2
,
255 &sw_registration_id_2_
));
256 base::RunLoop().RunUntilIdle();
257 EXPECT_TRUE(called_1
);
258 EXPECT_TRUE(called_2
);
260 // Hang onto the registrations as they need to be "live" when
261 // calling BackgroundSyncMasnager::Register.
262 helper_
->context_wrapper()->FindRegistrationForId(
263 sw_registration_id_1_
, GURL(kPattern1
).GetOrigin(),
264 base::Bind(FindServiceWorkerRegistrationCallback
, &sw_registration_1_
));
266 helper_
->context_wrapper()->FindRegistrationForId(
267 sw_registration_id_2_
, GURL(kPattern1
).GetOrigin(),
268 base::Bind(FindServiceWorkerRegistrationCallback
, &sw_registration_2_
));
269 base::RunLoop().RunUntilIdle();
270 EXPECT_TRUE(sw_registration_1_
);
271 EXPECT_TRUE(sw_registration_2_
);
274 void SetNetwork(net::NetworkChangeNotifier::ConnectionType connection_type
) {
275 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
277 base::RunLoop().RunUntilIdle();
280 void SetOnBatteryPower(bool on_battery_power
) {
281 power_monitor_source_
->GeneratePowerStateEvent(on_battery_power
);
282 base::RunLoop().RunUntilIdle();
285 void StatusAndRegistrationCallback(
287 BackgroundSyncManager::ErrorType error
,
288 const BackgroundSyncManager::BackgroundSyncRegistration
& registration
) {
290 callback_error_
= error
;
291 callback_registration_
= registration
;
294 void StatusAndRegistrationsCallback(
296 BackgroundSyncManager::ErrorType error
,
297 const std::vector
<BackgroundSyncManager::BackgroundSyncRegistration
>&
300 callback_error_
= error
;
301 callback_registrations_
= registrations
;
304 void StatusCallback(bool* was_called
,
305 BackgroundSyncManager::ErrorType error
) {
307 callback_error_
= error
;
311 void UseTestBackgroundSyncManager() {
312 test_background_sync_manager_
=
313 new TestBackgroundSyncManager(helper_
->context_wrapper());
314 test_background_sync_manager_
->DoInit();
315 background_sync_manager_
.reset(test_background_sync_manager_
);
318 void DeleteBackgroundSyncManager() {
319 background_sync_manager_
.reset();
320 test_background_sync_manager_
= nullptr;
323 bool Register(const BackgroundSyncManager::BackgroundSyncRegistration
&
325 return RegisterWithServiceWorkerId(sw_registration_id_1_
,
329 bool RegisterWithServiceWorkerId(
330 int64 sw_registration_id
,
331 const BackgroundSyncManager::BackgroundSyncRegistration
&
333 bool was_called
= false;
334 background_sync_manager_
->Register(
335 sw_registration_id
, sync_registration
,
336 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback
,
337 base::Unretained(this), &was_called
));
338 base::RunLoop().RunUntilIdle();
339 EXPECT_TRUE(was_called
);
340 return callback_error_
== BackgroundSyncManager::ERROR_TYPE_OK
;
343 bool Unregister(const BackgroundSyncManager::BackgroundSyncRegistration
&
345 return UnregisterWithServiceWorkerId(sw_registration_id_1_
,
349 bool UnregisterWithServiceWorkerId(
350 int64 sw_registration_id
,
351 const BackgroundSyncManager::BackgroundSyncRegistration
&
353 bool was_called
= false;
354 background_sync_manager_
->Unregister(
355 sw_registration_id
, sync_registration
.tag
,
356 sync_registration
.periodicity
, sync_registration
.id
,
357 base::Bind(&BackgroundSyncManagerTest::StatusCallback
,
358 base::Unretained(this), &was_called
));
359 base::RunLoop().RunUntilIdle();
360 EXPECT_TRUE(was_called
);
361 return callback_error_
== BackgroundSyncManager::ERROR_TYPE_OK
;
364 bool GetRegistration(const BackgroundSyncManager::BackgroundSyncRegistration
&
366 return GetRegistrationWithServiceWorkerId(sw_registration_id_1_
,
370 bool GetRegistrationWithServiceWorkerId(
371 int64 sw_registration_id
,
372 const BackgroundSyncManager::BackgroundSyncRegistration
&
374 bool was_called
= false;
375 background_sync_manager_
->GetRegistration(
376 sw_registration_id
, sync_registration
.tag
,
377 sync_registration
.periodicity
,
378 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback
,
379 base::Unretained(this), &was_called
));
380 base::RunLoop().RunUntilIdle();
381 EXPECT_TRUE(was_called
);
383 if (callback_error_
== BackgroundSyncManager::ERROR_TYPE_OK
) {
384 EXPECT_STREQ(sync_registration
.tag
.c_str(),
385 callback_registration_
.tag
.c_str());
388 return callback_error_
== BackgroundSyncManager::ERROR_TYPE_OK
;
391 bool GetRegistrations(SyncPeriodicity periodicity
) {
392 return GetRegistrationWithServiceWorkerId(sw_registration_id_1_
,
396 bool GetRegistrationWithServiceWorkerId(int64 sw_registration_id
,
397 SyncPeriodicity periodicity
) {
398 bool was_called
= false;
399 background_sync_manager_
->GetRegistrations(
400 sw_registration_id
, periodicity
,
401 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationsCallback
,
402 base::Unretained(this), &was_called
));
403 base::RunLoop().RunUntilIdle();
404 EXPECT_TRUE(was_called
);
406 return callback_error_
== BackgroundSyncManager::ERROR_TYPE_OK
;
409 void StorageRegistrationCallback(ServiceWorkerStatusCode result
) {
410 callback_sw_status_code_
= result
;
413 void UnregisterServiceWorker(uint64 sw_registration_id
) {
415 helper_
->context()->UnregisterServiceWorker(
416 PatternForSWId(sw_registration_id
),
417 base::Bind(&UnregisterServiceWorkerCallback
, &called
));
418 base::RunLoop().RunUntilIdle();
422 GURL
PatternForSWId(int64 sw_id
) {
423 EXPECT_TRUE(sw_id
== sw_registration_id_1_
||
424 sw_id
== sw_registration_id_2_
);
425 return sw_id
== sw_registration_id_1_
? GURL(kPattern1
) : GURL(kPattern2
);
428 void InitSyncEventTest() {
429 UseTestBackgroundSyncManager();
430 test_background_sync_manager_
->set_one_shot_callback(
431 base::Bind(OneShotSuccessfulCallback
, &sync_events_called_
));
432 base::RunLoop().RunUntilIdle();
435 void InitFailedSyncEventTest() {
436 UseTestBackgroundSyncManager();
437 test_background_sync_manager_
->set_one_shot_callback(
438 base::Bind(OneShotFailedCallback
, &sync_events_called_
));
439 base::RunLoop().RunUntilIdle();
442 void InitDelayedSyncEventTest() {
443 UseTestBackgroundSyncManager();
444 test_background_sync_manager_
->set_one_shot_callback(base::Bind(
445 OneShotDelayedCallback
, &sync_events_called_
, &sync_fired_callback_
));
446 base::RunLoop().RunUntilIdle();
449 void RegisterAndVerifySyncEventDelayed(
450 const BackgroundSyncManager::BackgroundSyncRegistration
&
452 int sync_events_called
= sync_events_called_
;
453 EXPECT_TRUE(sync_fired_callback_
.is_null());
455 EXPECT_TRUE(Register(sync_registration
));
457 EXPECT_EQ(sync_events_called
+ 1, sync_events_called_
);
458 EXPECT_TRUE(GetRegistration(sync_reg_1_
));
459 EXPECT_FALSE(sync_fired_callback_
.is_null());
462 TestBrowserThreadBundle browser_thread_bundle_
;
463 scoped_ptr
<net::NetworkChangeNotifier
> network_change_notifier_
;
464 TestPowerSource
* power_monitor_source_
; // owned by power_monitor_
465 scoped_ptr
<base::PowerMonitor
> power_monitor_
;
466 scoped_ptr
<EmbeddedWorkerTestHelper
> helper_
;
467 scoped_ptr
<BackgroundSyncManager
> background_sync_manager_
;
468 TestBackgroundSyncManager
* test_background_sync_manager_
;
470 int64 sw_registration_id_1_
;
471 int64 sw_registration_id_2_
;
472 scoped_refptr
<ServiceWorkerRegistration
> sw_registration_1_
;
473 scoped_refptr
<ServiceWorkerRegistration
> sw_registration_2_
;
475 BackgroundSyncManager::BackgroundSyncRegistration sync_reg_1_
;
476 BackgroundSyncManager::BackgroundSyncRegistration sync_reg_2_
;
479 BackgroundSyncManager::ErrorType callback_error_
;
480 BackgroundSyncManager::BackgroundSyncRegistration callback_registration_
;
481 std::vector
<BackgroundSyncManager::BackgroundSyncRegistration
>
482 callback_registrations_
;
483 ServiceWorkerStatusCode callback_sw_status_code_
;
484 int sync_events_called_
;
485 ServiceWorkerVersion::StatusCallback sync_fired_callback_
;
488 TEST_F(BackgroundSyncManagerTest
, Register
) {
489 EXPECT_TRUE(Register(sync_reg_1_
));
492 TEST_F(BackgroundSyncManagerTest
, RegistractionIntact
) {
493 EXPECT_TRUE(Register(sync_reg_1_
));
494 EXPECT_STREQ(sync_reg_1_
.tag
.c_str(), callback_registration_
.tag
.c_str());
496 BackgroundSyncManager::BackgroundSyncRegistration::kInvalidRegistrationId
,
497 callback_registration_
.id
);
500 TEST_F(BackgroundSyncManagerTest
, RegisterWithoutLiveSWRegistration
) {
501 sw_registration_1_
= nullptr;
502 EXPECT_FALSE(Register(sync_reg_1_
));
503 EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_NO_SERVICE_WORKER
,
507 TEST_F(BackgroundSyncManagerTest
, RegisterWithoutActiveSWRegistration
) {
508 sw_registration_1_
->UnsetVersion(sw_registration_1_
->active_version());
509 EXPECT_FALSE(Register(sync_reg_1_
));
510 EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_NO_SERVICE_WORKER
,
514 TEST_F(BackgroundSyncManagerTest
, RegisterExistingKeepsId
) {
515 EXPECT_TRUE(Register(sync_reg_1_
));
516 BackgroundSyncManager::BackgroundSyncRegistration first_registration
=
517 callback_registration_
;
518 EXPECT_TRUE(Register(sync_reg_1_
));
519 EXPECT_TRUE(callback_registration_
.Equals(first_registration
));
520 EXPECT_EQ(first_registration
.id
, callback_registration_
.id
);
523 TEST_F(BackgroundSyncManagerTest
, RegisterOverwrites
) {
524 EXPECT_TRUE(Register(sync_reg_1_
));
525 BackgroundSyncManager::BackgroundSyncRegistration first_registration
=
526 callback_registration_
;
528 sync_reg_1_
.min_period
= 100;
529 EXPECT_TRUE(Register(sync_reg_1_
));
530 EXPECT_LT(first_registration
.id
, callback_registration_
.id
);
531 EXPECT_FALSE(callback_registration_
.Equals(first_registration
));
534 TEST_F(BackgroundSyncManagerTest
, RegisterOverlappingPeriodicAndOneShotTags
) {
535 // Registrations with the same tags but different periodicities should not
537 sync_reg_1_
.tag
= "";
538 sync_reg_2_
.tag
= "";
539 sync_reg_1_
.periodicity
= SYNC_PERIODIC
;
540 sync_reg_2_
.periodicity
= SYNC_ONE_SHOT
;
541 EXPECT_TRUE(Register(sync_reg_1_
));
542 EXPECT_TRUE(Register(sync_reg_2_
));
543 EXPECT_TRUE(GetRegistration(sync_reg_1_
));
544 EXPECT_EQ(SYNC_PERIODIC
, callback_registration_
.periodicity
);
545 EXPECT_TRUE(GetRegistration(sync_reg_2_
));
546 EXPECT_EQ(SYNC_ONE_SHOT
, callback_registration_
.periodicity
);
549 TEST_F(BackgroundSyncManagerTest
, RegisterBadBackend
) {
550 UseTestBackgroundSyncManager();
551 test_background_sync_manager_
->set_corrupt_backend(true);
552 EXPECT_FALSE(Register(sync_reg_1_
));
553 test_background_sync_manager_
->set_corrupt_backend(false);
554 EXPECT_FALSE(Register(sync_reg_1_
));
555 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
558 TEST_F(BackgroundSyncManagerTest
, TwoRegistrations
) {
559 EXPECT_TRUE(Register(sync_reg_1_
));
560 EXPECT_TRUE(Register(sync_reg_2_
));
563 TEST_F(BackgroundSyncManagerTest
, GetRegistrationNonExisting
) {
564 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
567 TEST_F(BackgroundSyncManagerTest
, GetRegistrationExisting
) {
568 EXPECT_TRUE(Register(sync_reg_1_
));
569 EXPECT_TRUE(GetRegistration(sync_reg_1_
));
570 EXPECT_FALSE(GetRegistration(sync_reg_2_
));
573 TEST_F(BackgroundSyncManagerTest
, GetRegistrationBadBackend
) {
574 UseTestBackgroundSyncManager();
575 EXPECT_TRUE(Register(sync_reg_1_
));
576 test_background_sync_manager_
->set_corrupt_backend(true);
577 EXPECT_TRUE(GetRegistration(sync_reg_1_
));
578 EXPECT_FALSE(Register(sync_reg_2_
));
579 // Registration should have discovered the bad backend and disabled the
580 // BackgroundSyncManager.
581 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
582 test_background_sync_manager_
->set_corrupt_backend(false);
583 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
586 TEST_F(BackgroundSyncManagerTest
, GetRegistrationsZero
) {
587 EXPECT_TRUE(GetRegistrations(SYNC_ONE_SHOT
));
588 EXPECT_EQ(0u, callback_registrations_
.size());
591 TEST_F(BackgroundSyncManagerTest
, GetRegistrationsOne
) {
592 EXPECT_TRUE(Register(sync_reg_1_
));
593 EXPECT_TRUE(GetRegistrations(sync_reg_1_
.periodicity
));
595 EXPECT_EQ(1u, callback_registrations_
.size());
596 sync_reg_1_
.Equals(callback_registrations_
[0]);
599 TEST_F(BackgroundSyncManagerTest
, GetRegistrationsTwo
) {
600 EXPECT_EQ(sync_reg_1_
.periodicity
, sync_reg_2_
.periodicity
);
602 EXPECT_TRUE(Register(sync_reg_1_
));
603 EXPECT_TRUE(Register(sync_reg_2_
));
604 EXPECT_TRUE(GetRegistrations(sync_reg_1_
.periodicity
));
606 EXPECT_EQ(2u, callback_registrations_
.size());
607 sync_reg_1_
.Equals(callback_registrations_
[0]);
608 sync_reg_2_
.Equals(callback_registrations_
[1]);
611 TEST_F(BackgroundSyncManagerTest
, GetRegistrationsPeriodicity
) {
612 sync_reg_1_
.periodicity
= SYNC_ONE_SHOT
;
613 sync_reg_2_
.periodicity
= SYNC_PERIODIC
;
614 EXPECT_TRUE(Register(sync_reg_1_
));
615 EXPECT_TRUE(Register(sync_reg_2_
));
617 EXPECT_TRUE(GetRegistrations(SYNC_ONE_SHOT
));
618 EXPECT_EQ(1u, callback_registrations_
.size());
619 sync_reg_1_
.Equals(callback_registrations_
[0]);
621 EXPECT_TRUE(GetRegistrations(SYNC_PERIODIC
));
622 EXPECT_EQ(1u, callback_registrations_
.size());
623 sync_reg_2_
.Equals(callback_registrations_
[0]);
626 TEST_F(BackgroundSyncManagerTest
, GetRegistrationsBadBackend
) {
627 UseTestBackgroundSyncManager();
628 EXPECT_TRUE(Register(sync_reg_1_
));
629 test_background_sync_manager_
->set_corrupt_backend(true);
630 EXPECT_TRUE(GetRegistrations(sync_reg_1_
.periodicity
));
631 EXPECT_FALSE(Register(sync_reg_2_
));
632 // Registration should have discovered the bad backend and disabled the
633 // BackgroundSyncManager.
634 EXPECT_FALSE(GetRegistrations(sync_reg_1_
.periodicity
));
635 test_background_sync_manager_
->set_corrupt_backend(false);
636 EXPECT_FALSE(GetRegistrations(sync_reg_1_
.periodicity
));
639 TEST_F(BackgroundSyncManagerTest
, Unregister
) {
640 EXPECT_TRUE(Register(sync_reg_1_
));
641 EXPECT_TRUE(Unregister(callback_registration_
));
642 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
645 TEST_F(BackgroundSyncManagerTest
, UnregisterWrongId
) {
646 EXPECT_TRUE(Register(sync_reg_1_
));
647 callback_registration_
.id
+= 1;
648 EXPECT_FALSE(Unregister(callback_registration_
));
651 TEST_F(BackgroundSyncManagerTest
, Reregister
) {
652 EXPECT_TRUE(Register(sync_reg_1_
));
653 EXPECT_TRUE(Unregister(callback_registration_
));
654 EXPECT_TRUE(Register(sync_reg_1_
));
657 TEST_F(BackgroundSyncManagerTest
, UnregisterNonExisting
) {
658 EXPECT_FALSE(Unregister(sync_reg_1_
));
659 EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_NOT_FOUND
, callback_error_
);
662 TEST_F(BackgroundSyncManagerTest
, UnregisterSecond
) {
663 EXPECT_TRUE(Register(sync_reg_1_
));
664 EXPECT_TRUE(Register(sync_reg_2_
));
665 EXPECT_TRUE(Unregister(callback_registration_
));
666 EXPECT_TRUE(GetRegistration(sync_reg_1_
));
667 EXPECT_TRUE(Register(sync_reg_2_
));
670 TEST_F(BackgroundSyncManagerTest
, UnregisterBadBackend
) {
671 UseTestBackgroundSyncManager();
672 sync_reg_1_
.min_period
+= 1;
673 EXPECT_TRUE(Register(sync_reg_1_
));
674 EXPECT_TRUE(Register(sync_reg_2_
));
675 test_background_sync_manager_
->set_corrupt_backend(true);
676 EXPECT_FALSE(Unregister(callback_registration_
));
677 // Unregister should have discovered the bad backend and disabled the
678 // BackgroundSyncManager.
679 test_background_sync_manager_
->set_corrupt_backend(false);
680 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
681 EXPECT_FALSE(GetRegistration(sync_reg_2_
));
684 TEST_F(BackgroundSyncManagerTest
, RegistrationIncreasesId
) {
685 EXPECT_TRUE(Register(sync_reg_1_
));
686 BackgroundSyncManager::BackgroundSyncRegistration registered_sync
=
687 callback_registration_
;
688 BackgroundSyncManager::BackgroundSyncRegistration::RegistrationId cur_id
=
689 callback_registration_
.id
;
691 EXPECT_TRUE(GetRegistration(sync_reg_1_
));
692 EXPECT_TRUE(Register(sync_reg_2_
));
693 EXPECT_LT(cur_id
, callback_registration_
.id
);
694 cur_id
= callback_registration_
.id
;
696 EXPECT_TRUE(Unregister(registered_sync
));
697 EXPECT_TRUE(Register(sync_reg_1_
));
698 EXPECT_LT(cur_id
, callback_registration_
.id
);
701 TEST_F(BackgroundSyncManagerTest
, RebootRecovery
) {
702 EXPECT_TRUE(Register(sync_reg_1_
));
704 background_sync_manager_
=
705 BackgroundSyncManager::Create(helper_
->context_wrapper());
707 EXPECT_TRUE(GetRegistration(sync_reg_1_
));
708 EXPECT_FALSE(GetRegistration(sync_reg_2_
));
711 TEST_F(BackgroundSyncManagerTest
, RebootRecoveryTwoServiceWorkers
) {
712 EXPECT_TRUE(RegisterWithServiceWorkerId(sw_registration_id_1_
, sync_reg_1_
));
713 EXPECT_TRUE(RegisterWithServiceWorkerId(sw_registration_id_2_
, sync_reg_2_
));
715 background_sync_manager_
=
716 BackgroundSyncManager::Create(helper_
->context_wrapper());
719 GetRegistrationWithServiceWorkerId(sw_registration_id_1_
, sync_reg_1_
));
721 GetRegistrationWithServiceWorkerId(sw_registration_id_1_
, sync_reg_2_
));
723 GetRegistrationWithServiceWorkerId(sw_registration_id_2_
, sync_reg_1_
));
725 GetRegistrationWithServiceWorkerId(sw_registration_id_2_
, sync_reg_2_
));
728 GetRegistrationWithServiceWorkerId(sw_registration_id_1_
, sync_reg_1_
));
730 GetRegistrationWithServiceWorkerId(sw_registration_id_2_
, sync_reg_2_
));
732 EXPECT_TRUE(RegisterWithServiceWorkerId(sw_registration_id_1_
, sync_reg_2_
));
733 EXPECT_TRUE(RegisterWithServiceWorkerId(sw_registration_id_2_
, sync_reg_1_
));
736 TEST_F(BackgroundSyncManagerTest
, InitWithBadBackend
) {
737 DeleteBackgroundSyncManager();
738 test_background_sync_manager_
=
739 new TestBackgroundSyncManager(helper_
->context_wrapper());
740 background_sync_manager_
.reset(test_background_sync_manager_
);
741 test_background_sync_manager_
->set_corrupt_backend(true);
742 test_background_sync_manager_
->DoInit();
744 EXPECT_FALSE(Register(sync_reg_1_
));
745 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
748 TEST_F(BackgroundSyncManagerTest
, SequentialOperations
) {
749 // Schedule Init and all of the operations on a delayed backend. Verify that
750 // the operations complete sequentially.
751 DeleteBackgroundSyncManager();
753 test_background_sync_manager_
=
754 new TestBackgroundSyncManager(helper_
->context_wrapper());
755 background_sync_manager_
.reset(test_background_sync_manager_
);
757 test_background_sync_manager_
->set_delay_backend(true);
758 test_background_sync_manager_
->DoInit();
760 const int64 kExpectedInitialId
=
761 BackgroundSyncManager::BackgroundSyncRegistration::kInitialId
;
763 bool register_called
= false;
764 bool unregister_called
= false;
765 bool get_registration_called
= false;
766 test_background_sync_manager_
->Register(
767 sw_registration_id_1_
, sync_reg_1_
,
768 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback
,
769 base::Unretained(this), ®ister_called
));
770 test_background_sync_manager_
->Unregister(
771 sw_registration_id_1_
, sync_reg_1_
.tag
, sync_reg_1_
.periodicity
,
773 base::Bind(&BackgroundSyncManagerTest::StatusCallback
,
774 base::Unretained(this), &unregister_called
));
775 test_background_sync_manager_
->GetRegistration(
776 sw_registration_id_1_
, sync_reg_1_
.tag
, sync_reg_1_
.periodicity
,
777 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback
,
778 base::Unretained(this), &get_registration_called
));
780 base::RunLoop().RunUntilIdle();
781 // Init should be blocked while loading from the backend.
782 EXPECT_FALSE(register_called
);
783 EXPECT_FALSE(unregister_called
);
784 EXPECT_FALSE(get_registration_called
);
786 test_background_sync_manager_
->Continue();
787 base::RunLoop().RunUntilIdle();
788 // Register should be blocked while storing to the backend.
789 EXPECT_FALSE(register_called
);
790 EXPECT_FALSE(unregister_called
);
791 EXPECT_FALSE(get_registration_called
);
793 test_background_sync_manager_
->Continue();
794 base::RunLoop().RunUntilIdle();
795 EXPECT_TRUE(register_called
);
796 EXPECT_EQ(kExpectedInitialId
, callback_registration_
.id
);
797 EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_OK
, callback_error_
);
798 // Unregister should be blocked while storing to the backend.
799 EXPECT_FALSE(unregister_called
);
800 EXPECT_FALSE(get_registration_called
);
802 test_background_sync_manager_
->Continue();
803 base::RunLoop().RunUntilIdle();
804 // Unregister should be done and since GetRegistration doesn't require the
805 // backend it should be done too.
806 EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_NOT_FOUND
, callback_error_
);
807 EXPECT_TRUE(unregister_called
);
808 EXPECT_TRUE(get_registration_called
);
811 TEST_F(BackgroundSyncManagerTest
, UnregisterServiceWorker
) {
812 EXPECT_TRUE(Register(sync_reg_1_
));
813 UnregisterServiceWorker(sw_registration_id_1_
);
814 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
817 TEST_F(BackgroundSyncManagerTest
,
818 UnregisterServiceWorkerDuringSyncRegistration
) {
819 UseTestBackgroundSyncManager();
821 EXPECT_TRUE(Register(sync_reg_1_
));
823 test_background_sync_manager_
->set_delay_backend(true);
824 bool callback_called
= false;
825 test_background_sync_manager_
->Register(
826 sw_registration_id_1_
, sync_reg_2_
,
827 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback
,
828 base::Unretained(this), &callback_called
));
830 base::RunLoop().RunUntilIdle();
831 EXPECT_FALSE(callback_called
);
832 UnregisterServiceWorker(sw_registration_id_1_
);
834 test_background_sync_manager_
->Continue();
835 base::RunLoop().RunUntilIdle();
836 EXPECT_TRUE(callback_called
);
837 EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_STORAGE
, callback_error_
);
839 test_background_sync_manager_
->set_delay_backend(false);
840 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
843 TEST_F(BackgroundSyncManagerTest
, DeleteAndStartOverServiceWorkerContext
) {
844 EXPECT_TRUE(Register(sync_reg_1_
));
845 helper_
->context()->ScheduleDeleteAndStartOver();
846 base::RunLoop().RunUntilIdle();
847 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
850 TEST_F(BackgroundSyncManagerTest
, DisabledManagerWorksAfterBrowserRestart
) {
851 UseTestBackgroundSyncManager();
852 EXPECT_TRUE(Register(sync_reg_1_
));
853 test_background_sync_manager_
->set_corrupt_backend(true);
854 EXPECT_FALSE(Register(sync_reg_2_
));
856 // The manager is now disabled and not accepting new requests until browser
857 // restart or notification that the storage has been wiped.
858 test_background_sync_manager_
->set_corrupt_backend(false);
859 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
860 EXPECT_FALSE(Register(sync_reg_2_
));
862 // Simulate restarting the browser by creating a new BackgroundSyncManager.
863 UseTestBackgroundSyncManager();
864 EXPECT_TRUE(GetRegistration(sync_reg_1_
));
865 EXPECT_TRUE(Register(sync_reg_2_
));
868 TEST_F(BackgroundSyncManagerTest
, DisabledManagerWorksAfterDeleteAndStartOver
) {
869 UseTestBackgroundSyncManager();
870 EXPECT_TRUE(Register(sync_reg_1_
));
871 test_background_sync_manager_
->set_corrupt_backend(true);
872 EXPECT_FALSE(Register(sync_reg_2_
));
874 // The manager is now disabled and not accepting new requests until browser
875 // restart or notification that the storage has been wiped.
876 test_background_sync_manager_
->set_corrupt_backend(false);
877 helper_
->context()->ScheduleDeleteAndStartOver();
878 base::RunLoop().RunUntilIdle();
880 RegisterServiceWorkers();
882 EXPECT_TRUE(Register(sync_reg_2_
));
883 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
884 EXPECT_TRUE(GetRegistration(sync_reg_2_
));
887 TEST_F(BackgroundSyncManagerTest
, RegistrationEqualsId
) {
888 BackgroundSyncManager::BackgroundSyncRegistration reg_1
;
889 BackgroundSyncManager::BackgroundSyncRegistration reg_2
;
891 EXPECT_TRUE(reg_1
.Equals(reg_2
));
892 reg_2
.id
= reg_1
.id
+ 1;
893 EXPECT_TRUE(reg_1
.Equals(reg_2
));
896 TEST_F(BackgroundSyncManagerTest
, RegistrationEqualsTag
) {
897 BackgroundSyncManager::BackgroundSyncRegistration reg_1
;
898 BackgroundSyncManager::BackgroundSyncRegistration reg_2
;
899 EXPECT_TRUE(reg_1
.Equals(reg_2
));
901 EXPECT_FALSE(reg_1
.Equals(reg_2
));
904 TEST_F(BackgroundSyncManagerTest
, RegistrationEqualsPeriodicity
) {
905 BackgroundSyncManager::BackgroundSyncRegistration reg_1
;
906 BackgroundSyncManager::BackgroundSyncRegistration reg_2
;
907 EXPECT_TRUE(reg_1
.Equals(reg_2
));
908 reg_1
.periodicity
= SYNC_PERIODIC
;
909 reg_2
.periodicity
= SYNC_ONE_SHOT
;
910 EXPECT_FALSE(reg_1
.Equals(reg_2
));
913 TEST_F(BackgroundSyncManagerTest
, RegistrationEqualsMinPeriod
) {
914 BackgroundSyncManager::BackgroundSyncRegistration reg_1
;
915 BackgroundSyncManager::BackgroundSyncRegistration reg_2
;
916 EXPECT_TRUE(reg_1
.Equals(reg_2
));
917 reg_2
.min_period
= reg_1
.min_period
+ 1;
918 EXPECT_FALSE(reg_1
.Equals(reg_2
));
921 TEST_F(BackgroundSyncManagerTest
, RegistrationEqualsNetworkState
) {
922 BackgroundSyncManager::BackgroundSyncRegistration reg_1
;
923 BackgroundSyncManager::BackgroundSyncRegistration reg_2
;
924 EXPECT_TRUE(reg_1
.Equals(reg_2
));
925 reg_1
.network_state
= NETWORK_STATE_ANY
;
926 reg_2
.network_state
= NETWORK_STATE_ONLINE
;
927 EXPECT_FALSE(reg_1
.Equals(reg_2
));
930 TEST_F(BackgroundSyncManagerTest
, RegistrationEqualsPowerState
) {
931 BackgroundSyncManager::BackgroundSyncRegistration reg_1
;
932 BackgroundSyncManager::BackgroundSyncRegistration reg_2
;
933 EXPECT_TRUE(reg_1
.Equals(reg_2
));
934 reg_1
.power_state
= POWER_STATE_AUTO
;
935 reg_2
.power_state
= POWER_STATE_AVOID_DRAINING
;
936 EXPECT_FALSE(reg_1
.Equals(reg_2
));
939 TEST_F(BackgroundSyncManagerTest
, StoreAndRetrievePreservesValues
) {
940 BackgroundSyncManager::BackgroundSyncRegistration reg_1
;
941 // Set non-default values for each field.
943 EXPECT_NE(SYNC_PERIODIC
, reg_1
.periodicity
);
944 reg_1
.periodicity
= SYNC_PERIODIC
;
945 reg_1
.min_period
+= 1;
946 EXPECT_NE(NETWORK_STATE_ANY
, reg_1
.network_state
);
947 reg_1
.network_state
= NETWORK_STATE_ANY
;
948 EXPECT_NE(POWER_STATE_AUTO
, reg_1
.power_state
);
949 reg_1
.power_state
= POWER_STATE_AUTO
;
951 // Store the registration.
952 EXPECT_TRUE(Register(reg_1
));
954 // Simulate restarting the sync manager, forcing the next read to come from
956 UseTestBackgroundSyncManager();
958 EXPECT_TRUE(GetRegistration(reg_1
));
959 EXPECT_TRUE(reg_1
.Equals(callback_registration_
));
962 TEST_F(BackgroundSyncManagerTest
, EmptyTagSupported
) {
963 sync_reg_1_
.tag
= "a";
964 EXPECT_TRUE(Register(sync_reg_1_
));
965 EXPECT_TRUE(GetRegistration(sync_reg_1_
));
966 EXPECT_TRUE(sync_reg_1_
.Equals(callback_registration_
));
967 EXPECT_TRUE(Unregister(callback_registration_
));
968 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
971 TEST_F(BackgroundSyncManagerTest
, OverlappingPeriodicAndOneShotTags
) {
972 // Registrations with the same tags but different periodicities should not
974 sync_reg_1_
.tag
= "";
975 sync_reg_2_
.tag
= "";
976 sync_reg_1_
.periodicity
= SYNC_PERIODIC
;
977 sync_reg_2_
.periodicity
= SYNC_ONE_SHOT
;
979 EXPECT_TRUE(Register(sync_reg_1_
));
980 EXPECT_TRUE(Register(sync_reg_2_
));
982 EXPECT_TRUE(GetRegistration(sync_reg_1_
));
983 EXPECT_EQ(SYNC_PERIODIC
, callback_registration_
.periodicity
);
984 EXPECT_TRUE(GetRegistration(sync_reg_2_
));
985 EXPECT_EQ(SYNC_ONE_SHOT
, callback_registration_
.periodicity
);
987 EXPECT_TRUE(GetRegistration(sync_reg_1_
));
988 EXPECT_TRUE(Unregister(callback_registration_
));
989 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
990 EXPECT_TRUE(GetRegistration(sync_reg_2_
));
991 EXPECT_EQ(SYNC_ONE_SHOT
, callback_registration_
.periodicity
);
993 EXPECT_TRUE(Unregister(callback_registration_
));
994 EXPECT_FALSE(GetRegistration(sync_reg_2_
));
997 TEST_F(BackgroundSyncManagerTest
, OneShotFiresOnRegistration
) {
1000 EXPECT_TRUE(Register(sync_reg_1_
));
1001 EXPECT_EQ(1, sync_events_called_
);
1002 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
1005 // TODO(jkarlin): Change this to a periodic test as one-shots can't be power
1006 // dependent according to spec.
1007 TEST_F(BackgroundSyncManagerTest
, OneShotFiresOnPowerChange
) {
1008 InitSyncEventTest();
1009 sync_reg_1_
.power_state
= POWER_STATE_AVOID_DRAINING
;
1011 SetOnBatteryPower(true);
1012 EXPECT_TRUE(Register(sync_reg_1_
));
1013 EXPECT_EQ(0, sync_events_called_
);
1014 EXPECT_TRUE(GetRegistration(sync_reg_1_
));
1016 SetOnBatteryPower(false);
1017 EXPECT_EQ(1, sync_events_called_
);
1018 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
1021 // TODO(jkarlin): Change this to a periodic test as one-shots can't be power
1022 // dependent according to spec.
1023 TEST_F(BackgroundSyncManagerTest
, MultipleOneShotsFireOnPowerChange
) {
1024 InitSyncEventTest();
1025 sync_reg_1_
.power_state
= POWER_STATE_AVOID_DRAINING
;
1026 sync_reg_2_
.power_state
= POWER_STATE_AVOID_DRAINING
;
1028 SetOnBatteryPower(true);
1029 EXPECT_TRUE(Register(sync_reg_1_
));
1030 EXPECT_TRUE(Register(sync_reg_2_
));
1031 EXPECT_EQ(0, sync_events_called_
);
1032 EXPECT_TRUE(GetRegistration(sync_reg_1_
));
1033 EXPECT_TRUE(GetRegistration(sync_reg_2_
));
1035 SetOnBatteryPower(false);
1036 EXPECT_EQ(2, sync_events_called_
);
1037 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
1038 EXPECT_FALSE(GetRegistration(sync_reg_2_
));
1041 TEST_F(BackgroundSyncManagerTest
, OneShotFiresOnNetworkChange
) {
1042 InitSyncEventTest();
1044 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE
);
1045 EXPECT_TRUE(Register(sync_reg_1_
));
1046 EXPECT_EQ(0, sync_events_called_
);
1047 EXPECT_TRUE(GetRegistration(sync_reg_1_
));
1049 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI
);
1050 base::RunLoop().RunUntilIdle();
1051 EXPECT_EQ(1, sync_events_called_
);
1052 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
1055 TEST_F(BackgroundSyncManagerTest
, MultipleOneShotsFireOnNetworkChange
) {
1056 InitSyncEventTest();
1058 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE
);
1059 EXPECT_TRUE(Register(sync_reg_1_
));
1060 EXPECT_TRUE(Register(sync_reg_2_
));
1061 EXPECT_EQ(0, sync_events_called_
);
1062 EXPECT_TRUE(GetRegistration(sync_reg_1_
));
1063 EXPECT_TRUE(GetRegistration(sync_reg_2_
));
1065 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI
);
1067 EXPECT_EQ(2, sync_events_called_
);
1068 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
1069 EXPECT_FALSE(GetRegistration(sync_reg_2_
));
1072 TEST_F(BackgroundSyncManagerTest
, OneShotFiresOnManagerRestart
) {
1073 InitSyncEventTest();
1075 // Initially the event won't run because there is no network.
1076 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE
);
1077 EXPECT_TRUE(Register(sync_reg_1_
));
1078 EXPECT_EQ(0, sync_events_called_
);
1079 EXPECT_TRUE(GetRegistration(sync_reg_1_
));
1081 // Simulate closing the browser.
1082 DeleteBackgroundSyncManager();
1084 // The next time the manager is started, the network is good.
1085 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI
);
1086 InitSyncEventTest();
1088 // The event should have fired.
1089 EXPECT_EQ(1, sync_events_called_
);
1090 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
1093 TEST_F(BackgroundSyncManagerTest
, FailedOneShotStillExists
) {
1094 InitFailedSyncEventTest();
1096 EXPECT_TRUE(Register(sync_reg_1_
));
1097 EXPECT_EQ(1, sync_events_called_
);
1098 EXPECT_TRUE(GetRegistration(sync_reg_1_
));
1100 // The failed one-shot should stay registered but not fire until the
1101 // ServiceWorker is reloaded with an active client. Therefore, changing the
1102 // network should not cause the event to run again.
1103 SetNetwork(net::NetworkChangeNotifier::CONNECTION_2G
);
1104 EXPECT_EQ(1, sync_events_called_
);
1105 EXPECT_TRUE(GetRegistration(sync_reg_1_
));
1108 TEST_F(BackgroundSyncManagerTest
, DelayOneShotMidSync
) {
1109 InitDelayedSyncEventTest();
1111 RegisterAndVerifySyncEventDelayed(sync_reg_1_
);
1113 // Finish firing the event and verify that the registration is removed.
1114 sync_fired_callback_
.Run(SERVICE_WORKER_OK
);
1115 base::RunLoop().RunUntilIdle();
1116 EXPECT_EQ(1, sync_events_called_
);
1117 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
1120 TEST_F(BackgroundSyncManagerTest
, OverwriteRegistrationMidSync
) {
1121 InitDelayedSyncEventTest();
1123 sync_reg_1_
.network_state
= NETWORK_STATE_ANY
;
1124 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE
);
1126 RegisterAndVerifySyncEventDelayed(sync_reg_1_
);
1128 // Don't delay the next sync.
1129 test_background_sync_manager_
->set_one_shot_callback(
1130 base::Bind(OneShotSuccessfulCallback
, &sync_events_called_
));
1132 // Register a different sync event with the same tag, overwriting the first.
1133 sync_reg_1_
.network_state
= NETWORK_STATE_ONLINE
;
1134 EXPECT_TRUE(Register(sync_reg_1_
));
1136 // The new sync event won't run as the network requirements aren't met.
1137 EXPECT_EQ(1, sync_events_called_
);
1138 EXPECT_TRUE(GetRegistration(sync_reg_1_
));
1140 // Finish the first event, note that the second is still registered.
1141 sync_fired_callback_
.Run(SERVICE_WORKER_OK
);
1142 EXPECT_EQ(1, sync_events_called_
);
1143 EXPECT_TRUE(GetRegistration(sync_reg_1_
));
1145 // Change the network and the second should run.
1146 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI
);
1147 base::RunLoop().RunUntilIdle();
1148 EXPECT_EQ(2, sync_events_called_
);
1149 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
1152 TEST_F(BackgroundSyncManagerTest
, ReregisterOneShotMidSync
) {
1153 InitDelayedSyncEventTest();
1155 RegisterAndVerifySyncEventDelayed(sync_reg_1_
);
1157 // Register the same sync, but don't delay it. It shouldn't run as it's
1159 test_background_sync_manager_
->set_one_shot_callback(
1160 base::Bind(OneShotSuccessfulCallback
, &sync_events_called_
));
1161 EXPECT_TRUE(Register(sync_reg_1_
));
1162 EXPECT_EQ(1, sync_events_called_
);
1163 EXPECT_TRUE(GetRegistration(sync_reg_1_
));
1165 // Finish the original event, note that the second never runs.
1166 sync_fired_callback_
.Run(SERVICE_WORKER_OK
);
1167 base::RunLoop().RunUntilIdle();
1168 EXPECT_EQ(1, sync_events_called_
);
1169 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
1172 TEST_F(BackgroundSyncManagerTest
, UnregisterOneShotMidSync
) {
1173 InitDelayedSyncEventTest();
1175 RegisterAndVerifySyncEventDelayed(sync_reg_1_
);
1177 EXPECT_TRUE(Unregister(callback_registration_
));
1178 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
1180 sync_fired_callback_
.Run(SERVICE_WORKER_OK
);
1181 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
1184 TEST_F(BackgroundSyncManagerTest
, BadBackendMidSync
) {
1185 InitDelayedSyncEventTest();
1187 RegisterAndVerifySyncEventDelayed(sync_reg_1_
);
1189 test_background_sync_manager_
->set_corrupt_backend(true);
1190 sync_fired_callback_
.Run(SERVICE_WORKER_OK
);
1191 base::RunLoop().RunUntilIdle();
1193 // The backend should now be disabled because it couldn't unregister the
1195 EXPECT_FALSE(Register(sync_reg_2_
));
1196 EXPECT_FALSE(RegisterWithServiceWorkerId(sw_registration_id_2_
, sync_reg_2_
));
1199 TEST_F(BackgroundSyncManagerTest
, UnregisterServiceWorkerMidSync
) {
1200 InitDelayedSyncEventTest();
1202 RegisterAndVerifySyncEventDelayed(sync_reg_1_
);
1203 UnregisterServiceWorker(sw_registration_id_1_
);
1205 sync_fired_callback_
.Run(SERVICE_WORKER_OK
);
1207 // The backend isn't disabled, but the first service worker registration is
1209 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
1210 EXPECT_FALSE(Register(sync_reg_1_
));
1211 EXPECT_TRUE(RegisterWithServiceWorkerId(sw_registration_id_2_
, sync_reg_1_
));
1214 TEST_F(BackgroundSyncManagerTest
, KillManagerMidSync
) {
1215 InitDelayedSyncEventTest();
1217 RegisterAndVerifySyncEventDelayed(sync_reg_1_
);
1219 // Create a new manager which should fire the sync again on init.
1220 InitSyncEventTest();
1221 EXPECT_FALSE(GetRegistration(sync_reg_1_
));
1222 EXPECT_EQ(2, sync_events_called_
);
1225 } // namespace content