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

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

Issue 1617063002: [BackgroundSync Cleanup] Remove power observer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Found more Created 4 years, 11 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 <stdint.h> 7 #include <stdint.h>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/files/scoped_temp_dir.h" 10 #include "base/files/scoped_temp_dir.h"
11 #include "base/location.h" 11 #include "base/location.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/metrics/field_trial.h" 14 #include "base/metrics/field_trial.h"
15 #include "base/power_monitor/power_monitor.h"
16 #include "base/power_monitor/power_monitor_source.h"
17 #include "base/run_loop.h" 15 #include "base/run_loop.h"
18 #include "base/single_thread_task_runner.h" 16 #include "base/single_thread_task_runner.h"
19 #include "base/test/mock_entropy_provider.h" 17 #include "base/test/mock_entropy_provider.h"
20 #include "base/test/simple_test_clock.h" 18 #include "base/test/simple_test_clock.h"
21 #include "base/thread_task_runner_handle.h" 19 #include "base/thread_task_runner_handle.h"
22 #include "content/browser/background_sync/background_sync_network_observer.h" 20 #include "content/browser/background_sync/background_sync_network_observer.h"
23 #include "content/browser/background_sync/background_sync_registration_handle.h" 21 #include "content/browser/background_sync/background_sync_registration_handle.h"
24 #include "content/browser/background_sync/background_sync_status.h" 22 #include "content/browser/background_sync/background_sync_status.h"
25 #include "content/browser/browser_thread_impl.h" 23 #include "content/browser/browser_thread_impl.h"
26 #include "content/browser/service_worker/embedded_worker_test_helper.h" 24 #include "content/browser/service_worker/embedded_worker_test_helper.h"
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 void NotifyWhenFinishedCallback(bool* was_called, 95 void NotifyWhenFinishedCallback(bool* was_called,
98 BackgroundSyncStatus* out_status, 96 BackgroundSyncStatus* out_status,
99 BackgroundSyncState* out_state, 97 BackgroundSyncState* out_state,
100 BackgroundSyncStatus status, 98 BackgroundSyncStatus status,
101 BackgroundSyncState state) { 99 BackgroundSyncState state) {
102 *was_called = true; 100 *was_called = true;
103 *out_status = status; 101 *out_status = status;
104 *out_state = state; 102 *out_state = state;
105 } 103 }
106 104
107 class TestPowerSource : public base::PowerMonitorSource {
108 public:
109 void GeneratePowerStateEvent(bool on_battery_power) {
110 test_on_battery_power_ = on_battery_power;
111 ProcessPowerEvent(POWER_STATE_EVENT);
112 }
113
114 private:
115 bool IsOnBatteryPowerImpl() final { return test_on_battery_power_; }
116 bool test_on_battery_power_ = false;
117 };
118
119 class TestBackgroundSyncController : public BackgroundSyncController { 105 class TestBackgroundSyncController : public BackgroundSyncController {
120 public: 106 public:
121 TestBackgroundSyncController() = default; 107 TestBackgroundSyncController() = default;
122 108
123 // BackgroundSyncController Overrides 109 // BackgroundSyncController Overrides
124 void NotifyBackgroundSyncRegistered(const GURL& origin) override { 110 void NotifyBackgroundSyncRegistered(const GURL& origin) override {
125 registration_count_ += 1; 111 registration_count_ += 1;
126 registration_origin_ = origin; 112 registration_origin_ = origin;
127 } 113 }
128 void RunInBackground(bool enabled, int64_t min_ms) override { 114 void RunInBackground(bool enabled, int64_t min_ms) override {
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
297 }; 283 };
298 284
299 class BackgroundSyncManagerTest : public testing::Test { 285 class BackgroundSyncManagerTest : public testing::Test {
300 public: 286 public:
301 BackgroundSyncManagerTest() 287 BackgroundSyncManagerTest()
302 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), 288 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
303 network_change_notifier_(net::NetworkChangeNotifier::CreateMock()) { 289 network_change_notifier_(net::NetworkChangeNotifier::CreateMock()) {
304 sync_options_1_.tag = "foo"; 290 sync_options_1_.tag = "foo";
305 sync_options_1_.periodicity = SYNC_ONE_SHOT; 291 sync_options_1_.periodicity = SYNC_ONE_SHOT;
306 sync_options_1_.network_state = NETWORK_STATE_ONLINE; 292 sync_options_1_.network_state = NETWORK_STATE_ONLINE;
307 sync_options_1_.power_state = POWER_STATE_AUTO;
308 293
309 sync_options_2_.tag = "bar"; 294 sync_options_2_.tag = "bar";
310 sync_options_2_.periodicity = SYNC_ONE_SHOT; 295 sync_options_2_.periodicity = SYNC_ONE_SHOT;
311 sync_options_2_.network_state = NETWORK_STATE_ONLINE; 296 sync_options_2_.network_state = NETWORK_STATE_ONLINE;
312 sync_options_2_.power_state = POWER_STATE_AUTO;
313 } 297 }
314 298
315 void SetUp() override { 299 void SetUp() override {
316 // Don't let the tests be confused by the real-world device connectivity 300 // Don't let the tests be confused by the real-world device connectivity
317 background_sync_test_util::SetIgnoreNetworkChangeNotifier(true); 301 background_sync_test_util::SetIgnoreNetworkChangeNotifier(true);
318 302
319 // TODO(jkarlin): Create a new object with all of the necessary SW calls 303 // TODO(jkarlin): Create a new object with all of the necessary SW calls
320 // so that we can inject test versions instead of bringing up all of this 304 // so that we can inject test versions instead of bringing up all of this
321 // extra SW stuff. 305 // extra SW stuff.
322 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); 306 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath()));
323 307
324 // Create a StoragePartition with the correct BrowserContext so that the 308 // Create a StoragePartition with the correct BrowserContext so that the
325 // BackgroundSyncManager can find the BrowserContext through it. 309 // BackgroundSyncManager can find the BrowserContext through it.
326 storage_partition_impl_.reset(new StoragePartitionImpl( 310 storage_partition_impl_.reset(new StoragePartitionImpl(
327 helper_->browser_context(), base::FilePath(), nullptr, nullptr, nullptr, 311 helper_->browser_context(), base::FilePath(), nullptr, nullptr, nullptr,
328 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, 312 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr,
329 nullptr, nullptr, nullptr, nullptr)); 313 nullptr, nullptr, nullptr, nullptr));
330 helper_->context_wrapper()->set_storage_partition( 314 helper_->context_wrapper()->set_storage_partition(
331 storage_partition_impl_.get()); 315 storage_partition_impl_.get());
332 316
333 power_monitor_source_ = new TestPowerSource();
334 // power_monitor_ takes ownership of power_monitor_source.
335 power_monitor_.reset(new base::PowerMonitor(
336 scoped_ptr<base::PowerMonitorSource>(power_monitor_source_)));
337
338 SetOnBatteryPower(false);
339
340 scoped_ptr<TestBackgroundSyncController> background_sync_controller( 317 scoped_ptr<TestBackgroundSyncController> background_sync_controller(
341 new TestBackgroundSyncController()); 318 new TestBackgroundSyncController());
342 test_controller_ = background_sync_controller.get(); 319 test_controller_ = background_sync_controller.get();
343 helper_->browser_context()->SetBackgroundSyncController( 320 helper_->browser_context()->SetBackgroundSyncController(
344 std::move(background_sync_controller)); 321 std::move(background_sync_controller));
345 322
346 SetMaxSyncAttemptsAndRestartManager(1); 323 SetMaxSyncAttemptsAndRestartManager(1);
347 324
348 // Wait for storage to finish initializing before registering service 325 // Wait for storage to finish initializing before registering service
349 // workers. 326 // workers.
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 connection_type); 368 connection_type);
392 if (test_background_sync_manager_) { 369 if (test_background_sync_manager_) {
393 BackgroundSyncNetworkObserver* network_observer = 370 BackgroundSyncNetworkObserver* network_observer =
394 test_background_sync_manager_->GetNetworkObserverForTesting(); 371 test_background_sync_manager_->GetNetworkObserverForTesting();
395 network_observer->NotifyManagerIfNetworkChangedForTesting( 372 network_observer->NotifyManagerIfNetworkChangedForTesting(
396 connection_type); 373 connection_type);
397 base::RunLoop().RunUntilIdle(); 374 base::RunLoop().RunUntilIdle();
398 } 375 }
399 } 376 }
400 377
401 void SetOnBatteryPower(bool on_battery_power) {
402 power_monitor_source_->GeneratePowerStateEvent(on_battery_power);
403 base::RunLoop().RunUntilIdle();
404 }
405
406 void StatusAndRegistrationCallback( 378 void StatusAndRegistrationCallback(
407 bool* was_called, 379 bool* was_called,
408 BackgroundSyncStatus status, 380 BackgroundSyncStatus status,
409 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle) { 381 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle) {
410 *was_called = true; 382 *was_called = true;
411 callback_status_ = status; 383 callback_status_ = status;
412 callback_registration_handle_ = std::move(registration_handle); 384 callback_registration_handle_ = std::move(registration_handle);
413 } 385 }
414 386
415 void StatusAndRegistrationsCallback( 387 void StatusAndRegistrationsCallback(
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
658 BackgroundSyncParameters* parameters = 630 BackgroundSyncParameters* parameters =
659 test_controller_->background_sync_parameters(); 631 test_controller_->background_sync_parameters();
660 parameters->max_sync_attempts = max_sync_attempts; 632 parameters->max_sync_attempts = max_sync_attempts;
661 633
662 // Restart the BackgroundSyncManager so that it updates its parameters. 634 // Restart the BackgroundSyncManager so that it updates its parameters.
663 SetupBackgroundSyncManager(); 635 SetupBackgroundSyncManager();
664 } 636 }
665 637
666 TestBrowserThreadBundle browser_thread_bundle_; 638 TestBrowserThreadBundle browser_thread_bundle_;
667 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; 639 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_;
668 TestPowerSource* power_monitor_source_ = nullptr; // owned by power_monitor_
669 scoped_ptr<base::PowerMonitor> power_monitor_;
670 scoped_ptr<EmbeddedWorkerTestHelper> helper_; 640 scoped_ptr<EmbeddedWorkerTestHelper> helper_;
671 scoped_ptr<BackgroundSyncManager> background_sync_manager_; 641 scoped_ptr<BackgroundSyncManager> background_sync_manager_;
672 scoped_ptr<StoragePartitionImpl> storage_partition_impl_; 642 scoped_ptr<StoragePartitionImpl> storage_partition_impl_;
673 TestBackgroundSyncManager* test_background_sync_manager_ = nullptr; 643 TestBackgroundSyncManager* test_background_sync_manager_ = nullptr;
674 TestBackgroundSyncController* test_controller_; 644 TestBackgroundSyncController* test_controller_;
675 base::SimpleTestClock* test_clock_ = nullptr; 645 base::SimpleTestClock* test_clock_ = nullptr;
676 646
677 int64_t sw_registration_id_1_; 647 int64_t sw_registration_id_1_;
678 int64_t sw_registration_id_2_; 648 int64_t sw_registration_id_2_;
679 scoped_refptr<ServiceWorkerRegistration> sw_registration_1_; 649 scoped_refptr<ServiceWorkerRegistration> sw_registration_1_;
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after
1097 1067
1098 TEST_F(BackgroundSyncManagerTest, RegistrationEqualsNetworkState) { 1068 TEST_F(BackgroundSyncManagerTest, RegistrationEqualsNetworkState) {
1099 BackgroundSyncRegistration reg_1; 1069 BackgroundSyncRegistration reg_1;
1100 BackgroundSyncRegistration reg_2; 1070 BackgroundSyncRegistration reg_2;
1101 EXPECT_TRUE(reg_1.Equals(reg_2)); 1071 EXPECT_TRUE(reg_1.Equals(reg_2));
1102 reg_1.options()->network_state = NETWORK_STATE_ANY; 1072 reg_1.options()->network_state = NETWORK_STATE_ANY;
1103 reg_2.options()->network_state = NETWORK_STATE_ONLINE; 1073 reg_2.options()->network_state = NETWORK_STATE_ONLINE;
1104 EXPECT_FALSE(reg_1.Equals(reg_2)); 1074 EXPECT_FALSE(reg_1.Equals(reg_2));
1105 } 1075 }
1106 1076
1107 TEST_F(BackgroundSyncManagerTest, RegistrationEqualsPowerState) {
1108 BackgroundSyncRegistration reg_1;
1109 BackgroundSyncRegistration reg_2;
1110 EXPECT_TRUE(reg_1.Equals(reg_2));
1111 reg_1.options()->power_state = POWER_STATE_AUTO;
1112 reg_2.options()->power_state = POWER_STATE_AVOID_DRAINING;
1113 EXPECT_FALSE(reg_1.Equals(reg_2));
1114 }
1115
1116 TEST_F(BackgroundSyncManagerTest, StoreAndRetrievePreservesValues) { 1077 TEST_F(BackgroundSyncManagerTest, StoreAndRetrievePreservesValues) {
1117 BackgroundSyncRegistrationOptions options; 1078 BackgroundSyncRegistrationOptions options;
1118 // Set non-default values for each field. 1079 // Set non-default values for each field.
1119 options.tag = "foo"; 1080 options.tag = "foo";
1120 EXPECT_NE(SYNC_PERIODIC, options.periodicity); 1081 EXPECT_NE(SYNC_PERIODIC, options.periodicity);
1121 options.periodicity = SYNC_PERIODIC; 1082 options.periodicity = SYNC_PERIODIC;
1122 options.min_period += 1; 1083 options.min_period += 1;
1123 EXPECT_NE(NETWORK_STATE_ANY, options.network_state); 1084 EXPECT_NE(NETWORK_STATE_ANY, options.network_state);
1124 options.network_state = NETWORK_STATE_ANY; 1085 options.network_state = NETWORK_STATE_ANY;
1125 EXPECT_NE(POWER_STATE_AUTO, options.power_state);
1126 options.power_state = POWER_STATE_AUTO;
1127 1086
1128 // Store the registration. 1087 // Store the registration.
1129 EXPECT_TRUE(Register(options)); 1088 EXPECT_TRUE(Register(options));
1130 1089
1131 // Simulate restarting the sync manager, forcing the next read to come from 1090 // Simulate restarting the sync manager, forcing the next read to come from
1132 // disk. 1091 // disk.
1133 SetupBackgroundSyncManager(); 1092 SetupBackgroundSyncManager();
1134 1093
1135 EXPECT_TRUE(GetRegistration(options)); 1094 EXPECT_TRUE(GetRegistration(options));
1136 EXPECT_TRUE(options.Equals(*callback_registration_handle_->options())); 1095 EXPECT_TRUE(options.Equals(*callback_registration_handle_->options()));
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
1384 // Finish firing the event. 1343 // Finish firing the event.
1385 sync_fired_callback_.Run(SERVICE_WORKER_ERROR_FAILED); 1344 sync_fired_callback_.Run(SERVICE_WORKER_ERROR_FAILED);
1386 base::RunLoop().RunUntilIdle(); 1345 base::RunLoop().RunUntilIdle();
1387 // Since there was one retry attempt left, the sync didn't completely fail 1346 // Since there was one retry attempt left, the sync didn't completely fail
1388 // before it was unregistered. 1347 // before it was unregistered.
1389 EXPECT_EQ(BackgroundSyncState::UNREGISTERED, FinishedState()); 1348 EXPECT_EQ(BackgroundSyncState::UNREGISTERED, FinishedState());
1390 } 1349 }
1391 1350
1392 TEST_F(BackgroundSyncManagerTest, OverwritePendingRegistration) { 1351 TEST_F(BackgroundSyncManagerTest, OverwritePendingRegistration) {
1393 // An overwritten pending registration should complete with 1352 // An overwritten pending registration should complete with
1394 // BackgroundSyncState::UNREGISTERED. 1353 // BACKGROUND_SYNC_STATE_UNREGISTERED.
1395 sync_options_1_.power_state = POWER_STATE_AVOID_DRAINING; 1354 sync_options_1_.network_state = NETWORK_STATE_AVOID_CELLULAR;
1396 EXPECT_TRUE(Register(sync_options_1_)); 1355 EXPECT_TRUE(Register(sync_options_1_));
1397 EXPECT_TRUE(GetRegistration(sync_options_1_)); 1356 EXPECT_TRUE(GetRegistration(sync_options_1_));
1398 EXPECT_EQ(POWER_STATE_AVOID_DRAINING, 1357 EXPECT_EQ(NETWORK_STATE_AVOID_CELLULAR,
1399 callback_registration_handle_->options()->power_state); 1358 callback_registration_handle_->options()->network_state);
1400 scoped_ptr<BackgroundSyncRegistrationHandle> original_handle = 1359 scoped_ptr<BackgroundSyncRegistrationHandle> original_handle =
1401 std::move(callback_registration_handle_); 1360 std::move(callback_registration_handle_);
1402 1361
1403 // Overwrite the pending registration. 1362 // Overwrite the pending registration.
1404 sync_options_1_.power_state = POWER_STATE_AUTO; 1363 sync_options_1_.network_state = NETWORK_STATE_ONLINE;
1405 EXPECT_TRUE(Register(sync_options_1_)); 1364 EXPECT_TRUE(Register(sync_options_1_));
1406 EXPECT_TRUE(GetRegistration(sync_options_1_)); 1365 EXPECT_TRUE(GetRegistration(sync_options_1_));
1407 EXPECT_EQ(POWER_STATE_AUTO, 1366 EXPECT_EQ(NETWORK_STATE_ONLINE,
1408 callback_registration_handle_->options()->power_state); 1367 callback_registration_handle_->options()->network_state);
1409 1368
1410 EXPECT_TRUE(NotifyWhenFinished(original_handle.get())); 1369 EXPECT_TRUE(NotifyWhenFinished(original_handle.get()));
1411 EXPECT_EQ(BackgroundSyncState::UNREGISTERED, FinishedState()); 1370 EXPECT_EQ(BackgroundSyncState::UNREGISTERED, FinishedState());
1412 EXPECT_EQ(0, sync_events_called_); 1371 EXPECT_EQ(0, sync_events_called_);
1413 } 1372 }
1414 1373
1415 TEST_F(BackgroundSyncManagerTest, OverwriteFiringRegistrationWhichSucceeds) { 1374 TEST_F(BackgroundSyncManagerTest, OverwriteFiringRegistrationWhichSucceeds) {
1416 // An overwritten pending registration should complete with 1375 // An overwritten pending registration should complete with
1417 // BackgroundSyncState::SUCCESS if firing completes successfully. 1376 // BackgroundSyncState::SUCCESS if firing completes successfully.
1418 InitDelayedSyncEventTest(); 1377 InitDelayedSyncEventTest();
1419 1378
1420 sync_options_1_.power_state = POWER_STATE_AVOID_DRAINING; 1379 sync_options_1_.network_state = NETWORK_STATE_AVOID_CELLULAR;
1421 RegisterAndVerifySyncEventDelayed(sync_options_1_); 1380 RegisterAndVerifySyncEventDelayed(sync_options_1_);
1422 scoped_ptr<BackgroundSyncRegistrationHandle> original_handle = 1381 scoped_ptr<BackgroundSyncRegistrationHandle> original_handle =
1423 std::move(callback_registration_handle_); 1382 std::move(callback_registration_handle_);
1424 1383
1425 // The next registration won't block. 1384 // The next registration won't block.
1426 InitSyncEventTest(); 1385 InitSyncEventTest();
1427 1386
1428 // Overwrite the firing registration. 1387 // Overwrite the firing registration.
1429 sync_options_1_.power_state = POWER_STATE_AUTO; 1388 sync_options_1_.network_state = NETWORK_STATE_ONLINE;
1430 EXPECT_TRUE(Register(sync_options_1_)); 1389 EXPECT_TRUE(Register(sync_options_1_));
1431 EXPECT_FALSE(NotifyWhenFinished(original_handle.get())); 1390 EXPECT_FALSE(NotifyWhenFinished(original_handle.get()));
1432 1391
1433 // Successfully finish the first event. 1392 // Successfully finish the first event.
1434 sync_fired_callback_.Run(SERVICE_WORKER_OK); 1393 sync_fired_callback_.Run(SERVICE_WORKER_OK);
1435 base::RunLoop().RunUntilIdle(); 1394 base::RunLoop().RunUntilIdle();
1436 EXPECT_EQ(BackgroundSyncState::SUCCESS, FinishedState()); 1395 EXPECT_EQ(BackgroundSyncState::SUCCESS, FinishedState());
1437 } 1396 }
1438 1397
1439 TEST_F(BackgroundSyncManagerTest, OverwriteFiringRegistrationWhichFails) { 1398 TEST_F(BackgroundSyncManagerTest, OverwriteFiringRegistrationWhichFails) {
1440 // An overwritten pending registration should complete with 1399 // An overwritten pending registration should complete with
1441 // BackgroundSyncState::FAILED if firing fails. 1400 // BackgroundSyncState::FAILED if firing fails.
1442 InitDelayedSyncEventTest(); 1401 InitDelayedSyncEventTest();
1443 1402
1444 sync_options_1_.power_state = POWER_STATE_AVOID_DRAINING; 1403 sync_options_1_.network_state = NETWORK_STATE_AVOID_CELLULAR;
1445 RegisterAndVerifySyncEventDelayed(sync_options_1_); 1404 RegisterAndVerifySyncEventDelayed(sync_options_1_);
1446 scoped_ptr<BackgroundSyncRegistrationHandle> original_handle = 1405 scoped_ptr<BackgroundSyncRegistrationHandle> original_handle =
1447 std::move(callback_registration_handle_); 1406 std::move(callback_registration_handle_);
1448 1407
1449 // The next registration won't block. 1408 // The next registration won't block.
1450 InitSyncEventTest(); 1409 InitSyncEventTest();
1451 1410
1452 // Overwrite the firing registration. 1411 // Overwrite the firing registration.
1453 sync_options_1_.power_state = POWER_STATE_AUTO; 1412 sync_options_1_.network_state = NETWORK_STATE_ONLINE;
1454 EXPECT_TRUE(Register(sync_options_1_)); 1413 EXPECT_TRUE(Register(sync_options_1_));
1455 EXPECT_FALSE(NotifyWhenFinished(original_handle.get())); 1414 EXPECT_FALSE(NotifyWhenFinished(original_handle.get()));
1456 1415
1457 // Fail the first event. 1416 // Fail the first event.
1458 sync_fired_callback_.Run(SERVICE_WORKER_ERROR_FAILED); 1417 sync_fired_callback_.Run(SERVICE_WORKER_ERROR_FAILED);
1459 base::RunLoop().RunUntilIdle(); 1418 base::RunLoop().RunUntilIdle();
1460 EXPECT_EQ(BackgroundSyncState::FAILED, FinishedState()); 1419 EXPECT_EQ(BackgroundSyncState::FAILED, FinishedState());
1461 } 1420 }
1462 1421
1463 TEST_F(BackgroundSyncManagerTest, DisableWhilePendingNotifiesFinished) { 1422 TEST_F(BackgroundSyncManagerTest, DisableWhilePendingNotifiesFinished) {
(...skipping 27 matching lines...) Expand all
1491 EXPECT_FALSE(Register(sync_options_2_)); 1450 EXPECT_FALSE(Register(sync_options_2_));
1492 EXPECT_FALSE(callback_finished_called_); 1451 EXPECT_FALSE(callback_finished_called_);
1493 test_background_sync_manager_->set_corrupt_backend(false); 1452 test_background_sync_manager_->set_corrupt_backend(false);
1494 1453
1495 // Successfully complete the firing event. 1454 // Successfully complete the firing event.
1496 sync_fired_callback_.Run(SERVICE_WORKER_OK); 1455 sync_fired_callback_.Run(SERVICE_WORKER_OK);
1497 base::RunLoop().RunUntilIdle(); 1456 base::RunLoop().RunUntilIdle();
1498 EXPECT_EQ(BackgroundSyncState::SUCCESS, FinishedState()); 1457 EXPECT_EQ(BackgroundSyncState::SUCCESS, FinishedState());
1499 } 1458 }
1500 1459
1501 // TODO(jkarlin): Change this to a periodic test as one-shots can't be power
1502 // dependent according to spec.
1503 TEST_F(BackgroundSyncManagerTest, OneShotFiresOnPowerChange) {
1504 InitSyncEventTest();
1505 sync_options_1_.power_state = POWER_STATE_AVOID_DRAINING;
1506
1507 SetOnBatteryPower(true);
1508 EXPECT_TRUE(Register(sync_options_1_));
1509 EXPECT_EQ(0, sync_events_called_);
1510 EXPECT_TRUE(GetRegistration(sync_options_1_));
1511
1512 SetOnBatteryPower(false);
1513 EXPECT_EQ(1, sync_events_called_);
1514 EXPECT_FALSE(GetRegistration(sync_options_1_));
1515 }
1516
1517 // TODO(jkarlin): Change this to a periodic test as one-shots can't be power
1518 // dependent according to spec.
1519 TEST_F(BackgroundSyncManagerTest, MultipleOneShotsFireOnPowerChange) {
1520 InitSyncEventTest();
1521 sync_options_1_.power_state = POWER_STATE_AVOID_DRAINING;
1522 sync_options_2_.power_state = POWER_STATE_AVOID_DRAINING;
1523
1524 SetOnBatteryPower(true);
1525 EXPECT_TRUE(Register(sync_options_1_));
1526 EXPECT_TRUE(Register(sync_options_2_));
1527 EXPECT_EQ(0, sync_events_called_);
1528 EXPECT_TRUE(GetRegistration(sync_options_1_));
1529 EXPECT_TRUE(GetRegistration(sync_options_2_));
1530
1531 SetOnBatteryPower(false);
1532 EXPECT_EQ(2, sync_events_called_);
1533 EXPECT_FALSE(GetRegistration(sync_options_1_));
1534 EXPECT_FALSE(GetRegistration(sync_options_2_));
1535 }
1536
1537 TEST_F(BackgroundSyncManagerTest, OneShotFiresOnNetworkChange) { 1460 TEST_F(BackgroundSyncManagerTest, OneShotFiresOnNetworkChange) {
1538 InitSyncEventTest(); 1461 InitSyncEventTest();
1539 1462
1540 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE); 1463 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE);
1541 EXPECT_TRUE(Register(sync_options_1_)); 1464 EXPECT_TRUE(Register(sync_options_1_));
1542 EXPECT_EQ(0, sync_events_called_); 1465 EXPECT_EQ(0, sync_events_called_);
1543 EXPECT_TRUE(GetRegistration(sync_options_1_)); 1466 EXPECT_TRUE(GetRegistration(sync_options_1_));
1544 1467
1545 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI); 1468 SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI);
1546 base::RunLoop().RunUntilIdle(); 1469 base::RunLoop().RunUntilIdle();
(...skipping 482 matching lines...) Expand 10 before | Expand all | Expand 10 after
2029 // Run it again. 1952 // Run it again.
2030 test_clock_->Advance(test_background_sync_manager_->delayed_task_delta()); 1953 test_clock_->Advance(test_background_sync_manager_->delayed_task_delta());
2031 test_background_sync_manager_->delayed_task().Run(); 1954 test_background_sync_manager_->delayed_task().Run();
2032 base::RunLoop().RunUntilIdle(); 1955 base::RunLoop().RunUntilIdle();
2033 EXPECT_FALSE(GetRegistration(sync_options_1_)); 1956 EXPECT_FALSE(GetRegistration(sync_options_1_));
2034 EXPECT_EQ(BackgroundSyncEventLastChance::IS_LAST_CHANCE, 1957 EXPECT_EQ(BackgroundSyncEventLastChance::IS_LAST_CHANCE,
2035 test_background_sync_manager_->last_chance()); 1958 test_background_sync_manager_->last_chance());
2036 } 1959 }
2037 1960
2038 } // namespace content 1961 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698