|
81 | 81 | import com.onesignal.ShadowNotificationManagerCompat;
|
82 | 82 | import com.onesignal.ShadowOSUtils;
|
83 | 83 | import com.onesignal.ShadowOneSignal;
|
| 84 | +import com.onesignal.ShadowOneSignalNotificationManager; |
84 | 85 | import com.onesignal.ShadowOneSignalRestClient;
|
85 | 86 | import com.onesignal.ShadowPushRegistratorADM;
|
86 | 87 | import com.onesignal.ShadowPushRegistratorFCM;
|
@@ -222,6 +223,7 @@ private static void getGetTagsHandler() {
|
222 | 223 | private static OSSMSSubscriptionStateChanges lastSMSSubscriptionStateChanges;
|
223 | 224 |
|
224 | 225 | private static void cleanUp() throws Exception {
|
| 226 | + lastServiceNotificationReceivedEvent = null; |
225 | 227 | lastNotificationOpenedBody = null;
|
226 | 228 | lastGetTags = null;
|
227 | 229 | lastEmailSubscriptionStateChanges = null;
|
@@ -1227,6 +1229,154 @@ public void testNotificationReceivedWhenAppInFocus() throws Exception {
|
1227 | 1229 | assertEquals("Robo test message", notificationReceivedBody);
|
1228 | 1230 | }
|
1229 | 1231 |
|
| 1232 | + // Start Received Request tests (report_received) |
| 1233 | + |
| 1234 | + @Test |
| 1235 | + @Config(shadows = { ShadowGenerateNotification.class }) |
| 1236 | + public void testNotificationReceivedSendReceivedRequest_WhenAppInBackground() throws Exception { |
| 1237 | + // First init run for appId to be saved |
| 1238 | + // At least OneSignal was init once for user to be subscribed |
| 1239 | + // If this doesn't' happen, notifications will not arrive |
| 1240 | + OneSignal.setAppId(ONESIGNAL_APP_ID); |
| 1241 | + OneSignal.initWithContext(blankActivity); |
| 1242 | + threadAndTaskWait(); |
| 1243 | + fastColdRestartApp(); |
| 1244 | + |
| 1245 | + ShadowOneSignalRestClient.setRemoteParamsReceiveReceiptsEnable(true); |
| 1246 | + // 1. initWithContext is called when startProcessing notification |
| 1247 | + OneSignal.initWithContext(blankActivity.getApplicationContext()); |
| 1248 | + // 2. Receive a notification in background |
| 1249 | + FCMBroadcastReceiver_processBundle(blankActivity, getBaseNotifBundle()); |
| 1250 | + threadAndTaskWait(); |
| 1251 | + |
| 1252 | + // 3. Check that report_received where sent |
| 1253 | + assertEquals(4, ShadowOneSignalRestClient.requests.size()); |
| 1254 | + assertEquals("notifications/UUID/report_received", ShadowOneSignalRestClient.lastUrl); |
| 1255 | + } |
| 1256 | + |
| 1257 | + @Test |
| 1258 | + @Config(shadows = { ShadowGenerateNotification.class }) |
| 1259 | + public void testNotificationReceivedSendReceivedRequest_WhenAppInForeground() throws Exception { |
| 1260 | + ShadowOneSignalRestClient.setRemoteParamsReceiveReceiptsEnable(true); |
| 1261 | + // First init run for appId to be saved |
| 1262 | + // At least OneSignal was init once for user to be subscribed |
| 1263 | + // If this doesn't' happen, notifications will not arrive |
| 1264 | + OneSignal.setAppId(ONESIGNAL_APP_ID); |
| 1265 | + OneSignal.initWithContext(blankActivity); |
| 1266 | + threadAndTaskWait(); |
| 1267 | + |
| 1268 | + // 1. Receive a notification in background |
| 1269 | + FCMBroadcastReceiver_processBundle(blankActivity, getBaseNotifBundle()); |
| 1270 | + threadAndTaskWait(); |
| 1271 | + |
| 1272 | + // 2. Check that report_received where sent |
| 1273 | + assertEquals(3, ShadowOneSignalRestClient.requests.size()); |
| 1274 | + assertEquals("notifications/UUID/report_received", ShadowOneSignalRestClient.lastUrl); |
| 1275 | + } |
| 1276 | + |
| 1277 | + @Test |
| 1278 | + @Config(shadows = { ShadowGenerateNotification.class }) |
| 1279 | + public void testNotificationReceivedNoSendReceivedRequest_WhenDisabled() throws Exception { |
| 1280 | + ShadowOneSignalRestClient.setRemoteParamsReceiveReceiptsEnable(false); |
| 1281 | + // First init run for appId to be saved |
| 1282 | + // At least OneSignal was init once for user to be subscribed |
| 1283 | + // If this doesn't' happen, notifications will not arrive |
| 1284 | + OneSignal.setAppId(ONESIGNAL_APP_ID); |
| 1285 | + OneSignal.initWithContext(blankActivity); |
| 1286 | + threadAndTaskWait(); |
| 1287 | + |
| 1288 | + // 1. Receive a notification in background |
| 1289 | + FCMBroadcastReceiver_processBundle(blankActivity, getBaseNotifBundle()); |
| 1290 | + threadAndTaskWait(); |
| 1291 | + |
| 1292 | + // 2. Check that report_received where sent |
| 1293 | + assertEquals(2, ShadowOneSignalRestClient.requests.size()); |
| 1294 | + assertNotEquals("notifications/UUID/report_received", ShadowOneSignalRestClient.lastUrl); |
| 1295 | + } |
| 1296 | + |
| 1297 | + @Test |
| 1298 | + @Config(shadows = { ShadowGenerateNotification.class }) |
| 1299 | + public void testNotificationReceivedNoSendReceivedRequest_WhenNotificationNotDisplayed() throws Exception { |
| 1300 | + // 1. Setup correct notification extension service class |
| 1301 | + startRemoteNotificationReceivedHandlerService("com.test.onesignal.MainOneSignalClassRunner$" + |
| 1302 | + "RemoteNotificationReceivedHandler_NoDisplay"); |
| 1303 | + |
| 1304 | + ShadowOneSignalRestClient.setRemoteParamsReceiveReceiptsEnable(true); |
| 1305 | + // First init run for appId to be saved |
| 1306 | + // At least OneSignal was init once for user to be subscribed |
| 1307 | + // If this doesn't' happen, notifications will not arrive |
| 1308 | + OneSignal.setAppId(ONESIGNAL_APP_ID); |
| 1309 | + OneSignal.initWithContext(blankActivity); |
| 1310 | + threadAndTaskWait(); |
| 1311 | + |
| 1312 | + // 2. Receive a notification in background |
| 1313 | + FCMBroadcastReceiver_processBundle(blankActivity, getBaseNotifBundle()); |
| 1314 | + threadAndTaskWait(); |
| 1315 | + |
| 1316 | + // 3. Make sure service was called |
| 1317 | + assertNotNull(lastServiceNotificationReceivedEvent); |
| 1318 | + |
| 1319 | + // 4. Check that report_received where sent |
| 1320 | + assertEquals(2, ShadowOneSignalRestClient.requests.size()); |
| 1321 | + assertNotEquals("notifications/UUID/report_received", ShadowOneSignalRestClient.lastUrl); |
| 1322 | + } |
| 1323 | + |
| 1324 | + @Test |
| 1325 | + @Config(sdk = 26, shadows = { ShadowGenerateNotification.class, ShadowOneSignalNotificationManager.class }) |
| 1326 | + public void testNotificationReceivedNoSendReceivedRequest_WhenNotificationNotDisplayed_DisabledByChannel() throws Exception { |
| 1327 | + // 1. Setup correct notification extension service class |
| 1328 | + startRemoteNotificationReceivedHandlerService("com.test.onesignal.MainOneSignalClassRunner$" + |
| 1329 | + "RemoteNotificationReceivedHandler"); |
| 1330 | + |
| 1331 | + ShadowOneSignalRestClient.setRemoteParamsReceiveReceiptsEnable(true); |
| 1332 | + ShadowOneSignalNotificationManager.setNotificationChannelEnabled(false); |
| 1333 | + // First init run for appId to be saved |
| 1334 | + // At least OneSignal was init once for user to be subscribed |
| 1335 | + // If this doesn't' happen, notifications will not arrive |
| 1336 | + OneSignal.setAppId(ONESIGNAL_APP_ID); |
| 1337 | + OneSignal.initWithContext(blankActivity); |
| 1338 | + threadAndTaskWait(); |
| 1339 | + |
| 1340 | + // 2. Receive a notification in background |
| 1341 | + FCMBroadcastReceiver_processBundle(blankActivity, getBaseNotifBundle()); |
| 1342 | + threadAndTaskWait(); |
| 1343 | + |
| 1344 | + // 3. Make sure service was called |
| 1345 | + assertNotNull(lastServiceNotificationReceivedEvent); |
| 1346 | + |
| 1347 | + // 4. Check that report_received where sent |
| 1348 | + assertEquals(2, ShadowOneSignalRestClient.requests.size()); |
| 1349 | + assertNotEquals("notifications/UUID/report_received", ShadowOneSignalRestClient.lastUrl); |
| 1350 | + } |
| 1351 | + |
| 1352 | + /** |
| 1353 | + * @see #testNotificationReceivedNoSendReceivedRequest_WhenNotificationNotDisplayed |
| 1354 | + */ |
| 1355 | + public static class RemoteNotificationReceivedHandler_NoDisplay implements OneSignal.OSRemoteNotificationReceivedHandler { |
| 1356 | + |
| 1357 | + @Override |
| 1358 | + public void remoteNotificationReceived(Context context, OSNotificationReceivedEvent receivedEvent) { |
| 1359 | + lastServiceNotificationReceivedEvent = receivedEvent; |
| 1360 | + |
| 1361 | + receivedEvent.complete(null); |
| 1362 | + } |
| 1363 | + } |
| 1364 | + |
| 1365 | + /** |
| 1366 | + * @see #testNotificationReceivedNoSendReceivedRequest_WhenNotificationNotDisplayed_DisabledByChannel |
| 1367 | + */ |
| 1368 | + public static class RemoteNotificationReceivedHandler implements OneSignal.OSRemoteNotificationReceivedHandler { |
| 1369 | + |
| 1370 | + @Override |
| 1371 | + public void remoteNotificationReceived(Context context, OSNotificationReceivedEvent receivedEvent) { |
| 1372 | + lastServiceNotificationReceivedEvent = receivedEvent; |
| 1373 | + |
| 1374 | + receivedEvent.complete(receivedEvent.getNotification()); |
| 1375 | + } |
| 1376 | + } |
| 1377 | + |
| 1378 | + // End Received Request tests (report_received) |
| 1379 | + |
1230 | 1380 | @Test
|
1231 | 1381 | @Config(shadows = {ShadowBadgeCountUpdater.class})
|
1232 | 1382 | public void testBadgeClearOnFirstStart() throws Exception {
|
|
0 commit comments