Skip to content

Commit b5b1792

Browse files
committed
Add callback support to tests
1 parent 47ae123 commit b5b1792

File tree

4 files changed

+115
-16
lines changed

4 files changed

+115
-16
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ void removeTriggersForKeys(Collection<String> keys) { }
6262
@Override
6363
void setInAppMessagingEnabled(boolean enabled) { }
6464

65+
@Override
66+
void cleanCachedInAppMessages() {
67+
}
68+
6569
@Nullable
6670
@Override
6771
Object getTriggerValue(String key) { return null; }

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

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import android.content.Context;
55
import android.content.Intent;
66
import android.os.Bundle;
7-
import android.os.Looper;
7+
88
import androidx.annotation.NonNull;
99
import androidx.annotation.Nullable;
1010

@@ -138,9 +138,18 @@ public static void NotificationBundleProcessor_ProcessFromFCMIntentService_NoWra
138138
NotificationBundleProcessor.processFromFCMIntentService(context, bundle);
139139
}
140140

141-
public static boolean FCMBroadcastReceiver_processBundle(Context context, Bundle bundle) {
142-
NotificationBundleProcessor.ProcessedBundleResult processedResult = NotificationBundleProcessor.processBundleFromReceiver(context, bundle);
143-
return processedResult.processed();
141+
public static void FCMBroadcastReceiver_processBundle(Context context, Bundle bundle) {
142+
OneSignalPackagePrivateHelper.ProcessBundleReceiverCallback bundleReceiverCallback = new OneSignalPackagePrivateHelper.ProcessBundleReceiverCallback() {
143+
@Override
144+
public void onBundleProcessed(@Nullable OneSignalPackagePrivateHelper.ProcessedBundleResult processedResult) {
145+
}
146+
};
147+
148+
FCMBroadcastReceiver_processBundle(context, bundle, bundleReceiverCallback);
149+
}
150+
151+
public static void FCMBroadcastReceiver_processBundle(Context context, Bundle bundle, OneSignalPackagePrivateHelper.ProcessBundleReceiverCallback bundleReceiverCallback) {
152+
NotificationBundleProcessor.processBundleFromReceiver(context, bundle, bundleReceiverCallback);
144153
}
145154

146155
public static void FCMBroadcastReceiver_onReceived_withIntent(Context context, Intent intent) {
@@ -168,6 +177,30 @@ public static int NotificationBundleProcessor_Process(Context context, boolean r
168177
return NotificationBundleProcessor.processJobForDisplay(notificationJob, true);
169178
}
170179

180+
public static class ProcessBundleReceiverCallback implements com.onesignal.NotificationBundleProcessor.ProcessBundleReceiverCallback {
181+
182+
@Override
183+
public void onBundleProcessed(@Nullable com.onesignal.NotificationBundleProcessor.ProcessedBundleResult processedResult) {
184+
onBundleProcessed(new ProcessedBundleResult(processedResult));
185+
}
186+
187+
public void onBundleProcessed(@Nullable ProcessedBundleResult processedResult) {
188+
189+
}
190+
}
191+
192+
public static class ProcessedBundleResult extends NotificationBundleProcessor.ProcessedBundleResult {
193+
com.onesignal.NotificationBundleProcessor.ProcessedBundleResult processedResult;
194+
195+
public ProcessedBundleResult(com.onesignal.NotificationBundleProcessor.ProcessedBundleResult processedResult) {
196+
this.processedResult = processedResult;
197+
}
198+
199+
public boolean isProcessed() {
200+
return processedResult.processed();
201+
}
202+
}
203+
171204
public static class NotificationTable extends OneSignalDbContract.NotificationTable {
172205
}
173206

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

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -761,17 +761,19 @@ public void shouldSetBadgesWhenRestoringNotifications() throws Exception {
761761
}
762762

763763
@Test
764-
public void shouldNotShowNotificationWhenAlertIsBlankOrNull() {
764+
public void shouldNotShowNotificationWhenAlertIsBlankOrNull() throws Exception {
765765
Bundle bundle = getBaseNotifBundle();
766766
bundle.remove("alert");
767767
NotificationBundleProcessor_ProcessFromFCMIntentService(blankActivity, bundle);
768-
768+
threadAndTaskWait();
769+
769770
assertNoNotifications();
770771

771772
bundle = getBaseNotifBundle("UUID2");
772773
bundle.putString("alert", "");
773774
NotificationBundleProcessor_ProcessFromFCMIntentService(blankActivity, bundle);
774-
775+
threadAndTaskWait();
776+
775777
assertNoNotifications();
776778

777779
assertNotificationDbRecords(2);
@@ -790,11 +792,13 @@ public void shouldUpdateNormalNotificationDisplayWhenReplacingANotification() th
790792
bundle.putString("grp", "test1");
791793
bundle.putString("collapse_key", "1");
792794
NotificationBundleProcessor_ProcessFromFCMIntentService(blankActivity, bundle);
793-
795+
threadAndTaskWait();
796+
794797
bundle = getBaseNotifBundle("UUID2");
795798
bundle.putString("grp", "test1");
796799
bundle.putString("collapse_key", "1");
797800
NotificationBundleProcessor_ProcessFromFCMIntentService(blankActivity, bundle);
801+
threadAndTaskWait();
798802

799803
// Test - Summary created and sub notification. Summary will look the same as the normal notification.
800804
Map<Integer, PostedNotification> postedNotifs = ShadowRoboNotificationManager.notifications;
@@ -829,6 +833,8 @@ public void shouldHandleBasicNotifications() throws Exception {
829833
// Make sure the notification got posted and the content is correct.
830834
Bundle bundle = getBaseNotifBundle();
831835
NotificationBundleProcessor_ProcessFromFCMIntentService(blankActivity, bundle);
836+
threadAndTaskWait();
837+
832838
assertEquals(notifMessage, ShadowRoboNotificationManager.getLastShadowNotif().getContentText());
833839
assertEquals(1, ShadowBadgeCountUpdater.lastCount);
834840

@@ -852,6 +858,8 @@ public void shouldHandleBasicNotifications() throws Exception {
852858

853859
// Should not display a duplicate notification, count should still be 1
854860
NotificationBundleProcessor_ProcessFromFCMIntentService(blankActivity, bundle);
861+
threadAndTaskWait();
862+
855863
readableDb = dbHelper.getSQLiteDatabaseWithRetries();
856864
cursor = readableDb.query(NotificationTable.TABLE_NAME, null, null, null, null, null, null);
857865
assertEquals(1, cursor.getCount());
@@ -861,6 +869,7 @@ public void shouldHandleBasicNotifications() throws Exception {
861869
// Display a second notification
862870
bundle = getBaseNotifBundle("UUID2");
863871
NotificationBundleProcessor_ProcessFromFCMIntentService(blankActivity, bundle);
872+
threadAndTaskWait();
864873

865874
// Go forward 4 weeks
866875
// Note: Does not effect the SQL function strftime
@@ -876,6 +885,8 @@ public void shouldHandleBasicNotifications() throws Exception {
876885
// First opened should of been cleaned up, 1 week old non opened notification should stay, and one new record.
877886
bundle = getBaseNotifBundle("UUID3");
878887
NotificationBundleProcessor_ProcessFromFCMIntentService(blankActivity, bundle);
888+
threadAndTaskWait();
889+
879890
readableDb = dbHelper.getSQLiteDatabaseWithRetries();
880891
cursor = readableDb.query(NotificationTable.TABLE_NAME, new String[] { }, null, null, null, null, null);
881892

@@ -1317,11 +1328,13 @@ private void setupNotificationExtensionServiceOverridePropertiesWithSummary() th
13171328
// 3. Post 2 notifications with the same grp key so a summary is generated
13181329
Bundle bundle = getBaseNotifBundle("UUID1");
13191330
bundle.putString("grp", "test1");
1331+
13201332
FCMBroadcastReceiver_processBundle(blankActivity, bundle);
13211333
threadAndTaskWait();
13221334

13231335
bundle = getBaseNotifBundle("UUID2");
13241336
bundle.putString("grp", "test1");
1337+
13251338
FCMBroadcastReceiver_processBundle(blankActivity, bundle);
13261339
threadAndTaskWait();
13271340

@@ -1389,10 +1402,23 @@ public void testRemoteNotificationReceivedHandler_notificationProcessingProperti
13891402
OneSignal.initWithContext(ApplicationProvider.getApplicationContext());
13901403
OneSignal_setupNotificationServiceExtension();
13911404

1392-
// 3. Test that WorkManager begins processing the notification
1393-
boolean ret = FCMBroadcastReceiver_processBundle(blankActivity, getBaseNotifBundle());
1394-
threadAndTaskWait();
1395-
assertTrue(ret);
1405+
final boolean[] callbackEnded = {false};
1406+
OneSignalPackagePrivateHelper.ProcessBundleReceiverCallback processBundleReceiverCallback = new OneSignalPackagePrivateHelper.ProcessBundleReceiverCallback() {
1407+
public void onBundleProcessed(OneSignalPackagePrivateHelper.ProcessedBundleResult processedResult) {
1408+
assertNotNull(processedResult);
1409+
// 3. Test that WorkManager begins processing the notification
1410+
assertTrue(processedResult.isProcessed());
1411+
callbackEnded[0] = true;
1412+
}
1413+
};
1414+
1415+
FCMBroadcastReceiver_processBundle(blankActivity, getBaseNotifBundle(), processBundleReceiverCallback);
1416+
Awaitility.await()
1417+
.atMost(new Duration(3, TimeUnit.SECONDS))
1418+
.pollInterval(new Duration(100, TimeUnit.MILLISECONDS))
1419+
.untilAsserted(() -> {
1420+
assertTrue(callbackEnded[0]);
1421+
});
13961422

13971423
// 4. Receive a notification with all data fields used
13981424
FCMBroadcastReceiver_processBundle(blankActivity, getBundleWithAllOptionsSet());
@@ -2132,10 +2158,24 @@ public void testNotificationReceived_duplicatesInShortTime() throws Exception {
21322158
blankActivityController.resume();
21332159
threadAndTaskWait();
21342160

2161+
final boolean[] callbackEnded = {false};
2162+
OneSignalPackagePrivateHelper.ProcessBundleReceiverCallback processBundleReceiverCallback = new OneSignalPackagePrivateHelper.ProcessBundleReceiverCallback() {
2163+
public void onBundleProcessed(OneSignalPackagePrivateHelper.ProcessedBundleResult processedResult) {
2164+
assertNotNull(processedResult);
2165+
assertTrue(processedResult.isProcessed());
2166+
callbackEnded[0] = true;
2167+
}
2168+
};
2169+
21352170
Bundle bundle = getBaseNotifBundle();
2136-
boolean processResult = FCMBroadcastReceiver_processBundle(blankActivity, bundle);
2171+
FCMBroadcastReceiver_processBundle(blankActivity, bundle, processBundleReceiverCallback);
2172+
Awaitility.await()
2173+
.atMost(new Duration(3, TimeUnit.SECONDS))
2174+
.pollInterval(new Duration(100, TimeUnit.MILLISECONDS))
2175+
.untilAsserted(() -> {
2176+
assertTrue(callbackEnded[0]);
2177+
});
21372178

2138-
assertTrue(processResult);
21392179
assertNull(lastNotificationOpenedBody);
21402180

21412181
assertEquals("Robo test message", notificationReceivedBody);
@@ -2146,6 +2186,7 @@ public void testNotificationReceived_duplicatesInShortTime() throws Exception {
21462186
notificationReceivedBody = null;
21472187

21482188
FCMBroadcastReceiver_processBundle(blankActivity, bundle);
2189+
threadAndTaskWait();
21492190

21502191
assertNull(lastNotificationOpenedBody);
21512192
assertNull(notificationReceivedBody);
@@ -2156,6 +2197,7 @@ public void testNotificationReceived_duplicatesInShortTime() throws Exception {
21562197
// Test that only NotificationReceivedHandler fires
21572198
bundle = getBaseNotifBundle("UUID2");
21582199
FCMBroadcastReceiver_processBundle(blankActivity, bundle);
2200+
threadAndTaskWait();
21592201

21602202
assertNull(lastNotificationOpenedBody);
21612203
assertEquals("Robo test message", notificationReceivedBody);

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import android.net.ConnectivityManager;
4040
import android.os.Bundle;
4141

42+
import androidx.annotation.Nullable;
4243
import androidx.test.core.app.ApplicationProvider;
4344

4445
import com.onesignal.FocusDelaySyncJobService;
@@ -92,6 +93,8 @@
9293
import com.onesignal.example.MainActivity;
9394
import com.onesignal.influence.data.OSTrackerFactory;
9495

96+
import org.awaitility.Awaitility;
97+
import org.awaitility.Duration;
9598
import org.json.JSONArray;
9699
import org.json.JSONException;
97100
import org.json.JSONObject;
@@ -119,6 +122,7 @@
119122
import java.util.TimeZone;
120123
import java.util.concurrent.ArrayBlockingQueue;
121124
import java.util.concurrent.BlockingQueue;
125+
import java.util.concurrent.TimeUnit;
122126
import java.util.concurrent.atomic.AtomicBoolean;
123127
import java.util.regex.Pattern;
124128

@@ -1199,9 +1203,23 @@ public void testNotificationReceivedWhenAppInFocus() throws Exception {
11991203
threadAndTaskWait();
12001204

12011205
Bundle bundle = getBaseNotifBundle();
1202-
boolean processResult = FCMBroadcastReceiver_processBundle(blankActivity, bundle);
12031206

1204-
assertTrue(processResult);
1207+
final boolean[] callbackEnded = {false};
1208+
OneSignalPackagePrivateHelper.ProcessBundleReceiverCallback processBundleReceiverCallback = new OneSignalPackagePrivateHelper.ProcessBundleReceiverCallback() {
1209+
public void onBundleProcessed(OneSignalPackagePrivateHelper.ProcessedBundleResult processedResult) {
1210+
assertNotNull(processedResult);
1211+
assertTrue(processedResult.isProcessed());
1212+
callbackEnded[0] = true;
1213+
}
1214+
};
1215+
1216+
FCMBroadcastReceiver_processBundle(blankActivity, bundle, processBundleReceiverCallback);
1217+
Awaitility.await()
1218+
.atMost(new Duration(3, TimeUnit.SECONDS))
1219+
.pollInterval(new Duration(100, TimeUnit.MILLISECONDS))
1220+
.untilAsserted(() -> {
1221+
assertTrue(callbackEnded[0]);
1222+
});
12051223
assertNull(lastNotificationOpenedBody);
12061224

12071225
assertEquals("Robo test message", notificationReceivedBody);
@@ -1212,6 +1230,7 @@ public void testNotificationReceivedWhenAppInFocus() throws Exception {
12121230
notificationReceivedBody = null;
12131231

12141232
FCMBroadcastReceiver_processBundle(blankActivity, bundle);
1233+
threadAndTaskWait();
12151234

12161235
assertNull(lastNotificationOpenedBody);
12171236
assertNull(notificationReceivedBody);
@@ -1222,6 +1241,7 @@ public void testNotificationReceivedWhenAppInFocus() throws Exception {
12221241
// Test that only NotificationReceivedHandler fires
12231242
bundle = getBaseNotifBundle("UUID2");
12241243
FCMBroadcastReceiver_processBundle(blankActivity, bundle);
1244+
threadAndTaskWait();
12251245

12261246
assertNull(lastNotificationOpenedBody);
12271247
assertEquals("Robo test message", notificationReceivedBody);

0 commit comments

Comments
 (0)