Commit 6988e253 authored by jkarlin's avatar jkarlin Committed by Commit bot

[BackgroundSync] Clean up some tests

Cleaning up some tests to use helper functions and remove dangling
pointers. It also fixes the DisabledManagerWorksAfterBrowserRestart
test which wasn't initializing after creating a new manager.

Review URL: https://codereview.chromium.org/1148853005

Cr-Commit-Position: refs/heads/master@{#333074}
parent ad896439
...@@ -315,6 +315,11 @@ class BackgroundSyncManagerTest : public testing::Test { ...@@ -315,6 +315,11 @@ class BackgroundSyncManagerTest : public testing::Test {
background_sync_manager_.reset(test_background_sync_manager_); background_sync_manager_.reset(test_background_sync_manager_);
} }
void DeleteBackgroundSyncManager() {
background_sync_manager_.reset();
test_background_sync_manager_ = nullptr;
}
bool Register(const BackgroundSyncManager::BackgroundSyncRegistration& bool Register(const BackgroundSyncManager::BackgroundSyncRegistration&
sync_registration) { sync_registration) {
return RegisterWithServiceWorkerId(sw_registration_id_1_, return RegisterWithServiceWorkerId(sw_registration_id_1_,
...@@ -729,11 +734,12 @@ TEST_F(BackgroundSyncManagerTest, RebootRecoveryTwoServiceWorkers) { ...@@ -729,11 +734,12 @@ TEST_F(BackgroundSyncManagerTest, RebootRecoveryTwoServiceWorkers) {
} }
TEST_F(BackgroundSyncManagerTest, InitWithBadBackend) { TEST_F(BackgroundSyncManagerTest, InitWithBadBackend) {
TestBackgroundSyncManager* manager = DeleteBackgroundSyncManager();
test_background_sync_manager_ =
new TestBackgroundSyncManager(helper_->context_wrapper()); new TestBackgroundSyncManager(helper_->context_wrapper());
background_sync_manager_.reset(manager); background_sync_manager_.reset(test_background_sync_manager_);
manager->set_corrupt_backend(true); test_background_sync_manager_->set_corrupt_backend(true);
manager->DoInit(); test_background_sync_manager_->DoInit();
EXPECT_FALSE(Register(sync_reg_1_)); EXPECT_FALSE(Register(sync_reg_1_));
EXPECT_FALSE(GetRegistration(sync_reg_1_)); EXPECT_FALSE(GetRegistration(sync_reg_1_));
...@@ -742,11 +748,14 @@ TEST_F(BackgroundSyncManagerTest, InitWithBadBackend) { ...@@ -742,11 +748,14 @@ TEST_F(BackgroundSyncManagerTest, InitWithBadBackend) {
TEST_F(BackgroundSyncManagerTest, SequentialOperations) { TEST_F(BackgroundSyncManagerTest, SequentialOperations) {
// Schedule Init and all of the operations on a delayed backend. Verify that // Schedule Init and all of the operations on a delayed backend. Verify that
// the operations complete sequentially. // the operations complete sequentially.
TestBackgroundSyncManager* manager = DeleteBackgroundSyncManager();
test_background_sync_manager_ =
new TestBackgroundSyncManager(helper_->context_wrapper()); new TestBackgroundSyncManager(helper_->context_wrapper());
background_sync_manager_.reset(manager); background_sync_manager_.reset(test_background_sync_manager_);
manager->set_delay_backend(true);
manager->DoInit(); test_background_sync_manager_->set_delay_backend(true);
test_background_sync_manager_->DoInit();
const int64 kExpectedInitialId = const int64 kExpectedInitialId =
BackgroundSyncManager::BackgroundSyncRegistration::kInitialId; BackgroundSyncManager::BackgroundSyncRegistration::kInitialId;
...@@ -754,15 +763,16 @@ TEST_F(BackgroundSyncManagerTest, SequentialOperations) { ...@@ -754,15 +763,16 @@ TEST_F(BackgroundSyncManagerTest, SequentialOperations) {
bool register_called = false; bool register_called = false;
bool unregister_called = false; bool unregister_called = false;
bool get_registration_called = false; bool get_registration_called = false;
manager->Register( test_background_sync_manager_->Register(
sw_registration_id_1_, sync_reg_1_, sw_registration_id_1_, sync_reg_1_,
base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback,
base::Unretained(this), &register_called)); base::Unretained(this), &register_called));
manager->Unregister(sw_registration_id_1_, sync_reg_1_.tag, test_background_sync_manager_->Unregister(
sync_reg_1_.periodicity, kExpectedInitialId, sw_registration_id_1_, sync_reg_1_.tag, sync_reg_1_.periodicity,
base::Bind(&BackgroundSyncManagerTest::StatusCallback, kExpectedInitialId,
base::Unretained(this), &unregister_called)); base::Bind(&BackgroundSyncManagerTest::StatusCallback,
manager->GetRegistration( base::Unretained(this), &unregister_called));
test_background_sync_manager_->GetRegistration(
sw_registration_id_1_, sync_reg_1_.tag, sync_reg_1_.periodicity, sw_registration_id_1_, sync_reg_1_.tag, sync_reg_1_.periodicity,
base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback,
base::Unretained(this), &get_registration_called)); base::Unretained(this), &get_registration_called));
...@@ -773,14 +783,14 @@ TEST_F(BackgroundSyncManagerTest, SequentialOperations) { ...@@ -773,14 +783,14 @@ TEST_F(BackgroundSyncManagerTest, SequentialOperations) {
EXPECT_FALSE(unregister_called); EXPECT_FALSE(unregister_called);
EXPECT_FALSE(get_registration_called); EXPECT_FALSE(get_registration_called);
manager->Continue(); test_background_sync_manager_->Continue();
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
// Register should be blocked while storing to the backend. // Register should be blocked while storing to the backend.
EXPECT_FALSE(register_called); EXPECT_FALSE(register_called);
EXPECT_FALSE(unregister_called); EXPECT_FALSE(unregister_called);
EXPECT_FALSE(get_registration_called); EXPECT_FALSE(get_registration_called);
manager->Continue(); test_background_sync_manager_->Continue();
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
EXPECT_TRUE(register_called); EXPECT_TRUE(register_called);
EXPECT_EQ(kExpectedInitialId, callback_registration_.id); EXPECT_EQ(kExpectedInitialId, callback_registration_.id);
...@@ -789,7 +799,7 @@ TEST_F(BackgroundSyncManagerTest, SequentialOperations) { ...@@ -789,7 +799,7 @@ TEST_F(BackgroundSyncManagerTest, SequentialOperations) {
EXPECT_FALSE(unregister_called); EXPECT_FALSE(unregister_called);
EXPECT_FALSE(get_registration_called); EXPECT_FALSE(get_registration_called);
manager->Continue(); test_background_sync_manager_->Continue();
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
// Unregister should be done and since GetRegistration doesn't require the // Unregister should be done and since GetRegistration doesn't require the
// backend it should be done too. // backend it should be done too.
...@@ -806,16 +816,13 @@ TEST_F(BackgroundSyncManagerTest, UnregisterServiceWorker) { ...@@ -806,16 +816,13 @@ TEST_F(BackgroundSyncManagerTest, UnregisterServiceWorker) {
TEST_F(BackgroundSyncManagerTest, TEST_F(BackgroundSyncManagerTest,
UnregisterServiceWorkerDuringSyncRegistration) { UnregisterServiceWorkerDuringSyncRegistration) {
TestBackgroundSyncManager* manager = UseTestBackgroundSyncManager();
new TestBackgroundSyncManager(helper_->context_wrapper());
background_sync_manager_.reset(manager);
manager->DoInit();
EXPECT_TRUE(Register(sync_reg_1_)); EXPECT_TRUE(Register(sync_reg_1_));
manager->set_delay_backend(true); test_background_sync_manager_->set_delay_backend(true);
bool callback_called = false; bool callback_called = false;
manager->Register( test_background_sync_manager_->Register(
sw_registration_id_1_, sync_reg_2_, sw_registration_id_1_, sync_reg_2_,
base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback, base::Bind(&BackgroundSyncManagerTest::StatusAndRegistrationCallback,
base::Unretained(this), &callback_called)); base::Unretained(this), &callback_called));
...@@ -824,12 +831,12 @@ TEST_F(BackgroundSyncManagerTest, ...@@ -824,12 +831,12 @@ TEST_F(BackgroundSyncManagerTest,
EXPECT_FALSE(callback_called); EXPECT_FALSE(callback_called);
UnregisterServiceWorker(sw_registration_id_1_); UnregisterServiceWorker(sw_registration_id_1_);
manager->Continue(); test_background_sync_manager_->Continue();
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
EXPECT_TRUE(callback_called); EXPECT_TRUE(callback_called);
EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_STORAGE, callback_error_); EXPECT_EQ(BackgroundSyncManager::ERROR_TYPE_STORAGE, callback_error_);
manager->set_delay_backend(false); test_background_sync_manager_->set_delay_backend(false);
EXPECT_FALSE(GetRegistration(sync_reg_1_)); EXPECT_FALSE(GetRegistration(sync_reg_1_));
} }
...@@ -841,39 +848,32 @@ TEST_F(BackgroundSyncManagerTest, DeleteAndStartOverServiceWorkerContext) { ...@@ -841,39 +848,32 @@ TEST_F(BackgroundSyncManagerTest, DeleteAndStartOverServiceWorkerContext) {
} }
TEST_F(BackgroundSyncManagerTest, DisabledManagerWorksAfterBrowserRestart) { TEST_F(BackgroundSyncManagerTest, DisabledManagerWorksAfterBrowserRestart) {
TestBackgroundSyncManager* manager = UseTestBackgroundSyncManager();
new TestBackgroundSyncManager(helper_->context_wrapper());
background_sync_manager_.reset(manager);
manager->DoInit();
EXPECT_TRUE(Register(sync_reg_1_)); EXPECT_TRUE(Register(sync_reg_1_));
manager->set_corrupt_backend(true); test_background_sync_manager_->set_corrupt_backend(true);
EXPECT_FALSE(Register(sync_reg_2_)); EXPECT_FALSE(Register(sync_reg_2_));
// The manager is now disabled and not accepting new requests until browser // The manager is now disabled and not accepting new requests until browser
// restart or notification that the storage has been wiped. // restart or notification that the storage has been wiped.
manager->set_corrupt_backend(false); test_background_sync_manager_->set_corrupt_backend(false);
EXPECT_FALSE(GetRegistration(sync_reg_1_)); EXPECT_FALSE(GetRegistration(sync_reg_1_));
EXPECT_FALSE(Register(sync_reg_2_)); EXPECT_FALSE(Register(sync_reg_2_));
// Simulate restarting the browser by creating a new BackgroundSyncManager. // Simulate restarting the browser by creating a new BackgroundSyncManager.
background_sync_manager_.reset( UseTestBackgroundSyncManager();
new TestBackgroundSyncManager(helper_->context_wrapper())); EXPECT_TRUE(GetRegistration(sync_reg_1_));
EXPECT_FALSE(GetRegistration(sync_reg_1_)); EXPECT_TRUE(Register(sync_reg_2_));
EXPECT_TRUE(Register(sync_reg_1_));
} }
TEST_F(BackgroundSyncManagerTest, DisabledManagerWorksAfterDeleteAndStartOver) { TEST_F(BackgroundSyncManagerTest, DisabledManagerWorksAfterDeleteAndStartOver) {
TestBackgroundSyncManager* manager = UseTestBackgroundSyncManager();
new TestBackgroundSyncManager(helper_->context_wrapper());
background_sync_manager_.reset(manager);
manager->DoInit();
EXPECT_TRUE(Register(sync_reg_1_)); EXPECT_TRUE(Register(sync_reg_1_));
manager->set_corrupt_backend(true); test_background_sync_manager_->set_corrupt_backend(true);
EXPECT_FALSE(Register(sync_reg_2_)); EXPECT_FALSE(Register(sync_reg_2_));
// The manager is now disabled and not accepting new requests until browser // The manager is now disabled and not accepting new requests until browser
// restart or notification that the storage has been wiped. // restart or notification that the storage has been wiped.
manager->set_corrupt_backend(false); test_background_sync_manager_->set_corrupt_backend(false);
helper_->context()->ScheduleDeleteAndStartOver(); helper_->context()->ScheduleDeleteAndStartOver();
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
...@@ -1063,7 +1063,7 @@ TEST_F(BackgroundSyncManagerTest, MultipleOneShotsFireOnNetworkChange) { ...@@ -1063,7 +1063,7 @@ TEST_F(BackgroundSyncManagerTest, MultipleOneShotsFireOnNetworkChange) {
EXPECT_TRUE(GetRegistration(sync_reg_2_)); EXPECT_TRUE(GetRegistration(sync_reg_2_));
SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI); SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(2, sync_events_called_); EXPECT_EQ(2, sync_events_called_);
EXPECT_FALSE(GetRegistration(sync_reg_1_)); EXPECT_FALSE(GetRegistration(sync_reg_1_));
EXPECT_FALSE(GetRegistration(sync_reg_2_)); EXPECT_FALSE(GetRegistration(sync_reg_2_));
...@@ -1079,7 +1079,7 @@ TEST_F(BackgroundSyncManagerTest, OneShotFiresOnManagerRestart) { ...@@ -1079,7 +1079,7 @@ TEST_F(BackgroundSyncManagerTest, OneShotFiresOnManagerRestart) {
EXPECT_TRUE(GetRegistration(sync_reg_1_)); EXPECT_TRUE(GetRegistration(sync_reg_1_));
// Simulate closing the browser. // Simulate closing the browser.
background_sync_manager_.reset(); DeleteBackgroundSyncManager();
// The next time the manager is started, the network is good. // The next time the manager is started, the network is good.
SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI); SetNetwork(net::NetworkChangeNotifier::CONNECTION_WIFI);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment