Skip to content

Commit d80b60b

Browse files
committed
Don't have NetworkType.CONNECTED constraint for unit tests
- unit tests sending receive receipts were failing when the ReceiveReceiptWorker has constraint of `setRequiredNetworkType(NetworkType.CONNECTED)` - they were failing even though a check to `NetworkInfo.isConnected()` returns `true` - so, refactor `buildConstraints` out of the method `beginEnqueueingWork` so it can be shadowed to return no constraints when building the work request for unit tests
1 parent d4458ef commit d80b60b

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ void beginEnqueueingWork(Context context, String osNotificationId) {
7272
.putString(OS_NOTIFICATION_ID, osNotificationId)
7373
.build();
7474

75-
Constraints constraints = new Constraints.Builder()
76-
.setRequiredNetworkType(NetworkType.CONNECTED)
77-
.build();
75+
Constraints constraints = buildConstraints();
7876

7977
OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(ReceiveReceiptWorker.class)
8078
.setConstraints(constraints)
@@ -86,7 +84,12 @@ void beginEnqueueingWork(Context context, String osNotificationId) {
8684

8785
WorkManager.getInstance(context)
8886
.enqueueUniqueWork(osNotificationId + "_receive_receipt", ExistingWorkPolicy.KEEP, workRequest);
87+
}
8988

89+
Constraints buildConstraints() {
90+
return new Constraints.Builder()
91+
.setRequiredNetworkType(NetworkType.CONNECTED)
92+
.build();
9093
}
9194

9295
public static class ReceiveReceiptWorker extends Worker {
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package com.onesignal;
22

3+
import androidx.work.Constraints;
4+
35
import org.robolectric.annotation.Implementation;
46
import org.robolectric.annotation.Implements;
57

68
@Implements(OSReceiveReceiptController.class)
79
public class ShadowReceiveReceiptController {
810

11+
// Removes the constraint `setRequiredNetworkType(NetworkType.CONNECTED)` which was causing unit tests to fail
912
@Implementation
10-
public boolean isReceiveReceiptEnabled() {
11-
return true;
13+
public Constraints buildConstraints() {
14+
return Constraints.NONE;
1215
}
1316
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@
109109
import com.onesignal.ShadowOSWebView;
110110
import com.onesignal.ShadowOneSignal;
111111
import com.onesignal.ShadowOneSignalRestClient;
112+
import com.onesignal.ShadowReceiveReceiptController;
112113
import com.onesignal.ShadowResources;
113114
import com.onesignal.ShadowRoboNotificationManager;
114115
import com.onesignal.ShadowRoboNotificationManager.PostedNotification;
@@ -1360,7 +1361,7 @@ public void shouldShowInAppPreviewWhenOpeningPreviewNotification() throws Except
13601361
}
13611362

13621363
@Test
1363-
@Config(shadows = { ShadowGenerateNotification.class })
1364+
@Config(shadows = { ShadowGenerateNotification.class, ShadowReceiveReceiptController.class })
13641365
public void shouldSendReceivedReceiptWhenEnabled() throws Exception {
13651366
ShadowOneSignalRestClient.setRemoteParamsReceiveReceiptsEnable(true);
13661367

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
import com.onesignal.ShadowOneSignalRestClient;
8787
import com.onesignal.ShadowPushRegistratorADM;
8888
import com.onesignal.ShadowPushRegistratorFCM;
89+
import com.onesignal.ShadowReceiveReceiptController;
8990
import com.onesignal.ShadowRoboNotificationManager;
9091
import com.onesignal.StaticResetHelper;
9192
import com.onesignal.SyncJobService;
@@ -1109,7 +1110,7 @@ public void onBundleProcessed(OneSignalPackagePrivateHelper.ProcessedBundleResul
11091110
// Start Received Request tests (report_received)
11101111

11111112
@Test
1112-
@Config(shadows = { ShadowGenerateNotification.class })
1113+
@Config(shadows = { ShadowGenerateNotification.class, ShadowReceiveReceiptController.class })
11131114
public void testNotificationReceivedSendReceivedRequest_WhenAppInBackground() throws Exception {
11141115
// First init run for appId to be saved
11151116
// At least OneSignal was init once for user to be subscribed
@@ -1132,7 +1133,7 @@ public void testNotificationReceivedSendReceivedRequest_WhenAppInBackground() th
11321133
}
11331134

11341135
@Test
1135-
@Config(shadows = { ShadowGenerateNotification.class })
1136+
@Config(shadows = { ShadowGenerateNotification.class, ShadowReceiveReceiptController.class })
11361137
public void testNotificationReceivedSendReceivedRequest_WhenAppInForeground() throws Exception {
11371138
ShadowOneSignalRestClient.setRemoteParamsReceiveReceiptsEnable(true);
11381139
// First init run for appId to be saved
@@ -1152,7 +1153,7 @@ public void testNotificationReceivedSendReceivedRequest_WhenAppInForeground() th
11521153
}
11531154

11541155
@Test
1155-
@Config(shadows = { ShadowGenerateNotification.class })
1156+
@Config(shadows = { ShadowGenerateNotification.class, ShadowReceiveReceiptController.class })
11561157
public void testNotificationReceivedNoSendReceivedRequest_WhenDisabled() throws Exception {
11571158
ShadowOneSignalRestClient.setRemoteParamsReceiveReceiptsEnable(false);
11581159
// First init run for appId to be saved
@@ -1172,7 +1173,7 @@ public void testNotificationReceivedNoSendReceivedRequest_WhenDisabled() throws
11721173
}
11731174

11741175
@Test
1175-
@Config(shadows = { ShadowGenerateNotification.class })
1176+
@Config(shadows = { ShadowGenerateNotification.class, ShadowReceiveReceiptController.class })
11761177
public void testNotificationReceivedNoSendReceivedRequest_WhenNotificationNotDisplayed() throws Exception {
11771178
// 1. Setup correct notification extension service class
11781179
startRemoteNotificationReceivedHandlerService("com.test.onesignal.MainOneSignalClassRunner$" +
@@ -1199,7 +1200,7 @@ public void testNotificationReceivedNoSendReceivedRequest_WhenNotificationNotDis
11991200
}
12001201

12011202
@Test
1202-
@Config(sdk = 26, shadows = { ShadowGenerateNotification.class, ShadowOneSignalNotificationManager.class })
1203+
@Config(sdk = 26, shadows = { ShadowGenerateNotification.class, ShadowOneSignalNotificationManager.class, ShadowReceiveReceiptController.class })
12031204
public void testNotificationReceivedNoSendReceivedRequest_WhenNotificationNotDisplayed_DisabledByChannel() throws Exception {
12041205
// 1. Setup correct notification extension service class
12051206
startRemoteNotificationReceivedHandlerService("com.test.onesignal.MainOneSignalClassRunner$" +

0 commit comments

Comments
 (0)