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/power_monitor/power_monitor.h" | 9 #include "base/power_monitor/power_monitor.h" |
10 #include "base/power_monitor/power_monitor_source.h" | 10 #include "base/power_monitor/power_monitor_source.h" |
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 } | 308 } |
309 | 309 |
310 protected: | 310 protected: |
311 void UseTestBackgroundSyncManager() { | 311 void UseTestBackgroundSyncManager() { |
312 test_background_sync_manager_ = | 312 test_background_sync_manager_ = |
313 new TestBackgroundSyncManager(helper_->context_wrapper()); | 313 new TestBackgroundSyncManager(helper_->context_wrapper()); |
314 test_background_sync_manager_->DoInit(); | 314 test_background_sync_manager_->DoInit(); |
315 background_sync_manager_.reset(test_background_sync_manager_); | 315 background_sync_manager_.reset(test_background_sync_manager_); |
316 } | 316 } |
317 | 317 |
| 318 void DeleteBackgroundSyncManager() { |
| 319 background_sync_manager_.reset(); |
| 320 test_background_sync_manager_ = nullptr; |
| 321 } |
| 322 |
318 bool Register(const BackgroundSyncManager::BackgroundSyncRegistration& | 323 bool Register(const BackgroundSyncManager::BackgroundSyncRegistration& |
319 sync_registration) { | 324 sync_registration) { |
320 return RegisterWithServiceWorkerId(sw_registration_id_1_, | 325 return RegisterWithServiceWorkerId(sw_registration_id_1_, |
321 sync_registration); | 326 sync_registration); |
322 } | 327 } |
323 | 328 |
324 bool RegisterWithServiceWorkerId( | 329 bool RegisterWithServiceWorkerId( |
325 int64 sw_registration_id, | 330 int64 sw_registration_id, |
326 const BackgroundSyncManager::BackgroundSyncRegistration& | 331 const BackgroundSyncManager::BackgroundSyncRegistration& |
327 sync_registration) { | 332 sync_registration) { |
(...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
722 EXPECT_TRUE( | 727 EXPECT_TRUE( |
723 GetRegistrationWithServiceWorkerId(sw_registration_id_1_, sync_reg_1_)); | 728 GetRegistrationWithServiceWorkerId(sw_registration_id_1_, sync_reg_1_)); |
724 EXPECT_TRUE( | 729 EXPECT_TRUE( |
725 GetRegistrationWithServiceWorkerId(sw_registration_id_2_, sync_reg_2_)); | 730 GetRegistrationWithServiceWorkerId(sw_registration_id_2_, sync_reg_2_)); |
726 | 731 |
727 EXPECT_TRUE(RegisterWithServiceWorkerId(sw_registration_id_1_, sync_reg_2_)); | 732 EXPECT_TRUE(RegisterWithServiceWorkerId(sw_registration_id_1_, sync_reg_2_)); |
728 EXPECT_TRUE(RegisterWithServiceWorkerId(sw_registration_id_2_, sync_reg_1_)); | 733 EXPECT_TRUE(RegisterWithServiceWorkerId(sw_registration_id_2_, sync_reg_1_)); |
729 } | 734 } |
730 | 735 |
731 TEST_F(BackgroundSyncManagerTest, InitWithBadBackend) { | 736 TEST_F(BackgroundSyncManagerTest, InitWithBadBackend) { |
732 TestBackgroundSyncManager* manager = | 737 DeleteBackgroundSyncManager(); |
| 738 test_background_sync_manager_ = |
733 new TestBackgroundSyncManager(helper_->context_wrapper()); | 739 new TestBackgroundSyncManager(helper_->context_wrapper()); |
734 background_sync_manager_.reset(manager); | 740 background_sync_manager_.reset(test_background_sync_manager_); |
735 manager->set_corrupt_backend(true); | 741 test_background_sync_manager_->set_corrupt_backend(true); |
736 manager->DoInit(); | 742 test_background_sync_manager_->DoInit(); |
737 | 743 |
738 EXPECT_FALSE(Register(sync_reg_1_)); | 744 EXPECT_FALSE(Register(sync_reg_1_)); |
739 EXPECT_FALSE(GetRegistration(sync_reg_1_)); | 745 EXPECT_FALSE(GetRegistration(sync_reg_1_)); |
740 } | 746 } |
741 | 747 |
742 TEST_F(BackgroundSyncManagerTest, SequentialOperations) { | 748 TEST_F(BackgroundSyncManagerTest, SequentialOperations) { |
743 // Schedule Init and all of the operations on a delayed backend. Verify that | 749 // Schedule Init and all of the operations on a delayed backend. Verify that |
744 // the operations complete sequentially. | 750 // the operations complete sequentially. |
745 TestBackgroundSyncManager* manager = | 751 DeleteBackgroundSyncManager(); |
| 752 |
| 753 test_background_sync_manager_ = |
746 new TestBackgroundSyncManager(helper_->context_wrapper()); | 754 new TestBackgroundSyncManager(helper_->context_wrapper()); |
747 background_sync_manager_.reset(manager); | 755 background_sync_manager_.reset(test_background_sync_manager_); |
748 manager->set_delay_backend(true); | 756 |
749 manager->DoInit(); | 757 test_background_sync_manager_->set_delay_backend(true); |
| 758 test_background_sync_manager_->DoInit(); |
750 | 759 |
751 const int64 kExpectedInitialId = | 760 const int64 kExpectedInitialId = |
752 BackgroundSyncManager::BackgroundSyncRegistration::kInitialId; | 761 BackgroundSyncManager::BackgroundSyncRegistration::kInitialId; |
753 | 762 |
754 bool register_called = false; | 763 bool register_called = false; |
755 bool unregister_called = false; | 764 bool unregister_called = false; |
756 bool get_registration_called = false; | 765 bool get_registration_called = false; |
757 manager->Register( | 766 test_background_sync_manager_->Register( |
758 sw_registration_id_1_, sync_reg_1_, | 767 sw_registration_id_1_, sync_reg_1_, |
759 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, | 768 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, |
760 base::Unretained(this), ®ister_called)); | 769 base::Unretained(this), ®ister_called)); |
761 manager->Unregister(sw_registration_id_1_, sync_reg_1_.tag, | 770 test_background_sync_manager_->Unregister( |
762 sync_reg_1_.periodicity, kExpectedInitialId, | 771 sw_registration_id_1_, sync_reg_1_.tag, sync_reg_1_.periodicity, |
763 base::Bind(&BackgroundSyncManagerTest::StatusCallback, | 772 kExpectedInitialId, |
764 base::Unretained(this), &unregister_called)); | 773 base::Bind(&BackgroundSyncManagerTest::StatusCallback, |
765 manager->GetRegistration( | 774 base::Unretained(this), &unregister_called)); |
| 775 test_background_sync_manager_->GetRegistration( |
766 sw_registration_id_1_, sync_reg_1_.tag, sync_reg_1_.periodicity, | 776 sw_registration_id_1_, sync_reg_1_.tag, sync_reg_1_.periodicity, |
767 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, | 777 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, |
768 base::Unretained(this), &get_registration_called)); | 778 base::Unretained(this), &get_registration_called)); |
769 | 779 |
770 base::RunLoop().RunUntilIdle(); | 780 base::RunLoop().RunUntilIdle(); |
771 // Init should be blocked while loading from the backend. | 781 // Init should be blocked while loading from the backend. |
772 EXPECT_FALSE(register_called); | 782 EXPECT_FALSE(register_called); |
773 EXPECT_FALSE(unregister_called); | 783 EXPECT_FALSE(unregister_called); |
774 EXPECT_FALSE(get_registration_called); | 784 EXPECT_FALSE(get_registration_called); |
775 | 785 |
776 manager->Continue(); | 786 test_background_sync_manager_->Continue(); |
777 base::RunLoop().RunUntilIdle(); | 787 base::RunLoop().RunUntilIdle(); |
778 // Register should be blocked while storing to the backend. | 788 // Register should be blocked while storing to the backend. |
779 EXPECT_FALSE(register_called); | 789 EXPECT_FALSE(register_called); |
780 EXPECT_FALSE(unregister_called); | 790 EXPECT_FALSE(unregister_called); |
781 EXPECT_FALSE(get_registration_called); | 791 EXPECT_FALSE(get_registration_called); |
782 | 792 |
783 manager->Continue(); | 793 test_background_sync_manager_->Continue(); |
784 base::RunLoop().RunUntilIdle(); | 794 base::RunLoop().RunUntilIdle(); |
785 EXPECT_TRUE(register_called); | 795 EXPECT_TRUE(register_called); |
786 EXPECT_EQ(kExpectedInitialId, callback_registration_.id); | 796 EXPECT_EQ(kExpectedInitialId, callback_registration_.id); |
787 EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_OK, callback_error_); | 797 EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_OK, callback_error_); |
788 // Unregister should be blocked while storing to the backend. | 798 // Unregister should be blocked while storing to the backend. |
789 EXPECT_FALSE(unregister_called); | 799 EXPECT_FALSE(unregister_called); |
790 EXPECT_FALSE(get_registration_called); | 800 EXPECT_FALSE(get_registration_called); |
791 | 801 |
792 manager->Continue(); | 802 test_background_sync_manager_->Continue(); |
793 base::RunLoop().RunUntilIdle(); | 803 base::RunLoop().RunUntilIdle(); |
794 // Unregister should be done and since GetRegistration doesn't require the | 804 // Unregister should be done and since GetRegistration doesn't require the |
795 // backend it should be done too. | 805 // backend it should be done too. |
796 EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_NOT_FOUND, callback_error_); | 806 EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_NOT_FOUND, callback_error_); |
797 EXPECT_TRUE(unregister_called); | 807 EXPECT_TRUE(unregister_called); |
798 EXPECT_TRUE(get_registration_called); | 808 EXPECT_TRUE(get_registration_called); |
799 } | 809 } |
800 | 810 |
801 TEST_F(BackgroundSyncManagerTest, UnregisterServiceWorker) { | 811 TEST_F(BackgroundSyncManagerTest, UnregisterServiceWorker) { |
802 EXPECT_TRUE(Register(sync_reg_1_)); | 812 EXPECT_TRUE(Register(sync_reg_1_)); |
803 UnregisterServiceWorker(sw_registration_id_1_); | 813 UnregisterServiceWorker(sw_registration_id_1_); |
804 EXPECT_FALSE(GetRegistration(sync_reg_1_)); | 814 EXPECT_FALSE(GetRegistration(sync_reg_1_)); |
805 } | 815 } |
806 | 816 |
807 TEST_F(BackgroundSyncManagerTest, | 817 TEST_F(BackgroundSyncManagerTest, |
808 UnregisterServiceWorkerDuringSyncRegistration) { | 818 UnregisterServiceWorkerDuringSyncRegistration) { |
809 TestBackgroundSyncManager* manager = | 819 UseTestBackgroundSyncManager(); |
810 new TestBackgroundSyncManager(helper_->context_wrapper()); | |
811 background_sync_manager_.reset(manager); | |
812 manager->DoInit(); | |
813 | 820 |
814 EXPECT_TRUE(Register(sync_reg_1_)); | 821 EXPECT_TRUE(Register(sync_reg_1_)); |
815 | 822 |
816 manager->set_delay_backend(true); | 823 test_background_sync_manager_->set_delay_backend(true); |
817 bool callback_called = false; | 824 bool callback_called = false; |
818 manager->Register( | 825 test_background_sync_manager_->Register( |
819 sw_registration_id_1_, sync_reg_2_, | 826 sw_registration_id_1_, sync_reg_2_, |
820 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, | 827 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, |
821 base::Unretained(this), &callback_called)); | 828 base::Unretained(this), &callback_called)); |
822 | 829 |
823 base::RunLoop().RunUntilIdle(); | 830 base::RunLoop().RunUntilIdle(); |
824 EXPECT_FALSE(callback_called); | 831 EXPECT_FALSE(callback_called); |
825 UnregisterServiceWorker(sw_registration_id_1_); | 832 UnregisterServiceWorker(sw_registration_id_1_); |
826 | 833 |
827 manager->Continue(); | 834 test_background_sync_manager_->Continue(); |
828 base::RunLoop().RunUntilIdle(); | 835 base::RunLoop().RunUntilIdle(); |
829 EXPECT_TRUE(callback_called); | 836 EXPECT_TRUE(callback_called); |
830 EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_STORAGE, callback_error_); | 837 EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_STORAGE, callback_error_); |
831 | 838 |
832 manager->set_delay_backend(false); | 839 test_background_sync_manager_->set_delay_backend(false); |
833 EXPECT_FALSE(GetRegistration(sync_reg_1_)); | 840 EXPECT_FALSE(GetRegistration(sync_reg_1_)); |
834 } | 841 } |
835 | 842 |
836 TEST_F(BackgroundSyncManagerTest, DeleteAndStartOverServiceWorkerContext) { | 843 TEST_F(BackgroundSyncManagerTest, DeleteAndStartOverServiceWorkerContext) { |
837 EXPECT_TRUE(Register(sync_reg_1_)); | 844 EXPECT_TRUE(Register(sync_reg_1_)); |
838 helper_->context()->ScheduleDeleteAndStartOver(); | 845 helper_->context()->ScheduleDeleteAndStartOver(); |
839 base::RunLoop().RunUntilIdle(); | 846 base::RunLoop().RunUntilIdle(); |
840 EXPECT_FALSE(GetRegistration(sync_reg_1_)); | 847 EXPECT_FALSE(GetRegistration(sync_reg_1_)); |
841 } | 848 } |
842 | 849 |
843 TEST_F(BackgroundSyncManagerTest, DisabledManagerWorksAfterBrowserRestart) { | 850 TEST_F(BackgroundSyncManagerTest, DisabledManagerWorksAfterBrowserRestart) { |
844 TestBackgroundSyncManager* manager = | 851 UseTestBackgroundSyncManager(); |
845 new TestBackgroundSyncManager(helper_->context_wrapper()); | |
846 background_sync_manager_.reset(manager); | |
847 manager->DoInit(); | |
848 EXPECT_TRUE(Register(sync_reg_1_)); | 852 EXPECT_TRUE(Register(sync_reg_1_)); |
849 manager->set_corrupt_backend(true); | 853 test_background_sync_manager_->set_corrupt_backend(true); |
850 EXPECT_FALSE(Register(sync_reg_2_)); | 854 EXPECT_FALSE(Register(sync_reg_2_)); |
851 | 855 |
852 // The manager is now disabled and not accepting new requests until browser | 856 // The manager is now disabled and not accepting new requests until browser |
853 // restart or notification that the storage has been wiped. | 857 // restart or notification that the storage has been wiped. |
854 manager->set_corrupt_backend(false); | 858 test_background_sync_manager_->set_corrupt_backend(false); |
855 EXPECT_FALSE(GetRegistration(sync_reg_1_)); | 859 EXPECT_FALSE(GetRegistration(sync_reg_1_)); |
856 EXPECT_FALSE(Register(sync_reg_2_)); | 860 EXPECT_FALSE(Register(sync_reg_2_)); |
857 | 861 |
858 // Simulate restarting the browser by creating a new BackgroundSyncManager. | 862 // Simulate restarting the browser by creating a new BackgroundSyncManager. |
859 background_sync_manager_.reset( | 863 UseTestBackgroundSyncManager(); |
860 new TestBackgroundSyncManager(helper_->context_wrapper())); | 864 EXPECT_TRUE(GetRegistration(sync_reg_1_)); |
861 EXPECT_FALSE(GetRegistration(sync_reg_1_)); | 865 EXPECT_TRUE(Register(sync_reg_2_)); |
862 EXPECT_TRUE(Register(sync_reg_1_)); | |
863 } | 866 } |
864 | 867 |
865 TEST_F(BackgroundSyncManagerTest, DisabledManagerWorksAfterDeleteAndStartOver) { | 868 TEST_F(BackgroundSyncManagerTest, DisabledManagerWorksAfterDeleteAndStartOver) { |
866 TestBackgroundSyncManager* manager = | 869 UseTestBackgroundSyncManager(); |
867 new TestBackgroundSyncManager(helper_->context_wrapper()); | |
868 background_sync_manager_.reset(manager); | |
869 manager->DoInit(); | |
870 EXPECT_TRUE(Register(sync_reg_1_)); | 870 EXPECT_TRUE(Register(sync_reg_1_)); |
871 manager->set_corrupt_backend(true); | 871 test_background_sync_manager_->set_corrupt_backend(true); |
872 EXPECT_FALSE(Register(sync_reg_2_)); | 872 EXPECT_FALSE(Register(sync_reg_2_)); |
873 | 873 |
874 // The manager is now disabled and not accepting new requests until browser | 874 // The manager is now disabled and not accepting new requests until browser |
875 // restart or notification that the storage has been wiped. | 875 // restart or notification that the storage has been wiped. |
876 manager->set_corrupt_backend(false); | 876 test_background_sync_manager_->set_corrupt_backend(false); |
877 helper_->context()->ScheduleDeleteAndStartOver(); | 877 helper_->context()->ScheduleDeleteAndStartOver(); |
878 base::RunLoop().RunUntilIdle(); | 878 base::RunLoop().RunUntilIdle(); |
879 | 879 |
880 RegisterServiceWorkers(); | 880 RegisterServiceWorkers(); |
881 | 881 |
882 EXPECT_TRUE(Register(sync_reg_2_)); | 882 EXPECT_TRUE(Register(sync_reg_2_)); |
883 EXPECT_FALSE(GetRegistration(sync_reg_1_)); | 883 EXPECT_FALSE(GetRegistration(sync_reg_1_)); |
884 EXPECT_TRUE(GetRegistration(sync_reg_2_)); | 884 EXPECT_TRUE(GetRegistration(sync_reg_2_)); |
885 } | 885 } |
886 | 886 |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1056 InitSyncEventTest(); | 1056 InitSyncEventTest(); |
1057 | 1057 |
1058 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE); | 1058 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE); |
1059 EXPECT_TRUE(Register(sync_reg_1_)); | 1059 EXPECT_TRUE(Register(sync_reg_1_)); |
1060 EXPECT_TRUE(Register(sync_reg_2_)); | 1060 EXPECT_TRUE(Register(sync_reg_2_)); |
1061 EXPECT_EQ(0, sync_events_called_); | 1061 EXPECT_EQ(0, sync_events_called_); |
1062 EXPECT_TRUE(GetRegistration(sync_reg_1_)); | 1062 EXPECT_TRUE(GetRegistration(sync_reg_1_)); |
1063 EXPECT_TRUE(GetRegistration(sync_reg_2_)); | 1063 EXPECT_TRUE(GetRegistration(sync_reg_2_)); |
1064 | 1064 |
1065 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI); | 1065 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI); |
1066 base::RunLoop().RunUntilIdle(); | 1066 |
1067 EXPECT_EQ(2, sync_events_called_); | 1067 EXPECT_EQ(2, sync_events_called_); |
1068 EXPECT_FALSE(GetRegistration(sync_reg_1_)); | 1068 EXPECT_FALSE(GetRegistration(sync_reg_1_)); |
1069 EXPECT_FALSE(GetRegistration(sync_reg_2_)); | 1069 EXPECT_FALSE(GetRegistration(sync_reg_2_)); |
1070 } | 1070 } |
1071 | 1071 |
1072 TEST_F(BackgroundSyncManagerTest, OneShotFiresOnManagerRestart) { | 1072 TEST_F(BackgroundSyncManagerTest, OneShotFiresOnManagerRestart) { |
1073 InitSyncEventTest(); | 1073 InitSyncEventTest(); |
1074 | 1074 |
1075 // Initially the event won't run because there is no network. | 1075 // Initially the event won't run because there is no network. |
1076 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE); | 1076 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE); |
1077 EXPECT_TRUE(Register(sync_reg_1_)); | 1077 EXPECT_TRUE(Register(sync_reg_1_)); |
1078 EXPECT_EQ(0, sync_events_called_); | 1078 EXPECT_EQ(0, sync_events_called_); |
1079 EXPECT_TRUE(GetRegistration(sync_reg_1_)); | 1079 EXPECT_TRUE(GetRegistration(sync_reg_1_)); |
1080 | 1080 |
1081 // Simulate closing the browser. | 1081 // Simulate closing the browser. |
1082 background_sync_manager_.reset(); | 1082 DeleteBackgroundSyncManager(); |
1083 | 1083 |
1084 // The next time the manager is started, the network is good. | 1084 // The next time the manager is started, the network is good. |
1085 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI); | 1085 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI); |
1086 InitSyncEventTest(); | 1086 InitSyncEventTest(); |
1087 | 1087 |
1088 // The event should have fired. | 1088 // The event should have fired. |
1089 EXPECT_EQ(1, sync_events_called_); | 1089 EXPECT_EQ(1, sync_events_called_); |
1090 EXPECT_FALSE(GetRegistration(sync_reg_1_)); | 1090 EXPECT_FALSE(GetRegistration(sync_reg_1_)); |
1091 } | 1091 } |
1092 | 1092 |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1216 | 1216 |
1217 RegisterAndVerifySyncEventDelayed(sync_reg_1_); | 1217 RegisterAndVerifySyncEventDelayed(sync_reg_1_); |
1218 | 1218 |
1219 // Create a new manager which should fire the sync again on init. | 1219 // Create a new manager which should fire the sync again on init. |
1220 InitSyncEventTest(); | 1220 InitSyncEventTest(); |
1221 EXPECT_FALSE(GetRegistration(sync_reg_1_)); | 1221 EXPECT_FALSE(GetRegistration(sync_reg_1_)); |
1222 EXPECT_EQ(2, sync_events_called_); | 1222 EXPECT_EQ(2, sync_events_called_); |
1223 } | 1223 } |
1224 | 1224 |
1225 } // namespace content | 1225 } // namespace content |
OLD | NEW |