Skip to content

Commit 317107c

Browse files
authored
Merge pull request #867 from OneSignal/fix_get_nested_tags
Fix Flaky Tests
2 parents f6c1fb7 + 0a73a2d commit 317107c

File tree

5 files changed

+49
-55
lines changed

5 files changed

+49
-55
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignalRestClient.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,23 @@ public static void put(final String url, final JSONObject jsonBody, final Respon
6666
public void run() {
6767
makeRequest(url, "PUT", jsonBody, responseHandler, TIMEOUT, null);
6868
}
69-
}).start();
69+
}, "OS_REST_ASYNC_PUT").start();
7070
}
7171

7272
public static void post(final String url, final JSONObject jsonBody, final ResponseHandler responseHandler) {
7373
new Thread(new Runnable() {
7474
public void run() {
7575
makeRequest(url, "POST", jsonBody, responseHandler, TIMEOUT, null);
7676
}
77-
}).start();
77+
}, "OS_REST_ASYNC_POST").start();
7878
}
7979

8080
public static void get(final String url, final ResponseHandler responseHandler, @NonNull final String cacheKey) {
8181
new Thread(new Runnable() {
8282
public void run() {
8383
makeRequest(url, null, null, responseHandler, GET_TIMEOUT, cacheKey);
8484
}
85-
}).start();
85+
}, "OS_REST_ASYNC_GET").start();
8686
}
8787

8888
public static void getSync(final String url, final ResponseHandler responseHandler, @NonNull String cacheKey) {
@@ -261,7 +261,7 @@ private static Thread callResponseHandlerOnSuccess(final ResponseHandler handler
261261
public void run() {
262262
handler.onSuccess(response);
263263
}
264-
});
264+
}, "OS_REST_SUCCESS_CALLBACK");
265265
thread.start();
266266

267267
return thread;
@@ -275,7 +275,7 @@ private static Thread callResponseHandlerOnFailure(final ResponseHandler handler
275275
public void run() {
276276
handler.onFailure(statusCode, response, throwable);
277277
}
278-
});
278+
}, "OS_REST_FAILURE_CALLBACK");
279279
thread.start();
280280

281281
return thread;

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: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
import org.json.JSONObject;
9090
import org.junit.After;
9191
import org.junit.AfterClass;
92+
import org.junit.Assert;
9293
import org.junit.Before;
9394
import org.junit.BeforeClass;
9495
import org.junit.Test;
@@ -110,6 +111,8 @@
110111
import java.util.Iterator;
111112
import java.util.List;
112113
import java.util.Map;
114+
import java.util.concurrent.ArrayBlockingQueue;
115+
import java.util.concurrent.BlockingQueue;
113116
import java.util.concurrent.atomic.AtomicBoolean;
114117
import java.util.regex.Pattern;
115118

@@ -3817,14 +3820,13 @@ public void sendsExternalIdOnEmailPlayers() throws Exception {
38173820

38183821
@Test
38193822
public void testGetTagsQueuesCallbacks() throws Exception {
3823+
final BlockingQueue<Boolean> queue = new ArrayBlockingQueue<>(2);
38203824

38213825
// Allows us to validate that both handlers get executed independently
38223826
class DebugGetTagsHandler implements OneSignal.GetTagsHandler {
3823-
boolean executed = false;
3824-
38253827
@Override
38263828
public void tagsAvailable(JSONObject tags) {
3827-
executed = true;
3829+
queue.offer(true);
38283830
}
38293831
}
38303832

@@ -3841,43 +3843,41 @@ public void tagsAvailable(JSONObject tags) {
38413843
OneSignal.getTags(second);
38423844
threadAndTaskWait();
38433845

3844-
assertTrue(first.executed);
3845-
assertTrue(second.executed);
3846+
assertTrue(queue.take());
3847+
assertTrue(queue.take());
38463848
}
38473849

38483850
@Test
38493851
public void testNestedGetTags() throws Exception {
3852+
final BlockingQueue<Boolean> queue = new ArrayBlockingQueue<>(2);
38503853

38513854
// Validates that nested getTags calls won't throw a ConcurrentModificationException
38523855
class DebugGetTagsHandler implements OneSignal.GetTagsHandler {
3853-
boolean executed = false;
3854-
38553856
@Override
38563857
public void tagsAvailable(JSONObject tags) {
38573858
OneSignal.getTags(new OneSignal.GetTagsHandler() {
38583859
@Override
38593860
public void tagsAvailable(JSONObject tags) {
3860-
executed = true;
3861+
queue.offer(true);
38613862
}
38623863
});
38633864
}
38643865
}
38653866

3866-
DebugGetTagsHandler first = new DebugGetTagsHandler();
3867-
DebugGetTagsHandler second = new DebugGetTagsHandler();
3868-
38693867
OneSignalInit();
38703868
threadAndTaskWait();
38713869

38723870
OneSignal.sendTag("test", "value");
38733871
threadAndTaskWait();
38743872

3873+
DebugGetTagsHandler first = new DebugGetTagsHandler();
3874+
DebugGetTagsHandler second = new DebugGetTagsHandler();
38753875
OneSignal.getTags(first);
38763876
OneSignal.getTags(second);
38773877
threadAndTaskWait();
38783878

3879-
assertTrue(first.executed);
3880-
assertTrue(second.executed);
3879+
assertTrue(queue.take());
3880+
assertTrue(queue.take());
38813881
}
38823882

38833883
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,6 @@ public void testUniqueOutcomeFailSavedOnDBResetSession() throws Exception {
348348
controller.sendUniqueOutcomeEvent(OUTCOME_NAME);
349349
controller.sendUniqueOutcomeEvent(OUTCOME_NAME);
350350
controller.sendUniqueOutcomeEvent(OUTCOME_NAME);
351-
352351
threadAndTaskWait();
353352

354353
new Thread(new Runnable() {
@@ -367,6 +366,7 @@ public void run() {
367366

368367
controller.sendUniqueOutcomeEvent(OUTCOME_NAME);
369368
controller.sendUniqueOutcomeEvent(OUTCOME_NAME);
369+
threadAndTaskWait();
370370

371371
new Thread(new Runnable() {
372372
@Override

0 commit comments

Comments
 (0)