[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / background_sync / background_sync_manager_unittest.cc
blob99e211c986ca535179b60f0ee5b37c0f665b35e1
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/background_sync/background_sync_manager.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/power_monitor/power_monitor.h"
11 #include "base/power_monitor/power_monitor_source.h"
12 #include "base/run_loop.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "content/browser/background_sync/background_sync_registration_handle.h"
16 #include "content/browser/background_sync/background_sync_status.h"
17 #include "content/browser/browser_thread_impl.h"
18 #include "content/browser/service_worker/embedded_worker_test_helper.h"
19 #include "content/browser/service_worker/service_worker_context_core.h"
20 #include "content/browser/service_worker/service_worker_context_wrapper.h"
21 #include "content/browser/service_worker/service_worker_storage.h"
22 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "net/base/network_change_notifier.h"
24 #include "testing/gtest/include/gtest/gtest.h"
26 namespace content {
28 namespace {
30 const char kPattern1[] = "https://example.com/a";
31 const char kPattern2[] = "https://example.com/b";
32 const char kScript1[] = "https://example.com/a/script.js";
33 const char kScript2[] = "https://example.com/b/script.js";
34 const int kRenderProcessId = 99;
36 void RegisterServiceWorkerCallback(bool* called,
37 int64* store_registration_id,
38 ServiceWorkerStatusCode status,
39 const std::string& status_message,
40 int64 registration_id) {
41 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status);
42 *called = true;
43 *store_registration_id = registration_id;
46 void FindServiceWorkerRegistrationCallback(
47 scoped_refptr<ServiceWorkerRegistration>* out_registration,
48 ServiceWorkerStatusCode status,
49 const scoped_refptr<ServiceWorkerRegistration>& registration) {
50 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status);
51 *out_registration = registration;
54 void UnregisterServiceWorkerCallback(bool* called,
55 ServiceWorkerStatusCode code) {
56 EXPECT_EQ(SERVICE_WORKER_OK, code);
57 *called = true;
60 void OneShotSuccessfulCallback(
61 int* count,
62 const scoped_refptr<ServiceWorkerVersion>& active_version,
63 const ServiceWorkerVersion::StatusCallback& callback) {
64 *count += 1;
65 callback.Run(SERVICE_WORKER_OK);
68 void OneShotFailedCallback(
69 int* count,
70 const scoped_refptr<ServiceWorkerVersion>& active_version,
71 const ServiceWorkerVersion::StatusCallback& callback) {
72 *count += 1;
73 callback.Run(SERVICE_WORKER_ERROR_FAILED);
76 void OneShotDelayedCallback(
77 int* count,
78 ServiceWorkerVersion::StatusCallback* out_callback,
79 const scoped_refptr<ServiceWorkerVersion>& active_version,
80 const ServiceWorkerVersion::StatusCallback& callback) {
81 *count += 1;
82 *out_callback = callback;
85 void NotifyWhenDoneCallback(bool* was_called,
86 BackgroundSyncStatus* out_status,
87 BackgroundSyncState* out_state,
88 BackgroundSyncStatus status,
89 BackgroundSyncState state) {
90 *was_called = true;
91 *out_status = status;
92 *out_state = state;
95 class TestPowerSource : public base::PowerMonitorSource {
96 public:
97 void GeneratePowerStateEvent(bool on_battery_power) {
98 test_on_battery_power_ = on_battery_power;
99 ProcessPowerEvent(POWER_STATE_EVENT);
102 private:
103 bool IsOnBatteryPowerImpl() final { return test_on_battery_power_; }
104 bool test_on_battery_power_ = false;
107 } // namespace
109 // A BackgroundSyncManager that can simulate delaying and corrupting the backend
110 // storage and service worker onsync events.
111 class TestBackgroundSyncManager : public BackgroundSyncManager {
112 public:
113 using OneShotCallback =
114 base::Callback<void(const scoped_refptr<ServiceWorkerVersion>&,
115 const ServiceWorkerVersion::StatusCallback&)>;
117 explicit TestBackgroundSyncManager(
118 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context)
119 : BackgroundSyncManager(service_worker_context) {}
121 void DoInit() { Init(); }
123 void StoreDataInBackendContinue(
124 int64 sw_registration_id,
125 const GURL& origin,
126 const std::string& key,
127 const std::string& data,
128 const ServiceWorkerStorage::StatusCallback& callback) {
129 BackgroundSyncManager::StoreDataInBackend(sw_registration_id, origin, key,
130 data, callback);
133 void GetDataFromBackendContinue(
134 const std::string& key,
135 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback&
136 callback) {
137 BackgroundSyncManager::GetDataFromBackend(key, callback);
140 void Continue() {
141 ASSERT_FALSE(continuation_.is_null());
142 continuation_.Run();
143 continuation_.Reset();
146 void set_corrupt_backend(bool corrupt_backend) {
147 corrupt_backend_ = corrupt_backend;
149 void set_delay_backend(bool delay_backend) { delay_backend_ = delay_backend; }
150 void set_one_shot_callback(const OneShotCallback& callback) {
151 one_shot_callback_ = callback;
154 protected:
155 void StoreDataInBackend(
156 int64 sw_registration_id,
157 const GURL& origin,
158 const std::string& key,
159 const std::string& data,
160 const ServiceWorkerStorage::StatusCallback& callback) override {
161 EXPECT_TRUE(continuation_.is_null());
162 if (corrupt_backend_) {
163 base::ThreadTaskRunnerHandle::Get()->PostTask(
164 FROM_HERE, base::Bind(callback, SERVICE_WORKER_ERROR_FAILED));
165 return;
167 continuation_ =
168 base::Bind(&TestBackgroundSyncManager::StoreDataInBackendContinue,
169 base::Unretained(this), sw_registration_id, origin, key,
170 data, callback);
171 if (delay_backend_)
172 return;
174 Continue();
177 void GetDataFromBackend(
178 const std::string& key,
179 const ServiceWorkerStorage::GetUserDataForAllRegistrationsCallback&
180 callback) override {
181 EXPECT_TRUE(continuation_.is_null());
182 if (corrupt_backend_) {
183 base::ThreadTaskRunnerHandle::Get()->PostTask(
184 FROM_HERE,
185 base::Bind(callback, std::vector<std::pair<int64, std::string>>(),
186 SERVICE_WORKER_ERROR_FAILED));
187 return;
189 continuation_ =
190 base::Bind(&TestBackgroundSyncManager::GetDataFromBackendContinue,
191 base::Unretained(this), key, callback);
192 if (delay_backend_)
193 return;
195 Continue();
198 void FireOneShotSync(
199 BackgroundSyncRegistrationHandle::HandleId handle_id,
200 const scoped_refptr<ServiceWorkerVersion>& active_version,
201 const ServiceWorkerVersion::StatusCallback& callback) override {
202 ASSERT_FALSE(one_shot_callback_.is_null());
203 one_shot_callback_.Run(active_version, callback);
206 private:
207 bool corrupt_backend_ = false;
208 bool delay_backend_ = false;
209 base::Closure continuation_;
210 OneShotCallback one_shot_callback_;
213 class BackgroundSyncManagerTest : public testing::Test {
214 public:
215 BackgroundSyncManagerTest()
216 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
217 network_change_notifier_(net::NetworkChangeNotifier::CreateMock()),
218 test_background_sync_manager_(nullptr),
219 callback_status_(BACKGROUND_SYNC_STATUS_OK),
220 callback_sw_status_code_(SERVICE_WORKER_OK),
221 sync_events_called_(0) {
222 sync_options_1_.tag = "foo";
223 sync_options_1_.periodicity = SYNC_ONE_SHOT;
224 sync_options_1_.network_state = NETWORK_STATE_ONLINE;
225 sync_options_1_.power_state = POWER_STATE_AUTO;
227 sync_options_2_.tag = "bar";
228 sync_options_2_.periodicity = SYNC_ONE_SHOT;
229 sync_options_2_.network_state = NETWORK_STATE_ONLINE;
230 sync_options_2_.power_state = POWER_STATE_AUTO;
233 void SetUp() override {
234 helper_.reset(
235 new EmbeddedWorkerTestHelper(base::FilePath(), kRenderProcessId));
237 power_monitor_source_ = new TestPowerSource();
238 // power_monitor_ takes ownership of power_monitor_source.
239 power_monitor_.reset(new base::PowerMonitor(
240 scoped_ptr<base::PowerMonitorSource>(power_monitor_source_)));
242 SetOnBatteryPower(false);
244 SetupBackgroundSyncManager();
246 // Wait for storage to finish initializing before registering service
247 // workers.
248 base::RunLoop().RunUntilIdle();
249 RegisterServiceWorkers();
252 void RegisterServiceWorkers() {
253 bool called_1 = false;
254 bool called_2 = false;
255 helper_->context()->RegisterServiceWorker(
256 GURL(kPattern1), GURL(kScript1), NULL,
257 base::Bind(&RegisterServiceWorkerCallback, &called_1,
258 &sw_registration_id_1_));
260 helper_->context()->RegisterServiceWorker(
261 GURL(kPattern2), GURL(kScript2), NULL,
262 base::Bind(&RegisterServiceWorkerCallback, &called_2,
263 &sw_registration_id_2_));
264 base::RunLoop().RunUntilIdle();
265 EXPECT_TRUE(called_1);
266 EXPECT_TRUE(called_2);
268 // Register window clients for the service workers
269 host_1_.reset(new ServiceWorkerProviderHost(
270 34 /* dummy render proces id */, MSG_ROUTING_NONE /* render_frame_id */,
271 1 /* dummy provider id */, SERVICE_WORKER_PROVIDER_FOR_WINDOW,
272 helper_->context()->AsWeakPtr(), nullptr));
273 host_1_->SetDocumentUrl(GURL(kPattern1));
274 host_2_.reset(new ServiceWorkerProviderHost(
275 34 /* dummy render proces id */, MSG_ROUTING_NONE /* render_frame_id */,
276 1 /* dummy provider id */, SERVICE_WORKER_PROVIDER_FOR_WINDOW,
277 helper_->context()->AsWeakPtr(), nullptr));
278 host_2_->SetDocumentUrl(GURL(kPattern2));
280 // Hang onto the registrations as they need to be "live" when
281 // calling BackgroundSyncManager::Register.
282 helper_->context_wrapper()->FindRegistrationForId(
283 sw_registration_id_1_, GURL(kPattern1).GetOrigin(),
284 base::Bind(FindServiceWorkerRegistrationCallback, &sw_registration_1_));
286 helper_->context_wrapper()->FindRegistrationForId(
287 sw_registration_id_2_, GURL(kPattern1).GetOrigin(),
288 base::Bind(FindServiceWorkerRegistrationCallback, &sw_registration_2_));
289 base::RunLoop().RunUntilIdle();
290 EXPECT_TRUE(sw_registration_1_);
291 EXPECT_TRUE(sw_registration_2_);
293 sw_registration_1_->active_version()->AddControllee(host_1_.get());
294 sw_registration_2_->active_version()->AddControllee(host_2_.get());
297 void RemoveWindowClients() {
298 sw_registration_1_->active_version()->RemoveControllee(host_1_.get());
299 sw_registration_2_->active_version()->RemoveControllee(host_2_.get());
302 void SetNetwork(net::NetworkChangeNotifier::ConnectionType connection_type) {
303 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
304 connection_type);
305 base::RunLoop().RunUntilIdle();
308 void SetOnBatteryPower(bool on_battery_power) {
309 power_monitor_source_->GeneratePowerStateEvent(on_battery_power);
310 base::RunLoop().RunUntilIdle();
313 void StatusAndRegistrationCallback(
314 bool* was_called,
315 BackgroundSyncStatus status,
316 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle) {
317 *was_called = true;
318 callback_status_ = status;
319 callback_registration_handle_ = registration_handle.Pass();
322 void StatusAndRegistrationsCallback(
323 bool* was_called,
324 BackgroundSyncStatus status,
325 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>>
326 registration_handles) {
327 *was_called = true;
328 callback_status_ = status;
329 callback_registration_handles_ = registration_handles.Pass();
332 void StatusCallback(bool* was_called, BackgroundSyncStatus status) {
333 *was_called = true;
334 callback_status_ = status;
337 protected:
338 void CreateBackgroundSyncManager() {
339 ClearRegistrationHandles();
341 test_background_sync_manager_ =
342 new TestBackgroundSyncManager(helper_->context_wrapper());
343 background_sync_manager_.reset(test_background_sync_manager_);
346 void InitBackgroundSyncManager() {
347 test_background_sync_manager_->DoInit();
349 // Many tests do not expect the sync event to fire immediately after
350 // register (and cleanup up the sync registrations). Tests can control when
351 // the sync event fires by manipulating the network state as needed.
352 // NOTE: The setup of the network connection must happen after the
353 // BackgroundSyncManager has been setup.
354 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE);
357 // Clear the registrations so that the BackgroundSyncManager can release them.
358 void ClearRegistrationHandles() {
359 callback_registration_handle_.reset();
360 callback_registration_handles_.reset();
363 void SetupBackgroundSyncManager() {
364 CreateBackgroundSyncManager();
365 InitBackgroundSyncManager();
368 void SetupCorruptBackgroundSyncManager() {
369 CreateBackgroundSyncManager();
370 test_background_sync_manager_->set_corrupt_backend(true);
371 InitBackgroundSyncManager();
374 void SetupDelayedBackgroundSyncManager() {
375 CreateBackgroundSyncManager();
376 test_background_sync_manager_->set_delay_backend(true);
377 InitBackgroundSyncManager();
380 void DeleteBackgroundSyncManager() {
381 ClearRegistrationHandles();
382 background_sync_manager_.reset();
383 test_background_sync_manager_ = nullptr;
386 bool Register(const BackgroundSyncRegistrationOptions& sync_options) {
387 return RegisterWithServiceWorkerId(sw_registration_id_1_, sync_options);
390 bool RegisterWithServiceWorkerId(
391 int64 sw_registration_id,
392 const BackgroundSyncRegistrationOptions& options) {
393 bool was_called = false;
394 background_sync_manager_->Register(
395 sw_registration_id, options, true /* requested_from_service_worker */,
396 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback,
397 base::Unretained(this), &was_called));
398 base::RunLoop().RunUntilIdle();
399 EXPECT_TRUE(was_called);
400 return callback_status_ == BACKGROUND_SYNC_STATUS_OK;
403 bool RegisterFromDocumentWithServiceWorkerId(
404 int64 sw_registration_id,
405 const BackgroundSyncRegistrationOptions& options) {
406 bool was_called = false;
407 background_sync_manager_->Register(
408 sw_registration_id, options, false /* requested_from_service_worker */,
409 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback,
410 base::Unretained(this), &was_called));
411 base::RunLoop().RunUntilIdle();
412 EXPECT_TRUE(was_called);
413 return callback_status_ == BACKGROUND_SYNC_STATUS_OK;
416 bool Unregister(BackgroundSyncRegistrationHandle* registration_handle) {
417 return UnregisterWithServiceWorkerId(sw_registration_id_1_,
418 registration_handle);
421 bool UnregisterWithServiceWorkerId(
422 int64 sw_registration_id,
423 BackgroundSyncRegistrationHandle* registration_handle) {
424 bool was_called = false;
425 registration_handle->Unregister(
426 sw_registration_id,
427 base::Bind(&BackgroundSyncManagerTest::StatusCallback,
428 base::Unretained(this), &was_called));
429 base::RunLoop().RunUntilIdle();
430 EXPECT_TRUE(was_called);
431 return callback_status_ == BACKGROUND_SYNC_STATUS_OK;
434 bool GetRegistration(
435 const BackgroundSyncRegistrationOptions& registration_options) {
436 return GetRegistrationWithServiceWorkerId(sw_registration_id_1_,
437 registration_options);
440 bool GetRegistrationWithServiceWorkerId(
441 int64 sw_registration_id,
442 const BackgroundSyncRegistrationOptions& registration_options) {
443 bool was_called = false;
444 background_sync_manager_->GetRegistration(
445 sw_registration_id, registration_options.tag,
446 registration_options.periodicity,
447 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback,
448 base::Unretained(this), &was_called));
449 base::RunLoop().RunUntilIdle();
450 EXPECT_TRUE(was_called);
452 if (callback_status_ == BACKGROUND_SYNC_STATUS_OK) {
453 EXPECT_STREQ(registration_options.tag.c_str(),
454 callback_registration_handle_->options()->tag.c_str());
457 return callback_status_ == BACKGROUND_SYNC_STATUS_OK;
460 bool GetRegistrations(SyncPeriodicity periodicity) {
461 return GetRegistrationWithServiceWorkerId(sw_registration_id_1_,
462 periodicity);
465 bool GetRegistrationWithServiceWorkerId(int64 sw_registration_id,
466 SyncPeriodicity periodicity) {
467 bool was_called = false;
468 background_sync_manager_->GetRegistrations(
469 sw_registration_id, periodicity,
470 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationsCallback,
471 base::Unretained(this), &was_called));
472 base::RunLoop().RunUntilIdle();
473 EXPECT_TRUE(was_called);
475 return callback_status_ == BACKGROUND_SYNC_STATUS_OK;
478 void StorageRegistrationCallback(ServiceWorkerStatusCode result) {
479 callback_sw_status_code_ = result;
482 void UnregisterServiceWorker(uint64 sw_registration_id) {
483 bool called = false;
484 helper_->context()->UnregisterServiceWorker(
485 PatternForSWId(sw_registration_id),
486 base::Bind(&UnregisterServiceWorkerCallback, &called));
487 base::RunLoop().RunUntilIdle();
488 EXPECT_TRUE(called);
491 GURL PatternForSWId(int64 sw_id) {
492 EXPECT_TRUE(sw_id == sw_registration_id_1_ ||
493 sw_id == sw_registration_id_2_);
494 return sw_id == sw_registration_id_1_ ? GURL(kPattern1) : GURL(kPattern2);
497 void SetupForSyncEvent(
498 const TestBackgroundSyncManager::OneShotCallback& callback) {
499 test_background_sync_manager_->set_one_shot_callback(callback);
500 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI);
503 void InitSyncEventTest() {
504 SetupForSyncEvent(
505 base::Bind(OneShotSuccessfulCallback, &sync_events_called_));
508 void InitFailedSyncEventTest() {
509 SetupForSyncEvent(base::Bind(OneShotFailedCallback, &sync_events_called_));
512 void InitDelayedSyncEventTest() {
513 SetupForSyncEvent(base::Bind(OneShotDelayedCallback, &sync_events_called_,
514 &sync_fired_callback_));
517 void RegisterAndVerifySyncEventDelayed(
518 const BackgroundSyncRegistrationOptions& sync_options) {
519 int sync_events_called = sync_events_called_;
520 EXPECT_TRUE(sync_fired_callback_.is_null());
522 EXPECT_TRUE(Register(sync_options));
524 EXPECT_EQ(sync_events_called + 1, sync_events_called_);
525 EXPECT_TRUE(GetRegistration(sync_options_1_));
526 EXPECT_FALSE(sync_fired_callback_.is_null());
529 int MaxTagLength() const { return BackgroundSyncManager::kMaxTagLength; }
531 TestBrowserThreadBundle browser_thread_bundle_;
532 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_;
533 TestPowerSource* power_monitor_source_; // owned by power_monitor_
534 scoped_ptr<base::PowerMonitor> power_monitor_;
535 scoped_ptr<EmbeddedWorkerTestHelper> helper_;
536 scoped_ptr<BackgroundSyncManager> background_sync_manager_;
537 TestBackgroundSyncManager* test_background_sync_manager_;
539 scoped_ptr<ServiceWorkerProviderHost> host_1_;
540 scoped_ptr<ServiceWorkerProviderHost> host_2_;
541 int64 sw_registration_id_1_;
542 int64 sw_registration_id_2_;
543 scoped_refptr<ServiceWorkerRegistration> sw_registration_1_;
544 scoped_refptr<ServiceWorkerRegistration> sw_registration_2_;
546 BackgroundSyncRegistrationOptions sync_options_1_;
547 BackgroundSyncRegistrationOptions sync_options_2_;
549 // Callback values.
550 BackgroundSyncStatus callback_status_;
551 scoped_ptr<BackgroundSyncRegistrationHandle> callback_registration_handle_;
552 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>>
553 callback_registration_handles_;
554 ServiceWorkerStatusCode callback_sw_status_code_;
555 int sync_events_called_;
556 ServiceWorkerVersion::StatusCallback sync_fired_callback_;
559 TEST_F(BackgroundSyncManagerTest, Register) {
560 EXPECT_TRUE(Register(sync_options_1_));
563 TEST_F(BackgroundSyncManagerTest, RegistractionIntact) {
564 EXPECT_TRUE(Register(sync_options_1_));
565 EXPECT_STREQ(sync_options_1_.tag.c_str(),
566 callback_registration_handle_->options()->tag.c_str());
567 EXPECT_TRUE(callback_registration_handle_->IsValid());
570 TEST_F(BackgroundSyncManagerTest, RegisterWithoutLiveSWRegistration) {
571 sw_registration_1_ = nullptr;
572 EXPECT_FALSE(Register(sync_options_1_));
573 EXPECT_EQ(BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER, callback_status_);
576 TEST_F(BackgroundSyncManagerTest, RegisterWithoutActiveSWRegistration) {
577 sw_registration_1_->UnsetVersion(sw_registration_1_->active_version());
578 EXPECT_FALSE(Register(sync_options_1_));
579 EXPECT_EQ(BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER, callback_status_);
582 TEST_F(BackgroundSyncManagerTest, RegisterOverwrites) {
583 EXPECT_TRUE(Register(sync_options_1_));
584 scoped_ptr<BackgroundSyncRegistrationHandle> first_registration_handle =
585 callback_registration_handle_.Pass();
587 sync_options_1_.min_period = 100;
588 EXPECT_TRUE(Register(sync_options_1_));
589 EXPECT_LT(first_registration_handle->handle_id(),
590 callback_registration_handle_->handle_id());
591 EXPECT_FALSE(first_registration_handle->options()->Equals(
592 *callback_registration_handle_->options()));
595 TEST_F(BackgroundSyncManagerTest, RegisterOverlappingPeriodicAndOneShotTags) {
596 // Registrations with the same tags but different periodicities should not
597 // collide.
598 sync_options_1_.tag = "";
599 sync_options_2_.tag = "";
600 sync_options_1_.periodicity = SYNC_PERIODIC;
601 sync_options_2_.periodicity = SYNC_ONE_SHOT;
602 EXPECT_TRUE(Register(sync_options_1_));
603 EXPECT_TRUE(Register(sync_options_2_));
604 EXPECT_TRUE(GetRegistration(sync_options_1_));
605 EXPECT_EQ(SYNC_PERIODIC,
606 callback_registration_handle_->options()->periodicity);
607 EXPECT_TRUE(GetRegistration(sync_options_2_));
608 EXPECT_EQ(SYNC_ONE_SHOT,
609 callback_registration_handle_->options()->periodicity);
612 TEST_F(BackgroundSyncManagerTest, RegisterBadBackend) {
613 test_background_sync_manager_->set_corrupt_backend(true);
614 EXPECT_FALSE(Register(sync_options_1_));
615 test_background_sync_manager_->set_corrupt_backend(false);
616 EXPECT_FALSE(Register(sync_options_1_));
617 EXPECT_FALSE(GetRegistration(sync_options_1_));
620 TEST_F(BackgroundSyncManagerTest, DuplicateRegistrationHandle) {
621 EXPECT_TRUE(Register(sync_options_1_));
622 EXPECT_TRUE(
623 sync_options_1_.Equals(*callback_registration_handle_->options()));
625 scoped_ptr<BackgroundSyncRegistrationHandle> dup_handle =
626 background_sync_manager_->DuplicateRegistrationHandle(
627 callback_registration_handle_->handle_id());
629 EXPECT_TRUE(sync_options_1_.Equals(*dup_handle->options()));
630 EXPECT_NE(callback_registration_handle_->handle_id(),
631 dup_handle->handle_id());
634 TEST_F(BackgroundSyncManagerTest, TwoRegistrations) {
635 EXPECT_TRUE(Register(sync_options_1_));
636 EXPECT_TRUE(Register(sync_options_2_));
639 TEST_F(BackgroundSyncManagerTest, GetRegistrationNonExisting) {
640 EXPECT_FALSE(GetRegistration(sync_options_1_));
643 TEST_F(BackgroundSyncManagerTest, GetRegistrationExisting) {
644 EXPECT_TRUE(Register(sync_options_1_));
645 EXPECT_TRUE(GetRegistration(sync_options_1_));
646 EXPECT_FALSE(GetRegistration(sync_options_2_));
649 TEST_F(BackgroundSyncManagerTest, GetRegistrationBadBackend) {
650 EXPECT_TRUE(Register(sync_options_1_));
651 test_background_sync_manager_->set_corrupt_backend(true);
652 EXPECT_TRUE(GetRegistration(sync_options_1_));
653 EXPECT_FALSE(Register(sync_options_2_));
654 // Registration should have discovered the bad backend and disabled the
655 // BackgroundSyncManager.
656 EXPECT_FALSE(GetRegistration(sync_options_1_));
657 test_background_sync_manager_->set_corrupt_backend(false);
658 EXPECT_FALSE(GetRegistration(sync_options_1_));
661 TEST_F(BackgroundSyncManagerTest, GetRegistrationsZero) {
662 EXPECT_TRUE(GetRegistrations(SYNC_ONE_SHOT));
663 EXPECT_EQ(0u, callback_registration_handles_->size());
666 TEST_F(BackgroundSyncManagerTest, GetRegistrationsOne) {
667 EXPECT_TRUE(Register(sync_options_1_));
668 EXPECT_TRUE(GetRegistrations(sync_options_1_.periodicity));
670 EXPECT_EQ(1u, callback_registration_handles_->size());
671 sync_options_1_.Equals(*(*callback_registration_handles_)[0]->options());
674 TEST_F(BackgroundSyncManagerTest, GetRegistrationsTwo) {
675 EXPECT_EQ(sync_options_1_.periodicity, sync_options_2_.periodicity);
677 EXPECT_TRUE(Register(sync_options_1_));
678 EXPECT_TRUE(Register(sync_options_2_));
679 EXPECT_TRUE(GetRegistrations(sync_options_1_.periodicity));
681 EXPECT_EQ(2u, callback_registration_handles_->size());
682 sync_options_1_.Equals(*(*callback_registration_handles_)[0]->options());
683 sync_options_2_.Equals(*(*callback_registration_handles_)[1]->options());
686 TEST_F(BackgroundSyncManagerTest, GetRegistrationsPeriodicity) {
687 sync_options_1_.periodicity = SYNC_ONE_SHOT;
688 sync_options_2_.periodicity = SYNC_PERIODIC;
689 EXPECT_TRUE(Register(sync_options_1_));
690 EXPECT_TRUE(Register(sync_options_2_));
692 EXPECT_TRUE(GetRegistrations(SYNC_ONE_SHOT));
693 EXPECT_EQ(1u, callback_registration_handles_->size());
694 sync_options_1_.Equals(*(*callback_registration_handles_)[0]->options());
696 EXPECT_TRUE(GetRegistrations(SYNC_PERIODIC));
697 EXPECT_EQ(1u, callback_registration_handles_->size());
698 sync_options_2_.Equals(*(*callback_registration_handles_)[0]->options());
701 TEST_F(BackgroundSyncManagerTest, GetRegistrationsBadBackend) {
702 EXPECT_TRUE(Register(sync_options_1_));
703 test_background_sync_manager_->set_corrupt_backend(true);
704 EXPECT_TRUE(GetRegistrations(sync_options_1_.periodicity));
705 EXPECT_FALSE(Register(sync_options_2_));
706 // Registration should have discovered the bad backend and disabled the
707 // BackgroundSyncManager.
708 EXPECT_FALSE(GetRegistrations(sync_options_1_.periodicity));
709 test_background_sync_manager_->set_corrupt_backend(false);
710 EXPECT_FALSE(GetRegistrations(sync_options_1_.periodicity));
713 TEST_F(BackgroundSyncManagerTest, Unregister) {
714 EXPECT_TRUE(Register(sync_options_1_));
715 EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
716 EXPECT_FALSE(GetRegistration(sync_options_1_));
719 TEST_F(BackgroundSyncManagerTest, Reregister) {
720 EXPECT_TRUE(Register(sync_options_1_));
721 EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
722 EXPECT_TRUE(Register(sync_options_1_));
725 TEST_F(BackgroundSyncManagerTest, UnregisterSecond) {
726 EXPECT_TRUE(Register(sync_options_1_));
727 EXPECT_TRUE(Register(sync_options_2_));
728 EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
729 EXPECT_TRUE(GetRegistration(sync_options_1_));
730 EXPECT_TRUE(Register(sync_options_2_));
733 TEST_F(BackgroundSyncManagerTest, UnregisterBadBackend) {
734 sync_options_1_.min_period += 1;
735 EXPECT_TRUE(Register(sync_options_1_));
736 EXPECT_TRUE(Register(sync_options_2_));
737 test_background_sync_manager_->set_corrupt_backend(true);
738 EXPECT_FALSE(Unregister(callback_registration_handle_.get()));
739 // Unregister should have discovered the bad backend and disabled the
740 // BackgroundSyncManager.
741 test_background_sync_manager_->set_corrupt_backend(false);
742 EXPECT_FALSE(GetRegistration(sync_options_1_));
743 EXPECT_FALSE(GetRegistration(sync_options_2_));
746 TEST_F(BackgroundSyncManagerTest, RegisterMaxTagLength) {
747 sync_options_1_.tag = std::string(MaxTagLength(), 'a');
748 EXPECT_TRUE(Register(sync_options_1_));
749 EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
751 sync_options_1_.tag = std::string(MaxTagLength() + 1, 'a');
752 EXPECT_FALSE(Register(sync_options_1_));
753 EXPECT_EQ(BACKGROUND_SYNC_STATUS_NOT_ALLOWED, callback_status_);
756 TEST_F(BackgroundSyncManagerTest, RegistrationIncreasesId) {
757 EXPECT_TRUE(Register(sync_options_1_));
758 scoped_ptr<BackgroundSyncRegistrationHandle> registered_handle =
759 callback_registration_handle_.Pass();
760 BackgroundSyncRegistration::RegistrationId cur_id =
761 registered_handle->handle_id();
763 EXPECT_TRUE(GetRegistration(sync_options_1_));
764 EXPECT_TRUE(Register(sync_options_2_));
765 EXPECT_LT(cur_id, callback_registration_handle_->handle_id());
766 cur_id = callback_registration_handle_->handle_id();
768 EXPECT_TRUE(Unregister(registered_handle.get()));
769 EXPECT_TRUE(Register(sync_options_1_));
770 EXPECT_LT(cur_id, callback_registration_handle_->handle_id());
773 TEST_F(BackgroundSyncManagerTest, RebootRecovery) {
774 EXPECT_TRUE(Register(sync_options_1_));
776 SetupBackgroundSyncManager();
778 EXPECT_TRUE(GetRegistration(sync_options_1_));
779 EXPECT_FALSE(GetRegistration(sync_options_2_));
782 TEST_F(BackgroundSyncManagerTest, RebootRecoveryTwoServiceWorkers) {
783 EXPECT_TRUE(
784 RegisterWithServiceWorkerId(sw_registration_id_1_, sync_options_1_));
785 EXPECT_TRUE(
786 RegisterWithServiceWorkerId(sw_registration_id_2_, sync_options_2_));
788 SetupBackgroundSyncManager();
790 EXPECT_TRUE(GetRegistrationWithServiceWorkerId(sw_registration_id_1_,
791 sync_options_1_));
792 EXPECT_FALSE(GetRegistrationWithServiceWorkerId(sw_registration_id_1_,
793 sync_options_2_));
794 EXPECT_FALSE(GetRegistrationWithServiceWorkerId(sw_registration_id_2_,
795 sync_options_1_));
796 EXPECT_TRUE(GetRegistrationWithServiceWorkerId(sw_registration_id_2_,
797 sync_options_2_));
799 EXPECT_TRUE(GetRegistrationWithServiceWorkerId(sw_registration_id_1_,
800 sync_options_1_));
801 EXPECT_TRUE(GetRegistrationWithServiceWorkerId(sw_registration_id_2_,
802 sync_options_2_));
804 EXPECT_TRUE(
805 RegisterWithServiceWorkerId(sw_registration_id_1_, sync_options_2_));
806 EXPECT_TRUE(
807 RegisterWithServiceWorkerId(sw_registration_id_2_, sync_options_1_));
810 TEST_F(BackgroundSyncManagerTest, InitWithBadBackend) {
811 SetupCorruptBackgroundSyncManager();
813 EXPECT_FALSE(Register(sync_options_1_));
814 EXPECT_FALSE(GetRegistration(sync_options_1_));
817 TEST_F(BackgroundSyncManagerTest, SequentialOperations) {
818 // Schedule Init and all of the operations on a delayed backend. Verify that
819 // the operations complete sequentially.
820 SetupDelayedBackgroundSyncManager();
822 bool register_called = false;
823 bool get_registration_called = false;
824 test_background_sync_manager_->Register(
825 sw_registration_id_1_, sync_options_1_,
826 true /* requested_from_service_worker */,
827 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback,
828 base::Unretained(this), &register_called));
829 test_background_sync_manager_->GetRegistration(
830 sw_registration_id_1_, sync_options_1_.tag, sync_options_1_.periodicity,
831 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback,
832 base::Unretained(this), &get_registration_called));
834 base::RunLoop().RunUntilIdle();
835 // Init should be blocked while loading from the backend.
836 EXPECT_FALSE(register_called);
837 EXPECT_FALSE(get_registration_called);
839 test_background_sync_manager_->Continue();
840 base::RunLoop().RunUntilIdle();
841 // Register should be blocked while storing to the backend.
842 EXPECT_FALSE(register_called);
843 EXPECT_FALSE(get_registration_called);
845 test_background_sync_manager_->Continue();
846 base::RunLoop().RunUntilIdle();
847 EXPECT_TRUE(register_called);
848 EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, callback_status_);
849 // GetRegistration should run immediately as it doesn't write to disk.
850 EXPECT_TRUE(get_registration_called);
853 TEST_F(BackgroundSyncManagerTest, UnregisterServiceWorker) {
854 EXPECT_TRUE(Register(sync_options_1_));
855 UnregisterServiceWorker(sw_registration_id_1_);
856 EXPECT_FALSE(GetRegistration(sync_options_1_));
859 TEST_F(BackgroundSyncManagerTest,
860 UnregisterServiceWorkerDuringSyncRegistration) {
861 EXPECT_TRUE(Register(sync_options_1_));
863 test_background_sync_manager_->set_delay_backend(true);
864 bool callback_called = false;
865 test_background_sync_manager_->Register(
866 sw_registration_id_1_, sync_options_2_,
867 true /* requested_from_service_worker */,
868 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback,
869 base::Unretained(this), &callback_called));
871 base::RunLoop().RunUntilIdle();
872 EXPECT_FALSE(callback_called);
873 UnregisterServiceWorker(sw_registration_id_1_);
875 test_background_sync_manager_->Continue();
876 base::RunLoop().RunUntilIdle();
877 EXPECT_TRUE(callback_called);
878 EXPECT_EQ(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback_status_);
880 test_background_sync_manager_->set_delay_backend(false);
881 EXPECT_FALSE(GetRegistration(sync_options_1_));
884 TEST_F(BackgroundSyncManagerTest, DeleteAndStartOverServiceWorkerContext) {
885 EXPECT_TRUE(Register(sync_options_1_));
886 helper_->context()->ScheduleDeleteAndStartOver();
887 base::RunLoop().RunUntilIdle();
888 EXPECT_FALSE(GetRegistration(sync_options_1_));
891 TEST_F(BackgroundSyncManagerTest, DisabledManagerWorksAfterBrowserRestart) {
892 EXPECT_TRUE(Register(sync_options_1_));
893 test_background_sync_manager_->set_corrupt_backend(true);
894 EXPECT_FALSE(Register(sync_options_2_));
896 // The manager is now disabled and not accepting new requests until browser
897 // restart or notification that the storage has been wiped.
898 test_background_sync_manager_->set_corrupt_backend(false);
899 EXPECT_FALSE(GetRegistration(sync_options_1_));
900 EXPECT_FALSE(Register(sync_options_2_));
902 // Simulate restarting the browser by creating a new BackgroundSyncManager.
903 SetupBackgroundSyncManager();
904 EXPECT_TRUE(GetRegistration(sync_options_1_));
905 EXPECT_TRUE(Register(sync_options_2_));
908 TEST_F(BackgroundSyncManagerTest, DisabledManagerWorksAfterDeleteAndStartOver) {
909 EXPECT_TRUE(Register(sync_options_1_));
910 test_background_sync_manager_->set_corrupt_backend(true);
911 EXPECT_FALSE(Register(sync_options_2_));
913 // The manager is now disabled and not accepting new requests until browser
914 // restart or notification that the storage has been wiped.
915 test_background_sync_manager_->set_corrupt_backend(false);
916 helper_->context()->ScheduleDeleteAndStartOver();
917 base::RunLoop().RunUntilIdle();
919 RegisterServiceWorkers();
921 EXPECT_TRUE(Register(sync_options_2_));
922 EXPECT_FALSE(GetRegistration(sync_options_1_));
923 EXPECT_TRUE(GetRegistration(sync_options_2_));
926 TEST_F(BackgroundSyncManagerTest, RegistrationEqualsId) {
927 BackgroundSyncRegistration reg_1;
928 BackgroundSyncRegistration reg_2;
930 EXPECT_TRUE(reg_1.Equals(reg_2));
931 reg_2.set_id(reg_1.id() + 1);
932 EXPECT_TRUE(reg_1.Equals(reg_2));
935 TEST_F(BackgroundSyncManagerTest, RegistrationEqualsTag) {
936 BackgroundSyncRegistration reg_1;
937 BackgroundSyncRegistration reg_2;
938 EXPECT_TRUE(reg_1.Equals(reg_2));
939 reg_2.options()->tag = "bar";
940 EXPECT_FALSE(reg_1.Equals(reg_2));
943 TEST_F(BackgroundSyncManagerTest, RegistrationEqualsPeriodicity) {
944 BackgroundSyncRegistration reg_1;
945 BackgroundSyncRegistration reg_2;
946 EXPECT_TRUE(reg_1.Equals(reg_2));
947 reg_1.options()->periodicity = SYNC_PERIODIC;
948 reg_2.options()->periodicity = SYNC_ONE_SHOT;
949 EXPECT_FALSE(reg_1.Equals(reg_2));
952 TEST_F(BackgroundSyncManagerTest, RegistrationEqualsMinPeriod) {
953 BackgroundSyncRegistration reg_1;
954 BackgroundSyncRegistration reg_2;
955 EXPECT_TRUE(reg_1.Equals(reg_2));
956 reg_2.options()->min_period = reg_1.options()->min_period + 1;
957 EXPECT_FALSE(reg_1.Equals(reg_2));
960 TEST_F(BackgroundSyncManagerTest, RegistrationEqualsNetworkState) {
961 BackgroundSyncRegistration reg_1;
962 BackgroundSyncRegistration reg_2;
963 EXPECT_TRUE(reg_1.Equals(reg_2));
964 reg_1.options()->network_state = NETWORK_STATE_ANY;
965 reg_2.options()->network_state = NETWORK_STATE_ONLINE;
966 EXPECT_FALSE(reg_1.Equals(reg_2));
969 TEST_F(BackgroundSyncManagerTest, RegistrationEqualsPowerState) {
970 BackgroundSyncRegistration reg_1;
971 BackgroundSyncRegistration reg_2;
972 EXPECT_TRUE(reg_1.Equals(reg_2));
973 reg_1.options()->power_state = POWER_STATE_AUTO;
974 reg_2.options()->power_state = POWER_STATE_AVOID_DRAINING;
975 EXPECT_FALSE(reg_1.Equals(reg_2));
978 TEST_F(BackgroundSyncManagerTest, StoreAndRetrievePreservesValues) {
979 BackgroundSyncRegistrationOptions options;
980 // Set non-default values for each field.
981 options.tag = "foo";
982 EXPECT_NE(SYNC_PERIODIC, options.periodicity);
983 options.periodicity = SYNC_PERIODIC;
984 options.min_period += 1;
985 EXPECT_NE(NETWORK_STATE_ANY, options.network_state);
986 options.network_state = NETWORK_STATE_ANY;
987 EXPECT_NE(POWER_STATE_AUTO, options.power_state);
988 options.power_state = POWER_STATE_AUTO;
990 // Store the registration.
991 EXPECT_TRUE(Register(options));
993 // Simulate restarting the sync manager, forcing the next read to come from
994 // disk.
995 SetupBackgroundSyncManager();
997 EXPECT_TRUE(GetRegistration(options));
998 EXPECT_TRUE(options.Equals(*callback_registration_handle_->options()));
1001 TEST_F(BackgroundSyncManagerTest, EmptyTagSupported) {
1002 sync_options_1_.tag = "a";
1003 EXPECT_TRUE(Register(sync_options_1_));
1004 EXPECT_TRUE(GetRegistration(sync_options_1_));
1005 EXPECT_TRUE(
1006 sync_options_1_.Equals(*callback_registration_handle_->options()));
1007 EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
1008 EXPECT_FALSE(GetRegistration(sync_options_1_));
1011 TEST_F(BackgroundSyncManagerTest, OverlappingPeriodicAndOneShotTags) {
1012 // Registrations with the same tags but different periodicities should not
1013 // collide.
1014 sync_options_1_.tag = "";
1015 sync_options_2_.tag = "";
1016 sync_options_1_.periodicity = SYNC_PERIODIC;
1017 sync_options_2_.periodicity = SYNC_ONE_SHOT;
1019 EXPECT_TRUE(Register(sync_options_1_));
1020 EXPECT_TRUE(Register(sync_options_2_));
1022 EXPECT_TRUE(GetRegistration(sync_options_1_));
1023 EXPECT_EQ(SYNC_PERIODIC,
1024 callback_registration_handle_->options()->periodicity);
1025 EXPECT_TRUE(GetRegistration(sync_options_2_));
1026 EXPECT_EQ(SYNC_ONE_SHOT,
1027 callback_registration_handle_->options()->periodicity);
1029 EXPECT_TRUE(GetRegistration(sync_options_1_));
1030 EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
1031 EXPECT_FALSE(GetRegistration(sync_options_1_));
1032 EXPECT_TRUE(GetRegistration(sync_options_2_));
1033 EXPECT_EQ(SYNC_ONE_SHOT,
1034 callback_registration_handle_->options()->periodicity);
1036 EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
1037 EXPECT_FALSE(GetRegistration(sync_options_2_));
1040 TEST_F(BackgroundSyncManagerTest, OneShotFiresOnRegistration) {
1041 InitSyncEventTest();
1043 EXPECT_TRUE(Register(sync_options_1_));
1044 EXPECT_EQ(1, sync_events_called_);
1045 EXPECT_FALSE(GetRegistration(sync_options_1_));
1048 TEST_F(BackgroundSyncManagerTest, NotifyWhenDoneAfterEventSuccess) {
1049 InitSyncEventTest();
1051 EXPECT_TRUE(Register(sync_options_1_));
1052 EXPECT_EQ(1, sync_events_called_);
1054 bool notify_done_called = false;
1055 BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
1056 BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
1057 callback_registration_handle_->NotifyWhenDone(base::Bind(
1058 &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
1059 base::RunLoop().RunUntilIdle();
1060 EXPECT_TRUE(notify_done_called);
1061 EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
1062 EXPECT_EQ(BACKGROUND_SYNC_STATE_SUCCESS, sync_state);
1065 TEST_F(BackgroundSyncManagerTest, NotifyWhenDoneBeforeEventSuccess) {
1066 InitDelayedSyncEventTest();
1068 RegisterAndVerifySyncEventDelayed(sync_options_1_);
1070 bool notify_done_called = false;
1071 BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
1072 BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
1073 callback_registration_handle_->NotifyWhenDone(base::Bind(
1074 &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
1075 base::RunLoop().RunUntilIdle();
1076 EXPECT_FALSE(notify_done_called);
1078 // Finish firing the event.
1079 sync_fired_callback_.Run(SERVICE_WORKER_OK);
1080 base::RunLoop().RunUntilIdle();
1081 EXPECT_EQ(1, sync_events_called_);
1082 EXPECT_TRUE(notify_done_called);
1083 EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
1084 EXPECT_EQ(BACKGROUND_SYNC_STATE_SUCCESS, sync_state);
1087 TEST_F(BackgroundSyncManagerTest,
1088 NotifyWhenDoneBeforeUnregisteredEventSuccess) {
1089 InitDelayedSyncEventTest();
1091 RegisterAndVerifySyncEventDelayed(sync_options_1_);
1093 bool notify_done_called = false;
1094 BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
1095 BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
1096 callback_registration_handle_->NotifyWhenDone(base::Bind(
1097 &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
1098 base::RunLoop().RunUntilIdle();
1099 EXPECT_FALSE(notify_done_called);
1101 // Unregistering should set the state to UNREGISTERED but done shouldn't
1102 // be called until the event finishes firing, at which point its state should
1103 // be SUCCESS.
1104 EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
1105 EXPECT_FALSE(GetRegistration(sync_options_1_));
1107 // Finish firing the event.
1108 sync_fired_callback_.Run(SERVICE_WORKER_OK);
1109 base::RunLoop().RunUntilIdle();
1110 EXPECT_EQ(1, sync_events_called_);
1111 EXPECT_TRUE(notify_done_called);
1112 EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
1113 EXPECT_EQ(BACKGROUND_SYNC_STATE_SUCCESS, sync_state);
1116 TEST_F(BackgroundSyncManagerTest,
1117 NotifyWhenDoneBeforeUnregisteredEventFailure) {
1118 InitDelayedSyncEventTest();
1120 RegisterAndVerifySyncEventDelayed(sync_options_1_);
1122 bool notify_done_called = false;
1123 BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
1124 BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
1125 callback_registration_handle_->NotifyWhenDone(base::Bind(
1126 &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
1127 base::RunLoop().RunUntilIdle();
1128 EXPECT_FALSE(notify_done_called);
1130 // Unregistering should set the state to UNREGISTERED but done shouldn't
1131 // be called until the event finishes firing, at which point its state should
1132 // be FAILED.
1133 EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
1134 EXPECT_FALSE(GetRegistration(sync_options_1_));
1136 // Finish firing the event.
1137 sync_fired_callback_.Run(SERVICE_WORKER_ERROR_FAILED);
1138 base::RunLoop().RunUntilIdle();
1139 EXPECT_EQ(1, sync_events_called_);
1140 EXPECT_TRUE(notify_done_called);
1141 EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
1142 EXPECT_EQ(BACKGROUND_SYNC_STATE_FAILED, sync_state);
1145 TEST_F(BackgroundSyncManagerTest, NotifyWhenDoneBeforeUnregisteredEventFires) {
1146 InitSyncEventTest();
1148 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE);
1149 EXPECT_TRUE(Register(sync_options_1_));
1150 EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
1152 bool notify_done_called = false;
1153 BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
1154 BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
1155 callback_registration_handle_->NotifyWhenDone(base::Bind(
1156 &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
1157 base::RunLoop().RunUntilIdle();
1158 EXPECT_TRUE(notify_done_called);
1159 EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
1160 EXPECT_EQ(BACKGROUND_SYNC_STATE_UNREGISTERED, sync_state);
1163 TEST_F(BackgroundSyncManagerTest,
1164 NotifyWhenDoneBeforeEventSuccessDroppedHandle) {
1165 InitDelayedSyncEventTest();
1167 RegisterAndVerifySyncEventDelayed(sync_options_1_);
1169 bool notify_done_called = false;
1170 BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
1171 BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
1173 callback_registration_handle_->NotifyWhenDone(base::Bind(
1174 &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
1175 base::RunLoop().RunUntilIdle();
1176 EXPECT_FALSE(notify_done_called);
1178 // Drop the client's handle to the registration before the event fires, ensure
1179 // that the done callback is still run.
1180 callback_registration_handle_ = nullptr;
1182 // Finish firing the event.
1183 sync_fired_callback_.Run(SERVICE_WORKER_OK);
1184 base::RunLoop().RunUntilIdle();
1185 EXPECT_EQ(1, sync_events_called_);
1186 EXPECT_TRUE(notify_done_called);
1187 EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
1188 EXPECT_EQ(BACKGROUND_SYNC_STATE_SUCCESS, sync_state);
1191 TEST_F(BackgroundSyncManagerTest, NotifyWhenDoneAfterEventFailure) {
1192 InitFailedSyncEventTest();
1194 EXPECT_TRUE(Register(sync_options_1_));
1195 EXPECT_EQ(1, sync_events_called_);
1197 bool notify_done_called = false;
1198 BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
1199 BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
1200 callback_registration_handle_->NotifyWhenDone(base::Bind(
1201 &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
1202 base::RunLoop().RunUntilIdle();
1203 EXPECT_TRUE(notify_done_called);
1204 EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
1205 EXPECT_EQ(BACKGROUND_SYNC_STATE_FAILED, sync_state);
1208 TEST_F(BackgroundSyncManagerTest, NotifyWhenDoneBeforeEventFailure) {
1209 InitDelayedSyncEventTest();
1211 RegisterAndVerifySyncEventDelayed(sync_options_1_);
1213 bool notify_done_called = false;
1214 BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
1215 BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
1216 callback_registration_handle_->NotifyWhenDone(base::Bind(
1217 &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
1218 base::RunLoop().RunUntilIdle();
1219 EXPECT_FALSE(notify_done_called);
1221 // Finish firing the event.
1222 sync_fired_callback_.Run(SERVICE_WORKER_ERROR_FAILED);
1223 base::RunLoop().RunUntilIdle();
1224 EXPECT_TRUE(notify_done_called);
1225 EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
1226 EXPECT_EQ(BACKGROUND_SYNC_STATE_FAILED, sync_state);
1229 TEST_F(BackgroundSyncManagerTest, NotifyWhenDoneAfterUnregistered) {
1230 EXPECT_TRUE(Register(sync_options_1_));
1231 EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
1233 bool notify_done_called = false;
1234 BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
1235 BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
1236 callback_registration_handle_->NotifyWhenDone(base::Bind(
1237 &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
1238 base::RunLoop().RunUntilIdle();
1239 EXPECT_TRUE(notify_done_called);
1240 EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
1241 EXPECT_EQ(BACKGROUND_SYNC_STATE_UNREGISTERED, sync_state);
1244 TEST_F(BackgroundSyncManagerTest, NotifyWhenDoneBeforeUnregistered) {
1245 Register(sync_options_1_);
1246 bool notify_done_called = false;
1247 BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
1248 BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
1249 callback_registration_handle_->NotifyWhenDone(base::Bind(
1250 &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
1251 base::RunLoop().RunUntilIdle();
1252 EXPECT_FALSE(notify_done_called);
1254 EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
1255 EXPECT_TRUE(notify_done_called);
1256 EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
1257 EXPECT_EQ(BACKGROUND_SYNC_STATE_UNREGISTERED, sync_state);
1260 // TODO(jkarlin): Change this to a periodic test as one-shots can't be power
1261 // dependent according to spec.
1262 TEST_F(BackgroundSyncManagerTest, OneShotFiresOnPowerChange) {
1263 InitSyncEventTest();
1264 sync_options_1_.power_state = POWER_STATE_AVOID_DRAINING;
1266 SetOnBatteryPower(true);
1267 EXPECT_TRUE(Register(sync_options_1_));
1268 EXPECT_EQ(0, sync_events_called_);
1269 EXPECT_TRUE(GetRegistration(sync_options_1_));
1271 SetOnBatteryPower(false);
1272 EXPECT_EQ(1, sync_events_called_);
1273 EXPECT_FALSE(GetRegistration(sync_options_1_));
1276 // TODO(jkarlin): Change this to a periodic test as one-shots can't be power
1277 // dependent according to spec.
1278 TEST_F(BackgroundSyncManagerTest, MultipleOneShotsFireOnPowerChange) {
1279 InitSyncEventTest();
1280 sync_options_1_.power_state = POWER_STATE_AVOID_DRAINING;
1281 sync_options_2_.power_state = POWER_STATE_AVOID_DRAINING;
1283 SetOnBatteryPower(true);
1284 EXPECT_TRUE(Register(sync_options_1_));
1285 EXPECT_TRUE(Register(sync_options_2_));
1286 EXPECT_EQ(0, sync_events_called_);
1287 EXPECT_TRUE(GetRegistration(sync_options_1_));
1288 EXPECT_TRUE(GetRegistration(sync_options_2_));
1290 SetOnBatteryPower(false);
1291 EXPECT_EQ(2, sync_events_called_);
1292 EXPECT_FALSE(GetRegistration(sync_options_1_));
1293 EXPECT_FALSE(GetRegistration(sync_options_2_));
1296 TEST_F(BackgroundSyncManagerTest, OneShotFiresOnNetworkChange) {
1297 InitSyncEventTest();
1299 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE);
1300 EXPECT_TRUE(Register(sync_options_1_));
1301 EXPECT_EQ(0, sync_events_called_);
1302 EXPECT_TRUE(GetRegistration(sync_options_1_));
1304 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI);
1305 base::RunLoop().RunUntilIdle();
1306 EXPECT_EQ(1, sync_events_called_);
1307 EXPECT_FALSE(GetRegistration(sync_options_1_));
1310 TEST_F(BackgroundSyncManagerTest, MultipleOneShotsFireOnNetworkChange) {
1311 InitSyncEventTest();
1313 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE);
1314 EXPECT_TRUE(Register(sync_options_1_));
1315 EXPECT_TRUE(Register(sync_options_2_));
1316 EXPECT_EQ(0, sync_events_called_);
1317 EXPECT_TRUE(GetRegistration(sync_options_1_));
1318 EXPECT_TRUE(GetRegistration(sync_options_2_));
1320 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI);
1322 EXPECT_EQ(2, sync_events_called_);
1323 EXPECT_FALSE(GetRegistration(sync_options_1_));
1324 EXPECT_FALSE(GetRegistration(sync_options_2_));
1327 TEST_F(BackgroundSyncManagerTest, OneShotFiresOnManagerRestart) {
1328 InitSyncEventTest();
1330 // Initially the event won't run because there is no network.
1331 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE);
1332 EXPECT_TRUE(Register(sync_options_1_));
1333 EXPECT_EQ(0, sync_events_called_);
1334 EXPECT_TRUE(GetRegistration(sync_options_1_));
1336 // Simulate closing the browser.
1337 DeleteBackgroundSyncManager();
1339 // The next time the manager is started, the network is good.
1340 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI);
1341 SetupBackgroundSyncManager();
1342 InitSyncEventTest();
1344 // The event should have fired.
1345 EXPECT_EQ(1, sync_events_called_);
1346 EXPECT_FALSE(GetRegistration(sync_options_1_));
1349 TEST_F(BackgroundSyncManagerTest, FailedOneShotStillExists) {
1350 InitFailedSyncEventTest();
1352 EXPECT_TRUE(Register(sync_options_1_));
1353 EXPECT_EQ(1, sync_events_called_);
1354 EXPECT_TRUE(GetRegistration(sync_options_1_));
1356 // The failed one-shot should stay registered but not fire until the
1357 // ServiceWorker is reloaded with an active client. Therefore, changing the
1358 // network should not cause the event to run again.
1359 SetNetwork(net::NetworkChangeNotifier::CONNECTION_2G);
1360 EXPECT_EQ(1, sync_events_called_);
1361 EXPECT_TRUE(GetRegistration(sync_options_1_));
1364 TEST_F(BackgroundSyncManagerTest, FailedOneShotReregisteredAndFires) {
1365 InitFailedSyncEventTest();
1367 // The initial sync event fails.
1368 EXPECT_TRUE(Register(sync_options_1_));
1369 EXPECT_EQ(1, sync_events_called_);
1370 EXPECT_TRUE(GetRegistration(sync_options_1_));
1372 InitSyncEventTest();
1374 // Reregistering should cause the sync event to fire again, this time
1375 // succeeding.
1376 EXPECT_TRUE(Register(sync_options_1_));
1377 EXPECT_EQ(2, sync_events_called_);
1378 EXPECT_FALSE(GetRegistration(sync_options_1_));
1381 TEST_F(BackgroundSyncManagerTest, DelayOneShotMidSync) {
1382 InitDelayedSyncEventTest();
1384 RegisterAndVerifySyncEventDelayed(sync_options_1_);
1386 // Finish firing the event and verify that the registration is removed.
1387 sync_fired_callback_.Run(SERVICE_WORKER_OK);
1388 base::RunLoop().RunUntilIdle();
1389 EXPECT_EQ(1, sync_events_called_);
1390 EXPECT_FALSE(GetRegistration(sync_options_1_));
1393 TEST_F(BackgroundSyncManagerTest, OverwriteRegistrationMidSync) {
1394 InitDelayedSyncEventTest();
1396 sync_options_1_.network_state = NETWORK_STATE_ANY;
1397 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE);
1399 RegisterAndVerifySyncEventDelayed(sync_options_1_);
1401 // Don't delay the next sync.
1402 test_background_sync_manager_->set_one_shot_callback(
1403 base::Bind(OneShotSuccessfulCallback, &sync_events_called_));
1405 // Register a different sync event with the same tag, overwriting the first.
1406 sync_options_1_.network_state = NETWORK_STATE_ONLINE;
1407 EXPECT_TRUE(Register(sync_options_1_));
1409 // The new sync event won't run as the network requirements aren't met.
1410 EXPECT_EQ(1, sync_events_called_);
1411 EXPECT_TRUE(GetRegistration(sync_options_1_));
1413 // Finish the first event, note that the second is still registered.
1414 sync_fired_callback_.Run(SERVICE_WORKER_OK);
1415 EXPECT_EQ(1, sync_events_called_);
1416 EXPECT_TRUE(GetRegistration(sync_options_1_));
1418 // Change the network and the second should run.
1419 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI);
1420 base::RunLoop().RunUntilIdle();
1421 EXPECT_EQ(2, sync_events_called_);
1422 EXPECT_FALSE(GetRegistration(sync_options_1_));
1425 TEST_F(BackgroundSyncManagerTest, ReregisterOneShotMidSync) {
1426 InitDelayedSyncEventTest();
1428 RegisterAndVerifySyncEventDelayed(sync_options_1_);
1430 // Register the same sync, but don't delay it. It shouldn't run as it's
1431 // already firing.
1432 test_background_sync_manager_->set_one_shot_callback(
1433 base::Bind(OneShotSuccessfulCallback, &sync_events_called_));
1434 EXPECT_TRUE(Register(sync_options_1_));
1435 EXPECT_EQ(1, sync_events_called_);
1436 EXPECT_TRUE(GetRegistration(sync_options_1_));
1438 // Finish the original event, note that the second never runs.
1439 sync_fired_callback_.Run(SERVICE_WORKER_OK);
1440 base::RunLoop().RunUntilIdle();
1441 EXPECT_EQ(1, sync_events_called_);
1442 EXPECT_FALSE(GetRegistration(sync_options_1_));
1445 TEST_F(BackgroundSyncManagerTest, UnregisterOneShotMidSync) {
1446 InitDelayedSyncEventTest();
1448 RegisterAndVerifySyncEventDelayed(sync_options_1_);
1450 EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
1451 EXPECT_FALSE(GetRegistration(sync_options_1_));
1453 sync_fired_callback_.Run(SERVICE_WORKER_OK);
1454 EXPECT_FALSE(GetRegistration(sync_options_1_));
1457 TEST_F(BackgroundSyncManagerTest, BadBackendMidSync) {
1458 InitDelayedSyncEventTest();
1460 RegisterAndVerifySyncEventDelayed(sync_options_1_);
1462 test_background_sync_manager_->set_corrupt_backend(true);
1463 sync_fired_callback_.Run(SERVICE_WORKER_OK);
1464 base::RunLoop().RunUntilIdle();
1466 // The backend should now be disabled because it couldn't unregister the
1467 // one-shot.
1468 EXPECT_FALSE(Register(sync_options_2_));
1469 EXPECT_FALSE(
1470 RegisterWithServiceWorkerId(sw_registration_id_2_, sync_options_2_));
1473 TEST_F(BackgroundSyncManagerTest, UnregisterServiceWorkerMidSync) {
1474 InitDelayedSyncEventTest();
1476 RegisterAndVerifySyncEventDelayed(sync_options_1_);
1477 UnregisterServiceWorker(sw_registration_id_1_);
1479 sync_fired_callback_.Run(SERVICE_WORKER_OK);
1481 // The backend isn't disabled, but the first service worker registration is
1482 // gone.
1483 EXPECT_FALSE(GetRegistration(sync_options_1_));
1484 EXPECT_FALSE(Register(sync_options_1_));
1485 EXPECT_TRUE(
1486 RegisterWithServiceWorkerId(sw_registration_id_2_, sync_options_1_));
1489 TEST_F(BackgroundSyncManagerTest, KillManagerMidSync) {
1490 InitDelayedSyncEventTest();
1492 RegisterAndVerifySyncEventDelayed(sync_options_1_);
1494 // Create a new manager which should fire the sync again on init.
1495 SetupBackgroundSyncManager();
1496 InitSyncEventTest();
1497 EXPECT_FALSE(GetRegistration(sync_options_1_));
1498 EXPECT_EQ(2, sync_events_called_);
1501 TEST_F(BackgroundSyncManagerTest, RegisterFailsWithoutWindow) {
1502 RemoveWindowClients();
1503 EXPECT_FALSE(Register(sync_options_1_));
1506 TEST_F(BackgroundSyncManagerTest, RegisterExistingFailsWithoutWindow) {
1507 EXPECT_TRUE(Register(sync_options_1_));
1508 RemoveWindowClients();
1509 EXPECT_FALSE(Register(sync_options_1_));
1512 TEST_F(BackgroundSyncManagerTest, RegisterSucceedsFromUncontrolledWindow) {
1513 RemoveWindowClients();
1514 EXPECT_TRUE(RegisterFromDocumentWithServiceWorkerId(sw_registration_id_1_,
1515 sync_options_1_));
1518 TEST_F(BackgroundSyncManagerTest, UnregisterSucceedsWithoutWindow) {
1519 EXPECT_TRUE(Register(sync_options_1_));
1520 RemoveWindowClients();
1521 EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
1522 EXPECT_FALSE(GetRegistration(sync_options_1_));
1525 } // namespace content