Skip to content

Commit

Permalink
Don't have NetworkType.CONNECTED constraint for unit tests
Browse files Browse the repository at this point in the history
- 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, shadow the method `beginEnqueueingWork` to not use that constraint when building the work request
  • Loading branch information
nan-li committed Nov 13, 2021
1 parent d4458ef commit 552f467
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@
package com.onesignal;

import android.content.Context;

import androidx.work.Data;
import androidx.work.ExistingWorkPolicy;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkManager;

import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;

@Implements(OSReceiveReceiptController.class)
public class ShadowReceiveReceiptController {

boolean isReceiveReceiptEnabled() {
return OneSignalPrefs.getBool(
OneSignalPrefs.PREFS_ONESIGNAL,
OneSignalPrefs.PREFS_OS_RECEIVE_RECEIPTS_ENABLED,
false
);
}

// Removes the constraint `setRequiredNetworkType(NetworkType.CONNECTED)` which was causing unit tests to fail
@Implementation
public boolean isReceiveReceiptEnabled() {
return true;
public void beginEnqueueingWork(Context context, String osNotificationId) {
if (!isReceiveReceiptEnabled()) {
OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "sendReceiveReceipt disabled");
return;
}

Data inputData = new Data.Builder()
.putString("os_notification_id", osNotificationId)
.build();

OneTimeWorkRequest workRequest = new OneTimeWorkRequest.Builder(OSReceiveReceiptController.ReceiveReceiptWorker.class)
.setInputData(inputData)
.build();

OneSignal.Log(OneSignal.LOG_LEVEL.DEBUG, "OSReceiveReceiptController enqueueing send receive receipt work with notificationId: " + osNotificationId + " and delay: 0 seconds");

WorkManager.getInstance(context)
.enqueueUniqueWork(osNotificationId + "_receive_receipt", ExistingWorkPolicy.KEEP, workRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
import com.onesignal.ShadowOSWebView;
import com.onesignal.ShadowOneSignal;
import com.onesignal.ShadowOneSignalRestClient;
import com.onesignal.ShadowReceiveReceiptController;
import com.onesignal.ShadowResources;
import com.onesignal.ShadowRoboNotificationManager;
import com.onesignal.ShadowRoboNotificationManager.PostedNotification;
Expand Down Expand Up @@ -1360,7 +1361,7 @@ public void shouldShowInAppPreviewWhenOpeningPreviewNotification() throws Except
}

@Test
@Config(shadows = { ShadowGenerateNotification.class })
@Config(shadows = { ShadowGenerateNotification.class, ShadowReceiveReceiptController.class })
public void shouldSendReceivedReceiptWhenEnabled() throws Exception {
ShadowOneSignalRestClient.setRemoteParamsReceiveReceiptsEnable(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
import com.onesignal.ShadowOneSignalRestClient;
import com.onesignal.ShadowPushRegistratorADM;
import com.onesignal.ShadowPushRegistratorFCM;
import com.onesignal.ShadowReceiveReceiptController;
import com.onesignal.ShadowRoboNotificationManager;
import com.onesignal.StaticResetHelper;
import com.onesignal.SyncJobService;
Expand Down Expand Up @@ -1109,7 +1110,7 @@ public void onBundleProcessed(OneSignalPackagePrivateHelper.ProcessedBundleResul
// Start Received Request tests (report_received)

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

@Test
@Config(shadows = { ShadowGenerateNotification.class })
@Config(shadows = { ShadowGenerateNotification.class, ShadowReceiveReceiptController.class })
public void testNotificationReceivedSendReceivedRequest_WhenAppInForeground() throws Exception {
ShadowOneSignalRestClient.setRemoteParamsReceiveReceiptsEnable(true);
// First init run for appId to be saved
Expand All @@ -1152,7 +1153,7 @@ public void testNotificationReceivedSendReceivedRequest_WhenAppInForeground() th
}

@Test
@Config(shadows = { ShadowGenerateNotification.class })
@Config(shadows = { ShadowGenerateNotification.class, ShadowReceiveReceiptController.class })
public void testNotificationReceivedNoSendReceivedRequest_WhenDisabled() throws Exception {
ShadowOneSignalRestClient.setRemoteParamsReceiveReceiptsEnable(false);
// First init run for appId to be saved
Expand All @@ -1172,7 +1173,7 @@ public void testNotificationReceivedNoSendReceivedRequest_WhenDisabled() throws
}

@Test
@Config(shadows = { ShadowGenerateNotification.class })
@Config(shadows = { ShadowGenerateNotification.class, ShadowReceiveReceiptController.class })
public void testNotificationReceivedNoSendReceivedRequest_WhenNotificationNotDisplayed() throws Exception {
// 1. Setup correct notification extension service class
startRemoteNotificationReceivedHandlerService("com.test.onesignal.MainOneSignalClassRunner$" +
Expand All @@ -1199,7 +1200,7 @@ public void testNotificationReceivedNoSendReceivedRequest_WhenNotificationNotDis
}

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

0 comments on commit 552f467

Please sign in to comment.