Skip to content

Commit a0febec

Browse files
committed
Fixed flaky group notification tests
* Cleaned up tags tests using offer instead of put
1 parent b9f64f0 commit a0febec

File tree

3 files changed

+32
-46
lines changed

3 files changed

+32
-46
lines changed

OneSignalSDK/unittest/src/test/java/com/onesignal/ShadowRoboNotificationManager.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@
3131
import android.app.Notification;
3232
import android.app.NotificationChannelGroup;
3333
import android.app.NotificationManager;
34+
import android.support.annotation.NonNull;
35+
import android.support.v4.app.NotificationCompat;
3436

3537
import org.robolectric.annotation.Implements;
3638
import org.robolectric.shadows.ShadowNotification;
3739
import org.robolectric.shadows.ShadowNotificationManager;
3840

41+
import java.util.ArrayList;
3942
import java.util.LinkedHashMap;
43+
import java.util.List;
4044

4145
import static org.robolectric.Shadows.shadowOf;
4246

@@ -69,6 +73,11 @@ public static ShadowNotification getLastShadowNotif() {
6973
public static int lastNotifId;
7074

7175
public static LinkedHashMap<Integer, PostedNotification> notifications = new LinkedHashMap<>();
76+
77+
private static ShadowRoboNotificationManager mInstance;
78+
ShadowRoboNotificationManager() {
79+
mInstance = this;
80+
}
7281

7382
@Override
7483
public void cancelAll() {
@@ -96,6 +105,18 @@ public void notify(String tag, int id, Notification notification) {
96105
super.notify(tag, id, notification);
97106
}
98107

108+
public static @NonNull List<Notification> getNotificationsInGroup(@NonNull String group) {
109+
List<Notification> notifications = new ArrayList<>();
110+
for (Notification notification : mInstance.getAllNotifications()) {
111+
if (NotificationCompat.isGroupSummary(notification))
112+
continue;
113+
if (!group.equals(notification.getGroup()))
114+
continue;
115+
notifications.add(notification);
116+
}
117+
return notifications;
118+
}
119+
99120
public static NotificationChannel lastChannel;
100121
public void createNotificationChannel(NotificationChannel channel) {
101122
lastChannel = channel;

OneSignalSDK/unittest/src/test/java/com/test/onesignal/GenerateNotificationRunner.java

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@
9494
import org.robolectric.annotation.Config;
9595
import org.robolectric.shadows.ShadowAlertDialog;
9696
import org.robolectric.shadows.ShadowLog;
97-
import org.robolectric.shadows.ShadowSystemClock;
9897

9998
import java.lang.reflect.Field;
10099
import java.math.BigInteger;
@@ -107,6 +106,7 @@
107106
import static com.onesignal.OneSignalPackagePrivateHelper.NotificationOpenedProcessor_processFromContext;
108107
import static com.onesignal.OneSignalPackagePrivateHelper.NotificationSummaryManager_updateSummaryNotificationAfterChildRemoved;
109108
import static com.onesignal.OneSignalPackagePrivateHelper.createInternalPayloadBundle;
109+
import static com.onesignal.ShadowRoboNotificationManager.getNotificationsInGroup;
110110
import static com.test.onesignal.RestClientAsserts.assertReportReceivedAtIndex;
111111
import static com.test.onesignal.RestClientAsserts.assertRestCalls;
112112
import static com.test.onesignal.TestHelpers.advanceSystemTimeBy;
@@ -327,51 +327,26 @@ public void shouldCancelAllNotificationsPartOfAGroup() throws Exception {
327327

328328
@Test
329329
@Config(sdk = Build.VERSION_CODES.N)
330-
public void testGetMostRecentNotifIdFromGroup() throws Exception {
331-
OneSignal.setInFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification);
332-
OneSignal.init(blankActivity, "123456789", "b2f7f966-d8cc-11e4-bed1-df8f05be55ba");
330+
public void testFourNotificationsUseProvidedGroup() throws Exception {
331+
OneSignal.init(blankActivity.getApplicationContext(), "123456789", "b2f7f966-d8cc-11e4-bed1-df8f05be55ba");
333332
threadAndTaskWait();
334333

335334
// Add 4 grouped notifications
336335
postNotificationWithOptionalGroup(4, "test1");
337336

338-
SQLiteDatabase readableDb = OneSignalDbHelper.getInstance(blankActivity).getReadableDatabase();
339-
// Grab the most recent timestamped notification from local DB
340-
Integer mostRecentId = OneSignalNotificationManagerPackageHelper.getMostRecentNotifIdFromGroup(readableDb, "test1", false);
341-
342-
Map<Integer, PostedNotification> postedNotifs = ShadowRoboNotificationManager.notifications;
343-
// Iterator is ordered by notification post (0 index is most recent)
344-
Iterator<Map.Entry<Integer, PostedNotification>> postedNotifsIterator = postedNotifs.entrySet().iterator();
345-
// First notification is the summary so we skip it and move on to the first normal notif
346-
postedNotifsIterator.next();
347-
Integer expectedId = postedNotifsIterator.next().getKey();
348-
349-
// Assert our id equals the first real notif id (most recent posted)
350-
assertEquals(expectedId, mostRecentId);
337+
assertEquals(4, getNotificationsInGroup("test1").size());
351338
}
352339

353340
@Test
354341
@Config(sdk = Build.VERSION_CODES.N)
355-
public void testGetMostRecentNotifIdFromGroupless() throws Exception {
356-
OneSignal.setInFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification);
357-
OneSignal.init(blankActivity, "123456789", "b2f7f966-d8cc-11e4-bed1-df8f05be55ba");
342+
public void testFourGrouplessNotificationsUseDefaultGroup() throws Exception {
343+
OneSignal.init(blankActivity.getApplicationContext(), "123456789", "b2f7f966-d8cc-11e4-bed1-df8f05be55ba");
358344
threadAndTaskWait();
359345

360346
// Add 4 groupless notifications
361347
postNotificationWithOptionalGroup(4, null);
362348

363-
SQLiteDatabase readableDb = OneSignalDbHelper.getInstance(blankActivity).getReadableDatabase();
364-
// Grab the most recent timestamped notification from local DB
365-
Integer mostRecentId = OneSignalNotificationManagerPackageHelper.getMostRecentNotifIdFromGroup(readableDb, null, true);
366-
367-
Map<Integer, PostedNotification> postedNotifs = ShadowRoboNotificationManager.notifications;
368-
// Iterator is ordered by notification post (0 index is most recent)
369-
Iterator<Map.Entry<Integer, PostedNotification>> postedNotifsIterator = postedNotifs.entrySet().iterator();
370-
// Grab first active notification since groupless won't have a summary
371-
Integer expectedId = postedNotifsIterator.next().getKey();
372-
373-
// Assert our id equals the first real notif id (most recent posted)
374-
assertEquals(expectedId, mostRecentId);
349+
assertEquals(4, getNotificationsInGroup("os_group_undefined").size());
375350
}
376351

377352
@Test
@@ -526,13 +501,11 @@ public void testGrouplessSummaryKeyReassignmentAtFourOrMoreNotification() throws
526501
assertEquals(0, groupCount);
527502
}
528503

529-
public Bundle postNotificationWithOptionalGroup(int notifCount, String group) {
504+
private @Nullable Bundle postNotificationWithOptionalGroup(int notifCount, @Nullable String group) {
530505
Bundle bundle = null;
531506
for (int i = 0; i < notifCount; i++) {
532507
bundle = getBaseNotifBundle("UUID" + i);
533-
534-
if (group != null)
535-
bundle.putString("grp", group);
508+
bundle.putString("grp", group);
536509

537510
NotificationBundleProcessor_ProcessFromGCMIntentService(blankActivity, bundle, null);
538511
}

OneSignalSDK/unittest/src/test/java/com/test/onesignal/MainOneSignalClassRunner.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3826,11 +3826,7 @@ public void testGetTagsQueuesCallbacks() throws Exception {
38263826
class DebugGetTagsHandler implements OneSignal.GetTagsHandler {
38273827
@Override
38283828
public void tagsAvailable(JSONObject tags) {
3829-
try {
3830-
queue.put(true);
3831-
} catch (InterruptedException e) {
3832-
Assert.fail("Throw unexpected");
3833-
}
3829+
queue.offer(true);
38343830
}
38353831
}
38363832

@@ -3862,11 +3858,7 @@ public void tagsAvailable(JSONObject tags) {
38623858
OneSignal.getTags(new OneSignal.GetTagsHandler() {
38633859
@Override
38643860
public void tagsAvailable(JSONObject tags) {
3865-
try {
3866-
queue.put(true);
3867-
} catch (InterruptedException e) {
3868-
Assert.fail("Throw unexpected");
3869-
}
3861+
queue.offer(true);
38703862
}
38713863
});
38723864
}

0 commit comments

Comments
 (0)