OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/background_sync/background_sync_manager.h" | 5 #include "content/browser/background_sync/background_sync_manager.h" |
6 | 6 |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 private: | 130 private: |
131 bool corrupt_backend_ = false; | 131 bool corrupt_backend_ = false; |
132 bool delay_backend_ = false; | 132 bool delay_backend_ = false; |
133 base::Closure continuation_; | 133 base::Closure continuation_; |
134 }; | 134 }; |
135 | 135 |
136 class BackgroundSyncManagerTest : public testing::Test { | 136 class BackgroundSyncManagerTest : public testing::Test { |
137 public: | 137 public: |
138 BackgroundSyncManagerTest() | 138 BackgroundSyncManagerTest() |
139 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | 139 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
140 sync_reg_1_(BackgroundSyncManager::BackgroundSyncRegistration("foo")), | 140 sync_reg_1_(BackgroundSyncManager::BackgroundSyncRegistration()), |
141 sync_reg_2_(BackgroundSyncManager::BackgroundSyncRegistration("bar")), | 141 sync_reg_2_(BackgroundSyncManager::BackgroundSyncRegistration()), |
142 callback_error_(BackgroundSyncManager::ERROR_TYPE_OK), | 142 callback_error_(BackgroundSyncManager::ERROR_TYPE_OK), |
143 callback_sw_status_code_(SERVICE_WORKER_OK) {} | 143 callback_sw_status_code_(SERVICE_WORKER_OK) { |
| 144 sync_reg_1_.name = "foo"; |
| 145 sync_reg_2_.name = "bar"; |
| 146 } |
144 | 147 |
145 void SetUp() override { | 148 void SetUp() override { |
146 helper_.reset( | 149 helper_.reset( |
147 new EmbeddedWorkerTestHelper(base::FilePath(), kRenderProcessId)); | 150 new EmbeddedWorkerTestHelper(base::FilePath(), kRenderProcessId)); |
148 | 151 |
149 background_sync_manager_ = | 152 background_sync_manager_ = |
150 BackgroundSyncManager::Create(helper_->context_wrapper()); | 153 BackgroundSyncManager::Create(helper_->context_wrapper()); |
151 | 154 |
152 // Wait for storage to finish initializing before registering service | 155 // Wait for storage to finish initializing before registering service |
153 // workers. | 156 // workers. |
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
612 base::Bind(&RegisterServiceWorkerCallback, &called, | 615 base::Bind(&RegisterServiceWorkerCallback, &called, |
613 &sw_registration_id_1_)); | 616 &sw_registration_id_1_)); |
614 base::RunLoop().RunUntilIdle(); | 617 base::RunLoop().RunUntilIdle(); |
615 EXPECT_TRUE(called); | 618 EXPECT_TRUE(called); |
616 | 619 |
617 EXPECT_TRUE(Register(sync_reg_2_)); | 620 EXPECT_TRUE(Register(sync_reg_2_)); |
618 EXPECT_FALSE(GetRegistration(sync_reg_1_.name)); | 621 EXPECT_FALSE(GetRegistration(sync_reg_1_.name)); |
619 EXPECT_TRUE(GetRegistration(sync_reg_2_.name)); | 622 EXPECT_TRUE(GetRegistration(sync_reg_2_.name)); |
620 } | 623 } |
621 | 624 |
| 625 TEST_F(BackgroundSyncManagerTest, RegistrationEqualsId) { |
| 626 BackgroundSyncManager::BackgroundSyncRegistration reg_1; |
| 627 BackgroundSyncManager::BackgroundSyncRegistration reg_2; |
| 628 |
| 629 EXPECT_TRUE(reg_1.Equals(reg_2)); |
| 630 reg_2.id = reg_1.id + 1; |
| 631 EXPECT_TRUE(reg_1.Equals(reg_2)); |
| 632 } |
| 633 |
| 634 TEST_F(BackgroundSyncManagerTest, RegistrationEqualsName) { |
| 635 BackgroundSyncManager::BackgroundSyncRegistration reg_1; |
| 636 BackgroundSyncManager::BackgroundSyncRegistration reg_2; |
| 637 EXPECT_TRUE(reg_1.Equals(reg_2)); |
| 638 reg_2.name = "bar"; |
| 639 EXPECT_FALSE(reg_1.Equals(reg_2)); |
| 640 } |
| 641 |
| 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)); |
| 648 } |
| 649 |
| 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)); |
| 656 } |
| 657 |
| 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)); |
| 665 } |
| 666 |
| 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)); |
| 674 } |
| 675 |
| 676 TEST_F(BackgroundSyncManagerTest, StoreAndRetrievePreservesValues) { |
| 677 BackgroundSyncManager::BackgroundSyncRegistration reg_1; |
| 678 // Set non-default values for each field. |
| 679 reg_1.name = "foo"; |
| 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; |
| 686 |
| 687 // Store the registration. |
| 688 EXPECT_TRUE(Register(reg_1)); |
| 689 |
| 690 // Simulate restarting the sync manager, forcing the next read to come from |
| 691 // disk. |
| 692 UseTestBackgroundSyncManager(); |
| 693 |
| 694 EXPECT_TRUE(GetRegistration(reg_1.name)); |
| 695 EXPECT_TRUE(reg_1.Equals(callback_registration_)); |
| 696 } |
| 697 |
622 } // namespace content | 698 } // namespace content |
OLD | NEW |