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

Side by Side 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 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"
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 75
76 void OneShotDelayedCallback( 76 void OneShotDelayedCallback(
77 int* count, 77 int* count,
78 ServiceWorkerVersion::StatusCallback* out_callback, 78 ServiceWorkerVersion::StatusCallback* out_callback,
79 const scoped_refptr<ServiceWorkerVersion>& active_version, 79 const scoped_refptr<ServiceWorkerVersion>& active_version,
80 const ServiceWorkerVersion::StatusCallback& callback) { 80 const ServiceWorkerVersion::StatusCallback& callback) {
81 *count += 1; 81 *count += 1;
82 *out_callback = callback; 82 *out_callback = callback;
83 } 83 }
84 84
85 void NotifyWhenDoneCallback(bool* was_called,
86 BackgroundSyncStatus* out_status,
87 BackgroundSyncState* out_state,
88 BackgroundSyncStatus status,
89 BackgroundSyncState state) {
90 *was_called = true;
91 *out_status = status;
92 *out_state = state;
93 }
94
85 class TestPowerSource : public base::PowerMonitorSource { 95 class TestPowerSource : public base::PowerMonitorSource {
86 public: 96 public:
87 void GeneratePowerStateEvent(bool on_battery_power) { 97 void GeneratePowerStateEvent(bool on_battery_power) {
88 test_on_battery_power_ = on_battery_power; 98 test_on_battery_power_ = on_battery_power;
89 ProcessPowerEvent(POWER_STATE_EVENT); 99 ProcessPowerEvent(POWER_STATE_EVENT);
90 } 100 }
91 101
92 private: 102 private:
93 bool IsOnBatteryPowerImpl() final { return test_on_battery_power_; } 103 bool IsOnBatteryPowerImpl() final { return test_on_battery_power_; }
94 bool test_on_battery_power_ = false; 104 bool test_on_battery_power_ = false;
(...skipping 921 matching lines...) Expand 10 before | Expand all | Expand 10 after
1016 } 1026 }
1017 1027
1018 TEST_F(BackgroundSyncManagerTest, OneShotFiresOnRegistration) { 1028 TEST_F(BackgroundSyncManagerTest, OneShotFiresOnRegistration) {
1019 InitSyncEventTest(); 1029 InitSyncEventTest();
1020 1030
1021 EXPECT_TRUE(Register(sync_options_1_)); 1031 EXPECT_TRUE(Register(sync_options_1_));
1022 EXPECT_EQ(1, sync_events_called_); 1032 EXPECT_EQ(1, sync_events_called_);
1023 EXPECT_FALSE(GetRegistration(sync_options_1_)); 1033 EXPECT_FALSE(GetRegistration(sync_options_1_));
1024 } 1034 }
1025 1035
1036 TEST_F(BackgroundSyncManagerTest, NotifyWhenDoneAfterEventSuccess) {
1037 InitSyncEventTest();
1038
1039 EXPECT_TRUE(Register(sync_options_1_));
1040 EXPECT_EQ(1, sync_events_called_);
1041
1042 bool notify_done_called = false;
1043 BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
1044 BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
1045 callback_registration_handle_->NotifyWhenDone(base::Bind(
1046 &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
1047 base::RunLoop().RunUntilIdle();
1048 EXPECT_TRUE(notify_done_called);
1049 EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
1050 EXPECT_EQ(BACKGROUND_SYNC_STATE_SUCCESS, sync_state);
1051 }
1052
1053 TEST_F(BackgroundSyncManagerTest, NotifyWhenDoneBeforeEventSuccess) {
1054 InitDelayedSyncEventTest();
1055
1056 RegisterAndVerifySyncEventDelayed(sync_options_1_);
1057
1058 bool notify_done_called = false;
1059 BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
1060 BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
1061 callback_registration_handle_->NotifyWhenDone(base::Bind(
1062 &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
1063 base::RunLoop().RunUntilIdle();
1064 EXPECT_FALSE(notify_done_called);
1065
1066 // Finish firing the event.
1067 sync_fired_callback_.Run(SERVICE_WORKER_OK);
1068 base::RunLoop().RunUntilIdle();
1069 EXPECT_EQ(1, sync_events_called_);
1070 EXPECT_TRUE(notify_done_called);
1071 EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
1072 EXPECT_EQ(BACKGROUND_SYNC_STATE_SUCCESS, sync_state);
1073 }
1074
1075 TEST_F(BackgroundSyncManagerTest,
1076 NotifyWhenDoneBeforeUnregisteredEventSuccess) {
1077 InitDelayedSyncEventTest();
1078
1079 RegisterAndVerifySyncEventDelayed(sync_options_1_);
1080
1081 bool notify_done_called = false;
1082 BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
1083 BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
1084 callback_registration_handle_->NotifyWhenDone(base::Bind(
1085 &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
1086 base::RunLoop().RunUntilIdle();
1087 EXPECT_FALSE(notify_done_called);
1088
1089 // Unregistering should set the state to UNREGISTERED but done shouldn't
1090 // be called until the event finishes firing, at which point its state should
1091 // be SUCCESS.
1092 EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
1093 EXPECT_FALSE(GetRegistration(sync_options_1_));
1094
1095 // Finish firing the event.
1096 sync_fired_callback_.Run(SERVICE_WORKER_OK);
1097 base::RunLoop().RunUntilIdle();
1098 EXPECT_EQ(1, sync_events_called_);
1099 EXPECT_TRUE(notify_done_called);
1100 EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
1101 EXPECT_EQ(BACKGROUND_SYNC_STATE_SUCCESS, sync_state);
1102 }
1103
1104 TEST_F(BackgroundSyncManagerTest,
1105 NotifyWhenDoneBeforeUnregisteredEventFailure) {
1106 InitDelayedSyncEventTest();
1107
1108 RegisterAndVerifySyncEventDelayed(sync_options_1_);
1109
1110 bool notify_done_called = false;
1111 BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
1112 BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
1113 callback_registration_handle_->NotifyWhenDone(base::Bind(
1114 &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
1115 base::RunLoop().RunUntilIdle();
1116 EXPECT_FALSE(notify_done_called);
1117
1118 // Unregistering should set the state to UNREGISTERED but done shouldn't
1119 // be called until the event finishes firing, at which point its state should
1120 // be FAILED.
1121 EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
1122 EXPECT_FALSE(GetRegistration(sync_options_1_));
1123
1124 // Finish firing the event.
1125 sync_fired_callback_.Run(SERVICE_WORKER_ERROR_FAILED);
1126 base::RunLoop().RunUntilIdle();
1127 EXPECT_EQ(1, sync_events_called_);
1128 EXPECT_TRUE(notify_done_called);
1129 EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
1130 EXPECT_EQ(BACKGROUND_SYNC_STATE_FAILED, sync_state);
1131 }
1132
1133 TEST_F(BackgroundSyncManagerTest, NotifyWhenDoneBeforeUnregisteredEventFires) {
1134 InitSyncEventTest();
1135
1136 SetNetwork(net::NetworkChangeNotifier::CONNECTION_NONE);
1137 EXPECT_TRUE(Register(sync_options_1_));
1138 EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
1139
1140 bool notify_done_called = false;
1141 BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
1142 BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
1143 callback_registration_handle_->NotifyWhenDone(base::Bind(
1144 &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
1145 base::RunLoop().RunUntilIdle();
1146 EXPECT_TRUE(notify_done_called);
1147 EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
1148 EXPECT_EQ(BACKGROUND_SYNC_STATE_UNREGISTERED, sync_state);
1149 }
1150
1151 TEST_F(BackgroundSyncManagerTest,
1152 NotifyWhenDoneBeforeEventSuccessDroppedHandle) {
1153 InitDelayedSyncEventTest();
1154
1155 RegisterAndVerifySyncEventDelayed(sync_options_1_);
1156
1157 bool notify_done_called = false;
1158 BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
1159 BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
1160
1161 callback_registration_handle_->NotifyWhenDone(base::Bind(
1162 &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
1163 base::RunLoop().RunUntilIdle();
1164 EXPECT_FALSE(notify_done_called);
1165
1166 // Drop the client's handle to the registration before the event fires, ensure
1167 // that the done callback is still run.
1168 callback_registration_handle_ = nullptr;
1169
1170 // Finish firing the event.
1171 sync_fired_callback_.Run(SERVICE_WORKER_OK);
1172 base::RunLoop().RunUntilIdle();
1173 EXPECT_EQ(1, sync_events_called_);
1174 EXPECT_TRUE(notify_done_called);
1175 EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
1176 EXPECT_EQ(BACKGROUND_SYNC_STATE_SUCCESS, sync_state);
1177 }
1178
1179 TEST_F(BackgroundSyncManagerTest, NotifyWhenDoneAfterEventFailure) {
1180 InitFailedSyncEventTest();
1181
1182 EXPECT_TRUE(Register(sync_options_1_));
1183 EXPECT_EQ(1, sync_events_called_);
1184
1185 bool notify_done_called = false;
1186 BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
1187 BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
1188 callback_registration_handle_->NotifyWhenDone(base::Bind(
1189 &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
1190 base::RunLoop().RunUntilIdle();
1191 EXPECT_TRUE(notify_done_called);
1192 EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
1193 EXPECT_EQ(BACKGROUND_SYNC_STATE_FAILED, sync_state);
1194 }
1195
1196 TEST_F(BackgroundSyncManagerTest, NotifyWhenDoneBeforeEventFailure) {
1197 InitDelayedSyncEventTest();
1198
1199 RegisterAndVerifySyncEventDelayed(sync_options_1_);
1200
1201 bool notify_done_called = false;
1202 BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
1203 BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
1204 callback_registration_handle_->NotifyWhenDone(base::Bind(
1205 &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
1206 base::RunLoop().RunUntilIdle();
1207 EXPECT_FALSE(notify_done_called);
1208
1209 // Finish firing the event.
1210 sync_fired_callback_.Run(SERVICE_WORKER_ERROR_FAILED);
1211 base::RunLoop().RunUntilIdle();
1212 EXPECT_TRUE(notify_done_called);
1213 EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
1214 EXPECT_EQ(BACKGROUND_SYNC_STATE_FAILED, sync_state);
1215 }
1216
1217 TEST_F(BackgroundSyncManagerTest, NotifyWhenDoneAfterUnregistered) {
1218 EXPECT_TRUE(Register(sync_options_1_));
1219 EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
1220
1221 bool notify_done_called = false;
1222 BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
1223 BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
1224 callback_registration_handle_->NotifyWhenDone(base::Bind(
1225 &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
1226 base::RunLoop().RunUntilIdle();
1227 EXPECT_TRUE(notify_done_called);
1228 EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
1229 EXPECT_EQ(BACKGROUND_SYNC_STATE_UNREGISTERED, sync_state);
1230 }
1231
1232 TEST_F(BackgroundSyncManagerTest, NotifyWhenDoneBeforeUnregistered) {
1233 Register(sync_options_1_);
1234 bool notify_done_called = false;
1235 BackgroundSyncStatus status = BACKGROUND_SYNC_STATUS_OK;
1236 BackgroundSyncState sync_state = BACKGROUND_SYNC_STATE_SUCCESS;
1237 callback_registration_handle_->NotifyWhenDone(base::Bind(
1238 &NotifyWhenDoneCallback, &notify_done_called, &status, &sync_state));
1239 base::RunLoop().RunUntilIdle();
1240 EXPECT_FALSE(notify_done_called);
1241
1242 EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
1243 EXPECT_TRUE(notify_done_called);
1244 EXPECT_EQ(BACKGROUND_SYNC_STATUS_OK, status);
1245 EXPECT_EQ(BACKGROUND_SYNC_STATE_UNREGISTERED, sync_state);
1246 }
1247
1026 // TODO(jkarlin): Change this to a periodic test as one-shots can't be power 1248 // TODO(jkarlin): Change this to a periodic test as one-shots can't be power
1027 // dependent according to spec. 1249 // dependent according to spec.
1028 TEST_F(BackgroundSyncManagerTest, OneShotFiresOnPowerChange) { 1250 TEST_F(BackgroundSyncManagerTest, OneShotFiresOnPowerChange) {
1029 InitSyncEventTest(); 1251 InitSyncEventTest();
1030 sync_options_1_.power_state = POWER_STATE_AVOID_DRAINING; 1252 sync_options_1_.power_state = POWER_STATE_AVOID_DRAINING;
1031 1253
1032 SetOnBatteryPower(true); 1254 SetOnBatteryPower(true);
1033 EXPECT_TRUE(Register(sync_options_1_)); 1255 EXPECT_TRUE(Register(sync_options_1_));
1034 EXPECT_EQ(0, sync_events_called_); 1256 EXPECT_EQ(0, sync_events_called_);
1035 EXPECT_TRUE(GetRegistration(sync_options_1_)); 1257 EXPECT_TRUE(GetRegistration(sync_options_1_));
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
1282 } 1504 }
1283 1505
1284 TEST_F(BackgroundSyncManagerTest, UnregisterSucceedsWithoutWindow) { 1506 TEST_F(BackgroundSyncManagerTest, UnregisterSucceedsWithoutWindow) {
1285 EXPECT_TRUE(Register(sync_options_1_)); 1507 EXPECT_TRUE(Register(sync_options_1_));
1286 RemoveWindowClients(); 1508 RemoveWindowClients();
1287 EXPECT_TRUE(Unregister(callback_registration_handle_.get())); 1509 EXPECT_TRUE(Unregister(callback_registration_handle_.get()));
1288 EXPECT_FALSE(GetRegistration(sync_options_1_)); 1510 EXPECT_FALSE(GetRegistration(sync_options_1_));
1289 } 1511 }
1290 1512
1291 } // namespace content 1513 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698