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/run_loop.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "content/browser/browser_thread_impl.h"
12 #include "content/browser/service_worker/embedded_worker_test_helper.h"
13 #include "content/browser/service_worker/service_worker_context_core.h"
14 #include "content/browser/service_worker/service_worker_context_wrapper.h"
15 #include "content/browser/service_worker/service_worker_storage.h"
16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "testing/gtest/include/gtest/gtest.h"
23 const char kOrigin
[] = "https://example.com";
24 const char kPattern1
[] = "https://example.com/a";
25 const char kPattern2
[] = "https://example.com/b";
26 const char kScript1
[] = "https://example.com/a/script.js";
27 const char kScript2
[] = "https://example.com/b/script.js";
28 const int kRenderProcessId
= 99;
30 void RegisterServiceWorkerCallback(bool* called
,
31 int64
* store_registration_id
,
32 ServiceWorkerStatusCode status
,
33 const std::string
& status_message
,
34 int64 registration_id
) {
35 EXPECT_EQ(SERVICE_WORKER_OK
, status
) << ServiceWorkerStatusToString(status
);
37 *store_registration_id
= registration_id
;
40 void UnregisterServiceWorkerCallback(bool* called
,
41 ServiceWorkerStatusCode code
) {
42 EXPECT_EQ(SERVICE_WORKER_OK
, code
);
48 // A BackgroundSyncManager that can simulate delaying and corrupting the
49 // backend. This class assumes (and verifies) that only one operation runs at a
51 class TestBackgroundSyncManager
: public BackgroundSyncManager
{
53 explicit TestBackgroundSyncManager(
54 const scoped_refptr
<ServiceWorkerContextWrapper
>& service_worker_context
)
55 : BackgroundSyncManager(service_worker_context
) {}
57 void DoInit() { Init(); }
59 void StoreDataInBackendContinue(
60 int64 sw_registration_id
,
62 const std::string
& key
,
63 const std::string
& data
,
64 const ServiceWorkerStorage::StatusCallback
& callback
) {
65 BackgroundSyncManager::StoreDataInBackend(sw_registration_id
, origin
, key
,
69 void GetDataFromBackendContinue(
70 const std::string
& key
,
71 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback
&
73 BackgroundSyncManager::GetDataFromBackend(key
, callback
);
78 continuation_
.Reset();
81 void set_corrupt_backend(bool corrupt_backend
) {
82 corrupt_backend_
= corrupt_backend
;
84 void set_delay_backend(bool delay_backend
) { delay_backend_
= delay_backend
; }
87 void StoreDataInBackend(
88 int64 sw_registration_id
,
90 const std::string
& key
,
91 const std::string
& data
,
92 const ServiceWorkerStorage::StatusCallback
& callback
) override
{
93 EXPECT_TRUE(continuation_
.is_null());
94 if (corrupt_backend_
) {
95 base::MessageLoop::current()->PostTask(
96 FROM_HERE
, base::Bind(callback
, SERVICE_WORKER_ERROR_FAILED
));
100 base::Bind(&TestBackgroundSyncManager::StoreDataInBackendContinue
,
101 base::Unretained(this), sw_registration_id
, origin
, key
,
109 void GetDataFromBackend(
110 const std::string
& key
,
111 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback
&
113 EXPECT_TRUE(continuation_
.is_null());
114 if (corrupt_backend_
) {
115 base::MessageLoop::current()->PostTask(
117 base::Bind(callback
, std::vector
<std::pair
<int64
, std::string
>>(),
118 SERVICE_WORKER_ERROR_FAILED
));
122 base::Bind(&TestBackgroundSyncManager::GetDataFromBackendContinue
,
123 base::Unretained(this), key
, callback
);
131 bool corrupt_backend_
= false;
132 bool delay_backend_
= false;
133 base::Closure continuation_
;
136 class BackgroundSyncManagerTest
: public testing::Test
{
138 BackgroundSyncManagerTest()
139 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP
),
140 sync_reg_1_(BackgroundSyncManager::BackgroundSyncRegistration()),
141 sync_reg_2_(BackgroundSyncManager::BackgroundSyncRegistration()),
142 callback_error_(BackgroundSyncManager::ERROR_TYPE_OK
),
143 callback_sw_status_code_(SERVICE_WORKER_OK
) {
144 sync_reg_1_
.name
= "foo";
145 sync_reg_2_
.name
= "bar";
148 void SetUp() override
{
150 new EmbeddedWorkerTestHelper(base::FilePath(), kRenderProcessId
));
152 background_sync_manager_
=
153 BackgroundSyncManager::Create(helper_
->context_wrapper());
155 // Wait for storage to finish initializing before registering service
157 base::RunLoop().RunUntilIdle();
159 bool called_1
= false;
160 bool called_2
= false;
161 helper_
->context()->RegisterServiceWorker(
162 GURL(kPattern1
), GURL(kScript1
), NULL
,
163 base::Bind(&RegisterServiceWorkerCallback
, &called_1
,
164 &sw_registration_id_1_
));
166 helper_
->context()->RegisterServiceWorker(
167 GURL(kPattern2
), GURL(kScript2
), NULL
,
168 base::Bind(&RegisterServiceWorkerCallback
, &called_2
,
169 &sw_registration_id_2_
));
170 base::RunLoop().RunUntilIdle();
171 EXPECT_TRUE(called_1
);
172 EXPECT_TRUE(called_2
);
175 void StatusAndRegistrationCallback(
177 BackgroundSyncManager::ErrorType error
,
178 const BackgroundSyncManager::BackgroundSyncRegistration
& registration
) {
180 callback_error_
= error
;
181 callback_registration_
= registration
;
184 void StatusCallback(bool* was_called
,
185 BackgroundSyncManager::ErrorType error
) {
187 callback_error_
= error
;
191 TestBackgroundSyncManager
* UseTestBackgroundSyncManager() {
192 TestBackgroundSyncManager
* manager
=
193 new TestBackgroundSyncManager(helper_
->context_wrapper());
194 background_sync_manager_
.reset(manager
);
199 bool Register(const BackgroundSyncManager::BackgroundSyncRegistration
&
201 return RegisterWithServiceWorkerId(sw_registration_id_1_
,
205 bool RegisterWithServiceWorkerId(
206 int64 sw_registration_id
,
207 const BackgroundSyncManager::BackgroundSyncRegistration
&
209 bool was_called
= false;
210 background_sync_manager_
->Register(
211 GURL(kOrigin
), sw_registration_id
, sync_registration
,
212 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback
,
213 base::Unretained(this), &was_called
));
214 base::RunLoop().RunUntilIdle();
215 EXPECT_TRUE(was_called
);
216 return callback_error_
== BackgroundSyncManager::ERROR_TYPE_OK
;
219 bool Unregister(const BackgroundSyncManager::BackgroundSyncRegistration
&
221 return UnregisterWithServiceWorkerId(sw_registration_id_1_
,
225 bool UnregisterWithServiceWorkerId(
226 int64 sw_registration_id
,
227 const BackgroundSyncManager::BackgroundSyncRegistration
&
229 bool was_called
= false;
230 background_sync_manager_
->Unregister(
231 GURL(kOrigin
), sw_registration_id
, sync_registration
.name
,
232 sync_registration
.id
,
233 base::Bind(&BackgroundSyncManagerTest::StatusCallback
,
234 base::Unretained(this), &was_called
));
235 base::RunLoop().RunUntilIdle();
236 EXPECT_TRUE(was_called
);
237 return callback_error_
== BackgroundSyncManager::ERROR_TYPE_OK
;
240 bool GetRegistration(const std::string
& sync_registration_name
) {
241 return GetRegistrationWithServiceWorkerId(sw_registration_id_1_
,
242 sync_registration_name
);
245 bool GetRegistrationWithServiceWorkerId(
246 int64 sw_registration_id
,
247 const std::string
& sync_registration_name
) {
248 bool was_called
= false;
249 background_sync_manager_
->GetRegistration(
250 GURL(kOrigin
), sw_registration_id
, sync_registration_name
,
251 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback
,
252 base::Unretained(this), &was_called
));
253 base::RunLoop().RunUntilIdle();
254 EXPECT_TRUE(was_called
);
256 if (callback_error_
== BackgroundSyncManager::ERROR_TYPE_OK
)
257 EXPECT_TRUE(sync_registration_name
== callback_registration_
.name
);
259 return callback_error_
== BackgroundSyncManager::ERROR_TYPE_OK
;
262 void StorageRegistrationCallback(ServiceWorkerStatusCode result
) {
263 callback_sw_status_code_
= result
;
266 void UnregisterServiceWorker(uint64 sw_registration_id
) {
268 helper_
->context()->UnregisterServiceWorker(
269 PatternForSWId(sw_registration_id
),
270 base::Bind(&UnregisterServiceWorkerCallback
, &called
));
271 base::RunLoop().RunUntilIdle();
275 GURL
PatternForSWId(int64 sw_id
) {
276 EXPECT_TRUE(sw_id
== sw_registration_id_1_
||
277 sw_id
== sw_registration_id_2_
);
278 return sw_id
== sw_registration_id_1_
? GURL(kPattern1
) : GURL(kPattern2
);
281 TestBrowserThreadBundle browser_thread_bundle_
;
282 scoped_ptr
<EmbeddedWorkerTestHelper
> helper_
;
283 scoped_ptr
<BackgroundSyncManager
> background_sync_manager_
;
285 int64 sw_registration_id_1_
;
286 int64 sw_registration_id_2_
;
288 BackgroundSyncManager::BackgroundSyncRegistration sync_reg_1_
;
289 BackgroundSyncManager::BackgroundSyncRegistration sync_reg_2_
;
292 BackgroundSyncManager::ErrorType callback_error_
;
293 BackgroundSyncManager::BackgroundSyncRegistration callback_registration_
;
294 ServiceWorkerStatusCode callback_sw_status_code_
;
297 TEST_F(BackgroundSyncManagerTest
, Register
) {
298 EXPECT_TRUE(Register(sync_reg_1_
));
301 TEST_F(BackgroundSyncManagerTest
, RegistractionIntact
) {
302 EXPECT_TRUE(Register(sync_reg_1_
));
303 EXPECT_STREQ(sync_reg_1_
.name
.c_str(), callback_registration_
.name
.c_str());
305 BackgroundSyncManager::BackgroundSyncRegistration::kInvalidRegistrationId
,
306 callback_registration_
.id
);
309 TEST_F(BackgroundSyncManagerTest
, RegisterExistingKeepsId
) {
310 EXPECT_TRUE(Register(sync_reg_1_
));
311 BackgroundSyncManager::BackgroundSyncRegistration first_registration
=
312 callback_registration_
;
313 EXPECT_TRUE(Register(sync_reg_1_
));
314 EXPECT_TRUE(callback_registration_
.Equals(first_registration
));
315 EXPECT_EQ(first_registration
.id
, callback_registration_
.id
);
318 TEST_F(BackgroundSyncManagerTest
, RegisterOverwrites
) {
319 EXPECT_TRUE(Register(sync_reg_1_
));
320 BackgroundSyncManager::BackgroundSyncRegistration first_registration
=
321 callback_registration_
;
323 sync_reg_1_
.min_period
= 100;
324 EXPECT_TRUE(Register(sync_reg_1_
));
325 EXPECT_LT(first_registration
.id
, callback_registration_
.id
);
326 EXPECT_FALSE(callback_registration_
.Equals(first_registration
));
329 TEST_F(BackgroundSyncManagerTest
, RegisterBadBackend
) {
330 TestBackgroundSyncManager
* manager
= UseTestBackgroundSyncManager();
331 manager
->set_corrupt_backend(true);
332 EXPECT_FALSE(Register(sync_reg_1_
));
333 manager
->set_corrupt_backend(false);
334 EXPECT_FALSE(Register(sync_reg_1_
));
335 EXPECT_FALSE(GetRegistration(sync_reg_1_
.name
));
338 TEST_F(BackgroundSyncManagerTest
, TwoRegistrations
) {
339 EXPECT_TRUE(Register(sync_reg_1_
));
340 EXPECT_TRUE(Register(sync_reg_2_
));
343 TEST_F(BackgroundSyncManagerTest
, GetRegistrationNonExisting
) {
344 EXPECT_FALSE(GetRegistration(sync_reg_1_
.name
));
347 TEST_F(BackgroundSyncManagerTest
, GetRegistrationExisting
) {
348 EXPECT_TRUE(Register(sync_reg_1_
));
349 EXPECT_TRUE(GetRegistration(sync_reg_1_
.name
));
350 EXPECT_FALSE(GetRegistration(sync_reg_2_
.name
));
353 TEST_F(BackgroundSyncManagerTest
, GetRegistrationBadBackend
) {
354 TestBackgroundSyncManager
* manager
= UseTestBackgroundSyncManager();
355 EXPECT_TRUE(Register(sync_reg_1_
));
356 manager
->set_corrupt_backend(true);
357 EXPECT_TRUE(GetRegistration(sync_reg_1_
.name
));
358 EXPECT_FALSE(Register(sync_reg_2_
));
359 // Registration should have discovered the bad backend and disabled the
360 // BackgroundSyncManager.
361 EXPECT_FALSE(GetRegistration(sync_reg_1_
.name
));
362 manager
->set_corrupt_backend(false);
363 EXPECT_FALSE(GetRegistration(sync_reg_1_
.name
));
366 TEST_F(BackgroundSyncManagerTest
, Unregister
) {
367 EXPECT_TRUE(Register(sync_reg_1_
));
368 EXPECT_TRUE(Unregister(callback_registration_
));
369 EXPECT_FALSE(GetRegistration(sync_reg_1_
.name
));
372 TEST_F(BackgroundSyncManagerTest
, UnregisterWrongId
) {
373 EXPECT_TRUE(Register(sync_reg_1_
));
374 callback_registration_
.id
+= 1;
375 EXPECT_FALSE(Unregister(callback_registration_
));
378 TEST_F(BackgroundSyncManagerTest
, Reregister
) {
379 EXPECT_TRUE(Register(sync_reg_1_
));
380 EXPECT_TRUE(Unregister(callback_registration_
));
381 EXPECT_TRUE(Register(sync_reg_1_
));
384 TEST_F(BackgroundSyncManagerTest
, UnregisterNonExisting
) {
385 EXPECT_FALSE(Unregister(sync_reg_1_
));
386 EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_NOT_FOUND
, callback_error_
);
389 TEST_F(BackgroundSyncManagerTest
, UnregisterSecond
) {
390 EXPECT_TRUE(Register(sync_reg_1_
));
391 EXPECT_TRUE(Register(sync_reg_2_
));
392 EXPECT_TRUE(Unregister(callback_registration_
));
393 EXPECT_TRUE(GetRegistration(sync_reg_1_
.name
));
394 EXPECT_TRUE(Register(sync_reg_2_
));
397 TEST_F(BackgroundSyncManagerTest
, UnregisterBadBackend
) {
398 TestBackgroundSyncManager
* manager
= UseTestBackgroundSyncManager();
399 sync_reg_1_
.min_period
+= 1;
400 EXPECT_TRUE(Register(sync_reg_1_
));
401 EXPECT_TRUE(Register(sync_reg_2_
));
402 manager
->set_corrupt_backend(true);
403 EXPECT_FALSE(Unregister(callback_registration_
));
404 // Unregister should have discovered the bad backend and disabled the
405 // BackgroundSyncManager.
406 manager
->set_corrupt_backend(false);
407 EXPECT_FALSE(GetRegistration(sync_reg_1_
.name
));
408 EXPECT_FALSE(GetRegistration(sync_reg_2_
.name
));
411 TEST_F(BackgroundSyncManagerTest
, RegistrationIncreasesId
) {
412 EXPECT_TRUE(Register(sync_reg_1_
));
413 BackgroundSyncManager::BackgroundSyncRegistration registered_sync
=
414 callback_registration_
;
415 BackgroundSyncManager::BackgroundSyncRegistration::RegistrationId cur_id
=
416 callback_registration_
.id
;
418 EXPECT_TRUE(GetRegistration(sync_reg_1_
.name
));
419 EXPECT_TRUE(Register(sync_reg_2_
));
420 EXPECT_LT(cur_id
, callback_registration_
.id
);
421 cur_id
= callback_registration_
.id
;
423 EXPECT_TRUE(Unregister(registered_sync
));
424 EXPECT_TRUE(Register(sync_reg_1_
));
425 EXPECT_LT(cur_id
, callback_registration_
.id
);
428 TEST_F(BackgroundSyncManagerTest
, RebootRecovery
) {
429 EXPECT_TRUE(Register(sync_reg_1_
));
431 background_sync_manager_
=
432 BackgroundSyncManager::Create(helper_
->context_wrapper());
434 EXPECT_TRUE(GetRegistration(sync_reg_1_
.name
));
435 EXPECT_FALSE(GetRegistration(sync_reg_2_
.name
));
438 TEST_F(BackgroundSyncManagerTest
, RebootRecoveryTwoServiceWorkers
) {
439 EXPECT_TRUE(RegisterWithServiceWorkerId(sw_registration_id_1_
, sync_reg_1_
));
440 EXPECT_TRUE(RegisterWithServiceWorkerId(sw_registration_id_2_
, sync_reg_2_
));
442 background_sync_manager_
=
443 BackgroundSyncManager::Create(helper_
->context_wrapper());
445 EXPECT_TRUE(GetRegistrationWithServiceWorkerId(sw_registration_id_1_
,
447 EXPECT_FALSE(GetRegistrationWithServiceWorkerId(sw_registration_id_1_
,
449 EXPECT_FALSE(GetRegistrationWithServiceWorkerId(sw_registration_id_2_
,
451 EXPECT_TRUE(GetRegistrationWithServiceWorkerId(sw_registration_id_2_
,
454 EXPECT_TRUE(GetRegistrationWithServiceWorkerId(sw_registration_id_1_
,
456 EXPECT_TRUE(GetRegistrationWithServiceWorkerId(sw_registration_id_2_
,
459 EXPECT_TRUE(RegisterWithServiceWorkerId(sw_registration_id_1_
, sync_reg_2_
));
460 EXPECT_TRUE(RegisterWithServiceWorkerId(sw_registration_id_2_
, sync_reg_1_
));
463 TEST_F(BackgroundSyncManagerTest
, InitWithBadBackend
) {
464 TestBackgroundSyncManager
* manager
=
465 new TestBackgroundSyncManager(helper_
->context_wrapper());
466 background_sync_manager_
.reset(manager
);
467 manager
->set_corrupt_backend(true);
470 EXPECT_FALSE(Register(sync_reg_1_
));
471 EXPECT_FALSE(GetRegistration(sync_reg_1_
.name
));
474 TEST_F(BackgroundSyncManagerTest
, SequentialOperations
) {
475 // Schedule Init and all of the operations on a delayed backend. Verify that
476 // the operations complete sequentially.
477 TestBackgroundSyncManager
* manager
=
478 new TestBackgroundSyncManager(helper_
->context_wrapper());
479 background_sync_manager_
.reset(manager
);
480 manager
->set_delay_backend(true);
483 const int64 kExpectedInitialId
=
484 BackgroundSyncManager::BackgroundSyncRegistrations::kInitialId
;
486 bool register_called
= false;
487 bool unregister_called
= false;
488 bool get_registration_called
= false;
490 GURL(kOrigin
), sw_registration_id_1_
, sync_reg_1_
,
491 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback
,
492 base::Unretained(this), ®ister_called
));
493 manager
->Unregister(GURL(kOrigin
), sw_registration_id_1_
, sync_reg_1_
.name
,
495 base::Bind(&BackgroundSyncManagerTest::StatusCallback
,
496 base::Unretained(this), &unregister_called
));
497 manager
->GetRegistration(
498 GURL(kOrigin
), sw_registration_id_1_
, sync_reg_1_
.name
,
499 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback
,
500 base::Unretained(this), &get_registration_called
));
502 base::RunLoop().RunUntilIdle();
503 // Init should be blocked while loading from the backend.
504 EXPECT_FALSE(register_called
);
505 EXPECT_FALSE(unregister_called
);
506 EXPECT_FALSE(get_registration_called
);
509 base::RunLoop().RunUntilIdle();
510 // Register should be blocked while storing to the backend.
511 EXPECT_FALSE(register_called
);
512 EXPECT_FALSE(unregister_called
);
513 EXPECT_FALSE(get_registration_called
);
516 base::RunLoop().RunUntilIdle();
517 EXPECT_TRUE(register_called
);
518 EXPECT_EQ(kExpectedInitialId
, callback_registration_
.id
);
519 EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_OK
, callback_error_
);
520 // Unregister should be blocked while storing to the backend.
521 EXPECT_FALSE(unregister_called
);
522 EXPECT_FALSE(get_registration_called
);
525 base::RunLoop().RunUntilIdle();
526 // Unregister should be done and since GetRegistration doesn't require the
527 // backend it should be done too.
528 EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_NOT_FOUND
, callback_error_
);
529 EXPECT_TRUE(unregister_called
);
530 EXPECT_TRUE(get_registration_called
);
533 TEST_F(BackgroundSyncManagerTest
, UnregisterServiceWorker
) {
534 EXPECT_TRUE(Register(sync_reg_1_
));
535 UnregisterServiceWorker(sw_registration_id_1_
);
536 EXPECT_FALSE(GetRegistration(sync_reg_1_
.name
));
539 TEST_F(BackgroundSyncManagerTest
,
540 UnregisterServiceWorkerDuringSyncRegistration
) {
541 TestBackgroundSyncManager
* manager
=
542 new TestBackgroundSyncManager(helper_
->context_wrapper());
543 background_sync_manager_
.reset(manager
);
546 EXPECT_TRUE(Register(sync_reg_1_
));
548 manager
->set_delay_backend(true);
549 bool callback_called
= false;
551 GURL(kOrigin
), sw_registration_id_1_
, sync_reg_2_
,
552 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback
,
553 base::Unretained(this), &callback_called
));
555 base::RunLoop().RunUntilIdle();
556 EXPECT_FALSE(callback_called
);
557 UnregisterServiceWorker(sw_registration_id_1_
);
560 base::RunLoop().RunUntilIdle();
561 EXPECT_TRUE(callback_called
);
562 EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_STORAGE
, callback_error_
);
564 manager
->set_delay_backend(false);
565 EXPECT_FALSE(GetRegistration(sync_reg_1_
.name
));
568 TEST_F(BackgroundSyncManagerTest
, DeleteAndStartOverServiceWorkerContext
) {
569 EXPECT_TRUE(Register(sync_reg_1_
));
570 helper_
->context()->ScheduleDeleteAndStartOver();
571 base::RunLoop().RunUntilIdle();
572 EXPECT_FALSE(GetRegistration(sync_reg_1_
.name
));
575 TEST_F(BackgroundSyncManagerTest
, DisabledManagerWorksAfterBrowserRestart
) {
576 TestBackgroundSyncManager
* manager
=
577 new TestBackgroundSyncManager(helper_
->context_wrapper());
578 background_sync_manager_
.reset(manager
);
580 EXPECT_TRUE(Register(sync_reg_1_
));
581 manager
->set_corrupt_backend(true);
582 EXPECT_FALSE(Register(sync_reg_2_
));
584 // The manager is now disabled and not accepting new requests until browser
585 // restart or notification that the storage has been wiped.
586 manager
->set_corrupt_backend(false);
587 EXPECT_FALSE(GetRegistration(sync_reg_1_
.name
));
588 EXPECT_FALSE(Register(sync_reg_2_
));
590 // Simulate restarting the browser by creating a new BackgroundSyncManager.
591 background_sync_manager_
.reset(
592 new TestBackgroundSyncManager(helper_
->context_wrapper()));
593 EXPECT_FALSE(GetRegistration(sync_reg_1_
.name
));
594 EXPECT_TRUE(Register(sync_reg_1_
));
597 TEST_F(BackgroundSyncManagerTest
, DisabledManagerWorksAfterDeleteAndStartOver
) {
598 TestBackgroundSyncManager
* manager
=
599 new TestBackgroundSyncManager(helper_
->context_wrapper());
600 background_sync_manager_
.reset(manager
);
602 EXPECT_TRUE(Register(sync_reg_1_
));
603 manager
->set_corrupt_backend(true);
604 EXPECT_FALSE(Register(sync_reg_2_
));
606 // The manager is now disabled and not accepting new requests until browser
607 // restart or notification that the storage has been wiped.
608 manager
->set_corrupt_backend(false);
609 helper_
->context()->ScheduleDeleteAndStartOver();
610 base::RunLoop().RunUntilIdle();
613 helper_
->context()->RegisterServiceWorker(
614 GURL(kPattern1
), GURL(kScript1
), NULL
,
615 base::Bind(&RegisterServiceWorkerCallback
, &called
,
616 &sw_registration_id_1_
));
617 base::RunLoop().RunUntilIdle();
620 EXPECT_TRUE(Register(sync_reg_2_
));
621 EXPECT_FALSE(GetRegistration(sync_reg_1_
.name
));
622 EXPECT_TRUE(GetRegistration(sync_reg_2_
.name
));
625 TEST_F(BackgroundSyncManagerTest
, RegistrationEqualsId
) {
626 BackgroundSyncManager::BackgroundSyncRegistration reg_1
;
627 BackgroundSyncManager::BackgroundSyncRegistration reg_2
;
629 EXPECT_TRUE(reg_1
.Equals(reg_2
));
630 reg_2
.id
= reg_1
.id
+ 1;
631 EXPECT_TRUE(reg_1
.Equals(reg_2
));
634 TEST_F(BackgroundSyncManagerTest
, RegistrationEqualsName
) {
635 BackgroundSyncManager::BackgroundSyncRegistration reg_1
;
636 BackgroundSyncManager::BackgroundSyncRegistration reg_2
;
637 EXPECT_TRUE(reg_1
.Equals(reg_2
));
639 EXPECT_FALSE(reg_1
.Equals(reg_2
));
642 TEST_F(BackgroundSyncManagerTest
, RegistrationEqualsFireOnce
) {
643 BackgroundSyncManager::BackgroundSyncRegistration reg_1
;
644 BackgroundSyncManager::BackgroundSyncRegistration reg_2
;
645 EXPECT_TRUE(reg_1
.Equals(reg_2
));
646 reg_2
.fire_once
= !reg_1
.fire_once
;
647 EXPECT_FALSE(reg_1
.Equals(reg_2
));
650 TEST_F(BackgroundSyncManagerTest
, RegistrationEqualsMinPeriod
) {
651 BackgroundSyncManager::BackgroundSyncRegistration reg_1
;
652 BackgroundSyncManager::BackgroundSyncRegistration reg_2
;
653 EXPECT_TRUE(reg_1
.Equals(reg_2
));
654 reg_2
.min_period
= reg_1
.min_period
+ 1;
655 EXPECT_FALSE(reg_1
.Equals(reg_2
));
658 TEST_F(BackgroundSyncManagerTest
, RegistrationEqualsNetworkState
) {
659 BackgroundSyncManager::BackgroundSyncRegistration reg_1
;
660 BackgroundSyncManager::BackgroundSyncRegistration reg_2
;
661 EXPECT_TRUE(reg_1
.Equals(reg_2
));
662 reg_1
.network_state
= NETWORK_STATE_ANY
;
663 reg_2
.network_state
= NETWORK_STATE_ONLINE
;
664 EXPECT_FALSE(reg_1
.Equals(reg_2
));
667 TEST_F(BackgroundSyncManagerTest
, RegistrationEqualsPowerState
) {
668 BackgroundSyncManager::BackgroundSyncRegistration reg_1
;
669 BackgroundSyncManager::BackgroundSyncRegistration reg_2
;
670 EXPECT_TRUE(reg_1
.Equals(reg_2
));
671 reg_1
.power_state
= POWER_STATE_AUTO
;
672 reg_2
.power_state
= POWER_STATE_AVOID_DRAINING
;
673 EXPECT_FALSE(reg_1
.Equals(reg_2
));
676 TEST_F(BackgroundSyncManagerTest
, StoreAndRetrievePreservesValues
) {
677 BackgroundSyncManager::BackgroundSyncRegistration reg_1
;
678 // Set non-default values for each field.
680 reg_1
.fire_once
= !reg_1
.fire_once
;
681 reg_1
.min_period
+= 1;
682 EXPECT_NE(NETWORK_STATE_ANY
, reg_1
.network_state
);
683 reg_1
.network_state
= NETWORK_STATE_ANY
;
684 EXPECT_NE(POWER_STATE_AUTO
, reg_1
.power_state
);
685 reg_1
.power_state
= POWER_STATE_AUTO
;
687 // Store the registration.
688 EXPECT_TRUE(Register(reg_1
));
690 // Simulate restarting the sync manager, forcing the next read to come from
692 UseTestBackgroundSyncManager();
694 EXPECT_TRUE(GetRegistration(reg_1
.name
));
695 EXPECT_TRUE(reg_1
.Equals(callback_registration_
));
698 } // namespace content