Skip to content

Commit dfd8111

Browse files
committed
Fix failing test shouldHandleBasicNotifications
1 parent 3c87251 commit dfd8111

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public synchronized void complete(@Nullable final OSNotification notification) {
7777

7878
isComplete = true;
7979

80-
if (OSUtils.isRunningOnMainThread()) {
80+
if (isRunningOnMainThread()) {
8181
new Thread(new Runnable() {
8282
@Override
8383
public void run() {
@@ -119,4 +119,8 @@ public String toString() {
119119
", notification=" + notification +
120120
'}';
121121
}
122+
123+
static boolean isRunningOnMainThread() {
124+
return OSUtils.isRunningOnMainThread();
125+
}
122126
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,9 @@ public static void provideUserConsent(boolean consent) {
986986
}
987987

988988
private static boolean reassignDelayedInitParams() {
989+
if (initDone)
990+
return false;
991+
989992
String delayedAppId;
990993
Context delayedContext;
991994
if (delayedInitParams == null) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.onesignal;
2+
3+
import org.robolectric.annotation.Implementation;
4+
import org.robolectric.annotation.Implements;
5+
6+
@Implements(OSNotificationReceivedEvent.class)
7+
public class ShadowNotificationReceivedEvent {
8+
9+
private static boolean runningOnMainThreadCheck = false;
10+
11+
@Implementation
12+
public static boolean isRunningOnMainThread() {
13+
// Remove Main thread check and throw
14+
runningOnMainThreadCheck = true;
15+
return false;
16+
}
17+
18+
public static boolean isRunningOnMainThreadCheckCalled() {
19+
return runningOnMainThreadCheck;
20+
}
21+
22+
public static void resetStatics() {
23+
runningOnMainThreadCheck = false;
24+
}
25+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import com.onesignal.ShadowFCMBroadcastReceiver;
6969
import com.onesignal.ShadowGenerateNotification;
7070
import com.onesignal.ShadowNotificationManagerCompat;
71+
import com.onesignal.ShadowNotificationReceivedEvent;
7172
import com.onesignal.ShadowOSUtils;
7273
import com.onesignal.ShadowOSViewUtils;
7374
import com.onesignal.ShadowOSWebView;
@@ -823,7 +824,7 @@ public void shouldUpdateNormalNotificationDisplayWhenReplacingANotification() th
823824
}
824825

825826
@Test
826-
@Config(shadows = { ShadowGenerateNotification.class })
827+
@Config(shadows = { ShadowGenerateNotification.class, ShadowNotificationReceivedEvent.class })
827828
public void shouldHandleBasicNotifications() throws Exception {
828829
// Make sure the notification got posted and the content is correct.
829830
Bundle bundle = getBaseNotifBundle();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.onesignal.ShadowHMSFusedLocationProviderClient;
3838
import com.onesignal.ShadowHmsInstanceId;
3939
import com.onesignal.ShadowNotificationManagerCompat;
40+
import com.onesignal.ShadowNotificationReceivedEvent;
4041
import com.onesignal.ShadowOSUtils;
4142
import com.onesignal.ShadowOSWebView;
4243
import com.onesignal.ShadowOneSignalDbHelper;
@@ -128,6 +129,7 @@ static void beforeTestInitAndCleanup() throws Exception {
128129
ShadowOSUtils.resetStatics();
129130
ShadowTimeoutHandler.resetStatics();
130131
ShadowGenerateNotification.resetStatics();
132+
ShadowNotificationReceivedEvent.resetStatics();
131133

132134
lastException = null;
133135
}

0 commit comments

Comments
 (0)