Skip to content

Commit d4458ef

Browse files
committed
Make ReceiveReceiptWorker run with no delay for unit tests
- unit tests fail when ReceiveReceiptWorker runs with a delay so make no delay for unit tests - a few unit tests related to sending receive receipts were failing due to "Main looper has queued unexecuted runnables. This might be the cause of the test failure." even after awaiting a long time (flaky pass/fail) - removed test `testNotificationReceivedNoSendReceivedRequest_Delay` because no longer testing the delay in unit tests and other receive receipt tests are sufficient
1 parent 8b36886 commit d4458ef

File tree

2 files changed

+8
-35
lines changed

2 files changed

+8
-35
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.content.Context;
44

5+
import org.robolectric.annotation.Implementation;
56
import org.robolectric.annotation.Implements;
67

78
@Implements(OSUtils.class)
@@ -93,4 +94,10 @@ public String getCarrierName() {
9394
int initializationChecker(Context context, String oneSignalAppId) {
9495
return subscribableStatus;
9596
}
96-
}
97+
98+
@Implementation
99+
public static int getRandomDelay(int minDelay, int maxDelay) {
100+
// unit tests fail when ReceiveReceiptWorker runs with a delay so make no delay for unit tests
101+
return 0;
102+
}
103+
}

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

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,40 +1226,6 @@ public void testNotificationReceivedNoSendReceivedRequest_WhenNotificationNotDis
12261226
assertNotEquals("notifications/UUID/report_received", ShadowOneSignalRestClient.lastUrl);
12271227
}
12281228

1229-
@Test
1230-
@Config(shadows = { ShadowGenerateNotification.class })
1231-
public void testNotificationReceivedNoSendReceivedRequest_Delay() throws Exception {
1232-
int delay = 2;
1233-
MockDelayTaskController mockDelayTaskController = new MockDelayTaskController(new MockOSLog());
1234-
mockDelayTaskController.setMockedRandomValue(delay);
1235-
mockDelayTaskController.setRunOnSameThread(false);
1236-
OneSignal_setDelayTaskController(mockDelayTaskController);
1237-
1238-
ShadowOneSignalRestClient.setRemoteParamsReceiveReceiptsEnable(true);
1239-
// First init run for appId to be saved
1240-
// At least OneSignal was init once for user to be subscribed
1241-
// If this doesn't' happen, notifications will not arrive
1242-
OneSignal.setAppId(ONESIGNAL_APP_ID);
1243-
OneSignal.initWithContext(blankActivity);
1244-
threadAndTaskWait();
1245-
1246-
long calledTime = System.currentTimeMillis();
1247-
1248-
// 1. Receive a notification in background
1249-
FCMBroadcastReceiver_processBundle(blankActivity, getBaseNotifBundle());
1250-
threadAndTaskWait();
1251-
1252-
// 2. Check that report_received where sent
1253-
Awaitility.await()
1254-
.atMost(new Duration(3, TimeUnit.SECONDS))
1255-
.pollInterval(new Duration(1, TimeUnit.SECONDS))
1256-
.untilAsserted(() -> {
1257-
assertEquals(3, ShadowOneSignalRestClient.requests.size());
1258-
assertEquals("notifications/UUID/report_received", ShadowOneSignalRestClient.lastUrl);
1259-
assertTrue(System.currentTimeMillis() - calledTime >= delay * 1000);
1260-
});
1261-
}
1262-
12631229
/**
12641230
* @see #testNotificationReceivedNoSendReceivedRequest_WhenNotificationNotDisplayed
12651231
*/

0 commit comments

Comments
 (0)