| 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 <stdint.h> | 7 #include <stdint.h> |
| 8 |
| 9 #include <memory> |
| 8 #include <utility> | 10 #include <utility> |
| 9 | 11 |
| 10 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 11 #include "base/location.h" | 13 #include "base/location.h" |
| 12 #include "base/logging.h" | 14 #include "base/logging.h" |
| 13 #include "base/macros.h" | 15 #include "base/macros.h" |
| 14 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/ptr_util.h" |
| 15 #include "base/metrics/field_trial.h" | 17 #include "base/metrics/field_trial.h" |
| 16 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
| 17 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
| 18 #include "base/test/mock_entropy_provider.h" | 20 #include "base/test/mock_entropy_provider.h" |
| 19 #include "base/test/simple_test_clock.h" | 21 #include "base/test/simple_test_clock.h" |
| 20 #include "base/thread_task_runner_handle.h" | 22 #include "base/thread_task_runner_handle.h" |
| 21 #include "content/browser/background_sync/background_sync_network_observer.h" | 23 #include "content/browser/background_sync/background_sync_network_observer.h" |
| 22 #include "content/browser/background_sync/background_sync_status.h" | 24 #include "content/browser/background_sync/background_sync_status.h" |
| 23 #include "content/browser/browser_thread_impl.h" | 25 #include "content/browser/browser_thread_impl.h" |
| 24 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 26 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 | 118 |
| 117 void SetUp() override { | 119 void SetUp() override { |
| 118 // Don't let the tests be confused by the real-world device connectivity | 120 // Don't let the tests be confused by the real-world device connectivity |
| 119 background_sync_test_util::SetIgnoreNetworkChangeNotifier(true); | 121 background_sync_test_util::SetIgnoreNetworkChangeNotifier(true); |
| 120 | 122 |
| 121 // TODO(jkarlin): Create a new object with all of the necessary SW calls | 123 // TODO(jkarlin): Create a new object with all of the necessary SW calls |
| 122 // so that we can inject test versions instead of bringing up all of this | 124 // so that we can inject test versions instead of bringing up all of this |
| 123 // extra SW stuff. | 125 // extra SW stuff. |
| 124 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); | 126 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); |
| 125 | 127 |
| 126 scoped_ptr<MockPermissionManager> mock_permission_manager( | 128 std::unique_ptr<MockPermissionManager> mock_permission_manager( |
| 127 new testing::NiceMock<MockPermissionManager>()); | 129 new testing::NiceMock<MockPermissionManager>()); |
| 128 ON_CALL(*mock_permission_manager, | 130 ON_CALL(*mock_permission_manager, |
| 129 GetPermissionStatus(PermissionType::BACKGROUND_SYNC, _, _)) | 131 GetPermissionStatus(PermissionType::BACKGROUND_SYNC, _, _)) |
| 130 .WillByDefault(Return(blink::mojom::PermissionStatus::GRANTED)); | 132 .WillByDefault(Return(blink::mojom::PermissionStatus::GRANTED)); |
| 131 helper_->browser_context()->SetPermissionManager( | 133 helper_->browser_context()->SetPermissionManager( |
| 132 std::move(mock_permission_manager)); | 134 std::move(mock_permission_manager)); |
| 133 | 135 |
| 134 // Create a StoragePartition with the correct BrowserContext so that the | 136 // Create a StoragePartition with the correct BrowserContext so that the |
| 135 // BackgroundSyncManager can find the BrowserContext through it. | 137 // BackgroundSyncManager can find the BrowserContext through it. |
| 136 storage_partition_impl_.reset(new StoragePartitionImpl( | 138 storage_partition_impl_.reset(new StoragePartitionImpl( |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 test_background_sync_manager_->GetNetworkObserverForTesting(); | 193 test_background_sync_manager_->GetNetworkObserverForTesting(); |
| 192 network_observer->NotifyManagerIfNetworkChangedForTesting( | 194 network_observer->NotifyManagerIfNetworkChangedForTesting( |
| 193 connection_type); | 195 connection_type); |
| 194 base::RunLoop().RunUntilIdle(); | 196 base::RunLoop().RunUntilIdle(); |
| 195 } | 197 } |
| 196 } | 198 } |
| 197 | 199 |
| 198 void StatusAndRegistrationCallback( | 200 void StatusAndRegistrationCallback( |
| 199 bool* was_called, | 201 bool* was_called, |
| 200 BackgroundSyncStatus status, | 202 BackgroundSyncStatus status, |
| 201 scoped_ptr<BackgroundSyncRegistration> registration) { | 203 std::unique_ptr<BackgroundSyncRegistration> registration) { |
| 202 *was_called = true; | 204 *was_called = true; |
| 203 callback_status_ = status; | 205 callback_status_ = status; |
| 204 callback_registration_ = std::move(registration); | 206 callback_registration_ = std::move(registration); |
| 205 } | 207 } |
| 206 | 208 |
| 207 void StatusAndRegistrationsCallback( | 209 void StatusAndRegistrationsCallback( |
| 208 bool* was_called, | 210 bool* was_called, |
| 209 BackgroundSyncStatus status, | 211 BackgroundSyncStatus status, |
| 210 scoped_ptr<ScopedVector<BackgroundSyncRegistration>> registrations) { | 212 std::unique_ptr<ScopedVector<BackgroundSyncRegistration>> registrations) { |
| 211 *was_called = true; | 213 *was_called = true; |
| 212 callback_status_ = status; | 214 callback_status_ = status; |
| 213 callback_registrations_ = std::move(registrations); | 215 callback_registrations_ = std::move(registrations); |
| 214 } | 216 } |
| 215 | 217 |
| 216 void StatusCallback(bool* was_called, BackgroundSyncStatus status) { | 218 void StatusCallback(bool* was_called, BackgroundSyncStatus status) { |
| 217 *was_called = true; | 219 *was_called = true; |
| 218 callback_status_ = status; | 220 callback_status_ = status; |
| 219 } | 221 } |
| 220 | 222 |
| 221 protected: | 223 protected: |
| 222 void CreateBackgroundSyncManager() { | 224 void CreateBackgroundSyncManager() { |
| 223 test_background_sync_manager_ = | 225 test_background_sync_manager_ = |
| 224 new TestBackgroundSyncManager(helper_->context_wrapper()); | 226 new TestBackgroundSyncManager(helper_->context_wrapper()); |
| 225 background_sync_manager_.reset(test_background_sync_manager_); | 227 background_sync_manager_.reset(test_background_sync_manager_); |
| 226 | 228 |
| 227 test_clock_ = new base::SimpleTestClock(); | 229 test_clock_ = new base::SimpleTestClock(); |
| 228 background_sync_manager_->set_clock(make_scoped_ptr(test_clock_)); | 230 background_sync_manager_->set_clock(base::WrapUnique(test_clock_)); |
| 229 | 231 |
| 230 // Many tests do not expect the sync event to fire immediately after | 232 // Many tests do not expect the sync event to fire immediately after |
| 231 // register (and cleanup up the sync registrations). Tests can control when | 233 // register (and cleanup up the sync registrations). Tests can control when |
| 232 // the sync event fires by manipulating the network state as needed. | 234 // the sync event fires by manipulating the network state as needed. |
| 233 // NOTE: The setup of the network connection must happen after the | 235 // NOTE: The setup of the network connection must happen after the |
| 234 // BackgroundSyncManager has been created. | 236 // BackgroundSyncManager has been created. |
| 235 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE); | 237 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE); |
| 236 } | 238 } |
| 237 | 239 |
| 238 void InitBackgroundSyncManager() { | 240 void InitBackgroundSyncManager() { |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 void SetMaxSyncAttemptsAndRestartManager(int max_sync_attempts) { | 402 void SetMaxSyncAttemptsAndRestartManager(int max_sync_attempts) { |
| 401 BackgroundSyncParameters* parameters = | 403 BackgroundSyncParameters* parameters = |
| 402 GetController()->background_sync_parameters(); | 404 GetController()->background_sync_parameters(); |
| 403 parameters->max_sync_attempts = max_sync_attempts; | 405 parameters->max_sync_attempts = max_sync_attempts; |
| 404 | 406 |
| 405 // Restart the BackgroundSyncManager so that it updates its parameters. | 407 // Restart the BackgroundSyncManager so that it updates its parameters. |
| 406 SetupBackgroundSyncManager(); | 408 SetupBackgroundSyncManager(); |
| 407 } | 409 } |
| 408 | 410 |
| 409 TestBrowserThreadBundle browser_thread_bundle_; | 411 TestBrowserThreadBundle browser_thread_bundle_; |
| 410 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; | 412 std::unique_ptr<net::NetworkChangeNotifier> network_change_notifier_; |
| 411 scoped_ptr<EmbeddedWorkerTestHelper> helper_; | 413 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; |
| 412 scoped_ptr<BackgroundSyncManager> background_sync_manager_; | 414 std::unique_ptr<BackgroundSyncManager> background_sync_manager_; |
| 413 scoped_ptr<StoragePartitionImpl> storage_partition_impl_; | 415 std::unique_ptr<StoragePartitionImpl> storage_partition_impl_; |
| 414 TestBackgroundSyncManager* test_background_sync_manager_ = nullptr; | 416 TestBackgroundSyncManager* test_background_sync_manager_ = nullptr; |
| 415 base::SimpleTestClock* test_clock_ = nullptr; | 417 base::SimpleTestClock* test_clock_ = nullptr; |
| 416 | 418 |
| 417 int64_t sw_registration_id_1_; | 419 int64_t sw_registration_id_1_; |
| 418 int64_t sw_registration_id_2_; | 420 int64_t sw_registration_id_2_; |
| 419 scoped_refptr<ServiceWorkerRegistration> sw_registration_1_; | 421 scoped_refptr<ServiceWorkerRegistration> sw_registration_1_; |
| 420 scoped_refptr<ServiceWorkerRegistration> sw_registration_2_; | 422 scoped_refptr<ServiceWorkerRegistration> sw_registration_2_; |
| 421 | 423 |
| 422 BackgroundSyncRegistrationOptions sync_options_1_; | 424 BackgroundSyncRegistrationOptions sync_options_1_; |
| 423 BackgroundSyncRegistrationOptions sync_options_2_; | 425 BackgroundSyncRegistrationOptions sync_options_2_; |
| 424 | 426 |
| 425 // Callback values. | 427 // Callback values. |
| 426 BackgroundSyncStatus callback_status_ = BACKGROUND_SYNC_STATUS_OK; | 428 BackgroundSyncStatus callback_status_ = BACKGROUND_SYNC_STATUS_OK; |
| 427 scoped_ptr<BackgroundSyncRegistration> callback_registration_; | 429 std::unique_ptr<BackgroundSyncRegistration> callback_registration_; |
| 428 scoped_ptr<ScopedVector<BackgroundSyncRegistration>> callback_registrations_; | 430 std::unique_ptr<ScopedVector<BackgroundSyncRegistration>> |
| 431 callback_registrations_; |
| 429 ServiceWorkerStatusCode callback_sw_status_code_ = SERVICE_WORKER_OK; | 432 ServiceWorkerStatusCode callback_sw_status_code_ = SERVICE_WORKER_OK; |
| 430 int sync_events_called_ = 0; | 433 int sync_events_called_ = 0; |
| 431 ServiceWorkerVersion::StatusCallback sync_fired_callback_; | 434 ServiceWorkerVersion::StatusCallback sync_fired_callback_; |
| 432 }; | 435 }; |
| 433 | 436 |
| 434 TEST_F(BackgroundSyncManagerTest, Register) { | 437 TEST_F(BackgroundSyncManagerTest, Register) { |
| 435 EXPECT_TRUE(Register(sync_options_1_)); | 438 EXPECT_TRUE(Register(sync_options_1_)); |
| 436 } | 439 } |
| 437 | 440 |
| 438 TEST_F(BackgroundSyncManagerTest, RegistrationIntact) { | 441 TEST_F(BackgroundSyncManagerTest, RegistrationIntact) { |
| (...skipping 872 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1311 // Run it again. | 1314 // Run it again. |
| 1312 test_clock_->Advance(test_background_sync_manager_->delayed_task_delta()); | 1315 test_clock_->Advance(test_background_sync_manager_->delayed_task_delta()); |
| 1313 test_background_sync_manager_->delayed_task().Run(); | 1316 test_background_sync_manager_->delayed_task().Run(); |
| 1314 base::RunLoop().RunUntilIdle(); | 1317 base::RunLoop().RunUntilIdle(); |
| 1315 EXPECT_FALSE(GetRegistration(sync_options_1_)); | 1318 EXPECT_FALSE(GetRegistration(sync_options_1_)); |
| 1316 EXPECT_EQ(mojom::BackgroundSyncEventLastChance::IS_LAST_CHANCE, | 1319 EXPECT_EQ(mojom::BackgroundSyncEventLastChance::IS_LAST_CHANCE, |
| 1317 test_background_sync_manager_->last_chance()); | 1320 test_background_sync_manager_->last_chance()); |
| 1318 } | 1321 } |
| 1319 | 1322 |
| 1320 } // namespace content | 1323 } // namespace content |
| OLD | NEW |