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/metrics/field_trial.h" | 10 #include "base/metrics/field_trial.h" |
11 #include "base/power_monitor/power_monitor.h" | 11 #include "base/power_monitor/power_monitor.h" |
12 #include "base/power_monitor/power_monitor_source.h" | 12 #include "base/power_monitor/power_monitor_source.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
15 #include "base/test/mock_entropy_provider.h" | 15 #include "base/test/mock_entropy_provider.h" |
16 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
| 17 #include "content/browser/background_sync/background_sync_network_observer.h" |
17 #include "content/browser/background_sync/background_sync_registration_handle.h" | 18 #include "content/browser/background_sync/background_sync_registration_handle.h" |
18 #include "content/browser/background_sync/background_sync_status.h" | 19 #include "content/browser/background_sync/background_sync_status.h" |
19 #include "content/browser/browser_thread_impl.h" | 20 #include "content/browser/browser_thread_impl.h" |
20 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 21 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
21 #include "content/browser/service_worker/service_worker_context_core.h" | 22 #include "content/browser/service_worker/service_worker_context_core.h" |
22 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 23 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
23 #include "content/browser/service_worker/service_worker_storage.h" | 24 #include "content/browser/service_worker/service_worker_storage.h" |
24 #include "content/public/test/test_browser_thread_bundle.h" | 25 #include "content/public/test/test_browser_thread_bundle.h" |
25 #include "net/base/network_change_notifier.h" | 26 #include "net/base/network_change_notifier.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 sync_options_1_.network_state = NETWORK_STATE_ONLINE; | 227 sync_options_1_.network_state = NETWORK_STATE_ONLINE; |
227 sync_options_1_.power_state = POWER_STATE_AUTO; | 228 sync_options_1_.power_state = POWER_STATE_AUTO; |
228 | 229 |
229 sync_options_2_.tag = "bar"; | 230 sync_options_2_.tag = "bar"; |
230 sync_options_2_.periodicity = SYNC_ONE_SHOT; | 231 sync_options_2_.periodicity = SYNC_ONE_SHOT; |
231 sync_options_2_.network_state = NETWORK_STATE_ONLINE; | 232 sync_options_2_.network_state = NETWORK_STATE_ONLINE; |
232 sync_options_2_.power_state = POWER_STATE_AUTO; | 233 sync_options_2_.power_state = POWER_STATE_AUTO; |
233 } | 234 } |
234 | 235 |
235 void SetUp() override { | 236 void SetUp() override { |
| 237 // Don't let the tests be confused by the real-world device connectivity |
| 238 BackgroundSyncNetworkObserver::SetIgnoreNetworkChangeNotifierForTests(true); |
| 239 |
236 helper_.reset( | 240 helper_.reset( |
237 new EmbeddedWorkerTestHelper(base::FilePath(), kRenderProcessId)); | 241 new EmbeddedWorkerTestHelper(base::FilePath(), kRenderProcessId)); |
238 | 242 |
239 power_monitor_source_ = new TestPowerSource(); | 243 power_monitor_source_ = new TestPowerSource(); |
240 // power_monitor_ takes ownership of power_monitor_source. | 244 // power_monitor_ takes ownership of power_monitor_source. |
241 power_monitor_.reset(new base::PowerMonitor( | 245 power_monitor_.reset(new base::PowerMonitor( |
242 scoped_ptr<base::PowerMonitorSource>(power_monitor_source_))); | 246 scoped_ptr<base::PowerMonitorSource>(power_monitor_source_))); |
243 | 247 |
244 SetOnBatteryPower(false); | 248 SetOnBatteryPower(false); |
245 | 249 |
246 SetupBackgroundSyncManager(); | 250 SetupBackgroundSyncManager(); |
247 | 251 |
248 // Wait for storage to finish initializing before registering service | 252 // Wait for storage to finish initializing before registering service |
249 // workers. | 253 // workers. |
250 base::RunLoop().RunUntilIdle(); | 254 base::RunLoop().RunUntilIdle(); |
251 RegisterServiceWorkers(); | 255 RegisterServiceWorkers(); |
252 } | 256 } |
253 | 257 |
| 258 void TearDown() override { |
| 259 // Restore the network observer functionality for subsequent tests |
| 260 BackgroundSyncNetworkObserver::SetIgnoreNetworkChangeNotifierForTests( |
| 261 false); |
| 262 } |
| 263 |
254 void RegisterServiceWorkers() { | 264 void RegisterServiceWorkers() { |
255 bool called_1 = false; | 265 bool called_1 = false; |
256 bool called_2 = false; | 266 bool called_2 = false; |
257 helper_->context()->RegisterServiceWorker( | 267 helper_->context()->RegisterServiceWorker( |
258 GURL(kPattern1), GURL(kScript1), NULL, | 268 GURL(kPattern1), GURL(kScript1), NULL, |
259 base::Bind(&RegisterServiceWorkerCallback, &called_1, | 269 base::Bind(&RegisterServiceWorkerCallback, &called_1, |
260 &sw_registration_id_1_)); | 270 &sw_registration_id_1_)); |
261 | 271 |
262 helper_->context()->RegisterServiceWorker( | 272 helper_->context()->RegisterServiceWorker( |
263 GURL(kPattern2), GURL(kScript2), NULL, | 273 GURL(kPattern2), GURL(kScript2), NULL, |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 } | 307 } |
298 | 308 |
299 void RemoveWindowClients() { | 309 void RemoveWindowClients() { |
300 sw_registration_1_->active_version()->RemoveControllee(host_1_.get()); | 310 sw_registration_1_->active_version()->RemoveControllee(host_1_.get()); |
301 sw_registration_2_->active_version()->RemoveControllee(host_2_.get()); | 311 sw_registration_2_->active_version()->RemoveControllee(host_2_.get()); |
302 } | 312 } |
303 | 313 |
304 void SetNetwork(net::NetworkChangeNotifier::ConnectionType connection_type) { | 314 void SetNetwork(net::NetworkChangeNotifier::ConnectionType connection_type) { |
305 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( | 315 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( |
306 connection_type); | 316 connection_type); |
307 base::RunLoop().RunUntilIdle(); | 317 if (test_background_sync_manager_) { |
| 318 BackgroundSyncNetworkObserver* network_observer = |
| 319 test_background_sync_manager_->GetNetworkObserverForTesting(); |
| 320 network_observer->NotifyManagerIfNetworkChanged(connection_type); |
| 321 base::RunLoop().RunUntilIdle(); |
| 322 } |
308 } | 323 } |
309 | 324 |
310 void SetOnBatteryPower(bool on_battery_power) { | 325 void SetOnBatteryPower(bool on_battery_power) { |
311 power_monitor_source_->GeneratePowerStateEvent(on_battery_power); | 326 power_monitor_source_->GeneratePowerStateEvent(on_battery_power); |
312 base::RunLoop().RunUntilIdle(); | 327 base::RunLoop().RunUntilIdle(); |
313 } | 328 } |
314 | 329 |
315 void StatusAndRegistrationCallback( | 330 void StatusAndRegistrationCallback( |
316 bool* was_called, | 331 bool* was_called, |
317 BackgroundSyncStatus status, | 332 BackgroundSyncStatus status, |
(...skipping 1313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1631 helper_->context()->ScheduleDeleteAndStartOver(); | 1646 helper_->context()->ScheduleDeleteAndStartOver(); |
1632 base::RunLoop().RunUntilIdle(); | 1647 base::RunLoop().RunUntilIdle(); |
1633 | 1648 |
1634 RegisterServiceWorkers(); | 1649 RegisterServiceWorkers(); |
1635 | 1650 |
1636 EXPECT_FALSE(GetRegistrations(SYNC_ONE_SHOT)); | 1651 EXPECT_FALSE(GetRegistrations(SYNC_ONE_SHOT)); |
1637 EXPECT_EQ(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback_status_); | 1652 EXPECT_EQ(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback_status_); |
1638 } | 1653 } |
1639 | 1654 |
1640 } // namespace content | 1655 } // namespace content |
OLD | NEW |