@@ -183,7 +183,7 @@ public void testInitFromApplicationContext() throws Exception {
183
183
Assert .assertNotNull (ShadowOneSignalRestClient .lastPost );
184
184
185
185
ShadowOneSignalRestClient .lastPost = null ;
186
- StaticResetHelper . restSetStaticFields ();
186
+ restartAppAndElapseTimeToNextSession ();
187
187
188
188
// Restart app, should not send onSession automatically
189
189
OneSignal .init (RuntimeEnvironment .application , "123456789" , ONESIGNAL_APP_ID );
@@ -196,6 +196,87 @@ public void testInitFromApplicationContext() throws Exception {
196
196
Assert .assertNotNull (ShadowOneSignalRestClient .lastPost );
197
197
}
198
198
199
+ @ Test
200
+ public void testOnSessionCalledOnlyOncePer30Sec () throws Exception {
201
+ // Will call create
202
+ ShadowSystemClock .setCurrentTimeMillis (60 * 60 * 1000 );
203
+ OneSignalInit ();
204
+ threadAndTaskWait ();
205
+ blankActivityController .resume ();
206
+ Assert .assertEquals ("players" , ShadowOneSignalRestClient .lastUrl );
207
+
208
+ // Shouldn't call on_session if just resuming app with a short delay
209
+ blankActivityController .pause ();
210
+ threadAndTaskWait ();
211
+ ShadowOneSignalRestClient .lastUrl = null ;
212
+ blankActivityController .resume ();
213
+ threadAndTaskWait ();
214
+ Assert .assertNull (ShadowOneSignalRestClient .lastUrl );
215
+
216
+ // Or when restarting the app quickly.
217
+ ShadowOneSignalRestClient .lastPost = null ;
218
+ StaticResetHelper .restSetStaticFields ();
219
+ OneSignalInit ();
220
+ threadAndTaskWait ();
221
+ blankActivityController .resume ();
222
+ threadAndTaskWait ();
223
+ Assert .assertNull (ShadowOneSignalRestClient .lastUrl );
224
+
225
+ blankActivityController .pause ();
226
+ threadAndTaskWait ();
227
+ ShadowSystemClock .setCurrentTimeMillis (121 * 60 * 1000 );
228
+ ShadowOneSignalRestClient .lastUrl = null ;
229
+ blankActivityController .resume ();
230
+ threadAndTaskWait ();
231
+ Assert .assertTrue (ShadowOneSignalRestClient .lastUrl .matches ("players/.*/on_session" ));
232
+ Assert .assertEquals ("{\" app_id\" :\" b2f7f966-d8cc-11e4-bed1-df8f05be55ba\" }" , ShadowOneSignalRestClient .lastPost .toString ());
233
+ }
234
+
235
+ @ Test
236
+ public void testPutStillCalledOnChanges () throws Exception {
237
+ // Will call create
238
+ ShadowSystemClock .setCurrentTimeMillis (60 * 60 * 1000 );
239
+ OneSignalInit ();
240
+ threadAndTaskWait ();
241
+ blankActivityController .resume ();
242
+ Assert .assertEquals ("players" , ShadowOneSignalRestClient .lastUrl );
243
+
244
+ // Shouldn't call on_session if just resuming app with a short delay
245
+ blankActivityController .pause ();
246
+ threadAndTaskWait ();
247
+ ShadowOneSignalRestClient .lastUrl = null ;
248
+ blankActivityController .resume ();
249
+ threadAndTaskWait ();
250
+ Assert .assertNull (ShadowOneSignalRestClient .lastUrl );
251
+ Assert .assertEquals (1 , ShadowOneSignalRestClient .networkCallCount );
252
+
253
+ ShadowOSUtils .carrierName = "test2" ;
254
+
255
+ // Should make PUT call with changes on app restart
256
+ ShadowOneSignalRestClient .lastPost = null ;
257
+ StaticResetHelper .restSetStaticFields ();
258
+ OneSignalInit ();
259
+ threadAndTaskWait ();
260
+ blankActivityController .resume ();
261
+ threadAndTaskWait ();
262
+
263
+ Assert .assertEquals (2 , ShadowOneSignalRestClient .networkCallCount );
264
+ GetIdsAvailable ();
265
+ Assert .assertEquals ("players/" + callBackUseId , ShadowOneSignalRestClient .lastUrl );
266
+ Assert .assertEquals ("{\" carrier\" :\" test2\" ,\" app_id\" :\" b2f7f966-d8cc-11e4-bed1-df8f05be55ba\" }" , ShadowOneSignalRestClient .lastPost .toString ());
267
+ }
268
+
269
+
270
+ @ Test
271
+ public void testPutCallsMadeWhenUserStateChangesOnAppResume () throws Exception {
272
+ // Will call create
273
+ ShadowSystemClock .setCurrentTimeMillis (60 * 60 * 1000 );
274
+ OneSignalInit ();
275
+ threadAndTaskWait ();
276
+ blankActivityController .resume ();
277
+ Assert .assertEquals ("players" , ShadowOneSignalRestClient .lastUrl );
278
+ }
279
+
199
280
@ Test
200
281
public void testOpenFromNotificationWhenAppIsDead () throws Exception {
201
282
OneSignal .handleNotificationOpen (blankActivity , new JSONArray ("[{ \" alert\" : \" Robo test message\" , \" custom\" : { \" i\" : \" UUID\" } }]" ), false );
@@ -325,7 +406,7 @@ public void notificationReceived(OSNotification notification) {
325
406
});
326
407
threadAndTaskWait ();
327
408
328
- OneSignal .setInFocusDisplaying (OneSignal .OSInFocusDisplay .Notification );
409
+ OneSignal .setInFocusDisplaying (OneSignal .OSInFocusDisplayOption .Notification );
329
410
330
411
Bundle bundle = getBaseNotifBundle ();
331
412
boolean processResult = GcmBroadcastReceiver_processBundle (blankActivity , bundle );
@@ -340,7 +421,7 @@ public void notificationReceived(OSNotification notification) {
340
421
// Don't fire for duplicates
341
422
notificationOpenedMessage = null ;
342
423
notificationReceivedBody = null ;
343
- OneSignal .setInFocusDisplaying (OneSignal .OSInFocusDisplay .None );
424
+ OneSignal .setInFocusDisplaying (OneSignal .OSInFocusDisplayOption .None );
344
425
Assert .assertNull (notificationOpenedMessage );
345
426
346
427
GcmBroadcastReceiver_processBundle (blankActivity , bundle );
@@ -349,7 +430,7 @@ public void notificationReceived(OSNotification notification) {
349
430
Assert .assertEquals (null , notificationReceivedBody );
350
431
351
432
// Test that only NotificationReceivedHandler fires
352
- OneSignal .setInFocusDisplaying (OneSignal .OSInFocusDisplay .None );
433
+ OneSignal .setInFocusDisplaying (OneSignal .OSInFocusDisplayOption .None );
353
434
bundle = getBaseNotifBundle ("UUID2" );
354
435
notificationOpenedMessage = null ;
355
436
notificationReceivedBody = null ;
@@ -424,7 +505,7 @@ public void testSetSubscriptionShouldNotOverrideSubscribeError() throws Exceptio
424
505
Assert .assertNull (ShadowOneSignalRestClient .lastPost );
425
506
426
507
// Restart app - Should omit notification_types
427
- StaticResetHelper . restSetStaticFields ();
508
+ restartAppAndElapseTimeToNextSession ();
428
509
OneSignalInitWithBadProjectNum ();
429
510
threadAndTaskWait ();
430
511
Assert .assertFalse (ShadowOneSignalRestClient .lastPost .has ("notification_types" ));
@@ -437,7 +518,7 @@ public void shouldNotResetSubscriptionOnSession() throws Exception {
437
518
threadAndTaskWait ();
438
519
Assert .assertEquals (-2 , ShadowOneSignalRestClient .lastPost .getInt ("notification_types" ));
439
520
440
- StaticResetHelper . restSetStaticFields ();
521
+ restartAppAndElapseTimeToNextSession ();
441
522
442
523
OneSignalInit ();
443
524
threadAndTaskWait ();
@@ -455,13 +536,13 @@ public void shouldSetSubscriptionCorrectlyEvenAfterFirstOneSignalRestInitFail()
455
536
456
537
457
538
// Restart app - Should send unsubscribe with create player call.
458
- StaticResetHelper . restSetStaticFields ();
539
+ restartAppAndElapseTimeToNextSession ();
459
540
OneSignalInit ();
460
541
threadAndTaskWait ();
461
542
Assert .assertEquals (-2 , ShadowOneSignalRestClient .lastPost .getInt ("notification_types" ));
462
543
463
544
// Restart app again - Value synced last time so don't send again.
464
- StaticResetHelper . restSetStaticFields ();
545
+ restartAppAndElapseTimeToNextSession ();
465
546
OneSignalInit ();
466
547
threadAndTaskWait ();
467
548
Assert .assertFalse (ShadowOneSignalRestClient .lastPost .has ("notification_types" ));
@@ -544,7 +625,7 @@ public void testGCMTimeOutThenSuccessesLater() throws Exception {
544
625
545
626
// Cold restart app, should not send the same identifier again.
546
627
ShadowOneSignalRestClient .lastPost = null ;
547
- StaticResetHelper . restSetStaticFields ();
628
+ restartAppAndElapseTimeToNextSession ();
548
629
OneSignalInit ();
549
630
threadAndTaskWait ();
550
631
Assert .assertFalse (ShadowOneSignalRestClient .lastPost .has ("identifier" ));
@@ -573,7 +654,7 @@ public void testUserDeletedFromServer() throws Exception {
573
654
ShadowOneSignalRestClient .lastPost = null ;
574
655
575
656
// Developer deletes user, cold boots apps should resend all fields
576
- StaticResetHelper . restSetStaticFields ();
657
+ restartAppAndElapseTimeToNextSession ();
577
658
ShadowOneSignalRestClient .failNext = true ;
578
659
ShadowOneSignalRestClient .failResponse = "{\" errors\" :[\" Device type is not a valid device_type. Valid options are: 0 = iOS, 1 = Android, 2 = Amazon, 3 = WindowsPhone(MPNS), 4 = ChromeApp, 5 = ChromeWebsite, 6 = WindowsPhone(WNS), 7 = Safari(APNS), 8 = Firefox\" ]}" ;
579
660
OneSignalInit ();
@@ -1044,22 +1125,27 @@ public void testAppl() throws Exception {
1044
1125
Assert .assertFalse (toSyncValues .has (baseKey + "_a" ));
1045
1126
Assert .assertEquals (1 , toSyncValues .getJSONArray (baseKey ).length ());
1046
1127
1047
- StaticResetHelper .restSetStaticFields ();
1128
+ restartAppAndElapseTimeToNextSession ();
1129
+ ShadowOneSignalRestClient .lastPost = null ;
1048
1130
RuntimeEnvironment .getRobolectricPackageManager ().addPackage ("org.test.app2" );
1049
1131
OneSignalInit ();
1050
1132
threadAndTaskWait ();
1051
1133
Assert .assertEquals (1 , ShadowOneSignalRestClient .lastPost .getJSONArray (baseKey + "_a" ).length ());
1052
1134
1053
- StaticResetHelper .restSetStaticFields ();
1135
+ restartAppAndElapseTimeToNextSession ();
1136
+ ShadowOneSignalRestClient .lastPost = null ;
1054
1137
RuntimeEnvironment .getRobolectricPackageManager ().removePackage ("org.test.app2" );
1055
1138
OneSignalInit ();
1056
1139
threadAndTaskWait ();
1057
1140
Assert .assertEquals (1 , ShadowOneSignalRestClient .lastPost .getJSONArray (baseKey + "_d" ).length ());
1058
1141
1059
- StaticResetHelper .restSetStaticFields ();
1142
+ restartAppAndElapseTimeToNextSession ();
1143
+ ShadowOneSignalRestClient .lastPost = null ;
1060
1144
OneSignalInit ();
1061
1145
threadAndTaskWait ();
1062
1146
1147
+ System .out .println ("ShadowOneSignalRestClient.lastPost: " + ShadowOneSignalRestClient .lastPost );
1148
+
1063
1149
Assert .assertFalse (ShadowOneSignalRestClient .lastPost .has (baseKey + "_d" ));
1064
1150
Assert .assertFalse (ShadowOneSignalRestClient .lastPost .has (baseKey + "_a" ));
1065
1151
}
@@ -1174,4 +1260,10 @@ private static void AddLauncherIntentFilter() {
1174
1260
1175
1261
RuntimeEnvironment .getRobolectricPackageManager ().addResolveInfoForIntent (launchIntent , resolveInfo );
1176
1262
}
1263
+
1264
+ private static int sessionCountOffset = 1 ;
1265
+ private static void restartAppAndElapseTimeToNextSession () {
1266
+ StaticResetHelper .restSetStaticFields ();
1267
+ ShadowSystemClock .setCurrentTimeMillis (System .currentTimeMillis () + 1000 * 31 * sessionCountOffset ++);
1268
+ }
1177
1269
}
0 commit comments