| 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/location.h" | 8 #include "base/location.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/power_monitor/power_monitor.h" | 10 #include "base/power_monitor/power_monitor.h" |
| (...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 | 367 |
| 368 bool Register(const BackgroundSyncRegistrationOptions& sync_options) { | 368 bool Register(const BackgroundSyncRegistrationOptions& sync_options) { |
| 369 return RegisterWithServiceWorkerId(sw_registration_id_1_, sync_options); | 369 return RegisterWithServiceWorkerId(sw_registration_id_1_, sync_options); |
| 370 } | 370 } |
| 371 | 371 |
| 372 bool RegisterWithServiceWorkerId( | 372 bool RegisterWithServiceWorkerId( |
| 373 int64 sw_registration_id, | 373 int64 sw_registration_id, |
| 374 const BackgroundSyncRegistrationOptions& options) { | 374 const BackgroundSyncRegistrationOptions& options) { |
| 375 bool was_called = false; | 375 bool was_called = false; |
| 376 background_sync_manager_->Register( | 376 background_sync_manager_->Register( |
| 377 sw_registration_id, options, | 377 sw_registration_id, options, true /* requested_from_service_worker */, |
| 378 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, | 378 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, |
| 379 base::Unretained(this), &was_called)); | 379 base::Unretained(this), &was_called)); |
| 380 base::RunLoop().RunUntilIdle(); | 380 base::RunLoop().RunUntilIdle(); |
| 381 EXPECT_TRUE(was_called); |
| 382 return callback_status_ == BACKGROUND_SYNC_STATUS_OK; |
| 383 } |
| 384 |
| 385 bool RegisterFromDocumentWithServiceWorkerId( |
| 386 int64 sw_registration_id, |
| 387 const BackgroundSyncRegistrationOptions& options) { |
| 388 bool was_called = false; |
| 389 background_sync_manager_->Register( |
| 390 sw_registration_id, options, false /* requested_from_service_worker */, |
| 391 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, |
| 392 base::Unretained(this), &was_called)); |
| 393 base::RunLoop().RunUntilIdle(); |
| 381 EXPECT_TRUE(was_called); | 394 EXPECT_TRUE(was_called); |
| 382 return callback_status_ == BACKGROUND_SYNC_STATUS_OK; | 395 return callback_status_ == BACKGROUND_SYNC_STATUS_OK; |
| 383 } | 396 } |
| 384 | 397 |
| 385 bool Unregister(const BackgroundSyncRegistration& sync_registration) { | 398 bool Unregister(const BackgroundSyncRegistration& sync_registration) { |
| 386 return UnregisterWithServiceWorkerId(sw_registration_id_1_, | 399 return UnregisterWithServiceWorkerId(sw_registration_id_1_, |
| 387 sync_registration); | 400 sync_registration); |
| 388 } | 401 } |
| 389 | 402 |
| 390 bool UnregisterWithServiceWorkerId( | 403 bool UnregisterWithServiceWorkerId( |
| (...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 775 // the operations complete sequentially. | 788 // the operations complete sequentially. |
| 776 SetupDelayedBackgroundSyncManager(); | 789 SetupDelayedBackgroundSyncManager(); |
| 777 | 790 |
| 778 const int64 kExpectedInitialId = BackgroundSyncRegistration::kInitialId; | 791 const int64 kExpectedInitialId = BackgroundSyncRegistration::kInitialId; |
| 779 | 792 |
| 780 bool register_called = false; | 793 bool register_called = false; |
| 781 bool unregister_called = false; | 794 bool unregister_called = false; |
| 782 bool get_registration_called = false; | 795 bool get_registration_called = false; |
| 783 test_background_sync_manager_->Register( | 796 test_background_sync_manager_->Register( |
| 784 sw_registration_id_1_, sync_options_1_, | 797 sw_registration_id_1_, sync_options_1_, |
| 798 true /* requested_from_service_worker */, |
| 785 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, | 799 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, |
| 786 base::Unretained(this), ®ister_called)); | 800 base::Unretained(this), ®ister_called)); |
| 787 test_background_sync_manager_->Unregister( | 801 test_background_sync_manager_->Unregister( |
| 788 sw_registration_id_1_, sync_options_1_.tag, sync_options_1_.periodicity, | 802 sw_registration_id_1_, sync_options_1_.tag, sync_options_1_.periodicity, |
| 789 kExpectedInitialId, | 803 kExpectedInitialId, |
| 790 base::Bind(&BackgroundSyncManagerTest::StatusCallback, | 804 base::Bind(&BackgroundSyncManagerTest::StatusCallback, |
| 791 base::Unretained(this), &unregister_called)); | 805 base::Unretained(this), &unregister_called)); |
| 792 test_background_sync_manager_->GetRegistration( | 806 test_background_sync_manager_->GetRegistration( |
| 793 sw_registration_id_1_, sync_options_1_.tag, sync_options_1_.periodicity, | 807 sw_registration_id_1_, sync_options_1_.tag, sync_options_1_.periodicity, |
| 794 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, | 808 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 832 } | 846 } |
| 833 | 847 |
| 834 TEST_F(BackgroundSyncManagerTest, | 848 TEST_F(BackgroundSyncManagerTest, |
| 835 UnregisterServiceWorkerDuringSyncRegistration) { | 849 UnregisterServiceWorkerDuringSyncRegistration) { |
| 836 EXPECT_TRUE(Register(sync_options_1_)); | 850 EXPECT_TRUE(Register(sync_options_1_)); |
| 837 | 851 |
| 838 test_background_sync_manager_->set_delay_backend(true); | 852 test_background_sync_manager_->set_delay_backend(true); |
| 839 bool callback_called = false; | 853 bool callback_called = false; |
| 840 test_background_sync_manager_->Register( | 854 test_background_sync_manager_->Register( |
| 841 sw_registration_id_1_, sync_options_2_, | 855 sw_registration_id_1_, sync_options_2_, |
| 856 true /* requested_from_service_worker */, |
| 842 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, | 857 base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, |
| 843 base::Unretained(this), &callback_called)); | 858 base::Unretained(this), &callback_called)); |
| 844 | 859 |
| 845 base::RunLoop().RunUntilIdle(); | 860 base::RunLoop().RunUntilIdle(); |
| 846 EXPECT_FALSE(callback_called); | 861 EXPECT_FALSE(callback_called); |
| 847 UnregisterServiceWorker(sw_registration_id_1_); | 862 UnregisterServiceWorker(sw_registration_id_1_); |
| 848 | 863 |
| 849 test_background_sync_manager_->Continue(); | 864 test_background_sync_manager_->Continue(); |
| 850 base::RunLoop().RunUntilIdle(); | 865 base::RunLoop().RunUntilIdle(); |
| 851 EXPECT_TRUE(callback_called); | 866 EXPECT_TRUE(callback_called); |
| (...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1262 RemoveWindowClients(); | 1277 RemoveWindowClients(); |
| 1263 EXPECT_FALSE(Register(sync_options_1_)); | 1278 EXPECT_FALSE(Register(sync_options_1_)); |
| 1264 } | 1279 } |
| 1265 | 1280 |
| 1266 TEST_F(BackgroundSyncManagerTest, RegisterExistingFailsWithoutWindow) { | 1281 TEST_F(BackgroundSyncManagerTest, RegisterExistingFailsWithoutWindow) { |
| 1267 EXPECT_TRUE(Register(sync_options_1_)); | 1282 EXPECT_TRUE(Register(sync_options_1_)); |
| 1268 RemoveWindowClients(); | 1283 RemoveWindowClients(); |
| 1269 EXPECT_FALSE(Register(sync_options_1_)); | 1284 EXPECT_FALSE(Register(sync_options_1_)); |
| 1270 } | 1285 } |
| 1271 | 1286 |
| 1287 TEST_F(BackgroundSyncManagerTest, RegisterSucceedsFromUncontrolledWindow) { |
| 1288 RemoveWindowClients(); |
| 1289 EXPECT_TRUE(RegisterFromDocumentWithServiceWorkerId(sw_registration_id_1_, |
| 1290 sync_options_1_)); |
| 1291 } |
| 1292 |
| 1272 TEST_F(BackgroundSyncManagerTest, UnregisterSucceedsWithoutWindow) { | 1293 TEST_F(BackgroundSyncManagerTest, UnregisterSucceedsWithoutWindow) { |
| 1273 EXPECT_TRUE(Register(sync_options_1_)); | 1294 EXPECT_TRUE(Register(sync_options_1_)); |
| 1274 RemoveWindowClients(); | 1295 RemoveWindowClients(); |
| 1275 EXPECT_TRUE(Unregister(callback_registration_)); | 1296 EXPECT_TRUE(Unregister(callback_registration_)); |
| 1276 EXPECT_FALSE(GetRegistration(sync_options_1_)); | 1297 EXPECT_FALSE(GetRegistration(sync_options_1_)); |
| 1277 } | 1298 } |
| 1278 | 1299 |
| 1279 } // namespace content | 1300 } // namespace content |
| OLD | NEW |