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/browser_thread_impl.h"
16 #include "content/browser/service_worker/embedded_worker_test_helper.h"
17 #include "content/browser/service_worker/service_worker_context_core.h"
18 #include "content/browser/service_worker/service_worker_context_wrapper.h"
19 #include "content/browser/service_worker/service_worker_storage.h"
20 #include "content/public/test/test_browser_thread_bundle.h"
21 #include "net/base/network_change_notifier.h"
22 #include "testing/gtest/include/gtest/gtest.h"
28 const char kPattern1
[] = "https://example.com/a";
29 const char kPattern2
[] = "https://example.com/b";
30 const char kScript1
[] = "https://example.com/a/script.js";
31 const char kScript2
[] = "https://example.com/b/script.js";
32 const int kRenderProcessId
= 99;
34 void RegisterServiceWorkerCallback(bool* called
,
35 int64
* store_registration_id
,
36 ServiceWorkerStatusCode status
,
37 const std::string
& status_message
,
38 int64 registration_id
) {
39 EXPECT_EQ(SERVICE_WORKER_OK
, status
) << ServiceWorkerStatusToString(status
);
41 *store_registration_id
= registration_id
;
44 void FindServiceWorkerRegistrationCallback(
45 scoped_refptr
<ServiceWorkerRegistration
>* out_registration
,
46 ServiceWorkerStatusCode status
,
47 const scoped_refptr
<ServiceWorkerRegistration
>& registration
) {
48 EXPECT_EQ(SERVICE_WORKER_OK
, status
) << ServiceWorkerStatusToString(status
);
49 *out_registration
= registration
;
52 void UnregisterServiceWorkerCallback(bool* called
,
53 ServiceWorkerStatusCode code
) {
54 EXPECT_EQ(SERVICE_WORKER_OK
, code
);
58 void OneShotSuccessfulCallback(
60 const scoped_refptr
<ServiceWorkerVersion
>& active_version
,
61 const ServiceWorkerVersion::StatusCallback
& callback
) {
63 callback
.Run(SERVICE_WORKER_OK
);
66 void OneShotFailedCallback(
68 const scoped_refptr
<ServiceWorkerVersion
>& active_version
,
69 const ServiceWorkerVersion::StatusCallback
& callback
) {
71 callback
.Run(SERVICE_WORKER_ERROR_FAILED
);
74 void OneShotDelayedCallback(
76 ServiceWorkerVersion::StatusCallback
* out_callback
,
77 const scoped_refptr
<ServiceWorkerVersion
>& active_version
,
78 const ServiceWorkerVersion::StatusCallback
& callback
) {
80 *out_callback
= callback
;
83 class TestPowerSource
: public base::PowerMonitorSource
{
85 void GeneratePowerStateEvent(bool on_battery_power
) {
86 test_on_battery_power_
= on_battery_power
;
87 ProcessPowerEvent(POWER_STATE_EVENT
);
91 bool IsOnBatteryPowerImpl() final
{ return test_on_battery_power_
; }
92 bool test_on_battery_power_
= false;
97 // A BackgroundSyncManager that can simulate delaying and corrupting the backend
98 // storage and service worker onsync events.
99 class TestBackgroundSyncManager
: public BackgroundSyncManager
{
101 using OneShotCallback
=
102 base::Callback
<void(const scoped_refptr
<ServiceWorkerVersion
>&,
103 const ServiceWorkerVersion::StatusCallback
&)>;
105 explicit TestBackgroundSyncManager(
106 const scoped_refptr
<ServiceWorkerContextWrapper
>& service_worker_context
)
107 : BackgroundSyncManager(service_worker_context
) {}
109 void DoInit() { Init(); }
111 void StoreDataInBackendContinue(
112 int64 sw_registration_id
,
114 const std::string
& key
,
115 const std::string
& data
,
116 const ServiceWorkerStorage::StatusCallback
& callback
) {
117 BackgroundSyncManager::StoreDataInBackend(sw_registration_id
, origin
, key
,
121 void GetDataFromBackendContinue(
122 const std::string
& key
,
123 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback
&
125 BackgroundSyncManager::GetDataFromBackend(key
, callback
);
130 continuation_
.Reset();
133 void set_corrupt_backend(bool corrupt_backend
) {
134 corrupt_backend_
= corrupt_backend
;
136 void set_delay_backend(bool delay_backend
) { delay_backend_
= delay_backend
; }
137 void set_one_shot_callback(const OneShotCallback
& callback
) {
138 one_shot_callback_
= callback
;
142 void StoreDataInBackend(
143 int64 sw_registration_id
,
145 const std::string
& key
,
146 const std::string
& data
,
147 const ServiceWorkerStorage::StatusCallback
& callback
) override
{
148 EXPECT_TRUE(continuation_
.is_null());
149 if (corrupt_backend_
) {
150 base::ThreadTaskRunnerHandle::Get()->PostTask(
151 FROM_HERE
, base::Bind(callback
, SERVICE_WORKER_ERROR_FAILED
));
155 base::Bind(&TestBackgroundSyncManager::StoreDataInBackendContinue
,
156 base::Unretained(this), sw_registration_id
, origin
, key
,
164 void GetDataFromBackend(
165 const std::string
& key
,
166 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback
&
168 EXPECT_TRUE(continuation_
.is_null());
169 if (corrupt_backend_
) {
170 base::ThreadTaskRunnerHandle::Get()->PostTask(
172 base::Bind(callback
, std::vector
<std::pair
<int64
, std::string
>>(),
173 SERVICE_WORKER_ERROR_FAILED
));
177 base::Bind(&TestBackgroundSyncManager::GetDataFromBackendContinue
,
178 base::Unretained(this), key
, callback
);
185 void FireOneShotSync(
186 const BackgroundSyncRegistration
& registration
,
187 const scoped_refptr
<ServiceWorkerVersion
>& active_version
,
188 const ServiceWorkerVersion::StatusCallback
& callback
) override
{
189 if (one_shot_callback_
.is_null()) {
190 BackgroundSyncManager::FireOneShotSync(registration
, active_version
,
193 one_shot_callback_
.Run(active_version
, callback
);
198 bool corrupt_backend_
= false;
199 bool delay_backend_
= false;
200 base::Closure continuation_
;
201 OneShotCallback one_shot_callback_
;
204 class BackgroundSyncManagerTest
: public testing::Test
{
206 BackgroundSyncManagerTest()
207 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP
),
208 network_change_notifier_(net::NetworkChangeNotifier::CreateMock()),
209 test_background_sync_manager_(nullptr),
210 callback_error_(BackgroundSyncManager::ERROR_TYPE_OK
),
211 callback_sw_status_code_(SERVICE_WORKER_OK
),
212 sync_events_called_(0) {
213 sync_options_1_
.tag
= "foo";
214 sync_options_1_
.periodicity
= SYNC_ONE_SHOT
;
215 sync_options_1_
.network_state
= NETWORK_STATE_ONLINE
;
216 sync_options_1_
.power_state
= POWER_STATE_AUTO
;
218 sync_options_2_
.tag
= "bar";
219 sync_options_2_
.periodicity
= SYNC_ONE_SHOT
;
220 sync_options_2_
.network_state
= NETWORK_STATE_ONLINE
;
221 sync_options_2_
.power_state
= POWER_STATE_AUTO
;
224 void SetUp() override
{
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 SetupBackgroundSyncManager();
237 // Wait for storage to finish initializing before registering service
239 base::RunLoop().RunUntilIdle();
240 RegisterServiceWorkers();
243 void RegisterServiceWorkers() {
244 bool called_1
= false;
245 bool called_2
= false;
246 helper_
->context()->RegisterServiceWorker(
247 GURL(kPattern1
), GURL(kScript1
), NULL
,
248 base::Bind(&RegisterServiceWorkerCallback
, &called_1
,
249 &sw_registration_id_1_
));
251 helper_
->context()->RegisterServiceWorker(
252 GURL(kPattern2
), GURL(kScript2
), NULL
,
253 base::Bind(&RegisterServiceWorkerCallback
, &called_2
,
254 &sw_registration_id_2_
));
255 base::RunLoop().RunUntilIdle();
256 EXPECT_TRUE(called_1
);
257 EXPECT_TRUE(called_2
);
259 // Register window clients for the service workers
260 host_1_
.reset(new ServiceWorkerProviderHost(
261 34 /* dummy render proces id */, MSG_ROUTING_NONE
/* render_frame_id */,
262 1 /* dummy provider id */, SERVICE_WORKER_PROVIDER_FOR_WINDOW
,
263 helper_
->context()->AsWeakPtr(), nullptr));
264 host_1_
->SetDocumentUrl(GURL(kPattern1
));
265 host_2_
.reset(new ServiceWorkerProviderHost(
266 34 /* dummy render proces id */, MSG_ROUTING_NONE
/* render_frame_id */,
267 1 /* dummy provider id */, SERVICE_WORKER_PROVIDER_FOR_WINDOW
,
268 helper_
->context()->AsWeakPtr(), nullptr));
269 host_2_
->SetDocumentUrl(GURL(kPattern2
));
271 // Hang onto the registrations as they need to be "live" when
272 // calling BackgroundSyncManager::Register.
273 helper_
->context_wrapper()->FindRegistrationForId(
274 sw_registration_id_1_
, GURL(kPattern1
).GetOrigin(),
275 base::Bind(FindServiceWorkerRegistrationCallback
, &sw_registration_1_
));
277 helper_
->context_wrapper()->FindRegistrationForId(
278 sw_registration_id_2_
, GURL(kPattern1
).GetOrigin(),
279 base::Bind(FindServiceWorkerRegistrationCallback
, &sw_registration_2_
));
280 base::RunLoop().RunUntilIdle();
281 EXPECT_TRUE(sw_registration_1_
);
282 EXPECT_TRUE(sw_registration_2_
);
284 sw_registration_1_
->active_version()->AddControllee(host_1_
.get());
285 sw_registration_2_
->active_version()->AddControllee(host_2_
.get());
288 void RemoveWindowClients() {
289 sw_registration_1_
->active_version()->RemoveControllee(host_1_
.get());
290 sw_registration_2_
->active_version()->RemoveControllee(host_2_
.get());
293 void SetNetwork(net::NetworkChangeNotifier::ConnectionType connection_type
) {
294 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
296 base::RunLoop().RunUntilIdle();
299 void SetOnBatteryPower(bool on_battery_power
) {
300 power_monitor_source_
->GeneratePowerStateEvent(on_battery_power
);
301 base::RunLoop().RunUntilIdle();
304 void StatusAndRegistrationCallback(
306 BackgroundSyncManager::ErrorType error
,
307 const BackgroundSyncRegistration
& registration
) {
309 callback_error_
= error
;
310 callback_registration_
= registration
;
313 void StatusAndRegistrationsCallback(
315 BackgroundSyncManager::ErrorType error
,
316 const std::vector
<BackgroundSyncRegistration
>& registrations
) {
318 callback_error_
= error
;
319 callback_registrations_
= registrations
;
322 void StatusCallback(bool* was_called
,
323 BackgroundSyncManager::ErrorType error
) {
325 callback_error_
= error
;
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_error_
== BackgroundSyncManager::ERROR_TYPE_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_error_
== BackgroundSyncManager::ERROR_TYPE_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_error_
== BackgroundSyncManager::ERROR_TYPE_OK
) {
421 EXPECT_STREQ(sync_options
.tag
.c_str(),
422 callback_registration_
.options()->tag
.c_str());
425 return callback_error_
== BackgroundSyncManager::ERROR_TYPE_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_error_
== BackgroundSyncManager::ERROR_TYPE_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 BackgroundSyncManager::ErrorType callback_error_
;
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(BackgroundSyncManager::ERROR_TYPE_NO_SERVICE_WORKER
,
542 TEST_F(BackgroundSyncManagerTest
, RegisterWithoutActiveSWRegistration
) {
543 sw_registration_1_
->UnsetVersion(sw_registration_1_
->active_version());
544 EXPECT_FALSE(Register(sync_options_1_
));
545 EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_NO_SERVICE_WORKER
,
549 TEST_F(BackgroundSyncManagerTest
, RegisterExistingKeepsId
) {
550 EXPECT_TRUE(Register(sync_options_1_
));
551 BackgroundSyncRegistration first_registration
= callback_registration_
;
552 EXPECT_TRUE(Register(sync_options_1_
));
553 EXPECT_TRUE(callback_registration_
.Equals(first_registration
));
554 EXPECT_EQ(first_registration
.id(), callback_registration_
.id());
557 TEST_F(BackgroundSyncManagerTest
, RegisterOverwrites
) {
558 EXPECT_TRUE(Register(sync_options_1_
));
559 BackgroundSyncRegistration first_registration
= callback_registration_
;
561 sync_options_1_
.min_period
= 100;
562 EXPECT_TRUE(Register(sync_options_1_
));
563 EXPECT_LT(first_registration
.id(), callback_registration_
.id());
564 EXPECT_FALSE(callback_registration_
.Equals(first_registration
));
567 TEST_F(BackgroundSyncManagerTest
, RegisterOverlappingPeriodicAndOneShotTags
) {
568 // Registrations with the same tags but different periodicities should not
570 sync_options_1_
.tag
= "";
571 sync_options_2_
.tag
= "";
572 sync_options_1_
.periodicity
= SYNC_PERIODIC
;
573 sync_options_2_
.periodicity
= SYNC_ONE_SHOT
;
574 EXPECT_TRUE(Register(sync_options_1_
));
575 EXPECT_TRUE(Register(sync_options_2_
));
576 EXPECT_TRUE(GetRegistration(sync_options_1_
));
577 EXPECT_EQ(SYNC_PERIODIC
, callback_registration_
.options()->periodicity
);
578 EXPECT_TRUE(GetRegistration(sync_options_2_
));
579 EXPECT_EQ(SYNC_ONE_SHOT
, callback_registration_
.options()->periodicity
);
582 TEST_F(BackgroundSyncManagerTest
, RegisterBadBackend
) {
583 test_background_sync_manager_
->set_corrupt_backend(true);
584 EXPECT_FALSE(Register(sync_options_1_
));
585 test_background_sync_manager_
->set_corrupt_backend(false);
586 EXPECT_FALSE(Register(sync_options_1_
));
587 EXPECT_FALSE(GetRegistration(sync_options_1_
));
590 TEST_F(BackgroundSyncManagerTest
, TwoRegistrations
) {
591 EXPECT_TRUE(Register(sync_options_1_
));
592 EXPECT_TRUE(Register(sync_options_2_
));
595 TEST_F(BackgroundSyncManagerTest
, GetRegistrationNonExisting
) {
596 EXPECT_FALSE(GetRegistration(sync_options_1_
));
599 TEST_F(BackgroundSyncManagerTest
, GetRegistrationExisting
) {
600 EXPECT_TRUE(Register(sync_options_1_
));
601 EXPECT_TRUE(GetRegistration(sync_options_1_
));
602 EXPECT_FALSE(GetRegistration(sync_options_2_
));
605 TEST_F(BackgroundSyncManagerTest
, GetRegistrationBadBackend
) {
606 EXPECT_TRUE(Register(sync_options_1_
));
607 test_background_sync_manager_
->set_corrupt_backend(true);
608 EXPECT_TRUE(GetRegistration(sync_options_1_
));
609 EXPECT_FALSE(Register(sync_options_2_
));
610 // Registration should have discovered the bad backend and disabled the
611 // BackgroundSyncManager.
612 EXPECT_FALSE(GetRegistration(sync_options_1_
));
613 test_background_sync_manager_
->set_corrupt_backend(false);
614 EXPECT_FALSE(GetRegistration(sync_options_1_
));
617 TEST_F(BackgroundSyncManagerTest
, GetRegistrationsZero
) {
618 EXPECT_TRUE(GetRegistrations(SYNC_ONE_SHOT
));
619 EXPECT_EQ(0u, callback_registrations_
.size());
622 TEST_F(BackgroundSyncManagerTest
, GetRegistrationsOne
) {
623 EXPECT_TRUE(Register(sync_options_1_
));
624 EXPECT_TRUE(GetRegistrations(sync_options_1_
.periodicity
));
626 EXPECT_EQ(1u, callback_registrations_
.size());
627 sync_options_1_
.Equals(*callback_registrations_
[0].options());
630 TEST_F(BackgroundSyncManagerTest
, GetRegistrationsTwo
) {
631 EXPECT_EQ(sync_options_1_
.periodicity
, sync_options_2_
.periodicity
);
633 EXPECT_TRUE(Register(sync_options_1_
));
634 EXPECT_TRUE(Register(sync_options_2_
));
635 EXPECT_TRUE(GetRegistrations(sync_options_1_
.periodicity
));
637 EXPECT_EQ(2u, callback_registrations_
.size());
638 sync_options_1_
.Equals(*callback_registrations_
[0].options());
639 sync_options_2_
.Equals(*callback_registrations_
[1].options());
642 TEST_F(BackgroundSyncManagerTest
, GetRegistrationsPeriodicity
) {
643 sync_options_1_
.periodicity
= SYNC_ONE_SHOT
;
644 sync_options_2_
.periodicity
= SYNC_PERIODIC
;
645 EXPECT_TRUE(Register(sync_options_1_
));
646 EXPECT_TRUE(Register(sync_options_2_
));
648 EXPECT_TRUE(GetRegistrations(SYNC_ONE_SHOT
));
649 EXPECT_EQ(1u, callback_registrations_
.size());
650 sync_options_1_
.Equals(*callback_registrations_
[0].options());
652 EXPECT_TRUE(GetRegistrations(SYNC_PERIODIC
));
653 EXPECT_EQ(1u, callback_registrations_
.size());
654 sync_options_2_
.Equals(*callback_registrations_
[0].options());
657 TEST_F(BackgroundSyncManagerTest
, GetRegistrationsBadBackend
) {
658 EXPECT_TRUE(Register(sync_options_1_
));
659 test_background_sync_manager_
->set_corrupt_backend(true);
660 EXPECT_TRUE(GetRegistrations(sync_options_1_
.periodicity
));
661 EXPECT_FALSE(Register(sync_options_2_
));
662 // Registration should have discovered the bad backend and disabled the
663 // BackgroundSyncManager.
664 EXPECT_FALSE(GetRegistrations(sync_options_1_
.periodicity
));
665 test_background_sync_manager_
->set_corrupt_backend(false);
666 EXPECT_FALSE(GetRegistrations(sync_options_1_
.periodicity
));
669 TEST_F(BackgroundSyncManagerTest
, Unregister
) {
670 EXPECT_TRUE(Register(sync_options_1_
));
671 EXPECT_TRUE(Unregister(callback_registration_
));
672 EXPECT_FALSE(GetRegistration(sync_options_1_
));
675 TEST_F(BackgroundSyncManagerTest
, UnregisterWrongId
) {
676 EXPECT_TRUE(Register(sync_options_1_
));
677 callback_registration_
.set_id(callback_registration_
.id() + 1);
678 EXPECT_FALSE(Unregister(callback_registration_
));
681 TEST_F(BackgroundSyncManagerTest
, Reregister
) {
682 EXPECT_TRUE(Register(sync_options_1_
));
683 EXPECT_TRUE(Unregister(callback_registration_
));
684 EXPECT_TRUE(Register(sync_options_1_
));
687 TEST_F(BackgroundSyncManagerTest
, UnregisterNonExisting
) {
688 BackgroundSyncRegistration nonexistant_registration
;
689 nonexistant_registration
.set_id(1);
690 EXPECT_FALSE(Unregister(nonexistant_registration
));
691 EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_NOT_FOUND
, callback_error_
);
694 TEST_F(BackgroundSyncManagerTest
, UnregisterSecond
) {
695 EXPECT_TRUE(Register(sync_options_1_
));
696 EXPECT_TRUE(Register(sync_options_2_
));
697 EXPECT_TRUE(Unregister(callback_registration_
));
698 EXPECT_TRUE(GetRegistration(sync_options_1_
));
699 EXPECT_TRUE(Register(sync_options_2_
));
702 TEST_F(BackgroundSyncManagerTest
, UnregisterBadBackend
) {
703 sync_options_1_
.min_period
+= 1;
704 EXPECT_TRUE(Register(sync_options_1_
));
705 EXPECT_TRUE(Register(sync_options_2_
));
706 test_background_sync_manager_
->set_corrupt_backend(true);
707 EXPECT_FALSE(Unregister(callback_registration_
));
708 // Unregister should have discovered the bad backend and disabled the
709 // BackgroundSyncManager.
710 test_background_sync_manager_
->set_corrupt_backend(false);
711 EXPECT_FALSE(GetRegistration(sync_options_1_
));
712 EXPECT_FALSE(GetRegistration(sync_options_2_
));
715 TEST_F(BackgroundSyncManagerTest
, RegistrationIncreasesId
) {
716 EXPECT_TRUE(Register(sync_options_1_
));
717 BackgroundSyncRegistration registered_sync
= callback_registration_
;
718 BackgroundSyncRegistration::RegistrationId cur_id
=
719 callback_registration_
.id();
721 EXPECT_TRUE(GetRegistration(sync_options_1_
));
722 EXPECT_TRUE(Register(sync_options_2_
));
723 EXPECT_LT(cur_id
, callback_registration_
.id());
724 cur_id
= callback_registration_
.id();
726 EXPECT_TRUE(Unregister(registered_sync
));
727 EXPECT_TRUE(Register(sync_options_1_
));
728 EXPECT_LT(cur_id
, callback_registration_
.id());
731 TEST_F(BackgroundSyncManagerTest
, RebootRecovery
) {
732 EXPECT_TRUE(Register(sync_options_1_
));
734 SetupBackgroundSyncManager();
736 EXPECT_TRUE(GetRegistration(sync_options_1_
));
737 EXPECT_FALSE(GetRegistration(sync_options_2_
));
740 TEST_F(BackgroundSyncManagerTest
, RebootRecoveryTwoServiceWorkers
) {
742 RegisterWithServiceWorkerId(sw_registration_id_1_
, sync_options_1_
));
744 RegisterWithServiceWorkerId(sw_registration_id_2_
, sync_options_2_
));
746 SetupBackgroundSyncManager();
748 EXPECT_TRUE(GetRegistrationWithServiceWorkerId(sw_registration_id_1_
,
750 EXPECT_FALSE(GetRegistrationWithServiceWorkerId(sw_registration_id_1_
,
752 EXPECT_FALSE(GetRegistrationWithServiceWorkerId(sw_registration_id_2_
,
754 EXPECT_TRUE(GetRegistrationWithServiceWorkerId(sw_registration_id_2_
,
757 EXPECT_TRUE(GetRegistrationWithServiceWorkerId(sw_registration_id_1_
,
759 EXPECT_TRUE(GetRegistrationWithServiceWorkerId(sw_registration_id_2_
,
763 RegisterWithServiceWorkerId(sw_registration_id_1_
, sync_options_2_
));
765 RegisterWithServiceWorkerId(sw_registration_id_2_
, sync_options_1_
));
768 TEST_F(BackgroundSyncManagerTest
, InitWithBadBackend
) {
769 SetupCorruptBackgroundSyncManager();
771 EXPECT_FALSE(Register(sync_options_1_
));
772 EXPECT_FALSE(GetRegistration(sync_options_1_
));
775 TEST_F(BackgroundSyncManagerTest
, SequentialOperations
) {
776 // Schedule Init and all of the operations on a delayed backend. Verify that
777 // the operations complete sequentially.
778 SetupDelayedBackgroundSyncManager();
780 const int64 kExpectedInitialId
= BackgroundSyncRegistration::kInitialId
;
782 bool register_called
= false;
783 bool unregister_called
= false;
784 bool get_registration_called
= false;
785 test_background_sync_manager_
->Register(
786 sw_registration_id_1_
, sync_options_1_
,
787 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback
,
788 base::Unretained(this), ®ister_called
));
789 test_background_sync_manager_
->Unregister(
790 sw_registration_id_1_
, sync_options_1_
.tag
, sync_options_1_
.periodicity
,
792 base::Bind(&BackgroundSyncManagerTest::StatusCallback
,
793 base::Unretained(this), &unregister_called
));
794 test_background_sync_manager_
->GetRegistration(
795 sw_registration_id_1_
, sync_options_1_
.tag
, sync_options_1_
.periodicity
,
796 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback
,
797 base::Unretained(this), &get_registration_called
));
799 base::RunLoop().RunUntilIdle();
800 // Init should be blocked while loading from the backend.
801 EXPECT_FALSE(register_called
);
802 EXPECT_FALSE(unregister_called
);
803 EXPECT_FALSE(get_registration_called
);
805 test_background_sync_manager_
->Continue();
806 base::RunLoop().RunUntilIdle();
807 // Register should be blocked while storing to the backend.
808 EXPECT_FALSE(register_called
);
809 EXPECT_FALSE(unregister_called
);
810 EXPECT_FALSE(get_registration_called
);
812 test_background_sync_manager_
->Continue();
813 base::RunLoop().RunUntilIdle();
814 EXPECT_TRUE(register_called
);
815 EXPECT_EQ(kExpectedInitialId
, callback_registration_
.id());
816 EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_OK
, callback_error_
);
817 // Unregister should be blocked while storing to the backend.
818 EXPECT_FALSE(unregister_called
);
819 EXPECT_FALSE(get_registration_called
);
821 test_background_sync_manager_
->Continue();
822 base::RunLoop().RunUntilIdle();
823 // Unregister should be done and since GetRegistration doesn't require the
824 // backend it should be done too.
825 EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_NOT_FOUND
, callback_error_
);
826 EXPECT_TRUE(unregister_called
);
827 EXPECT_TRUE(get_registration_called
);
830 TEST_F(BackgroundSyncManagerTest
, UnregisterServiceWorker
) {
831 EXPECT_TRUE(Register(sync_options_1_
));
832 UnregisterServiceWorker(sw_registration_id_1_
);
833 EXPECT_FALSE(GetRegistration(sync_options_1_
));
836 TEST_F(BackgroundSyncManagerTest
,
837 UnregisterServiceWorkerDuringSyncRegistration
) {
838 EXPECT_TRUE(Register(sync_options_1_
));
840 test_background_sync_manager_
->set_delay_backend(true);
841 bool callback_called
= false;
842 test_background_sync_manager_
->Register(
843 sw_registration_id_1_
, sync_options_2_
,
844 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback
,
845 base::Unretained(this), &callback_called
));
847 base::RunLoop().RunUntilIdle();
848 EXPECT_FALSE(callback_called
);
849 UnregisterServiceWorker(sw_registration_id_1_
);
851 test_background_sync_manager_
->Continue();
852 base::RunLoop().RunUntilIdle();
853 EXPECT_TRUE(callback_called
);
854 EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_STORAGE
, callback_error_
);
856 test_background_sync_manager_
->set_delay_backend(false);
857 EXPECT_FALSE(GetRegistration(sync_options_1_
));
860 TEST_F(BackgroundSyncManagerTest
, DeleteAndStartOverServiceWorkerContext
) {
861 EXPECT_TRUE(Register(sync_options_1_
));
862 helper_
->context()->ScheduleDeleteAndStartOver();
863 base::RunLoop().RunUntilIdle();
864 EXPECT_FALSE(GetRegistration(sync_options_1_
));
867 TEST_F(BackgroundSyncManagerTest
, DisabledManagerWorksAfterBrowserRestart
) {
868 EXPECT_TRUE(Register(sync_options_1_
));
869 test_background_sync_manager_
->set_corrupt_backend(true);
870 EXPECT_FALSE(Register(sync_options_2_
));
872 // The manager is now disabled and not accepting new requests until browser
873 // restart or notification that the storage has been wiped.
874 test_background_sync_manager_
->set_corrupt_backend(false);
875 EXPECT_FALSE(GetRegistration(sync_options_1_
));
876 EXPECT_FALSE(Register(sync_options_2_
));
878 // Simulate restarting the browser by creating a new BackgroundSyncManager.
879 SetupBackgroundSyncManager();
880 EXPECT_TRUE(GetRegistration(sync_options_1_
));
881 EXPECT_TRUE(Register(sync_options_2_
));
884 TEST_F(BackgroundSyncManagerTest
, DisabledManagerWorksAfterDeleteAndStartOver
) {
885 EXPECT_TRUE(Register(sync_options_1_
));
886 test_background_sync_manager_
->set_corrupt_backend(true);
887 EXPECT_FALSE(Register(sync_options_2_
));
889 // The manager is now disabled and not accepting new requests until browser
890 // restart or notification that the storage has been wiped.
891 test_background_sync_manager_
->set_corrupt_backend(false);
892 helper_
->context()->ScheduleDeleteAndStartOver();
893 base::RunLoop().RunUntilIdle();
895 RegisterServiceWorkers();
897 EXPECT_TRUE(Register(sync_options_2_
));
898 EXPECT_FALSE(GetRegistration(sync_options_1_
));
899 EXPECT_TRUE(GetRegistration(sync_options_2_
));
902 TEST_F(BackgroundSyncManagerTest
, RegistrationEqualsId
) {
903 BackgroundSyncRegistration reg_1
;
904 BackgroundSyncRegistration reg_2
;
906 EXPECT_TRUE(reg_1
.Equals(reg_2
));
907 reg_2
.set_id(reg_1
.id() + 1);
908 EXPECT_TRUE(reg_1
.Equals(reg_2
));
911 TEST_F(BackgroundSyncManagerTest
, RegistrationEqualsTag
) {
912 BackgroundSyncRegistration reg_1
;
913 BackgroundSyncRegistration reg_2
;
914 EXPECT_TRUE(reg_1
.Equals(reg_2
));
915 reg_2
.options()->tag
= "bar";
916 EXPECT_FALSE(reg_1
.Equals(reg_2
));
919 TEST_F(BackgroundSyncManagerTest
, RegistrationEqualsPeriodicity
) {
920 BackgroundSyncRegistration reg_1
;
921 BackgroundSyncRegistration reg_2
;
922 EXPECT_TRUE(reg_1
.Equals(reg_2
));
923 reg_1
.options()->periodicity
= SYNC_PERIODIC
;
924 reg_2
.options()->periodicity
= SYNC_ONE_SHOT
;
925 EXPECT_FALSE(reg_1
.Equals(reg_2
));
928 TEST_F(BackgroundSyncManagerTest
, RegistrationEqualsMinPeriod
) {
929 BackgroundSyncRegistration reg_1
;
930 BackgroundSyncRegistration reg_2
;
931 EXPECT_TRUE(reg_1
.Equals(reg_2
));
932 reg_2
.options()->min_period
= reg_1
.options()->min_period
+ 1;
933 EXPECT_FALSE(reg_1
.Equals(reg_2
));
936 TEST_F(BackgroundSyncManagerTest
, RegistrationEqualsNetworkState
) {
937 BackgroundSyncRegistration reg_1
;
938 BackgroundSyncRegistration reg_2
;
939 EXPECT_TRUE(reg_1
.Equals(reg_2
));
940 reg_1
.options()->network_state
= NETWORK_STATE_ANY
;
941 reg_2
.options()->network_state
= NETWORK_STATE_ONLINE
;
942 EXPECT_FALSE(reg_1
.Equals(reg_2
));
945 TEST_F(BackgroundSyncManagerTest
, RegistrationEqualsPowerState
) {
946 BackgroundSyncRegistration reg_1
;
947 BackgroundSyncRegistration reg_2
;
948 EXPECT_TRUE(reg_1
.Equals(reg_2
));
949 reg_1
.options()->power_state
= POWER_STATE_AUTO
;
950 reg_2
.options()->power_state
= POWER_STATE_AVOID_DRAINING
;
951 EXPECT_FALSE(reg_1
.Equals(reg_2
));
954 TEST_F(BackgroundSyncManagerTest
, StoreAndRetrievePreservesValues
) {
955 BackgroundSyncRegistrationOptions options
;
956 // Set non-default values for each field.
958 EXPECT_NE(SYNC_PERIODIC
, options
.periodicity
);
959 options
.periodicity
= SYNC_PERIODIC
;
960 options
.min_period
+= 1;
961 EXPECT_NE(NETWORK_STATE_ANY
, options
.network_state
);
962 options
.network_state
= NETWORK_STATE_ANY
;
963 EXPECT_NE(POWER_STATE_AUTO
, options
.power_state
);
964 options
.power_state
= POWER_STATE_AUTO
;
966 // Store the registration.
967 EXPECT_TRUE(Register(options
));
969 // Simulate restarting the sync manager, forcing the next read to come from
971 SetupBackgroundSyncManager();
973 EXPECT_TRUE(GetRegistration(options
));
974 EXPECT_TRUE(options
.Equals(*callback_registration_
.options()));
977 TEST_F(BackgroundSyncManagerTest
, EmptyTagSupported
) {
978 sync_options_1_
.tag
= "a";
979 EXPECT_TRUE(Register(sync_options_1_
));
980 EXPECT_TRUE(GetRegistration(sync_options_1_
));
981 EXPECT_TRUE(sync_options_1_
.Equals(*callback_registration_
.options()));
982 EXPECT_TRUE(Unregister(callback_registration_
));
983 EXPECT_FALSE(GetRegistration(sync_options_1_
));
986 TEST_F(BackgroundSyncManagerTest
, OverlappingPeriodicAndOneShotTags
) {
987 // Registrations with the same tags but different periodicities should not
989 sync_options_1_
.tag
= "";
990 sync_options_2_
.tag
= "";
991 sync_options_1_
.periodicity
= SYNC_PERIODIC
;
992 sync_options_2_
.periodicity
= SYNC_ONE_SHOT
;
994 EXPECT_TRUE(Register(sync_options_1_
));
995 EXPECT_TRUE(Register(sync_options_2_
));
997 EXPECT_TRUE(GetRegistration(sync_options_1_
));
998 EXPECT_EQ(SYNC_PERIODIC
, callback_registration_
.options()->periodicity
);
999 EXPECT_TRUE(GetRegistration(sync_options_2_
));
1000 EXPECT_EQ(SYNC_ONE_SHOT
, callback_registration_
.options()->periodicity
);
1002 EXPECT_TRUE(GetRegistration(sync_options_1_
));
1003 EXPECT_TRUE(Unregister(callback_registration_
));
1004 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1005 EXPECT_TRUE(GetRegistration(sync_options_2_
));
1006 EXPECT_EQ(SYNC_ONE_SHOT
, callback_registration_
.options()->periodicity
);
1008 EXPECT_TRUE(Unregister(callback_registration_
));
1009 EXPECT_FALSE(GetRegistration(sync_options_2_
));
1012 TEST_F(BackgroundSyncManagerTest
, OneShotFiresOnRegistration
) {
1013 InitSyncEventTest();
1015 EXPECT_TRUE(Register(sync_options_1_
));
1016 EXPECT_EQ(1, sync_events_called_
);
1017 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1020 // TODO(jkarlin): Change this to a periodic test as one-shots can't be power
1021 // dependent according to spec.
1022 TEST_F(BackgroundSyncManagerTest
, OneShotFiresOnPowerChange
) {
1023 InitSyncEventTest();
1024 sync_options_1_
.power_state
= POWER_STATE_AVOID_DRAINING
;
1026 SetOnBatteryPower(true);
1027 EXPECT_TRUE(Register(sync_options_1_
));
1028 EXPECT_EQ(0, sync_events_called_
);
1029 EXPECT_TRUE(GetRegistration(sync_options_1_
));
1031 SetOnBatteryPower(false);
1032 EXPECT_EQ(1, sync_events_called_
);
1033 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1036 // TODO(jkarlin): Change this to a periodic test as one-shots can't be power
1037 // dependent according to spec.
1038 TEST_F(BackgroundSyncManagerTest
, MultipleOneShotsFireOnPowerChange
) {
1039 InitSyncEventTest();
1040 sync_options_1_
.power_state
= POWER_STATE_AVOID_DRAINING
;
1041 sync_options_2_
.power_state
= POWER_STATE_AVOID_DRAINING
;
1043 SetOnBatteryPower(true);
1044 EXPECT_TRUE(Register(sync_options_1_
));
1045 EXPECT_TRUE(Register(sync_options_2_
));
1046 EXPECT_EQ(0, sync_events_called_
);
1047 EXPECT_TRUE(GetRegistration(sync_options_1_
));
1048 EXPECT_TRUE(GetRegistration(sync_options_2_
));
1050 SetOnBatteryPower(false);
1051 EXPECT_EQ(2, sync_events_called_
);
1052 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1053 EXPECT_FALSE(GetRegistration(sync_options_2_
));
1056 TEST_F(BackgroundSyncManagerTest
, OneShotFiresOnNetworkChange
) {
1057 InitSyncEventTest();
1059 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE
);
1060 EXPECT_TRUE(Register(sync_options_1_
));
1061 EXPECT_EQ(0, sync_events_called_
);
1062 EXPECT_TRUE(GetRegistration(sync_options_1_
));
1064 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI
);
1065 base::RunLoop().RunUntilIdle();
1066 EXPECT_EQ(1, sync_events_called_
);
1067 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1070 TEST_F(BackgroundSyncManagerTest
, MultipleOneShotsFireOnNetworkChange
) {
1071 InitSyncEventTest();
1073 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE
);
1074 EXPECT_TRUE(Register(sync_options_1_
));
1075 EXPECT_TRUE(Register(sync_options_2_
));
1076 EXPECT_EQ(0, sync_events_called_
);
1077 EXPECT_TRUE(GetRegistration(sync_options_1_
));
1078 EXPECT_TRUE(GetRegistration(sync_options_2_
));
1080 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI
);
1082 EXPECT_EQ(2, sync_events_called_
);
1083 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1084 EXPECT_FALSE(GetRegistration(sync_options_2_
));
1087 TEST_F(BackgroundSyncManagerTest
, OneShotFiresOnManagerRestart
) {
1088 InitSyncEventTest();
1090 // Initially the event won't run because there is no network.
1091 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE
);
1092 EXPECT_TRUE(Register(sync_options_1_
));
1093 EXPECT_EQ(0, sync_events_called_
);
1094 EXPECT_TRUE(GetRegistration(sync_options_1_
));
1096 // Simulate closing the browser.
1097 DeleteBackgroundSyncManager();
1099 // The next time the manager is started, the network is good.
1100 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI
);
1101 SetupBackgroundSyncManager();
1102 InitSyncEventTest();
1104 // The event should have fired.
1105 EXPECT_EQ(1, sync_events_called_
);
1106 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1109 TEST_F(BackgroundSyncManagerTest
, FailedOneShotStillExists
) {
1110 InitFailedSyncEventTest();
1112 EXPECT_TRUE(Register(sync_options_1_
));
1113 EXPECT_EQ(1, sync_events_called_
);
1114 EXPECT_TRUE(GetRegistration(sync_options_1_
));
1116 // The failed one-shot should stay registered but not fire until the
1117 // ServiceWorker is reloaded with an active client. Therefore, changing the
1118 // network should not cause the event to run again.
1119 SetNetwork(net::NetworkChangeNotifier::CONNECTION_2G
);
1120 EXPECT_EQ(1, sync_events_called_
);
1121 EXPECT_TRUE(GetRegistration(sync_options_1_
));
1124 TEST_F(BackgroundSyncManagerTest
, FailedOneShotReregisteredAndFires
) {
1125 InitFailedSyncEventTest();
1127 // The initial sync event fails.
1128 EXPECT_TRUE(Register(sync_options_1_
));
1129 EXPECT_EQ(1, sync_events_called_
);
1130 EXPECT_TRUE(GetRegistration(sync_options_1_
));
1131 BackgroundSyncRegistration first_registration
= callback_registration_
;
1133 InitSyncEventTest();
1135 // Reregistering should cause the sync event to fire again, this time
1137 EXPECT_TRUE(Register(sync_options_1_
));
1138 EXPECT_EQ(first_registration
.id(), callback_registration_
.id());
1139 EXPECT_EQ(2, sync_events_called_
);
1140 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1143 TEST_F(BackgroundSyncManagerTest
, DelayOneShotMidSync
) {
1144 InitDelayedSyncEventTest();
1146 RegisterAndVerifySyncEventDelayed(sync_options_1_
);
1148 // Finish firing the event and verify that the registration is removed.
1149 sync_fired_callback_
.Run(SERVICE_WORKER_OK
);
1150 base::RunLoop().RunUntilIdle();
1151 EXPECT_EQ(1, sync_events_called_
);
1152 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1155 TEST_F(BackgroundSyncManagerTest
, OverwriteRegistrationMidSync
) {
1156 InitDelayedSyncEventTest();
1158 sync_options_1_
.network_state
= NETWORK_STATE_ANY
;
1159 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE
);
1161 RegisterAndVerifySyncEventDelayed(sync_options_1_
);
1163 // Don't delay the next sync.
1164 test_background_sync_manager_
->set_one_shot_callback(
1165 base::Bind(OneShotSuccessfulCallback
, &sync_events_called_
));
1167 // Register a different sync event with the same tag, overwriting the first.
1168 sync_options_1_
.network_state
= NETWORK_STATE_ONLINE
;
1169 EXPECT_TRUE(Register(sync_options_1_
));
1171 // The new sync event won't run as the network requirements aren't met.
1172 EXPECT_EQ(1, sync_events_called_
);
1173 EXPECT_TRUE(GetRegistration(sync_options_1_
));
1175 // Finish the first event, note that the second is still registered.
1176 sync_fired_callback_
.Run(SERVICE_WORKER_OK
);
1177 EXPECT_EQ(1, sync_events_called_
);
1178 EXPECT_TRUE(GetRegistration(sync_options_1_
));
1180 // Change the network and the second should run.
1181 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI
);
1182 base::RunLoop().RunUntilIdle();
1183 EXPECT_EQ(2, sync_events_called_
);
1184 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1187 TEST_F(BackgroundSyncManagerTest
, ReregisterOneShotMidSync
) {
1188 InitDelayedSyncEventTest();
1190 RegisterAndVerifySyncEventDelayed(sync_options_1_
);
1192 // Register the same sync, but don't delay it. It shouldn't run as it's
1194 test_background_sync_manager_
->set_one_shot_callback(
1195 base::Bind(OneShotSuccessfulCallback
, &sync_events_called_
));
1196 EXPECT_TRUE(Register(sync_options_1_
));
1197 EXPECT_EQ(1, sync_events_called_
);
1198 EXPECT_TRUE(GetRegistration(sync_options_1_
));
1200 // Finish the original event, note that the second never runs.
1201 sync_fired_callback_
.Run(SERVICE_WORKER_OK
);
1202 base::RunLoop().RunUntilIdle();
1203 EXPECT_EQ(1, sync_events_called_
);
1204 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1207 TEST_F(BackgroundSyncManagerTest
, UnregisterOneShotMidSync
) {
1208 InitDelayedSyncEventTest();
1210 RegisterAndVerifySyncEventDelayed(sync_options_1_
);
1212 EXPECT_TRUE(Unregister(callback_registration_
));
1213 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1215 sync_fired_callback_
.Run(SERVICE_WORKER_OK
);
1216 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1219 TEST_F(BackgroundSyncManagerTest
, BadBackendMidSync
) {
1220 InitDelayedSyncEventTest();
1222 RegisterAndVerifySyncEventDelayed(sync_options_1_
);
1224 test_background_sync_manager_
->set_corrupt_backend(true);
1225 sync_fired_callback_
.Run(SERVICE_WORKER_OK
);
1226 base::RunLoop().RunUntilIdle();
1228 // The backend should now be disabled because it couldn't unregister the
1230 EXPECT_FALSE(Register(sync_options_2_
));
1232 RegisterWithServiceWorkerId(sw_registration_id_2_
, sync_options_2_
));
1235 TEST_F(BackgroundSyncManagerTest
, UnregisterServiceWorkerMidSync
) {
1236 InitDelayedSyncEventTest();
1238 RegisterAndVerifySyncEventDelayed(sync_options_1_
);
1239 UnregisterServiceWorker(sw_registration_id_1_
);
1241 sync_fired_callback_
.Run(SERVICE_WORKER_OK
);
1243 // The backend isn't disabled, but the first service worker registration is
1245 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1246 EXPECT_FALSE(Register(sync_options_1_
));
1248 RegisterWithServiceWorkerId(sw_registration_id_2_
, sync_options_1_
));
1251 TEST_F(BackgroundSyncManagerTest
, KillManagerMidSync
) {
1252 InitDelayedSyncEventTest();
1254 RegisterAndVerifySyncEventDelayed(sync_options_1_
);
1256 // Create a new manager which should fire the sync again on init.
1257 SetupBackgroundSyncManager();
1258 InitSyncEventTest();
1259 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1260 EXPECT_EQ(2, sync_events_called_
);
1263 TEST_F(BackgroundSyncManagerTest
, RegisterFailsWithoutWindow
) {
1264 RemoveWindowClients();
1265 EXPECT_FALSE(Register(sync_options_1_
));
1268 TEST_F(BackgroundSyncManagerTest
, RegisterExistingFailsWithoutWindow
) {
1269 EXPECT_TRUE(Register(sync_options_1_
));
1270 RemoveWindowClients();
1271 EXPECT_FALSE(Register(sync_options_1_
));
1274 TEST_F(BackgroundSyncManagerTest
, UnregisterSucceedsWithoutWindow
) {
1275 EXPECT_TRUE(Register(sync_options_1_
));
1276 RemoveWindowClients();
1277 EXPECT_TRUE(Unregister(callback_registration_
));
1278 EXPECT_FALSE(GetRegistration(sync_options_1_
));
1281 } // namespace content