-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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, shadow the method `beginEnqueueingWork` to not use that constraint when building the work request
- Loading branch information
Showing
3 changed files
with
42 additions
and
8 deletions.
There are no files selected for viewing
36 changes: 34 additions & 2 deletions
36
OneSignalSDK/unittest/src/test/java/com/onesignal/ShadowReceiveReceiptController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters