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

Unified Diff: content/browser/background_sync/background_sync_manager_unittest.cc

Issue 1344843003: [BackgroundSync] Add browser side support for SyncRegistration.done (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@ncn_max
Patch Set: Address comments from PS7 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/background_sync/background_sync_manager_unittest.cc
diff --git a/content/browser/background_sync/background_sync_manager_unittest.cc b/content/browser/background_sync/background_sync_manager_unittest.cc
index d073b06bbda4de026897c8c29de7f821cb7fbf2f..d8cd6935e9dd8c6d6ea95ce45f938da56fcda847 100644
--- a/content/browser/background_sync/background_sync_manager_unittest.cc
+++ b/content/browser/background_sync/background_sync_manager_unittest.cc
@@ -82,6 +82,16 @@ void OneShotDelayedCallback(
*out_callback = callback;
}
+void NotifyWhenDoneCallback(bool* was_called,
+ BackgroundSyncStatus* out_status,
+ BackgroundSyncState* out_state,
+ BackgroundSyncStatus status,
+ BackgroundSyncState state) {
+ *was_called = true;
+ *out_status = status;
+ *out_state = state;
+}
+
class TestPowerSource : public base::PowerMonitorSource {
public:
void GeneratePowerStateEvent(bool on_battery_power) {
@@ -1023,6 +1033,218 @@ TEST_F(BackgroundSyncManagerTest, OneShotFiresOnRegistration) {
EXPECT_FALSE(GetRegistration(sync_options_1_));
}
+TEST_F(BackgroundSyncManagerTest, NotifyWhenDoneAfterEventSuccess) {
+ InitSyncEventTest();
+
+ EXPECT_TRUE(Register(sync_options_1_));
+ EXPECT_EQ(1, sync_events_called_);
+
+ bool notify_done_called = false;
+ BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
+ BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
+ callback_registration_handle_->NotifyWhenDone(base::Bind(
+ &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
+ base::RunLoop().RunUntilIdle();
+ EXPECT_TRUE(notify_done_called);
+ EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
+ EXPECT_EQ(BACKGROUND_SYNC_STATE_SUCCESS, sync_state);
+}
+
+TEST_F(BackgroundSyncManagerTest, NotifyWhenDoneBeforeEventSuccess) {
+ InitDelayedSyncEventTest();
+
+ RegisterAndVerifySyncEventDelayed(sync_options_1_);
+
+ bool notify_done_called = false;
+ BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
+ BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
+ callback_registration_handle_->NotifyWhenDone(base::Bind(
+ &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
+ base::RunLoop().RunUntilIdle();
+ EXPECT_FALSE(notify_done_called);
+
+ // Finish firing the event.
+ sync_fired_callback_.Run(SERVICE_WORKER_OK);
+ base::RunLoop().RunUntilIdle();
+ EXPECT_EQ(1, sync_events_called_);
+ EXPECT_TRUE(notify_done_called);
+ EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
+ EXPECT_EQ(BACKGROUND_SYNC_STATE_SUCCESS, sync_state);
+}
+
+TEST_F(BackgroundSyncManagerTest,
+ NotifyWhenDoneBeforeUnregisteredEventSuccess) {
+ InitDelayedSyncEventTest();
+
+ RegisterAndVerifySyncEventDelayed(sync_options_1_);
+
+ bool notify_done_called = false;
+ BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
+ BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
+ callback_registration_handle_->NotifyWhenDone(base::Bind(
+ &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
+ base::RunLoop().RunUntilIdle();
+ EXPECT_FALSE(notify_done_called);
+
+ // Unregistering should set the state to UNREGISTERED but done shouldn't
+ // be called until the event finishes firing, at which point its state should
+ // be SUCCESS.
+ EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
+ EXPECT_FALSE(GetRegistration(sync_options_1_));
+
+ // Finish firing the event.
+ sync_fired_callback_.Run(SERVICE_WORKER_OK);
+ base::RunLoop().RunUntilIdle();
+ EXPECT_EQ(1, sync_events_called_);
+ EXPECT_TRUE(notify_done_called);
+ EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
+ EXPECT_EQ(BACKGROUND_SYNC_STATE_SUCCESS, sync_state);
+}
+
+TEST_F(BackgroundSyncManagerTest,
+ NotifyWhenDoneBeforeUnregisteredEventFailure) {
+ InitDelayedSyncEventTest();
+
+ RegisterAndVerifySyncEventDelayed(sync_options_1_);
+
+ bool notify_done_called = false;
+ BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
+ BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
+ callback_registration_handle_->NotifyWhenDone(base::Bind(
+ &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
+ base::RunLoop().RunUntilIdle();
+ EXPECT_FALSE(notify_done_called);
+
+ // Unregistering should set the state to UNREGISTERED but done shouldn't
+ // be called until the event finishes firing, at which point its state should
+ // be FAILED.
+ EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
+ EXPECT_FALSE(GetRegistration(sync_options_1_));
+
+ // Finish firing the event.
+ sync_fired_callback_.Run(SERVICE_WORKER_ERROR_FAILED);
+ base::RunLoop().RunUntilIdle();
+ EXPECT_EQ(1, sync_events_called_);
+ EXPECT_TRUE(notify_done_called);
+ EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
+ EXPECT_EQ(BACKGROUND_SYNC_STATE_FAILED, sync_state);
+}
+
+TEST_F(BackgroundSyncManagerTest, NotifyWhenDoneBeforeUnregisteredEventFires) {
+ InitSyncEventTest();
+
+ SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE);
+ EXPECT_TRUE(Register(sync_options_1_));
+ EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
+
+ bool notify_done_called = false;
+ BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
+ BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
+ callback_registration_handle_->NotifyWhenDone(base::Bind(
+ &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
+ base::RunLoop().RunUntilIdle();
+ EXPECT_TRUE(notify_done_called);
+ EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
+ EXPECT_EQ(BACKGROUND_SYNC_STATE_UNREGISTERED, sync_state);
+}
+
+TEST_F(BackgroundSyncManagerTest,
+ NotifyWhenDoneBeforeEventSuccessDroppedHandle) {
+ InitDelayedSyncEventTest();
+
+ RegisterAndVerifySyncEventDelayed(sync_options_1_);
+
+ bool notify_done_called = false;
+ BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
+ BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
+
+ callback_registration_handle_->NotifyWhenDone(base::Bind(
+ &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
+ base::RunLoop().RunUntilIdle();
+ EXPECT_FALSE(notify_done_called);
+
+ // Drop the client's handle to the registration before the event fires, ensure
+ // that the done callback is still run.
+ callback_registration_handle_ = nullptr;
+
+ // Finish firing the event.
+ sync_fired_callback_.Run(SERVICE_WORKER_OK);
+ base::RunLoop().RunUntilIdle();
+ EXPECT_EQ(1, sync_events_called_);
+ EXPECT_TRUE(notify_done_called);
+ EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
+ EXPECT_EQ(BACKGROUND_SYNC_STATE_SUCCESS, sync_state);
+}
+
+TEST_F(BackgroundSyncManagerTest, NotifyWhenDoneAfterEventFailure) {
+ InitFailedSyncEventTest();
+
+ EXPECT_TRUE(Register(sync_options_1_));
+ EXPECT_EQ(1, sync_events_called_);
+
+ bool notify_done_called = false;
+ BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
+ BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
+ callback_registration_handle_->NotifyWhenDone(base::Bind(
+ &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
+ base::RunLoop().RunUntilIdle();
+ EXPECT_TRUE(notify_done_called);
+ EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
+ EXPECT_EQ(BACKGROUND_SYNC_STATE_FAILED, sync_state);
+}
+
+TEST_F(BackgroundSyncManagerTest, NotifyWhenDoneBeforeEventFailure) {
+ InitDelayedSyncEventTest();
+
+ RegisterAndVerifySyncEventDelayed(sync_options_1_);
+
+ bool notify_done_called = false;
+ BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
+ BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
+ callback_registration_handle_->NotifyWhenDone(base::Bind(
+ &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
+ base::RunLoop().RunUntilIdle();
+ EXPECT_FALSE(notify_done_called);
+
+ // Finish firing the event.
+ sync_fired_callback_.Run(SERVICE_WORKER_ERROR_FAILED);
+ base::RunLoop().RunUntilIdle();
+ EXPECT_TRUE(notify_done_called);
+ EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
+ EXPECT_EQ(BACKGROUND_SYNC_STATE_FAILED, sync_state);
+}
+
+TEST_F(BackgroundSyncManagerTest, NotifyWhenDoneAfterUnregistered) {
+ EXPECT_TRUE(Register(sync_options_1_));
+ EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
+
+ bool notify_done_called = false;
+ BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
+ BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
+ callback_registration_handle_->NotifyWhenDone(base::Bind(
+ &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
+ base::RunLoop().RunUntilIdle();
+ EXPECT_TRUE(notify_done_called);
+ EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
+ EXPECT_EQ(BACKGROUND_SYNC_STATE_UNREGISTERED, sync_state);
+}
+
+TEST_F(BackgroundSyncManagerTest, NotifyWhenDoneBeforeUnregistered) {
+ Register(sync_options_1_);
+ bool notify_done_called = false;
+ BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
+ BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
+ callback_registration_handle_->NotifyWhenDone(base::Bind(
+ &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
+ base::RunLoop().RunUntilIdle();
+ EXPECT_FALSE(notify_done_called);
+
+ EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
+ EXPECT_TRUE(notify_done_called);
+ EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
+ EXPECT_EQ(BACKGROUND_SYNC_STATE_UNREGISTERED, sync_state);
+}
+
// TODO(jkarlin): Change this to a periodic test as one-shots can't be power
// dependent according to spec.
TEST_F(BackgroundSyncManagerTest, OneShotFiresOnPowerChange) {

Powered by Google App Engine
This is Rietveld 408576698