Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(299)

Side by Side Diff: content/browser/background_sync/background_sync_manager_unittest.cc

Issue 1294603003: [BackgroundSync] Trigger Background Sync events when Chrome is backgrounded on Android (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase (Post-Blink-Chromium-Merge) Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
11 #include "base/power_monitor/power_monitor_source.h" 11 #include "base/power_monitor/power_monitor_source.h"
12 #include "base/run_loop.h" 12 #include "base/run_loop.h"
13 #include "base/single_thread_task_runner.h" 13 #include "base/single_thread_task_runner.h"
14 #include "base/thread_task_runner_handle.h" 14 #include "base/thread_task_runner_handle.h"
15 #include "content/browser/background_sync/background_sync_network_observer.h"
15 #include "content/browser/background_sync/background_sync_registration_handle.h" 16 #include "content/browser/background_sync/background_sync_registration_handle.h"
16 #include "content/browser/background_sync/background_sync_status.h" 17 #include "content/browser/background_sync/background_sync_status.h"
17 #include "content/browser/browser_thread_impl.h" 18 #include "content/browser/browser_thread_impl.h"
18 #include "content/browser/service_worker/embedded_worker_test_helper.h" 19 #include "content/browser/service_worker/embedded_worker_test_helper.h"
19 #include "content/browser/service_worker/service_worker_context_core.h" 20 #include "content/browser/service_worker/service_worker_context_core.h"
20 #include "content/browser/service_worker/service_worker_context_wrapper.h" 21 #include "content/browser/service_worker/service_worker_context_wrapper.h"
21 #include "content/browser/service_worker/service_worker_storage.h" 22 #include "content/browser/service_worker/service_worker_storage.h"
22 #include "content/public/test/test_browser_thread_bundle.h" 23 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "net/base/network_change_notifier.h" 24 #include "net/base/network_change_notifier.h"
24 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 sync_options_1_.network_state = NETWORK_STATE_ONLINE; 225 sync_options_1_.network_state = NETWORK_STATE_ONLINE;
225 sync_options_1_.power_state = POWER_STATE_AUTO; 226 sync_options_1_.power_state = POWER_STATE_AUTO;
226 227
227 sync_options_2_.tag = "bar"; 228 sync_options_2_.tag = "bar";
228 sync_options_2_.periodicity = SYNC_ONE_SHOT; 229 sync_options_2_.periodicity = SYNC_ONE_SHOT;
229 sync_options_2_.network_state = NETWORK_STATE_ONLINE; 230 sync_options_2_.network_state = NETWORK_STATE_ONLINE;
230 sync_options_2_.power_state = POWER_STATE_AUTO; 231 sync_options_2_.power_state = POWER_STATE_AUTO;
231 } 232 }
232 233
233 void SetUp() override { 234 void SetUp() override {
235 // Don't let the tests be confused by the real-world device connectivity
236 BackgroundSyncNetworkObserver::SetIgnoreNetworkChangeNotifierForTests(true);
237
234 helper_.reset( 238 helper_.reset(
235 new EmbeddedWorkerTestHelper(base::FilePath(), kRenderProcessId)); 239 new EmbeddedWorkerTestHelper(base::FilePath(), kRenderProcessId));
236 240
237 power_monitor_source_ = new TestPowerSource(); 241 power_monitor_source_ = new TestPowerSource();
238 // power_monitor_ takes ownership of power_monitor_source. 242 // power_monitor_ takes ownership of power_monitor_source.
239 power_monitor_.reset(new base::PowerMonitor( 243 power_monitor_.reset(new base::PowerMonitor(
240 scoped_ptr<base::PowerMonitorSource>(power_monitor_source_))); 244 scoped_ptr<base::PowerMonitorSource>(power_monitor_source_)));
241 245
242 SetOnBatteryPower(false); 246 SetOnBatteryPower(false);
243 247
244 SetupBackgroundSyncManager(); 248 SetupBackgroundSyncManager();
245 249
246 // Wait for storage to finish initializing before registering service 250 // Wait for storage to finish initializing before registering service
247 // workers. 251 // workers.
248 base::RunLoop().RunUntilIdle(); 252 base::RunLoop().RunUntilIdle();
249 RegisterServiceWorkers(); 253 RegisterServiceWorkers();
250 } 254 }
251 255
256 void TearDown() override {
257 // Restore the network observer functionality for subsequent tests
jkarlin 2015/09/24 17:51:30 I don't believe this is necessary? Each test runs
iclelland 2015/09/24 20:37:19 They're not completely isolated, and this is a pro
jkarlin 2015/09/24 23:33:12 Acknowledged.
258 BackgroundSyncNetworkObserver::SetIgnoreNetworkChangeNotifierForTests(
259 false);
260 }
261
252 void RegisterServiceWorkers() { 262 void RegisterServiceWorkers() {
253 bool called_1 = false; 263 bool called_1 = false;
254 bool called_2 = false; 264 bool called_2 = false;
255 helper_->context()->RegisterServiceWorker( 265 helper_->context()->RegisterServiceWorker(
256 GURL(kPattern1), GURL(kScript1), NULL, 266 GURL(kPattern1), GURL(kScript1), NULL,
257 base::Bind(&RegisterServiceWorkerCallback, &called_1, 267 base::Bind(&RegisterServiceWorkerCallback, &called_1,
258 &sw_registration_id_1_)); 268 &sw_registration_id_1_));
259 269
260 helper_->context()->RegisterServiceWorker( 270 helper_->context()->RegisterServiceWorker(
261 GURL(kPattern2), GURL(kScript2), NULL, 271 GURL(kPattern2), GURL(kScript2), NULL,
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 } 305 }
296 306
297 void RemoveWindowClients() { 307 void RemoveWindowClients() {
298 sw_registration_1_->active_version()->RemoveControllee(host_1_.get()); 308 sw_registration_1_->active_version()->RemoveControllee(host_1_.get());
299 sw_registration_2_->active_version()->RemoveControllee(host_2_.get()); 309 sw_registration_2_->active_version()->RemoveControllee(host_2_.get());
300 } 310 }
301 311
302 void SetNetwork(net::NetworkChangeNotifier::ConnectionType connection_type) { 312 void SetNetwork(net::NetworkChangeNotifier::ConnectionType connection_type) {
303 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests( 313 net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
304 connection_type); 314 connection_type);
305 base::RunLoop().RunUntilIdle(); 315 if (test_background_sync_manager_) {
316 BackgroundSyncNetworkObserver* network_observer =
317 test_background_sync_manager_->GetNetworkObserverForTesting();
318 network_observer->NotifyManagerIfNetworkChanged(connection_type);
jkarlin 2015/09/24 17:51:30 The name "NotifyManagerIfNetworkChanged" doesn't s
iclelland 2015/09/24 20:37:19 This code is actually calling the BSNO method whic
jkarlin 2015/09/24 23:33:12 I realize that, but the name seems strange from th
iclelland 2015/09/25 13:05:01 Would something like OnNetworkChangedImpl or OnNet
jkarlin 2015/09/25 13:08:57 Yes, OnNetworkChangedImpl would do nicely.
319 base::RunLoop().RunUntilIdle();
320 }
306 } 321 }
307 322
308 void SetOnBatteryPower(bool on_battery_power) { 323 void SetOnBatteryPower(bool on_battery_power) {
309 power_monitor_source_->GeneratePowerStateEvent(on_battery_power); 324 power_monitor_source_->GeneratePowerStateEvent(on_battery_power);
310 base::RunLoop().RunUntilIdle(); 325 base::RunLoop().RunUntilIdle();
311 } 326 }
312 327
313 void StatusAndRegistrationCallback( 328 void StatusAndRegistrationCallback(
314 bool* was_called, 329 bool* was_called,
315 BackgroundSyncStatus status, 330 BackgroundSyncStatus status,
(...skipping 1291 matching lines...) Expand 10 before | Expand all | Expand 10 after
1607 } 1622 }
1608 1623
1609 TEST_F(BackgroundSyncManagerTest, UnregisterSucceedsWithoutWindow) { 1624 TEST_F(BackgroundSyncManagerTest, UnregisterSucceedsWithoutWindow) {
1610 EXPECT_TRUE(Register(sync_options_1_)); 1625 EXPECT_TRUE(Register(sync_options_1_));
1611 RemoveWindowClients(); 1626 RemoveWindowClients();
1612 EXPECT_TRUE(Unregister(callback_registration_handle_.get())); 1627 EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
1613 EXPECT_FALSE(GetRegistration(sync_options_1_)); 1628 EXPECT_FALSE(GetRegistration(sync_options_1_));
1614 } 1629 }
1615 1630
1616 } // namespace content 1631 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698