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/location.h"
9 #include "base/logging.h"
10 #include "base/power_monitor/power_monitor.h"
11 #include "base/power_monitor/power_monitor_source.h"
12 #include "base/run_loop.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "content/browser/background_sync/background_sync_status.h"
16 #include "content/browser/browser_thread_impl.h"
17 #include "content/browser/service_worker/embedded_worker_test_helper.h"
18 #include "content/browser/service_worker/service_worker_context_core.h"
19 #include "content/browser/service_worker/service_worker_context_wrapper.h"
20 #include "content/browser/service_worker/service_worker_storage.h"
21 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "net/base/network_change_notifier.h"
23 #include "testing/gtest/include/gtest/gtest.h"
29 const char kPattern1
[] = "https://example.com/a";
30 const char kPattern2
[] = "https://example.com/b";
31 const char kScript1
[] = "https://example.com/a/script.js";
32 const char kScript2
[] = "https://example.com/b/script.js";
33 const int kRenderProcessId
= 99;
35 void RegisterServiceWorkerCallback(bool* called
,
36 int64
* store_registration_id
,
37 ServiceWorkerStatusCode status
,
38 const std::string
& status_message
,
39 int64 registration_id
) {
40 EXPECT_EQ(SERVICE_WORKER_OK
, status
) << ServiceWorkerStatusToString(status
);
42 *store_registration_id
= registration_id
;
45 void FindServiceWorkerRegistrationCallback(
46 scoped_refptr
<ServiceWorkerRegistration
>* out_registration
,
47 ServiceWorkerStatusCode status
,
48 const scoped_refptr
<ServiceWorkerRegistration
>& registration
) {
49 EXPECT_EQ(SERVICE_WORKER_OK
, status
) << ServiceWorkerStatusToString(status
);
50 *out_registration
= registration
;
53 void UnregisterServiceWorkerCallback(bool* called
,
54 ServiceWorkerStatusCode code
) {
55 EXPECT_EQ(SERVICE_WORKER_OK
, code
);
59 void OneShotSuccessfulCallback(
61 const scoped_refptr
<ServiceWorkerVersion
>& active_version
,
62 const ServiceWorkerVersion::StatusCallback
& callback
) {
64 callback
.Run(SERVICE_WORKER_OK
);
67 void OneShotFailedCallback(
69 const scoped_refptr
<ServiceWorkerVersion
>& active_version
,
70 const ServiceWorkerVersion::StatusCallback
& callback
) {
72 callback
.Run(SERVICE_WORKER_ERROR_FAILED
);
75 void OneShotDelayedCallback(
77 ServiceWorkerVersion::StatusCallback
* out_callback
,
78 const scoped_refptr
<ServiceWorkerVersion
>& active_version
,
79 const ServiceWorkerVersion::StatusCallback
& callback
) {
81 *out_callback
= callback
;
84 class TestPowerSource
: public base::PowerMonitorSource
{
86 void GeneratePowerStateEvent(bool on_battery_power
) {
87 test_on_battery_power_
= on_battery_power
;
88 ProcessPowerEvent(POWER_STATE_EVENT
);
92 bool IsOnBatteryPowerImpl() final
{ return test_on_battery_power_
; }
93 bool test_on_battery_power_
= false;
98 // A BackgroundSyncManager that can simulate delaying and corrupting the backend
99 // storage and service worker onsync events.
100 class TestBackgroundSyncManager
: public BackgroundSyncManager
{
102 using OneShotCallback
=
103 base::Callback
<void(const scoped_refptr
<ServiceWorkerVersion
>&,
104 const ServiceWorkerVersion::StatusCallback
&)>;
106 explicit TestBackgroundSyncManager(
107 const scoped_refptr
<ServiceWorkerContextWrapper
>& service_worker_context
)
108 : BackgroundSyncManager(service_worker_context
) {}
110 void DoInit() { Init(); }
112 void StoreDataInBackendContinue(
113 int64 sw_registration_id
,
115 const std::string
& key
,
116 const std::string
& data
,
117 const ServiceWorkerStorage::StatusCallback
& callback
) {
118 BackgroundSyncManager::StoreDataInBackend(sw_registration_id
, origin
, key
,
122 void GetDataFromBackendContinue(
123 const std::string
& key
,
124 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback
&
126 BackgroundSyncManager::GetDataFromBackend(key
, callback
);
131 continuation_
.Reset();
134 void set_corrupt_backend(bool corrupt_backend
) {
135 corrupt_backend_
= corrupt_backend
;
137 void set_delay_backend(bool delay_backend
) { delay_backend_
= delay_backend
; }
138 void set_one_shot_callback(const OneShotCallback
& callback
) {
139 one_shot_callback_
= callback
;
143 void StoreDataInBackend(
144 int64 sw_registration_id
,
146 const std::string
& key
,
147 const std::string
& data
,
148 const ServiceWorkerStorage::StatusCallback
& callback
) override
{
149 EXPECT_TRUE(continuation_
.is_null());
150 if (corrupt_backend_
) {
151 base::ThreadTaskRunnerHandle::Get()->PostTask(
152 FROM_HERE
, base::Bind(callback
, SERVICE_WORKER_ERROR_FAILED
));
156 base::Bind(&TestBackgroundSyncManager::StoreDataInBackendContinue
,
157 base::Unretained(this), sw_registration_id
, origin
, key
,
165 void GetDataFromBackend(
166 const std::string
& key
,
167 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback
&
169 EXPECT_TRUE(continuation_
.is_null());
170 if (corrupt_backend_
) {
171 base::ThreadTaskRunnerHandle::Get()->PostTask(
173 base::Bind(callback
, std::vector
<std::pair
<int64
, std::string
>>(),
174 SERVICE_WORKER_ERROR_FAILED
));
178 base::Bind(&TestBackgroundSyncManager::GetDataFromBackendContinue
,
179 base::Unretained(this), key
, callback
);
186 void FireOneShotSync(
187 const BackgroundSyncRegistration
& registration
,
188 const scoped_refptr
<ServiceWorkerVersion
>& active_version
,
189 const ServiceWorkerVersion::StatusCallback
& callback
) override
{
190 if (one_shot_callback_
.is_null()) {
191 BackgroundSyncManager::FireOneShotSync(registration
, active_version
,
194 one_shot_callback_
.Run(active_version
, callback
);
199 bool corrupt_backend_
= false;
200 bool delay_backend_
= false;
201 base::Closure continuation_
;
202 OneShotCallback one_shot_callback_
;
205 class BackgroundSyncManagerTest
: public testing::Test
{
207 BackgroundSyncManagerTest()
208 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP
),
209 network_change_notifier_(net::NetworkChangeNotifier::CreateMock()),
210 test_background_sync_manager_(nullptr),
211 callback_status_(BACKGROUND_SYNC_STATUS_OK
),
212 callback_sw_status_code_(SERVICE_WORKER_OK
),
213 sync_events_called_(0) {
214 sync_options_1_
.tag
= "foo";
215 sync_options_1_
.periodicity
= SYNC_ONE_SHOT
;
216 sync_options_1_
.network_state
= NETWORK_STATE_ONLINE
;
217 sync_options_1_
.power_state
= POWER_STATE_AUTO
;
219 sync_options_2_
.tag
= "bar";
220 sync_options_2_
.periodicity
= SYNC_ONE_SHOT
;
221 sync_options_2_
.network_state
= NETWORK_STATE_ONLINE
;
222 sync_options_2_
.power_state
= POWER_STATE_AUTO
;
225 void SetUp() override
{
227 new EmbeddedWorkerTestHelper(base::FilePath(), kRenderProcessId
));
229 power_monitor_source_
= new TestPowerSource();
230 // power_monitor_ takes ownership of power_monitor_source.
231 power_monitor_
.reset(new base::PowerMonitor(
232 scoped_ptr
<base::PowerMonitorSource
>(power_monitor_source_
)));
234 SetOnBatteryPower(false);
236 SetupBackgroundSyncManager();
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 // Register window clients for the service workers
261 host_1_
.reset(new ServiceWorkerProviderHost(
262 34 /* dummy render proces id */, MSG_ROUTING_NONE
/* render_frame_id */,
263 1 /* dummy provider id */, SERVICE_WORKER_PROVIDER_FOR_WINDOW
,
264 helper_
->context()->AsWeakPtr(), nullptr));
265 host_1_
->SetDocumentUrl(GURL(kPattern1
));
266 host_2_
.reset(new ServiceWorkerProviderHost(
267 34 /* dummy render proces id */, MSG_ROUTING_NONE
/* render_frame_id */,
268 1 /* dummy provider id */, SERVICE_WORKER_PROVIDER_FOR_WINDOW
,
269 helper_
->context()->AsWeakPtr(), nullptr));
270 host_2_
->SetDocumentUrl(GURL(kPattern2
));
272 // Hang onto the registrations as they need to be "live" when
273 // calling BackgroundSyncManager::Register.
274 helper_
->context_wrapper()->FindRegistrationForId(
275 sw_registration_id_1_
, GURL(kPattern1
).GetOrigin(),
276 base::Bind(FindServiceWorkerRegistrationCallback
, &sw_registration_1_
));
278 helper_
->context_wrapper()->FindRegistrationForId(
279 sw_registration_id_2_
, GURL(kPattern1
).GetOrigin(),
280 base::Bind(FindServiceWorkerRegistrationCallback
, &sw_registration_2_
));
281 base::RunLoop().RunUntilIdle();
282 EXPECT_TRUE(sw_registration_1_
);
283 EXPECT_TRUE(sw_registration_2_
);
285 sw_registration_1_
->active_version()->AddControllee(host_1_
.get());
286 sw_registration_2_
->active_version()->AddControllee(host_2_
.get());
289 void RemoveWindowClients() {
290 sw_registration_1_
->active_version()->RemoveControllee(host_1_
.get());
291 sw_registration_2_
->active_version()->RemoveControllee(host_2_
.get());
294 void SetNetwork(net::NetworkChangeNotifier::ConnectionType connection_type
) {
295 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
297 base::RunLoop().RunUntilIdle();
300 void SetOnBatteryPower(bool on_battery_power
) {
301 power_monitor_source_
->GeneratePowerStateEvent(on_battery_power
);
302 base::RunLoop().RunUntilIdle();
305 void StatusAndRegistrationCallback(
307 BackgroundSyncStatus status
,
308 const BackgroundSyncRegistration
& registration
) {
310 callback_status_
= status
;
311 callback_registration_
= registration
;
314 void StatusAndRegistrationsCallback(
316 BackgroundSyncStatus status
,
317 const std::vector
<BackgroundSyncRegistration
>& registrations
) {
319 callback_status_
= status
;
320 callback_registrations_
= registrations
;
323 void StatusCallback(bool* was_called
, BackgroundSyncStatus status
) {
325 callback_status_
= status
;
329 void CreateBackgroundSyncManager() {
330 test_background_sync_manager_
=
331 new TestBackgroundSyncManager(helper_
->context_wrapper());
332 background_sync_manager_
.reset(test_background_sync_manager_
);
335 void InitBackgroundSyncManager() {
336 test_background_sync_manager_
->DoInit();
338 // Many tests do not expect the sync event to fire immediately after
339 // register (and cleanup up the sync registrations). Tests can control when
340 // the sync event fires by manipulating the network state as needed.
341 // NOTE: The setup of the network connection must happen after the
342 // BackgroundSyncManager has been setup.
343 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE
);
346 void SetupBackgroundSyncManager() {
347 CreateBackgroundSyncManager();
348 InitBackgroundSyncManager();
351 void SetupCorruptBackgroundSyncManager() {
352 CreateBackgroundSyncManager();
353 test_background_sync_manager_
->set_corrupt_backend(true);
354 InitBackgroundSyncManager();
357 void SetupDelayedBackgroundSyncManager() {
358 CreateBackgroundSyncManager();
359 test_background_sync_manager_
->set_delay_backend(true);
360 InitBackgroundSyncManager();
363 void DeleteBackgroundSyncManager() {
364 background_sync_manager_
.reset();
365 test_background_sync_manager_
= nullptr;
368 bool Register(const BackgroundSyncRegistrationOptions
& sync_options
) {
369 return RegisterWithServiceWorkerId(sw_registration_id_1_
, sync_options
);
372 bool RegisterWithServiceWorkerId(
373 int64 sw_registration_id
,
374 const BackgroundSyncRegistrationOptions
& options
) {
375 bool was_called
= false;
376 background_sync_manager_
->Register(
377 sw_registration_id
, options
,
378 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback
,
379 base::Unretained(this), &was_called
));
380 base::RunLoop().RunUntilIdle();
381 EXPECT_TRUE(was_called
);
382 return callback_status_
== BACKGROUND_SYNC_STATUS_OK
;
385 bool Unregister(const BackgroundSyncRegistration
& sync_registration
) {
386 return UnregisterWithServiceWorkerId(sw_registration_id_1_
,
390 bool UnregisterWithServiceWorkerId(
391 int64 sw_registration_id
,
392 const BackgroundSyncRegistration
& sync_registration
) {
393 bool was_called
= false;
394 background_sync_manager_
->Unregister(
395 sw_registration_id
, sync_registration
.options()->tag
,
396 sync_registration
.options()->periodicity
, sync_registration
.id(),
397 base::Bind(&BackgroundSyncManagerTest::StatusCallback
,
398 base::Unretained(this), &was_called
));
399 base::RunLoop().RunUntilIdle();
400 EXPECT_TRUE(was_called
);
401 return callback_status_
== BACKGROUND_SYNC_STATUS_OK
;
404 bool GetRegistration(const BackgroundSyncRegistrationOptions
& sync_options
) {
405 return GetRegistrationWithServiceWorkerId(sw_registration_id_1_
,
409 bool GetRegistrationWithServiceWorkerId(
410 int64 sw_registration_id
,
411 const BackgroundSyncRegistrationOptions
& sync_options
) {
412 bool was_called
= false;
413 background_sync_manager_
->GetRegistration(
414 sw_registration_id
, sync_options
.tag
, sync_options
.periodicity
,
415 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback
,
416 base::Unretained(this), &was_called
));
417 base::RunLoop().RunUntilIdle();
418 EXPECT_TRUE(was_called
);
420 if (callback_status_
== BACKGROUND_SYNC_STATUS_OK
) {
421 EXPECT_STREQ(sync_options
.tag
.c_str(),
422 callback_registration_
.options()->tag
.c_str());
425 return callback_status_
== BACKGROUND_SYNC_STATUS_OK
;
428 bool GetRegistrations(SyncPeriodicity periodicity
) {
429 return GetRegistrationWithServiceWorkerId(sw_registration_id_1_
,
433 bool GetRegistrationWithServiceWorkerId(int64 sw_registration_id
,
434 SyncPeriodicity periodicity
) {
435 bool was_called
= false;
436 background_sync_manager_
->GetRegistrations(
437 sw_registration_id
, periodicity
,
438 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationsCallback
,
439 base::Unretained(this), &was_called
));
440 base::RunLoop().RunUntilIdle();
441 EXPECT_TRUE(was_called
);
443 return callback_status_
== BACKGROUND_SYNC_STATUS_OK
;
446 void StorageRegistrationCallback(ServiceWorkerStatusCode result
) {
447 callback_sw_status_code_
= result
;
450 void UnregisterServiceWorker(uint64 sw_registration_id
) {
452 helper_
->context()->UnregisterServiceWorker(
453 PatternForSWId(sw_registration_id
),
454 base::Bind(&UnregisterServiceWorkerCallback
, &called
));
455 base::RunLoop().RunUntilIdle();
459 GURL
PatternForSWId(int64 sw_id
) {
460 EXPECT_TRUE(sw_id
== sw_registration_id_1_
||
461 sw_id
== sw_registration_id_2_
);
462 return sw_id
== sw_registration_id_1_
? GURL(kPattern1
) : GURL(kPattern2
);
465 void SetupForSyncEvent(
466 const TestBackgroundSyncManager::OneShotCallback
& callback
) {
467 test_background_sync_manager_
->set_one_shot_callback(callback
);
468 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI
);
471 void InitSyncEventTest() {
473 base::Bind(OneShotSuccessfulCallback
, &sync_events_called_
));
476 void InitFailedSyncEventTest() {
477 SetupForSyncEvent(base::Bind(OneShotFailedCallback
, &sync_events_called_
));
480 void InitDelayedSyncEventTest() {
481 SetupForSyncEvent(base::Bind(OneShotDelayedCallback
, &sync_events_called_
,
482 &sync_fired_callback_
));
485 void RegisterAndVerifySyncEventDelayed(
486 const BackgroundSyncRegistrationOptions
& sync_options
) {
487 int sync_events_called
= sync_events_called_
;
488 EXPECT_TRUE(sync_fired_callback_
.is_null());
490 EXPECT_TRUE(Register(sync_options
));
492 EXPECT_EQ(sync_events_called
+ 1, sync_events_called_
);
493 EXPECT_TRUE(GetRegistration(sync_options_1_
));
494 EXPECT_FALSE(sync_fired_callback_
.is_null());
497 TestBrowserThreadBundle browser_thread_bundle_
;
498 scoped_ptr
<net::NetworkChangeNotifier
> network_change_notifier_
;
499 TestPowerSource
* power_monitor_source_
; // owned by power_monitor_
500 scoped_ptr
<base::PowerMonitor
> power_monitor_
;
501 scoped_ptr
<EmbeddedWorkerTestHelper
> helper_
;
502 scoped_ptr
<BackgroundSyncManager
> background_sync_manager_
;
503 TestBackgroundSyncManager
* test_background_sync_manager_
;
505 scoped_ptr
<ServiceWorkerProviderHost
> host_1_
;
506 scoped_ptr
<ServiceWorkerProviderHost
> host_2_
;
507 int64 sw_registration_id_1_
;
508 int64 sw_registration_id_2_
;
509 scoped_refptr
<ServiceWorkerRegistration
> sw_registration_1_
;
510 scoped_refptr
<ServiceWorkerRegistration
> sw_registration_2_
;
512 BackgroundSyncRegistrationOptions sync_options_1_
;
513 BackgroundSyncRegistrationOptions sync_options_2_
;
516 BackgroundSyncStatus callback_status_
;
517 BackgroundSyncRegistration callback_registration_
;
518 std::vector
<BackgroundSyncRegistration
> callback_registrations_
;
519 ServiceWorkerStatusCode callback_sw_status_code_
;
520 int sync_events_called_
;
521 ServiceWorkerVersion::StatusCallback sync_fired_callback_
;
524 TEST_F(BackgroundSyncManagerTest
, Register
) {
525 EXPECT_TRUE(Register(sync_options_1_
));
528 TEST_F(BackgroundSyncManagerTest
, RegistractionIntact
) {
529 EXPECT_TRUE(Register(sync_options_1_
));
530 EXPECT_STREQ(sync_options_1_
.tag
.c_str(),
531 callback_registration_
.options()->tag
.c_str());
532 EXPECT_TRUE(callback_registration_
.IsValid());
535 TEST_F(BackgroundSyncManagerTest
, RegisterWithoutLiveSWRegistration
) {
536 sw_registration_1_
= nullptr;
537 EXPECT_FALSE(Register(sync_options_1_
));
538 EXPECT_EQ(BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER
, callback_status_
);
541 TEST_F(BackgroundSyncManagerTest
, RegisterWithoutActiveSWRegistration
) {
542 sw_registration_1_
->UnsetVersion(sw_registration_1_
->active_version());
543 EXPECT_FALSE(Register(sync_options_1_
));
544 EXPECT_EQ(BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER
, callback_status_
);
547 TEST_F(BackgroundSyncManagerTest
, RegisterExistingKeepsId
) {
548 EXPECT_TRUE(Register(sync_options_1_
));
549 BackgroundSyncRegistration first_registration
= callback_registration_
;
550 EXPECT_TRUE(Register(sync_options_1_
));
551 EXPECT_TRUE(callback_registration_
.Equals(first_registration
));
552 EXPECT_EQ(first_registration
.id(), callback_registration_
.id());
555 TEST_F(BackgroundSyncManagerTest
, RegisterOverwrites
) {
556 EXPECT_TRUE(Register(sync_options_1_
));
557 BackgroundSyncRegistration first_registration
= callback_registration_
;
559 sync_options_1_
.min_period
= 100;
560 EXPECT_TRUE(Register(sync_options_1_
));
561 EXPECT_LT(first_registration
.id(), callback_registration_
.id());
562 EXPECT_FALSE(callback_registration_
.Equals(first_registration
));
565 TEST_F(BackgroundSyncManagerTest
, RegisterOverlappingPeriodicAndOneShotTags
) {
566 // Registrations with the same tags but different periodicities should not
568 sync_options_1_
.tag
= "";
569 sync_options_2_
.tag
= "";
570 sync_options_1_
.periodicity
= SYNC_PERIODIC
;
571 sync_options_2_
.periodicity
= SYNC_ONE_SHOT
;
572 EXPECT_TRUE(Register(sync_options_1_
));
573 EXPECT_TRUE(Register(sync_options_2_
));
574 EXPECT_TRUE(GetRegistration(sync_options_1_
));
575 EXPECT_EQ(SYNC_PERIODIC
, callback_registration_
.options()->periodicity
);
576 EXPECT_TRUE(GetRegistration(sync_options_2_
));
577 EXPECT_EQ(SYNC_ONE_SHOT
, callback_registration_
.options()->periodicity
);
580 TEST_F(BackgroundSyncManagerTest
, RegisterBadBackend
) {
581 test_background_sync_manager_
->set_corrupt_backend(true);
582 EXPECT_FALSE(Register(sync_options_1_
));
583 test_background_sync_manager_
->set_corrupt_backend(false);
584 EXPECT_FALSE(Register(sync_options_1_
));
585 EXPECT_FALSE(GetRegistration(sync_options_1_
));
588 TEST_F(BackgroundSyncManagerTest
, TwoRegistrations
) {
589 EXPECT_TRUE(Register(sync_options_1_
));
590 EXPECT_TRUE(Register(sync_options_2_
));
593 TEST_F(BackgroundSyncManagerTest
, GetRegistrationNonExisting
) {
594 EXPECT_FALSE(GetRegistration(sync_options_1_
));
597 TEST_F(BackgroundSyncManagerTest
, GetRegistrationExisting
) {
598 EXPECT_TRUE(Register(sync_options_1_
));
599 EXPECT_TRUE(GetRegistration(sync_options_1_
));
600 EXPECT_FALSE(GetRegistration(sync_options_2_
));
603 TEST_F(BackgroundSyncManagerTest
, GetRegistrationBadBackend
) {
604 EXPECT_TRUE(Register(sync_options_1_
));
605 test_background_sync_manager_
->set_corrupt_backend(true);
606 EXPECT_TRUE(GetRegistration(sync_options_1_
));
607 EXPECT_FALSE(Register(sync_options_2_
));
608 // Registration should have discovered the bad backend and disabled the
609 // BackgroundSyncManager.
610 EXPECT_FALSE(GetRegistration(sync_options_1_
));
611 test_background_sync_manager_
->set_corrupt_backend(false);
612 EXPECT_FALSE(GetRegistration(sync_options_1_
));
615 TEST_F(BackgroundSyncManagerTest
, GetRegistrationsZero
) {
616 EXPECT_TRUE(GetRegistrations(SYNC_ONE_SHOT
));
617 EXPECT_EQ(0u, callback_registrations_
.size());
620 TEST_F(BackgroundSyncManagerTest
, GetRegistrationsOne
) {
621 EXPECT_TRUE(Register(sync_options_1_
));
622 EXPECT_TRUE(GetRegistrations(sync_options_1_
.periodicity
));
624 EXPECT_EQ(1u, callback_registrations_
.size());
625 sync_options_1_
.Equals(*callback_registrations_
[0].options());
628 TEST_F(BackgroundSyncManagerTest
, GetRegistrationsTwo
) {
629 EXPECT_EQ(sync_options_1_
.periodicity
, sync_options_2_
.periodicity
);
631 EXPECT_TRUE(Register(sync_options_1_
));
632 EXPECT_TRUE(Register(sync_options_2_
));
633 EXPECT_TRUE(GetRegistrations(sync_options_1_
.periodicity
));
635 EXPECT_EQ(2u, callback_registrations_
.size());
636 sync_options_1_
.Equals(*callback_registrations_
[0].options());
637 sync_options_2_
.Equals(*callback_registrations_
[1].options());
640 TEST_F(BackgroundSyncManagerTest
, GetRegistrationsPeriodicity
) {
641 sync_options_1_
.periodicity
= SYNC_ONE_SHOT
;
642 sync_options_2_
.periodicity
= SYNC_PERIODIC
;
643 EXPECT_TRUE(Register(sync_options_1_
));
644 EXPECT_TRUE(Register(sync_options_2_
));
646 EXPECT_TRUE(GetRegistrations(SYNC_ONE_SHOT
));
647 EXPECT_EQ(1u, callback_registrations_
.size());
648 sync_options_1_
.Equals(*callback_registrations_
[0].options());
650 EXPECT_TRUE(GetRegistrations(SYNC_PERIODIC
));
651 EXPECT_EQ(1u, callback_registrations_
.size());
652 sync_options_2_
.Equals(*callback_registrations_
[0].options());
655 TEST_F(BackgroundSyncManagerTest
, GetRegistrationsBadBackend
) {
656 EXPECT_TRUE(Register(sync_options_1_
));
657 test_background_sync_manager_
->set_corrupt_backend(true);
658 EXPECT_TRUE(GetRegistrations(sync_options_1_
.periodicity
));
659 EXPECT_FALSE(Register(sync_options_2_
));
660 // Registration should have discovered the bad backend and disabled the
661 // BackgroundSyncManager.
662 EXPECT_FALSE(GetRegistrations(sync_options_1_
.periodicity
));
663 test_background_sync_manager_
->set_corrupt_backend(false);
664 EXPECT_FALSE(GetRegistrations(sync_options_1_
.periodicity
));
667 TEST_F(BackgroundSyncManagerTest
, Unregister
) {
668 EXPECT_TRUE(Register(sync_options_1_
));
669 EXPECT_TRUE(Unregister(callback_registration_
));
670 EXPECT_FALSE(GetRegistration(sync_options_1_
));
673 TEST_F(BackgroundSyncManagerTest
, UnregisterWrongId
) {
674 EXPECT_TRUE(Register(sync_options_1_
));
675 callback_registration_
.set_id(callback_registration_
.id() + 1);
676 EXPECT_FALSE(Unregister(callback_registration_
));
679 TEST_F(BackgroundSyncManagerTest
, Reregister
) {
680 EXPECT_TRUE(Register(sync_options_1_
));
681 EXPECT_TRUE(Unregister(callback_registration_
));
682 EXPECT_TRUE(Register(sync_options_1_
));
685 TEST_F(BackgroundSyncManagerTest
, UnregisterNonExisting
) {
686 BackgroundSyncRegistration nonexistant_registration
;
687 nonexistant_registration
.set_id(1);
688 EXPECT_FALSE(Unregister(nonexistant_registration
));
689 EXPECT_EQ(BACKGROUND_SYNC_STATUS_NOT_FOUND
, callback_status_
);
692 TEST_F(BackgroundSyncManagerTest
, UnregisterSecond
) {
693 EXPECT_TRUE(Register(sync_options_1_
));
694 EXPECT_TRUE(Register(sync_options_2_
));
695 EXPECT_TRUE(Unregister(callback_registration_
));
696 EXPECT_TRUE(GetRegistration(sync_options_1_
));
697 EXPECT_TRUE(Register(sync_options_2_
));
700 TEST_F(BackgroundSyncManagerTest
, UnregisterBadBackend
) {
701 sync_options_1_
.min_period
+= 1;
702 EXPECT_TRUE(Register(sync_options_1_
));
703 EXPECT_TRUE(Register(sync_options_2_
));
704 test_background_sync_manager_
->set_corrupt_backend(true);
705 EXPECT_FALSE(Unregister(callback_registration_
));
706 // Unregister should have discovered the bad backend and disabled the
707 // BackgroundSyncManager.
708 test_background_sync_manager_
->set_corrupt_backend(false);
709 EXPECT_FALSE(GetRegistration(sync_options_1_
));
710 EXPECT_FALSE(GetRegistration(sync_options_2_
));
713 TEST_F(BackgroundSyncManagerTest
, RegistrationIncreasesId
) {
714 EXPECT_TRUE(Register(sync_options_1_
));
715 BackgroundSyncRegistration registered_sync
= callback_registration_
;
716 BackgroundSyncRegistration::RegistrationId cur_id
=
717 callback_registration_
.id();
719 EXPECT_TRUE(GetRegistration(sync_options_1_
));
720 EXPECT_TRUE(Register(sync_options_2_
));
721 EXPECT_LT(cur_id
, callback_registration_
.id());
722 cur_id
= callback_registration_
.id();
724 EXPECT_TRUE(Unregister(registered_sync
));
725 EXPECT_TRUE(Register(sync_options_1_
));
726 EXPECT_LT(cur_id
, callback_registration_
.id());
729 TEST_F(BackgroundSyncManagerTest
, RebootRecovery
) {
730 EXPECT_TRUE(Register(sync_options_1_
));
732 SetupBackgroundSyncManager();
734 EXPECT_TRUE(GetRegistration(sync_options_1_
));
735 EXPECT_FALSE(GetRegistration(sync_options_2_
));
738 TEST_F(BackgroundSyncManagerTest
, RebootRecoveryTwoServiceWorkers
) {
740 RegisterWithServiceWorkerId(sw_registration_id_1_
, sync_options_1_
));
742 RegisterWithServiceWorkerId(sw_registration_id_2_
, sync_options_2_
));
744 SetupBackgroundSyncManager();
746 EXPECT_TRUE(GetRegistrationWithServiceWorkerId(sw_registration_id_1_
,
748 EXPECT_FALSE(GetRegistrationWithServiceWorkerId(sw_registration_id_1_
,
750 EXPECT_FALSE(GetRegistrationWithServiceWorkerId(sw_registration_id_2_
,
752 EXPECT_TRUE(GetRegistrationWithServiceWorkerId(sw_registration_id_2_
,
755 EXPECT_TRUE(GetRegistrationWithServiceWorkerId(sw_registration_id_1_
,
757 EXPECT_TRUE(GetRegistrationWithServiceWorkerId(sw_registration_id_2_
,
761 RegisterWithServiceWorkerId(sw_registration_id_1_
, sync_options_2_
));
763 RegisterWithServiceWorkerId(sw_registration_id_2_
, sync_options_1_
));
766 TEST_F(BackgroundSyncManagerTest
, InitWithBadBackend
) {
767 SetupCorruptBackgroundSyncManager();
769 EXPECT_FALSE(Register(sync_options_1_
));
770 EXPECT_FALSE(GetRegistration(sync_options_1_
));
773 TEST_F(BackgroundSyncManagerTest
, SequentialOperations
) {
774 // Schedule Init and all of the operations on a delayed backend. Verify that
775 // the operations complete sequentially.
776 SetupDelayedBackgroundSyncManager();
778 const int64 kExpectedInitialId
= BackgroundSyncRegistration::kInitialId
;
780 bool register_called
= false;
781 bool unregister_called
= false;
782 bool get_registration_called
= false;
783 test_background_sync_manager_
->Register(
784 sw_registration_id_1_
, sync_options_1_
,
785 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback
,
786 base::Unretained(this), ®ister_called
));
787 test_background_sync_manager_
->Unregister(
788 sw_registration_id_1_
, sync_options_1_
.tag
, sync_options_1_
.periodicity
,
790 base::Bind(&BackgroundSyncManagerTest::StatusCallback
,
791 base::Unretained(this), &unregister_called
));
792 test_background_sync_manager_
->GetRegistration(
793 sw_registration_id_1_
, sync_options_1_
.tag
, sync_options_1_
.periodicity
,
794 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback
,
795 base::Unretained(this), &get_registration_called
));
797 base::RunLoop().RunUntilIdle();
798 // Init should be blocked while loading from the backend.
799 EXPECT_FALSE(register_called
);
800 EXPECT_FALSE(unregister_called
);
801 EXPECT_FALSE(get_registration_called
);
803 test_background_sync_manager_
->Continue();
804 base::RunLoop().RunUntilIdle();
805 // Register should be blocked while storing to the backend.
806 EXPECT_FALSE(register_called
);
807 EXPECT_FALSE(unregister_called
);
808 EXPECT_FALSE(get_registration_called
);
810 test_background_sync_manager_
->Continue();
811 base::RunLoop().RunUntilIdle();
812 EXPECT_TRUE(register_called
);
813 EXPECT_EQ(kExpectedInitialId
, callback_registration_
.id());
814 EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK
, callback_status_
);
815 // Unregister should be blocked while storing to the backend.
816 EXPECT_FALSE(unregister_called
);
817 EXPECT_FALSE(get_registration_called
);
819 test_background_sync_manager_
->Continue();
820 base::RunLoop().RunUntilIdle();
821 // Unregister should be done and since GetRegistration doesn't require the
822 // backend it should be done too.
823 EXPECT_EQ(BACKGROUND_SYNC_STATUS_NOT_FOUND
, callback_status_
);
824 EXPECT_TRUE(unregister_called
);
825 EXPECT_TRUE(get_registration_called
);
828 TEST_F(BackgroundSyncManagerTest
, UnregisterServiceWorker
) {
829 EXPECT_TRUE(Register(sync_options_1_
));
830 UnregisterServiceWorker(sw_registration_id_1_
);
831 EXPECT_FALSE(GetRegistration(sync_options_1_
));
834 TEST_F(BackgroundSyncManagerTest
,
835 UnregisterServiceWorkerDuringSyncRegistration
) {
836 EXPECT_TRUE(Register(sync_options_1_
));
838 test_background_sync_manager_
->set_delay_backend(true);
839 bool callback_called
= false;
840 test_background_sync_manager_
->Register(
841 sw_registration_id_1_
, sync_options_2_
,
842 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback
,
843 base::Unretained(this), &callback_called
));
845 base::RunLoop().RunUntilIdle();
846 EXPECT_FALSE(callback_called
);
847 UnregisterServiceWorker(sw_registration_id_1_
);
849 test_background_sync_manager_
->Continue();
850 base::RunLoop().RunUntilIdle();
851 EXPECT_TRUE(callback_called
);
852 EXPECT_EQ(BACKGROUND_SYNC_STATUS_STORAGE_ERROR
, callback_status_
);
854 test_background_sync_manager_
->set_delay_backend(false);
855 EXPECT_FALSE(GetRegistration(sync_options_1_
));
858 TEST_F(BackgroundSyncManagerTest
, DeleteAndStartOverServiceWorkerContext
) {
859 EXPECT_TRUE(Register(sync_options_1_
));
860 helper_
->context()->ScheduleDeleteAndStartOver();
861 base::RunLoop().RunUntilIdle();
862 EXPECT_FALSE(GetRegistration(sync_options_1_
));
865 TEST_F(BackgroundSyncManagerTest
, DisabledManagerWorksAfterBrowserRestart
) {
866 EXPECT_TRUE(Register(sync_options_1_
));
867 test_background_sync_manager_
->set_corrupt_backend(true);
868 EXPECT_FALSE(Register(sync_options_2_
));
870 // The manager is now disabled and not accepting new requests until browser
871 // restart or notification that the storage has been wiped.
872 test_background_sync_manager_
->set_corrupt_backend(false);
873 EXPECT_FALSE(GetRegistration(sync_options_1_
));
874 EXPECT_FALSE(Register(sync_options_2_
));
876 // Simulate restarting the browser by creating a new BackgroundSyncManager.
877 SetupBackgroundSyncManager();
878 EXPECT_TRUE(GetRegistration(sync_options_1_
));
879 EXPECT_TRUE(Register(sync_options_2_
));
882 TEST_F(BackgroundSyncManagerTest
, DisabledManagerWorksAfterDeleteAndStartOver
) {
883 EXPECT_TRUE(Register(sync_options_1_
));
884 test_background_sync_manager_
->set_corrupt_backend(true);
885 EXPECT_FALSE(Register(sync_options_2_
));
887 // The manager is now disabled and not accepting new requests until browser
888 // restart or notification that the storage has been wiped.
889 test_background_sync_manager_
->set_corrupt_backend(false);
890 helper_
->context()->ScheduleDeleteAndStartOver();
891 base::RunLoop().RunUntilIdle();
893 RegisterServiceWorkers();
895 EXPECT_TRUE(Register(sync_options_2_
));
896 EXPECT_FALSE(GetRegistration(sync_options_1_
));
897 EXPECT_TRUE(GetRegistration(sync_options_2_
));
900 TEST_F(BackgroundSyncManagerTest
, RegistrationEqualsId
) {
901 BackgroundSyncRegistration reg_1
;
902 BackgroundSyncRegistration reg_2
;
904 EXPECT_TRUE(reg_1
.Equals(reg_2
));
905 reg_2
.set_id(reg_1
.id() + 1);
906 EXPECT_TRUE(reg_1
.Equals(reg_2
));
909 TEST_F(BackgroundSyncManagerTest
, RegistrationEqualsTag
) {
910 BackgroundSyncRegistration reg_1
;
911 BackgroundSyncRegistration reg_2
;
912 EXPECT_TRUE(reg_1
.Equals(reg_2
));
913 reg_2
.options()->tag
= "bar";
914 EXPECT_FALSE(reg_1
.Equals(reg_2
));
917 TEST_F(BackgroundSyncManagerTest
, RegistrationEqualsPeriodicity
) {
918 BackgroundSyncRegistration reg_1
;
919 BackgroundSyncRegistration reg_2
;
920 EXPECT_TRUE(reg_1
.Equals(reg_2
));
921 reg_1
.options()->periodicity
= SYNC_PERIODIC
;
922 reg_2
.options()->periodicity
= SYNC_ONE_SHOT
;
923 EXPECT_FALSE(reg_1
.Equals(reg_2
));
926 TEST_F(BackgroundSyncManagerTest
, RegistrationEqualsMinPeriod
) {
927 BackgroundSyncRegistration reg_1
;
928 BackgroundSyncRegistration reg_2
;
929 EXPECT_TRUE(reg_1
.Equals(reg_2
));
930 reg_2
.options()->min_period
= reg_1
.options()->min_period
+ 1;
931 EXPECT_FALSE(reg_1
.Equals(reg_2
));
934 TEST_F(BackgroundSyncManagerTest
, RegistrationEqualsNetworkState
) {
935 BackgroundSyncRegistration reg_1
;
936 BackgroundSyncRegistration reg_2
;
937 EXPECT_TRUE(reg_1
.Equals(reg_2
));
938 reg_1
.options()->network_state
= NETWORK_STATE_ANY
;
939 reg_2
.options()->network_state
= NETWORK_STATE_ONLINE
;
940 EXPECT_FALSE(reg_1
.Equals(reg_2
));
943 TEST_F(BackgroundSyncManagerTest
, RegistrationEqualsPowerState
) {
944 BackgroundSyncRegistration reg_1
;
945 BackgroundSyncRegistration reg_2
;
946 EXPECT_TRUE(reg_1
.Equals(reg_2
));
947 reg_1
.options()->power_state
= POWER_STATE_AUTO
;
948 reg_2
.options()->power_state
= POWER_STATE_AVOID_DRAINING
;
949 EXPECT_FALSE(reg_1
.Equals(reg_2
));
952 TEST_F(BackgroundSyncManagerTest
, StoreAndRetrievePreservesValues
) {
953 BackgroundSyncRegistrationOptions options
;
954 // Set non-default values for each field.
956 EXPECT_NE(SYNC_PERIODIC
, options
.periodicity
);
957 options
.periodicity
= SYNC_PERIODIC
;
958 options
.min_period
+= 1;
959 EXPECT_NE(NETWORK_STATE_ANY
, options
.network_state
);
960 options
.network_state
= NETWORK_STATE_ANY
;
961 EXPECT_NE(POWER_STATE_AUTO
, options
.power_state
);
962 options
.power_state
= POWER_STATE_AUTO
;
964 // Store the registration.
965 EXPECT_TRUE(Register(options
));
967 // Simulate restarting the sync manager, forcing the next read to come from
969 SetupBackgroundSyncManager();
971 EXPECT_TRUE(GetRegistration(options
));
972 EXPECT_TRUE(options
.Equals(*callback_registration_
.options()));
975 TEST_F(BackgroundSyncManagerTest
, EmptyTagSupported
) {
976 sync_options_1_
.tag
= "a";
977 EXPECT_TRUE(Register(sync_options_1_
));
978 EXPECT_TRUE(GetRegistration(sync_options_1_
));
979 EXPECT_TRUE(sync_options_1_
.Equals(*callback_registration_
.options()));
980 EXPECT_TRUE(Unregister(callback_registration_
));
981 EXPECT_FALSE(GetRegistration(sync_options_1_
));
984 TEST_F(BackgroundSyncManagerTest
, OverlappingPeriodicAndOneShotTags
) {
985 // Registrations with the same tags but different periodicities should not
987 sync_options_1_
.tag
= "";
988 sync_options_2_
.tag
= "";
989 sync_options_1_
.periodicity
= SYNC_PERIODIC
;
990 sync_options_2_
.periodicity
= SYNC_ONE_SHOT
;
992 EXPECT_TRUE(Register(sync_options_1_
));
993 EXPECT_TRUE(Register(sync_options_2_
));
995 EXPECT_TRUE(GetRegistration(sync_options_1_
));
996 EXPECT_EQ(SYNC_PERIODIC
, callback_registration_
.options()->periodicity
);
997 EXPECT_TRUE(GetRegistration(sync_options_2_
));
998 EXPECT_EQ(SYNC_ONE_SHOT
, callback_registration_
.options()->periodicity
);
1000 EXPECT_TRUE(GetRegistration(sync_options_1_
));
1001 EXPECT_TRUE(Unregister(callback_registration_
));
1002 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1003 EXPECT_TRUE(GetRegistration(sync_options_2_
));
1004 EXPECT_EQ(SYNC_ONE_SHOT
, callback_registration_
.options()->periodicity
);
1006 EXPECT_TRUE(Unregister(callback_registration_
));
1007 EXPECT_FALSE(GetRegistration(sync_options_2_
));
1010 TEST_F(BackgroundSyncManagerTest
, OneShotFiresOnRegistration
) {
1011 InitSyncEventTest();
1013 EXPECT_TRUE(Register(sync_options_1_
));
1014 EXPECT_EQ(1, sync_events_called_
);
1015 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1018 // TODO(jkarlin): Change this to a periodic test as one-shots can't be power
1019 // dependent according to spec.
1020 TEST_F(BackgroundSyncManagerTest
, OneShotFiresOnPowerChange
) {
1021 InitSyncEventTest();
1022 sync_options_1_
.power_state
= POWER_STATE_AVOID_DRAINING
;
1024 SetOnBatteryPower(true);
1025 EXPECT_TRUE(Register(sync_options_1_
));
1026 EXPECT_EQ(0, sync_events_called_
);
1027 EXPECT_TRUE(GetRegistration(sync_options_1_
));
1029 SetOnBatteryPower(false);
1030 EXPECT_EQ(1, sync_events_called_
);
1031 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1034 // TODO(jkarlin): Change this to a periodic test as one-shots can't be power
1035 // dependent according to spec.
1036 TEST_F(BackgroundSyncManagerTest
, MultipleOneShotsFireOnPowerChange
) {
1037 InitSyncEventTest();
1038 sync_options_1_
.power_state
= POWER_STATE_AVOID_DRAINING
;
1039 sync_options_2_
.power_state
= POWER_STATE_AVOID_DRAINING
;
1041 SetOnBatteryPower(true);
1042 EXPECT_TRUE(Register(sync_options_1_
));
1043 EXPECT_TRUE(Register(sync_options_2_
));
1044 EXPECT_EQ(0, sync_events_called_
);
1045 EXPECT_TRUE(GetRegistration(sync_options_1_
));
1046 EXPECT_TRUE(GetRegistration(sync_options_2_
));
1048 SetOnBatteryPower(false);
1049 EXPECT_EQ(2, sync_events_called_
);
1050 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1051 EXPECT_FALSE(GetRegistration(sync_options_2_
));
1054 TEST_F(BackgroundSyncManagerTest
, OneShotFiresOnNetworkChange
) {
1055 InitSyncEventTest();
1057 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE
);
1058 EXPECT_TRUE(Register(sync_options_1_
));
1059 EXPECT_EQ(0, sync_events_called_
);
1060 EXPECT_TRUE(GetRegistration(sync_options_1_
));
1062 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI
);
1063 base::RunLoop().RunUntilIdle();
1064 EXPECT_EQ(1, sync_events_called_
);
1065 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1068 TEST_F(BackgroundSyncManagerTest
, MultipleOneShotsFireOnNetworkChange
) {
1069 InitSyncEventTest();
1071 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE
);
1072 EXPECT_TRUE(Register(sync_options_1_
));
1073 EXPECT_TRUE(Register(sync_options_2_
));
1074 EXPECT_EQ(0, sync_events_called_
);
1075 EXPECT_TRUE(GetRegistration(sync_options_1_
));
1076 EXPECT_TRUE(GetRegistration(sync_options_2_
));
1078 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI
);
1080 EXPECT_EQ(2, sync_events_called_
);
1081 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1082 EXPECT_FALSE(GetRegistration(sync_options_2_
));
1085 TEST_F(BackgroundSyncManagerTest
, OneShotFiresOnManagerRestart
) {
1086 InitSyncEventTest();
1088 // Initially the event won't run because there is no network.
1089 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE
);
1090 EXPECT_TRUE(Register(sync_options_1_
));
1091 EXPECT_EQ(0, sync_events_called_
);
1092 EXPECT_TRUE(GetRegistration(sync_options_1_
));
1094 // Simulate closing the browser.
1095 DeleteBackgroundSyncManager();
1097 // The next time the manager is started, the network is good.
1098 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI
);
1099 SetupBackgroundSyncManager();
1100 InitSyncEventTest();
1102 // The event should have fired.
1103 EXPECT_EQ(1, sync_events_called_
);
1104 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1107 TEST_F(BackgroundSyncManagerTest
, FailedOneShotStillExists
) {
1108 InitFailedSyncEventTest();
1110 EXPECT_TRUE(Register(sync_options_1_
));
1111 EXPECT_EQ(1, sync_events_called_
);
1112 EXPECT_TRUE(GetRegistration(sync_options_1_
));
1114 // The failed one-shot should stay registered but not fire until the
1115 // ServiceWorker is reloaded with an active client. Therefore, changing the
1116 // network should not cause the event to run again.
1117 SetNetwork(net::NetworkChangeNotifier::CONNECTION_2G
);
1118 EXPECT_EQ(1, sync_events_called_
);
1119 EXPECT_TRUE(GetRegistration(sync_options_1_
));
1122 TEST_F(BackgroundSyncManagerTest
, FailedOneShotReregisteredAndFires
) {
1123 InitFailedSyncEventTest();
1125 // The initial sync event fails.
1126 EXPECT_TRUE(Register(sync_options_1_
));
1127 EXPECT_EQ(1, sync_events_called_
);
1128 EXPECT_TRUE(GetRegistration(sync_options_1_
));
1129 BackgroundSyncRegistration first_registration
= callback_registration_
;
1131 InitSyncEventTest();
1133 // Reregistering should cause the sync event to fire again, this time
1135 EXPECT_TRUE(Register(sync_options_1_
));
1136 EXPECT_EQ(first_registration
.id(), callback_registration_
.id());
1137 EXPECT_EQ(2, sync_events_called_
);
1138 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1141 TEST_F(BackgroundSyncManagerTest
, DelayOneShotMidSync
) {
1142 InitDelayedSyncEventTest();
1144 RegisterAndVerifySyncEventDelayed(sync_options_1_
);
1146 // Finish firing the event and verify that the registration is removed.
1147 sync_fired_callback_
.Run(SERVICE_WORKER_OK
);
1148 base::RunLoop().RunUntilIdle();
1149 EXPECT_EQ(1, sync_events_called_
);
1150 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1153 TEST_F(BackgroundSyncManagerTest
, OverwriteRegistrationMidSync
) {
1154 InitDelayedSyncEventTest();
1156 sync_options_1_
.network_state
= NETWORK_STATE_ANY
;
1157 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE
);
1159 RegisterAndVerifySyncEventDelayed(sync_options_1_
);
1161 // Don't delay the next sync.
1162 test_background_sync_manager_
->set_one_shot_callback(
1163 base::Bind(OneShotSuccessfulCallback
, &sync_events_called_
));
1165 // Register a different sync event with the same tag, overwriting the first.
1166 sync_options_1_
.network_state
= NETWORK_STATE_ONLINE
;
1167 EXPECT_TRUE(Register(sync_options_1_
));
1169 // The new sync event won't run as the network requirements aren't met.
1170 EXPECT_EQ(1, sync_events_called_
);
1171 EXPECT_TRUE(GetRegistration(sync_options_1_
));
1173 // Finish the first event, note that the second is still registered.
1174 sync_fired_callback_
.Run(SERVICE_WORKER_OK
);
1175 EXPECT_EQ(1, sync_events_called_
);
1176 EXPECT_TRUE(GetRegistration(sync_options_1_
));
1178 // Change the network and the second should run.
1179 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI
);
1180 base::RunLoop().RunUntilIdle();
1181 EXPECT_EQ(2, sync_events_called_
);
1182 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1185 TEST_F(BackgroundSyncManagerTest
, ReregisterOneShotMidSync
) {
1186 InitDelayedSyncEventTest();
1188 RegisterAndVerifySyncEventDelayed(sync_options_1_
);
1190 // Register the same sync, but don't delay it. It shouldn't run as it's
1192 test_background_sync_manager_
->set_one_shot_callback(
1193 base::Bind(OneShotSuccessfulCallback
, &sync_events_called_
));
1194 EXPECT_TRUE(Register(sync_options_1_
));
1195 EXPECT_EQ(1, sync_events_called_
);
1196 EXPECT_TRUE(GetRegistration(sync_options_1_
));
1198 // Finish the original event, note that the second never runs.
1199 sync_fired_callback_
.Run(SERVICE_WORKER_OK
);
1200 base::RunLoop().RunUntilIdle();
1201 EXPECT_EQ(1, sync_events_called_
);
1202 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1205 TEST_F(BackgroundSyncManagerTest
, UnregisterOneShotMidSync
) {
1206 InitDelayedSyncEventTest();
1208 RegisterAndVerifySyncEventDelayed(sync_options_1_
);
1210 EXPECT_TRUE(Unregister(callback_registration_
));
1211 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1213 sync_fired_callback_
.Run(SERVICE_WORKER_OK
);
1214 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1217 TEST_F(BackgroundSyncManagerTest
, BadBackendMidSync
) {
1218 InitDelayedSyncEventTest();
1220 RegisterAndVerifySyncEventDelayed(sync_options_1_
);
1222 test_background_sync_manager_
->set_corrupt_backend(true);
1223 sync_fired_callback_
.Run(SERVICE_WORKER_OK
);
1224 base::RunLoop().RunUntilIdle();
1226 // The backend should now be disabled because it couldn't unregister the
1228 EXPECT_FALSE(Register(sync_options_2_
));
1230 RegisterWithServiceWorkerId(sw_registration_id_2_
, sync_options_2_
));
1233 TEST_F(BackgroundSyncManagerTest
, UnregisterServiceWorkerMidSync
) {
1234 InitDelayedSyncEventTest();
1236 RegisterAndVerifySyncEventDelayed(sync_options_1_
);
1237 UnregisterServiceWorker(sw_registration_id_1_
);
1239 sync_fired_callback_
.Run(SERVICE_WORKER_OK
);
1241 // The backend isn't disabled, but the first service worker registration is
1243 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1244 EXPECT_FALSE(Register(sync_options_1_
));
1246 RegisterWithServiceWorkerId(sw_registration_id_2_
, sync_options_1_
));
1249 TEST_F(BackgroundSyncManagerTest
, KillManagerMidSync
) {
1250 InitDelayedSyncEventTest();
1252 RegisterAndVerifySyncEventDelayed(sync_options_1_
);
1254 // Create a new manager which should fire the sync again on init.
1255 SetupBackgroundSyncManager();
1256 InitSyncEventTest();
1257 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1258 EXPECT_EQ(2, sync_events_called_
);
1261 TEST_F(BackgroundSyncManagerTest
, RegisterFailsWithoutWindow
) {
1262 RemoveWindowClients();
1263 EXPECT_FALSE(Register(sync_options_1_
));
1266 TEST_F(BackgroundSyncManagerTest
, RegisterExistingFailsWithoutWindow
) {
1267 EXPECT_TRUE(Register(sync_options_1_
));
1268 RemoveWindowClients();
1269 EXPECT_FALSE(Register(sync_options_1_
));
1272 TEST_F(BackgroundSyncManagerTest
, UnregisterSucceedsWithoutWindow
) {
1273 EXPECT_TRUE(Register(sync_options_1_
));
1274 RemoveWindowClients();
1275 EXPECT_TRUE(Unregister(callback_registration_
));
1276 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1279 } // namespace content