29
29
30
30
import android .app .Activity ;
31
31
import android .app .Notification ;
32
+ import android .app .NotificationManager ;
33
+ import android .content .Context ;
32
34
import android .content .Intent ;
33
- import android .content .pm .ActivityInfo ;
34
35
import android .content .pm .ResolveInfo ;
35
36
import android .content .pm .ServiceInfo ;
36
37
import android .database .Cursor ;
41
42
import com .onesignal .GcmBroadcastReceiver ;
42
43
import com .onesignal .NotificationExtenderService ;
43
44
import com .onesignal .NotificationOpenedProcessor ;
44
- import com .onesignal .OSNotificationDisplayedResult ;
45
45
import com .onesignal .OSNotificationPayload ;
46
+ import com .onesignal .OneSignal ;
46
47
import com .onesignal .OneSignalDbHelper ;
47
48
import com .onesignal .OneSignalPackagePrivateHelper ;
48
49
import com .onesignal .ShadowBadgeCountUpdater ;
68
69
import org .robolectric .shadows .ShadowSystemClock ;
69
70
import org .robolectric .util .ServiceController ;
70
71
72
+ import java .util .Iterator ;
71
73
import java .util .List ;
74
+ import java .util .Map ;
72
75
73
76
@ Config (packageName = "com.onesignal.example" ,
74
77
constants = BuildConfig .class ,
@@ -88,15 +91,15 @@ public static void setUpClass() throws Exception {
88
91
89
92
@ Before // Before each test
90
93
public void beforeEachTest () throws Exception {
91
- ShadowRoboNotificationManager .notifications .clear ();
92
-
93
94
// Robolectric mocks System.currentTimeMillis() to 0, we need the current real time to match our SQL records.
94
95
ShadowSystemClock .setCurrentTimeMillis (System .currentTimeMillis ());
95
96
96
97
blankActivity = Robolectric .buildActivity (BlankActivity .class ).create ().get ();
97
98
blankActivity .getApplicationInfo ().name = "UnitTestApp" ;
98
99
99
100
ShadowBadgeCountUpdater .lastCount = 0 ;
101
+ NotificationManager notificationManager = (NotificationManager )blankActivity .getSystemService (Context .NOTIFICATION_SERVICE );
102
+ notificationManager .cancelAll ();
100
103
}
101
104
102
105
@@ -207,16 +210,20 @@ public void shouldGenerate2BasicGroupNotifications() throws Exception {
207
210
bundle .putString ("grp" , "test1" );
208
211
OneSignalPackagePrivateHelper .NotificationBundleProcessor_ProcessFromGCMIntentService (blankActivity , bundle , null );
209
212
210
- List < PostedNotification > postedNotifs = ShadowRoboNotificationManager .notifications ;
213
+ Map < Integer , PostedNotification > postedNotifs = ShadowRoboNotificationManager .notifications ;
211
214
Assert .assertEquals (2 , postedNotifs .size ());
212
215
213
216
// Test summary notification
214
- Assert .assertEquals (notifMessage , postedNotifs .get (0 ).notif .getContentText ());
215
- Assert .assertEquals (Notification .FLAG_GROUP_SUMMARY , postedNotifs .get (0 ).notif .getRealNotification ().flags & Notification .FLAG_GROUP_SUMMARY );
217
+ Iterator <Map .Entry <Integer , PostedNotification >> postedNotifsIterator = postedNotifs .entrySet ().iterator ();
218
+ PostedNotification postedNotification = postedNotifsIterator .next ().getValue ();
219
+
220
+ Assert .assertEquals (notifMessage , postedNotification .notif .getContentText ());
221
+ Assert .assertEquals (Notification .FLAG_GROUP_SUMMARY ,postedNotification .notif .getRealNotification ().flags & Notification .FLAG_GROUP_SUMMARY );
216
222
217
223
// Test Android Wear notification
218
- Assert .assertEquals (notifMessage , postedNotifs .get (1 ).notif .getContentText ());
219
- Assert .assertEquals (0 , postedNotifs .get (1 ).notif .getRealNotification ().flags & Notification .FLAG_GROUP_SUMMARY );
224
+ postedNotification = postedNotifsIterator .next ().getValue ();
225
+ Assert .assertEquals (notifMessage , postedNotification .notif .getContentText ());
226
+ Assert .assertEquals (0 , postedNotification .notif .getRealNotification ().flags & Notification .FLAG_GROUP_SUMMARY );
220
227
// Badge count should only be one as only one notification is visible in the notification area.
221
228
Assert .assertEquals (1 , ShadowBadgeCountUpdater .lastCount );
222
229
@@ -239,12 +246,15 @@ public void shouldGenerate2BasicGroupNotifications() throws Exception {
239
246
Assert .assertEquals (2 , postedNotifs .size ());
240
247
Assert .assertEquals (2 , ShadowBadgeCountUpdater .lastCount );
241
248
242
- Assert .assertEquals ("2 new messages" , postedNotifs .get (0 ).notif .getContentText ());
243
- Assert .assertEquals (Notification .FLAG_GROUP_SUMMARY , postedNotifs .get (0 ).notif .getRealNotification ().flags & Notification .FLAG_GROUP_SUMMARY );
249
+ postedNotifsIterator = postedNotifs .entrySet ().iterator ();
250
+ postedNotification = postedNotifsIterator .next ().getValue ();
251
+ Assert .assertEquals ("2 new messages" ,postedNotification .notif .getContentText ());
252
+ Assert .assertEquals (Notification .FLAG_GROUP_SUMMARY , postedNotification .notif .getRealNotification ().flags & Notification .FLAG_GROUP_SUMMARY );
244
253
245
254
// Test Android Wear notification
246
- Assert .assertEquals ("Notif test 2" , postedNotifs .get (1 ).notif .getContentText ());
247
- Assert .assertEquals (0 , postedNotifs .get (1 ).notif .getRealNotification ().flags & Notification .FLAG_GROUP_SUMMARY );
255
+ postedNotification = postedNotifsIterator .next ().getValue ();
256
+ Assert .assertEquals ("Notif test 2" , postedNotification .notif .getContentText ());
257
+ Assert .assertEquals (0 , postedNotification .notif .getRealNotification ().flags & Notification .FLAG_GROUP_SUMMARY );
248
258
249
259
250
260
// Should be 3 DB entries (summary and 2 individual)
@@ -253,7 +263,9 @@ public void shouldGenerate2BasicGroupNotifications() throws Exception {
253
263
254
264
255
265
// Open summary notification
256
- Intent intent = createOpenIntent (postedNotifs .get (0 ).id , bundle ).putExtra ("summary" , "test1" );
266
+ postedNotifsIterator = postedNotifs .entrySet ().iterator ();
267
+ postedNotification = postedNotifsIterator .next ().getValue ();
268
+ Intent intent = createOpenIntent (postedNotification .id , bundle ).putExtra ("summary" , "test1" );
257
269
NotificationOpenedProcessor .processFromActivity (blankActivity , intent );
258
270
Assert .assertEquals (0 , ShadowBadgeCountUpdater .lastCount );
259
271
ShadowRoboNotificationManager .notifications .clear ();
@@ -265,8 +277,10 @@ public void shouldGenerate2BasicGroupNotifications() throws Exception {
265
277
bundle .putString ("grp" , "test1" );
266
278
OneSignalPackagePrivateHelper .NotificationBundleProcessor_ProcessFromGCMIntentService (blankActivity , bundle , null );
267
279
268
- Assert .assertEquals ("Notif test 3" , postedNotifs .get (0 ).notif .getContentText ());
269
- Assert .assertEquals (Notification .FLAG_GROUP_SUMMARY , postedNotifs .get (0 ).notif .getRealNotification ().flags & Notification .FLAG_GROUP_SUMMARY );
280
+ postedNotifsIterator = postedNotifs .entrySet ().iterator ();
281
+ postedNotification = postedNotifsIterator .next ().getValue ();
282
+ Assert .assertEquals ("Notif test 3" , postedNotification .notif .getContentText ());
283
+ Assert .assertEquals (Notification .FLAG_GROUP_SUMMARY , postedNotification .notif .getRealNotification ().flags & Notification .FLAG_GROUP_SUMMARY );
270
284
Assert .assertEquals (1 , ShadowBadgeCountUpdater .lastCount );
271
285
cursor .close ();
272
286
}
@@ -290,8 +304,11 @@ public void shouldSetButtonsCorrectly() throws Exception {
290
304
291
305
Intent intent = Shadows .shadowOf (blankActivity ).getNextStartedService ();
292
306
Assert .assertEquals ("com.onesignal.GcmIntentService" , intent .getComponent ().getClassName ());
293
- Assert .assertEquals (null , intent .getStringExtra ("o" ));
294
- JSONObject customJson = new JSONObject (intent .getStringExtra ("custom" ));
307
+
308
+ JSONObject jsonPayload = new JSONObject (intent .getStringExtra ("json_payload" ));
309
+
310
+ Assert .assertEquals (null , jsonPayload .optString ("o" , null ));
311
+ JSONObject customJson = new JSONObject (jsonPayload .optString ("custom" ));
295
312
JSONObject additionalData = new JSONObject ((customJson .getString ("a" )));
296
313
Assert .assertEquals ("id1" , additionalData .getJSONArray ("actionButtons" ).getJSONObject (0 ).getString ("id" ));
297
314
}
@@ -319,7 +336,7 @@ public void shouldFireNotificationExtenderService() throws Exception {
319
336
ServiceController <NotificationExtenderServiceTest > controller = Robolectric .buildService (NotificationExtenderServiceTest .class );
320
337
NotificationExtenderServiceTest service = controller .attach ().create ().get ();
321
338
Intent testIntent = new Intent (RuntimeEnvironment .application , NotificationExtenderServiceTest .class );
322
- testIntent .putExtras (getBundleWithAllOptionsSet ());
339
+ testIntent .putExtras (OneSignalPackagePrivateHelper . createInternalPayloadBundle ( getBundleWithAllOptionsSet () ));
323
340
controller .withIntent (testIntent ).startCommand (0 , 0 );
324
341
325
342
OSNotificationPayload notification = service .notification ;
@@ -355,19 +372,19 @@ public void shouldFireNotificationExtenderService() throws Exception {
355
372
356
373
// Test a basic notification without anything special.
357
374
testIntent = new Intent (RuntimeEnvironment .application , NotificationExtenderServiceTest .class );
358
- testIntent .putExtras (getBaseNotifBundle ());
375
+ testIntent .putExtras (OneSignalPackagePrivateHelper . createInternalPayloadBundle ( getBaseNotifBundle () ));
359
376
controller .withIntent (testIntent ).startCommand (0 , 0 );
360
377
Assert .assertFalse (ShadowOneSignal .messages .contains ("Error assigning" ));
361
378
362
379
363
380
// Test that a notification is still displayed if the developer's code in onNotificationProcessing throws an Exception.
364
381
NotificationExtenderServiceTest .throwInAppCode = true ;
365
382
testIntent = new Intent (RuntimeEnvironment .application , NotificationExtenderServiceTest .class );
366
- testIntent .putExtras (getBaseNotifBundle ("NewUUID1" ));
383
+ testIntent .putExtras (OneSignalPackagePrivateHelper . createInternalPayloadBundle ( getBaseNotifBundle ("NewUUID1" ) ));
367
384
controller .withIntent (testIntent ).startCommand (0 , 0 );
368
385
369
386
Assert .assertTrue (ShadowOneSignal .messages .contains ("onNotificationProcessing throw an exception" ));
370
- List < PostedNotification > postedNotifs = ShadowRoboNotificationManager .notifications ;
387
+ Map < Integer , PostedNotification > postedNotifs = ShadowRoboNotificationManager .notifications ;
371
388
Assert .assertEquals (3 , postedNotifs .size ());
372
389
}
373
390
0 commit comments