Roll DEPS for PDFium to 19ae17578f99621100a26dac3e2c7c3dbf7c7cd1
[chromium-blink-merge.git] / content / browser / background_sync / background_sync_manager_unittest.cc
blob83b6970789aca046980e487a8f4548f5f54394d2
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"
19 namespace content {
21 namespace {
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);
36 *called = true;
37 *store_registration_id = registration_id;
40 void UnregisterServiceWorkerCallback(bool* called,
41 ServiceWorkerStatusCode code) {
42 EXPECT_EQ(SERVICE_WORKER_OK, code);
43 *called = true;
46 } // namespace
48 // A BackgroundSyncManager that can simulate delaying and corrupting the
49 // backend. This class assumes (and verifies) that only one operation runs at a
50 // time.
51 class TestBackgroundSyncManager : public BackgroundSyncManager {
52 public:
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,
61 const GURL& origin,
62 const std::string& key,
63 const std::string& data,
64 const ServiceWorkerStorage::StatusCallback& callback) {
65 BackgroundSyncManager::StoreDataInBackend(sw_registration_id, origin, key,
66 data, callback);
69 void GetDataFromBackendContinue(
70 const std::string& key,
71 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback&
72 callback) {
73 BackgroundSyncManager::GetDataFromBackend(key, callback);
76 void Continue() {
77 continuation_.Run();
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; }
86 protected:
87 void StoreDataInBackend(
88 int64 sw_registration_id,
89 const GURL& origin,
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));
97 return;
99 continuation_ =
100 base::Bind(&TestBackgroundSyncManager::StoreDataInBackendContinue,
101 base::Unretained(this), sw_registration_id, origin, key,
102 data, callback);
103 if (delay_backend_)
104 return;
106 Continue();
109 void GetDataFromBackend(
110 const std::string& key,
111 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback&
112 callback) override {
113 EXPECT_TRUE(continuation_.is_null());
114 if (corrupt_backend_) {
115 base::MessageLoop::current()->PostTask(
116 FROM_HERE,
117 base::Bind(callback, std::vector<std::pair<int64, std::string>>(),
118 SERVICE_WORKER_ERROR_FAILED));
119 return;
121 continuation_ =
122 base::Bind(&TestBackgroundSyncManager::GetDataFromBackendContinue,
123 base::Unretained(this), key, callback);
124 if (delay_backend_)
125 return;
127 Continue();
130 private:
131 bool corrupt_backend_ = false;
132 bool delay_backend_ = false;
133 base::Closure continuation_;
136 class BackgroundSyncManagerTest : public testing::Test {
137 public:
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_.tag = "foo";
145 sync_reg_2_.tag = "bar";
148 void SetUp() override {
149 helper_.reset(
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
156 // workers.
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(
176 bool* was_called,
177 BackgroundSyncManager::ErrorType error,
178 const BackgroundSyncManager::BackgroundSyncRegistration& registration) {
179 *was_called = true;
180 callback_error_ = error;
181 callback_registration_ = registration;
184 void StatusCallback(bool* was_called,
185 BackgroundSyncManager::ErrorType error) {
186 *was_called = true;
187 callback_error_ = error;
190 protected:
191 TestBackgroundSyncManager* UseTestBackgroundSyncManager() {
192 TestBackgroundSyncManager* manager =
193 new TestBackgroundSyncManager(helper_->context_wrapper());
194 background_sync_manager_.reset(manager);
195 manager->DoInit();
196 return manager;
199 bool Register(const BackgroundSyncManager::BackgroundSyncRegistration&
200 sync_registration) {
201 return RegisterWithServiceWorkerId(sw_registration_id_1_,
202 sync_registration);
205 bool RegisterWithServiceWorkerId(
206 int64 sw_registration_id,
207 const BackgroundSyncManager::BackgroundSyncRegistration&
208 sync_registration) {
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&
220 sync_registration) {
221 return UnregisterWithServiceWorkerId(sw_registration_id_1_,
222 sync_registration);
225 bool UnregisterWithServiceWorkerId(
226 int64 sw_registration_id,
227 const BackgroundSyncManager::BackgroundSyncRegistration&
228 sync_registration) {
229 bool was_called = false;
230 background_sync_manager_->Unregister(
231 GURL(kOrigin), sw_registration_id, sync_registration.tag,
232 sync_registration.periodicity, 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 BackgroundSyncManager::BackgroundSyncRegistration&
241 sync_registration) {
242 return GetRegistrationWithServiceWorkerId(sw_registration_id_1_,
243 sync_registration);
246 bool GetRegistrationWithServiceWorkerId(
247 int64 sw_registration_id,
248 const BackgroundSyncManager::BackgroundSyncRegistration&
249 sync_registration) {
250 bool was_called = false;
251 background_sync_manager_->GetRegistration(
252 GURL(kOrigin), sw_registration_id, sync_registration.tag,
253 sync_registration.periodicity,
254 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback,
255 base::Unretained(this), &was_called));
256 base::RunLoop().RunUntilIdle();
257 EXPECT_TRUE(was_called);
259 if (callback_error_ == BackgroundSyncManager::ERROR_TYPE_OK) {
260 EXPECT_STREQ(sync_registration.tag.c_str(),
261 callback_registration_.tag.c_str());
264 return callback_error_ == BackgroundSyncManager::ERROR_TYPE_OK;
267 void StorageRegistrationCallback(ServiceWorkerStatusCode result) {
268 callback_sw_status_code_ = result;
271 void UnregisterServiceWorker(uint64 sw_registration_id) {
272 bool called = false;
273 helper_->context()->UnregisterServiceWorker(
274 PatternForSWId(sw_registration_id),
275 base::Bind(&UnregisterServiceWorkerCallback, &called));
276 base::RunLoop().RunUntilIdle();
277 EXPECT_TRUE(called);
280 GURL PatternForSWId(int64 sw_id) {
281 EXPECT_TRUE(sw_id == sw_registration_id_1_ ||
282 sw_id == sw_registration_id_2_);
283 return sw_id == sw_registration_id_1_ ? GURL(kPattern1) : GURL(kPattern2);
286 TestBrowserThreadBundle browser_thread_bundle_;
287 scoped_ptr<EmbeddedWorkerTestHelper> helper_;
288 scoped_ptr<BackgroundSyncManager> background_sync_manager_;
290 int64 sw_registration_id_1_;
291 int64 sw_registration_id_2_;
293 BackgroundSyncManager::BackgroundSyncRegistration sync_reg_1_;
294 BackgroundSyncManager::BackgroundSyncRegistration sync_reg_2_;
296 // Callback values.
297 BackgroundSyncManager::ErrorType callback_error_;
298 BackgroundSyncManager::BackgroundSyncRegistration callback_registration_;
299 ServiceWorkerStatusCode callback_sw_status_code_;
302 TEST_F(BackgroundSyncManagerTest, Register) {
303 EXPECT_TRUE(Register(sync_reg_1_));
306 TEST_F(BackgroundSyncManagerTest, RegistractionIntact) {
307 EXPECT_TRUE(Register(sync_reg_1_));
308 EXPECT_STREQ(sync_reg_1_.tag.c_str(), callback_registration_.tag.c_str());
309 EXPECT_NE(
310 BackgroundSyncManager::BackgroundSyncRegistration::kInvalidRegistrationId,
311 callback_registration_.id);
314 TEST_F(BackgroundSyncManagerTest, RegisterExistingKeepsId) {
315 EXPECT_TRUE(Register(sync_reg_1_));
316 BackgroundSyncManager::BackgroundSyncRegistration first_registration =
317 callback_registration_;
318 EXPECT_TRUE(Register(sync_reg_1_));
319 EXPECT_TRUE(callback_registration_.Equals(first_registration));
320 EXPECT_EQ(first_registration.id, callback_registration_.id);
323 TEST_F(BackgroundSyncManagerTest, RegisterOverwrites) {
324 EXPECT_TRUE(Register(sync_reg_1_));
325 BackgroundSyncManager::BackgroundSyncRegistration first_registration =
326 callback_registration_;
328 sync_reg_1_.min_period = 100;
329 EXPECT_TRUE(Register(sync_reg_1_));
330 EXPECT_LT(first_registration.id, callback_registration_.id);
331 EXPECT_FALSE(callback_registration_.Equals(first_registration));
334 TEST_F(BackgroundSyncManagerTest, RegisterOverlappingPeriodicAndOneShotTags) {
335 // Registrations with the same tags but different periodicities should not
336 // collide.
337 sync_reg_1_.tag = "";
338 sync_reg_2_.tag = "";
339 sync_reg_1_.periodicity = SYNC_PERIODIC;
340 sync_reg_2_.periodicity = SYNC_ONE_SHOT;
341 EXPECT_TRUE(Register(sync_reg_1_));
342 EXPECT_TRUE(Register(sync_reg_2_));
343 EXPECT_TRUE(GetRegistration(sync_reg_1_));
344 EXPECT_EQ(SYNC_PERIODIC, callback_registration_.periodicity);
345 EXPECT_TRUE(GetRegistration(sync_reg_2_));
346 EXPECT_EQ(SYNC_ONE_SHOT, callback_registration_.periodicity);
349 TEST_F(BackgroundSyncManagerTest, RegisterBadBackend) {
350 TestBackgroundSyncManager* manager = UseTestBackgroundSyncManager();
351 manager->set_corrupt_backend(true);
352 EXPECT_FALSE(Register(sync_reg_1_));
353 manager->set_corrupt_backend(false);
354 EXPECT_FALSE(Register(sync_reg_1_));
355 EXPECT_FALSE(GetRegistration(sync_reg_1_));
358 TEST_F(BackgroundSyncManagerTest, TwoRegistrations) {
359 EXPECT_TRUE(Register(sync_reg_1_));
360 EXPECT_TRUE(Register(sync_reg_2_));
363 TEST_F(BackgroundSyncManagerTest, GetRegistrationNonExisting) {
364 EXPECT_FALSE(GetRegistration(sync_reg_1_));
367 TEST_F(BackgroundSyncManagerTest, GetRegistrationExisting) {
368 EXPECT_TRUE(Register(sync_reg_1_));
369 EXPECT_TRUE(GetRegistration(sync_reg_1_));
370 EXPECT_FALSE(GetRegistration(sync_reg_2_));
373 TEST_F(BackgroundSyncManagerTest, GetRegistrationBadBackend) {
374 TestBackgroundSyncManager* manager = UseTestBackgroundSyncManager();
375 EXPECT_TRUE(Register(sync_reg_1_));
376 manager->set_corrupt_backend(true);
377 EXPECT_TRUE(GetRegistration(sync_reg_1_));
378 EXPECT_FALSE(Register(sync_reg_2_));
379 // Registration should have discovered the bad backend and disabled the
380 // BackgroundSyncManager.
381 EXPECT_FALSE(GetRegistration(sync_reg_1_));
382 manager->set_corrupt_backend(false);
383 EXPECT_FALSE(GetRegistration(sync_reg_1_));
386 TEST_F(BackgroundSyncManagerTest, Unregister) {
387 EXPECT_TRUE(Register(sync_reg_1_));
388 EXPECT_TRUE(Unregister(callback_registration_));
389 EXPECT_FALSE(GetRegistration(sync_reg_1_));
392 TEST_F(BackgroundSyncManagerTest, UnregisterWrongId) {
393 EXPECT_TRUE(Register(sync_reg_1_));
394 callback_registration_.id += 1;
395 EXPECT_FALSE(Unregister(callback_registration_));
398 TEST_F(BackgroundSyncManagerTest, Reregister) {
399 EXPECT_TRUE(Register(sync_reg_1_));
400 EXPECT_TRUE(Unregister(callback_registration_));
401 EXPECT_TRUE(Register(sync_reg_1_));
404 TEST_F(BackgroundSyncManagerTest, UnregisterNonExisting) {
405 EXPECT_FALSE(Unregister(sync_reg_1_));
406 EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_NOT_FOUND, callback_error_);
409 TEST_F(BackgroundSyncManagerTest, UnregisterSecond) {
410 EXPECT_TRUE(Register(sync_reg_1_));
411 EXPECT_TRUE(Register(sync_reg_2_));
412 EXPECT_TRUE(Unregister(callback_registration_));
413 EXPECT_TRUE(GetRegistration(sync_reg_1_));
414 EXPECT_TRUE(Register(sync_reg_2_));
417 TEST_F(BackgroundSyncManagerTest, UnregisterBadBackend) {
418 TestBackgroundSyncManager* manager = UseTestBackgroundSyncManager();
419 sync_reg_1_.min_period += 1;
420 EXPECT_TRUE(Register(sync_reg_1_));
421 EXPECT_TRUE(Register(sync_reg_2_));
422 manager->set_corrupt_backend(true);
423 EXPECT_FALSE(Unregister(callback_registration_));
424 // Unregister should have discovered the bad backend and disabled the
425 // BackgroundSyncManager.
426 manager->set_corrupt_backend(false);
427 EXPECT_FALSE(GetRegistration(sync_reg_1_));
428 EXPECT_FALSE(GetRegistration(sync_reg_2_));
431 TEST_F(BackgroundSyncManagerTest, RegistrationIncreasesId) {
432 EXPECT_TRUE(Register(sync_reg_1_));
433 BackgroundSyncManager::BackgroundSyncRegistration registered_sync =
434 callback_registration_;
435 BackgroundSyncManager::BackgroundSyncRegistration::RegistrationId cur_id =
436 callback_registration_.id;
438 EXPECT_TRUE(GetRegistration(sync_reg_1_));
439 EXPECT_TRUE(Register(sync_reg_2_));
440 EXPECT_LT(cur_id, callback_registration_.id);
441 cur_id = callback_registration_.id;
443 EXPECT_TRUE(Unregister(registered_sync));
444 EXPECT_TRUE(Register(sync_reg_1_));
445 EXPECT_LT(cur_id, callback_registration_.id);
448 TEST_F(BackgroundSyncManagerTest, RebootRecovery) {
449 EXPECT_TRUE(Register(sync_reg_1_));
451 background_sync_manager_ =
452 BackgroundSyncManager::Create(helper_->context_wrapper());
454 EXPECT_TRUE(GetRegistration(sync_reg_1_));
455 EXPECT_FALSE(GetRegistration(sync_reg_2_));
458 TEST_F(BackgroundSyncManagerTest, RebootRecoveryTwoServiceWorkers) {
459 EXPECT_TRUE(RegisterWithServiceWorkerId(sw_registration_id_1_, sync_reg_1_));
460 EXPECT_TRUE(RegisterWithServiceWorkerId(sw_registration_id_2_, sync_reg_2_));
462 background_sync_manager_ =
463 BackgroundSyncManager::Create(helper_->context_wrapper());
465 EXPECT_TRUE(
466 GetRegistrationWithServiceWorkerId(sw_registration_id_1_, sync_reg_1_));
467 EXPECT_FALSE(
468 GetRegistrationWithServiceWorkerId(sw_registration_id_1_, sync_reg_2_));
469 EXPECT_FALSE(
470 GetRegistrationWithServiceWorkerId(sw_registration_id_2_, sync_reg_1_));
471 EXPECT_TRUE(
472 GetRegistrationWithServiceWorkerId(sw_registration_id_2_, sync_reg_2_));
474 EXPECT_TRUE(
475 GetRegistrationWithServiceWorkerId(sw_registration_id_1_, sync_reg_1_));
476 EXPECT_TRUE(
477 GetRegistrationWithServiceWorkerId(sw_registration_id_2_, sync_reg_2_));
479 EXPECT_TRUE(RegisterWithServiceWorkerId(sw_registration_id_1_, sync_reg_2_));
480 EXPECT_TRUE(RegisterWithServiceWorkerId(sw_registration_id_2_, sync_reg_1_));
483 TEST_F(BackgroundSyncManagerTest, InitWithBadBackend) {
484 TestBackgroundSyncManager* manager =
485 new TestBackgroundSyncManager(helper_->context_wrapper());
486 background_sync_manager_.reset(manager);
487 manager->set_corrupt_backend(true);
488 manager->DoInit();
490 EXPECT_FALSE(Register(sync_reg_1_));
491 EXPECT_FALSE(GetRegistration(sync_reg_1_));
494 TEST_F(BackgroundSyncManagerTest, SequentialOperations) {
495 // Schedule Init and all of the operations on a delayed backend. Verify that
496 // the operations complete sequentially.
497 TestBackgroundSyncManager* manager =
498 new TestBackgroundSyncManager(helper_->context_wrapper());
499 background_sync_manager_.reset(manager);
500 manager->set_delay_backend(true);
501 manager->DoInit();
503 const int64 kExpectedInitialId =
504 BackgroundSyncManager::BackgroundSyncRegistration::kInitialId;
506 bool register_called = false;
507 bool unregister_called = false;
508 bool get_registration_called = false;
509 manager->Register(
510 GURL(kOrigin), sw_registration_id_1_, sync_reg_1_,
511 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback,
512 base::Unretained(this), &register_called));
513 manager->Unregister(GURL(kOrigin), sw_registration_id_1_, sync_reg_1_.tag,
514 sync_reg_1_.periodicity, kExpectedInitialId,
515 base::Bind(&BackgroundSyncManagerTest::StatusCallback,
516 base::Unretained(this), &unregister_called));
517 manager->GetRegistration(
518 GURL(kOrigin), sw_registration_id_1_, sync_reg_1_.tag,
519 sync_reg_1_.periodicity,
520 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback,
521 base::Unretained(this), &get_registration_called));
523 base::RunLoop().RunUntilIdle();
524 // Init should be blocked while loading from the backend.
525 EXPECT_FALSE(register_called);
526 EXPECT_FALSE(unregister_called);
527 EXPECT_FALSE(get_registration_called);
529 manager->Continue();
530 base::RunLoop().RunUntilIdle();
531 // Register should be blocked while storing to the backend.
532 EXPECT_FALSE(register_called);
533 EXPECT_FALSE(unregister_called);
534 EXPECT_FALSE(get_registration_called);
536 manager->Continue();
537 base::RunLoop().RunUntilIdle();
538 EXPECT_TRUE(register_called);
539 EXPECT_EQ(kExpectedInitialId, callback_registration_.id);
540 EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_OK, callback_error_);
541 // Unregister should be blocked while storing to the backend.
542 EXPECT_FALSE(unregister_called);
543 EXPECT_FALSE(get_registration_called);
545 manager->Continue();
546 base::RunLoop().RunUntilIdle();
547 // Unregister should be done and since GetRegistration doesn't require the
548 // backend it should be done too.
549 EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_NOT_FOUND, callback_error_);
550 EXPECT_TRUE(unregister_called);
551 EXPECT_TRUE(get_registration_called);
554 TEST_F(BackgroundSyncManagerTest, UnregisterServiceWorker) {
555 EXPECT_TRUE(Register(sync_reg_1_));
556 UnregisterServiceWorker(sw_registration_id_1_);
557 EXPECT_FALSE(GetRegistration(sync_reg_1_));
560 TEST_F(BackgroundSyncManagerTest,
561 UnregisterServiceWorkerDuringSyncRegistration) {
562 TestBackgroundSyncManager* manager =
563 new TestBackgroundSyncManager(helper_->context_wrapper());
564 background_sync_manager_.reset(manager);
565 manager->DoInit();
567 EXPECT_TRUE(Register(sync_reg_1_));
569 manager->set_delay_backend(true);
570 bool callback_called = false;
571 manager->Register(
572 GURL(kOrigin), sw_registration_id_1_, sync_reg_2_,
573 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback,
574 base::Unretained(this), &callback_called));
576 base::RunLoop().RunUntilIdle();
577 EXPECT_FALSE(callback_called);
578 UnregisterServiceWorker(sw_registration_id_1_);
580 manager->Continue();
581 base::RunLoop().RunUntilIdle();
582 EXPECT_TRUE(callback_called);
583 EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_STORAGE, callback_error_);
585 manager->set_delay_backend(false);
586 EXPECT_FALSE(GetRegistration(sync_reg_1_));
589 TEST_F(BackgroundSyncManagerTest, DeleteAndStartOverServiceWorkerContext) {
590 EXPECT_TRUE(Register(sync_reg_1_));
591 helper_->context()->ScheduleDeleteAndStartOver();
592 base::RunLoop().RunUntilIdle();
593 EXPECT_FALSE(GetRegistration(sync_reg_1_));
596 TEST_F(BackgroundSyncManagerTest, DisabledManagerWorksAfterBrowserRestart) {
597 TestBackgroundSyncManager* manager =
598 new TestBackgroundSyncManager(helper_->context_wrapper());
599 background_sync_manager_.reset(manager);
600 manager->DoInit();
601 EXPECT_TRUE(Register(sync_reg_1_));
602 manager->set_corrupt_backend(true);
603 EXPECT_FALSE(Register(sync_reg_2_));
605 // The manager is now disabled and not accepting new requests until browser
606 // restart or notification that the storage has been wiped.
607 manager->set_corrupt_backend(false);
608 EXPECT_FALSE(GetRegistration(sync_reg_1_));
609 EXPECT_FALSE(Register(sync_reg_2_));
611 // Simulate restarting the browser by creating a new BackgroundSyncManager.
612 background_sync_manager_.reset(
613 new TestBackgroundSyncManager(helper_->context_wrapper()));
614 EXPECT_FALSE(GetRegistration(sync_reg_1_));
615 EXPECT_TRUE(Register(sync_reg_1_));
618 TEST_F(BackgroundSyncManagerTest, DisabledManagerWorksAfterDeleteAndStartOver) {
619 TestBackgroundSyncManager* manager =
620 new TestBackgroundSyncManager(helper_->context_wrapper());
621 background_sync_manager_.reset(manager);
622 manager->DoInit();
623 EXPECT_TRUE(Register(sync_reg_1_));
624 manager->set_corrupt_backend(true);
625 EXPECT_FALSE(Register(sync_reg_2_));
627 // The manager is now disabled and not accepting new requests until browser
628 // restart or notification that the storage has been wiped.
629 manager->set_corrupt_backend(false);
630 helper_->context()->ScheduleDeleteAndStartOver();
631 base::RunLoop().RunUntilIdle();
633 bool called = false;
634 helper_->context()->RegisterServiceWorker(
635 GURL(kPattern1), GURL(kScript1), NULL,
636 base::Bind(&RegisterServiceWorkerCallback, &called,
637 &sw_registration_id_1_));
638 base::RunLoop().RunUntilIdle();
639 EXPECT_TRUE(called);
641 EXPECT_TRUE(Register(sync_reg_2_));
642 EXPECT_FALSE(GetRegistration(sync_reg_1_));
643 EXPECT_TRUE(GetRegistration(sync_reg_2_));
646 TEST_F(BackgroundSyncManagerTest, RegistrationEqualsId) {
647 BackgroundSyncManager::BackgroundSyncRegistration reg_1;
648 BackgroundSyncManager::BackgroundSyncRegistration reg_2;
650 EXPECT_TRUE(reg_1.Equals(reg_2));
651 reg_2.id = reg_1.id + 1;
652 EXPECT_TRUE(reg_1.Equals(reg_2));
655 TEST_F(BackgroundSyncManagerTest, RegistrationEqualsTag) {
656 BackgroundSyncManager::BackgroundSyncRegistration reg_1;
657 BackgroundSyncManager::BackgroundSyncRegistration reg_2;
658 EXPECT_TRUE(reg_1.Equals(reg_2));
659 reg_2.tag = "bar";
660 EXPECT_FALSE(reg_1.Equals(reg_2));
663 TEST_F(BackgroundSyncManagerTest, RegistrationEqualsPeriodicity) {
664 BackgroundSyncManager::BackgroundSyncRegistration reg_1;
665 BackgroundSyncManager::BackgroundSyncRegistration reg_2;
666 EXPECT_TRUE(reg_1.Equals(reg_2));
667 reg_1.periodicity = SYNC_PERIODIC;
668 reg_2.periodicity = SYNC_ONE_SHOT;
669 EXPECT_FALSE(reg_1.Equals(reg_2));
672 TEST_F(BackgroundSyncManagerTest, RegistrationEqualsMinPeriod) {
673 BackgroundSyncManager::BackgroundSyncRegistration reg_1;
674 BackgroundSyncManager::BackgroundSyncRegistration reg_2;
675 EXPECT_TRUE(reg_1.Equals(reg_2));
676 reg_2.min_period = reg_1.min_period + 1;
677 EXPECT_FALSE(reg_1.Equals(reg_2));
680 TEST_F(BackgroundSyncManagerTest, RegistrationEqualsNetworkState) {
681 BackgroundSyncManager::BackgroundSyncRegistration reg_1;
682 BackgroundSyncManager::BackgroundSyncRegistration reg_2;
683 EXPECT_TRUE(reg_1.Equals(reg_2));
684 reg_1.network_state = NETWORK_STATE_ANY;
685 reg_2.network_state = NETWORK_STATE_ONLINE;
686 EXPECT_FALSE(reg_1.Equals(reg_2));
689 TEST_F(BackgroundSyncManagerTest, RegistrationEqualsPowerState) {
690 BackgroundSyncManager::BackgroundSyncRegistration reg_1;
691 BackgroundSyncManager::BackgroundSyncRegistration reg_2;
692 EXPECT_TRUE(reg_1.Equals(reg_2));
693 reg_1.power_state = POWER_STATE_AUTO;
694 reg_2.power_state = POWER_STATE_AVOID_DRAINING;
695 EXPECT_FALSE(reg_1.Equals(reg_2));
698 TEST_F(BackgroundSyncManagerTest, StoreAndRetrievePreservesValues) {
699 BackgroundSyncManager::BackgroundSyncRegistration reg_1;
700 // Set non-default values for each field.
701 reg_1.tag = "foo";
702 EXPECT_NE(SYNC_PERIODIC, reg_1.periodicity);
703 reg_1.periodicity = SYNC_PERIODIC;
704 reg_1.min_period += 1;
705 EXPECT_NE(NETWORK_STATE_ANY, reg_1.network_state);
706 reg_1.network_state = NETWORK_STATE_ANY;
707 EXPECT_NE(POWER_STATE_AUTO, reg_1.power_state);
708 reg_1.power_state = POWER_STATE_AUTO;
710 // Store the registration.
711 EXPECT_TRUE(Register(reg_1));
713 // Simulate restarting the sync manager, forcing the next read to come from
714 // disk.
715 UseTestBackgroundSyncManager();
717 EXPECT_TRUE(GetRegistration(reg_1));
718 EXPECT_TRUE(reg_1.Equals(callback_registration_));
721 TEST_F(BackgroundSyncManagerTest, EmptyTagSupported) {
722 sync_reg_1_.tag = "a";
723 EXPECT_TRUE(Register(sync_reg_1_));
724 EXPECT_TRUE(GetRegistration(sync_reg_1_));
725 EXPECT_TRUE(sync_reg_1_.Equals(callback_registration_));
726 EXPECT_TRUE(Unregister(callback_registration_));
727 EXPECT_FALSE(GetRegistration(sync_reg_1_));
730 TEST_F(BackgroundSyncManagerTest, OverlappingPeriodicAndOneShotTags) {
731 // Registrations with the same tags but different periodicities should not
732 // collide.
733 sync_reg_1_.tag = "";
734 sync_reg_2_.tag = "";
735 sync_reg_1_.periodicity = SYNC_PERIODIC;
736 sync_reg_2_.periodicity = SYNC_ONE_SHOT;
738 EXPECT_TRUE(Register(sync_reg_1_));
739 EXPECT_TRUE(Register(sync_reg_2_));
741 EXPECT_TRUE(GetRegistration(sync_reg_1_));
742 EXPECT_EQ(SYNC_PERIODIC, callback_registration_.periodicity);
743 EXPECT_TRUE(GetRegistration(sync_reg_2_));
744 EXPECT_EQ(SYNC_ONE_SHOT, callback_registration_.periodicity);
746 EXPECT_TRUE(GetRegistration(sync_reg_1_));
747 EXPECT_TRUE(Unregister(callback_registration_));
748 EXPECT_FALSE(GetRegistration(sync_reg_1_));
749 EXPECT_TRUE(GetRegistration(sync_reg_2_));
750 EXPECT_EQ(SYNC_ONE_SHOT, callback_registration_.periodicity);
752 EXPECT_TRUE(Unregister(callback_registration_));
753 EXPECT_FALSE(GetRegistration(sync_reg_2_));
756 } // namespace content