2929
3030import android .app .Activity ;
3131import android .app .Notification ;
32+ import android .app .NotificationManager ;
33+ import android .content .Context ;
3234import android .content .Intent ;
33- import android .content .pm .ActivityInfo ;
3435import android .content .pm .ResolveInfo ;
3536import android .content .pm .ServiceInfo ;
3637import android .database .Cursor ;
4142import com .onesignal .GcmBroadcastReceiver ;
4243import com .onesignal .NotificationExtenderService ;
4344import com .onesignal .NotificationOpenedProcessor ;
44- import com .onesignal .OSNotificationDisplayedResult ;
4545import com .onesignal .OSNotificationPayload ;
46+ import com .onesignal .OneSignal ;
4647import com .onesignal .OneSignalDbHelper ;
4748import com .onesignal .OneSignalPackagePrivateHelper ;
4849import com .onesignal .ShadowBadgeCountUpdater ;
6869import org .robolectric .shadows .ShadowSystemClock ;
6970import org .robolectric .util .ServiceController ;
7071
72+ import java .util .Iterator ;
7173import java .util .List ;
74+ import java .util .Map ;
7275
7376@ Config (packageName = "com.onesignal.example" ,
7477 constants = BuildConfig .class ,
@@ -88,15 +91,15 @@ public static void setUpClass() throws Exception {
8891
8992 @ Before // Before each test
9093 public void beforeEachTest () throws Exception {
91- ShadowRoboNotificationManager .notifications .clear ();
92-
9394 // Robolectric mocks System.currentTimeMillis() to 0, we need the current real time to match our SQL records.
9495 ShadowSystemClock .setCurrentTimeMillis (System .currentTimeMillis ());
9596
9697 blankActivity = Robolectric .buildActivity (BlankActivity .class ).create ().get ();
9798 blankActivity .getApplicationInfo ().name = "UnitTestApp" ;
9899
99100 ShadowBadgeCountUpdater .lastCount = 0 ;
101+ NotificationManager notificationManager = (NotificationManager )blankActivity .getSystemService (Context .NOTIFICATION_SERVICE );
102+ notificationManager .cancelAll ();
100103 }
101104
102105
@@ -207,16 +210,20 @@ public void shouldGenerate2BasicGroupNotifications() throws Exception {
207210 bundle .putString ("grp" , "test1" );
208211 OneSignalPackagePrivateHelper .NotificationBundleProcessor_ProcessFromGCMIntentService (blankActivity , bundle , null );
209212
210- List < PostedNotification > postedNotifs = ShadowRoboNotificationManager .notifications ;
213+ Map < Integer , PostedNotification > postedNotifs = ShadowRoboNotificationManager .notifications ;
211214 Assert .assertEquals (2 , postedNotifs .size ());
212215
213216 // 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 );
216222
217223 // 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 );
220227 // Badge count should only be one as only one notification is visible in the notification area.
221228 Assert .assertEquals (1 , ShadowBadgeCountUpdater .lastCount );
222229
@@ -239,12 +246,15 @@ public void shouldGenerate2BasicGroupNotifications() throws Exception {
239246 Assert .assertEquals (2 , postedNotifs .size ());
240247 Assert .assertEquals (2 , ShadowBadgeCountUpdater .lastCount );
241248
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 );
244253
245254 // 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 );
248258
249259
250260 // Should be 3 DB entries (summary and 2 individual)
@@ -253,7 +263,9 @@ public void shouldGenerate2BasicGroupNotifications() throws Exception {
253263
254264
255265 // 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" );
257269 NotificationOpenedProcessor .processFromActivity (blankActivity , intent );
258270 Assert .assertEquals (0 , ShadowBadgeCountUpdater .lastCount );
259271 ShadowRoboNotificationManager .notifications .clear ();
@@ -265,8 +277,10 @@ public void shouldGenerate2BasicGroupNotifications() throws Exception {
265277 bundle .putString ("grp" , "test1" );
266278 OneSignalPackagePrivateHelper .NotificationBundleProcessor_ProcessFromGCMIntentService (blankActivity , bundle , null );
267279
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 );
270284 Assert .assertEquals (1 , ShadowBadgeCountUpdater .lastCount );
271285 cursor .close ();
272286 }
@@ -290,8 +304,11 @@ public void shouldSetButtonsCorrectly() throws Exception {
290304
291305 Intent intent = Shadows .shadowOf (blankActivity ).getNextStartedService ();
292306 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" ));
295312 JSONObject additionalData = new JSONObject ((customJson .getString ("a" )));
296313 Assert .assertEquals ("id1" , additionalData .getJSONArray ("actionButtons" ).getJSONObject (0 ).getString ("id" ));
297314 }
@@ -319,7 +336,7 @@ public void shouldFireNotificationExtenderService() throws Exception {
319336 ServiceController <NotificationExtenderServiceTest > controller = Robolectric .buildService (NotificationExtenderServiceTest .class );
320337 NotificationExtenderServiceTest service = controller .attach ().create ().get ();
321338 Intent testIntent = new Intent (RuntimeEnvironment .application , NotificationExtenderServiceTest .class );
322- testIntent .putExtras (getBundleWithAllOptionsSet ());
339+ testIntent .putExtras (OneSignalPackagePrivateHelper . createInternalPayloadBundle ( getBundleWithAllOptionsSet () ));
323340 controller .withIntent (testIntent ).startCommand (0 , 0 );
324341
325342 OSNotificationPayload notification = service .notification ;
@@ -355,19 +372,19 @@ public void shouldFireNotificationExtenderService() throws Exception {
355372
356373 // Test a basic notification without anything special.
357374 testIntent = new Intent (RuntimeEnvironment .application , NotificationExtenderServiceTest .class );
358- testIntent .putExtras (getBaseNotifBundle ());
375+ testIntent .putExtras (OneSignalPackagePrivateHelper . createInternalPayloadBundle ( getBaseNotifBundle () ));
359376 controller .withIntent (testIntent ).startCommand (0 , 0 );
360377 Assert .assertFalse (ShadowOneSignal .messages .contains ("Error assigning" ));
361378
362379
363380 // Test that a notification is still displayed if the developer's code in onNotificationProcessing throws an Exception.
364381 NotificationExtenderServiceTest .throwInAppCode = true ;
365382 testIntent = new Intent (RuntimeEnvironment .application , NotificationExtenderServiceTest .class );
366- testIntent .putExtras (getBaseNotifBundle ("NewUUID1" ));
383+ testIntent .putExtras (OneSignalPackagePrivateHelper . createInternalPayloadBundle ( getBaseNotifBundle ("NewUUID1" ) ));
367384 controller .withIntent (testIntent ).startCommand (0 , 0 );
368385
369386 Assert .assertTrue (ShadowOneSignal .messages .contains ("onNotificationProcessing throw an exception" ));
370- List < PostedNotification > postedNotifs = ShadowRoboNotificationManager .notifications ;
387+ Map < Integer , PostedNotification > postedNotifs = ShadowRoboNotificationManager .notifications ;
371388 Assert .assertEquals (3 , postedNotifs .size ());
372389 }
373390
0 commit comments