|
| 1 | +package com.test.onesignal; |
| 2 | + |
| 3 | +import android.annotation.SuppressLint; |
| 4 | +import android.app.Activity; |
| 5 | + |
| 6 | +import com.onesignal.OneSignalPackagePrivateHelper.NotificationPayloadProcessorHMS; |
| 7 | +import com.onesignal.ShadowNotificationManagerCompat; |
| 8 | +import com.onesignal.ShadowOSUtils; |
| 9 | +import com.onesignal.ShadowRoboNotificationManager; |
| 10 | +import com.onesignal.StaticResetHelper; |
| 11 | +import com.onesignal.example.BlankActivity; |
| 12 | + |
| 13 | +import org.checkerframework.checker.nullness.qual.NonNull; |
| 14 | +import org.json.JSONException; |
| 15 | +import org.json.JSONObject; |
| 16 | +import org.junit.Before; |
| 17 | +import org.junit.BeforeClass; |
| 18 | +import org.junit.Test; |
| 19 | +import org.junit.runner.RunWith; |
| 20 | +import org.robolectric.Robolectric; |
| 21 | +import org.robolectric.RobolectricTestRunner; |
| 22 | +import org.robolectric.android.controller.ActivityController; |
| 23 | +import org.robolectric.annotation.Config; |
| 24 | +import org.robolectric.shadows.ShadowLog; |
| 25 | + |
| 26 | +import java.util.UUID; |
| 27 | + |
| 28 | +import static com.onesignal.OneSignalPackagePrivateHelper.OSNotificationFormatHelper.PAYLOAD_OS_NOTIFICATION_ID; |
| 29 | +import static com.onesignal.OneSignalPackagePrivateHelper.OSNotificationFormatHelper.PAYLOAD_OS_ROOT_CUSTOM; |
| 30 | +import static junit.framework.Assert.assertEquals; |
| 31 | + |
| 32 | +@Config( |
| 33 | + // NOTE: We can remove "instrumentedPackages" if we make ShadowRoboNotificationManager's constructor public |
| 34 | + instrumentedPackages = { "com.onesignal" }, |
| 35 | + packageName = "com.onesignal.example", |
| 36 | + shadows = { |
| 37 | + ShadowRoboNotificationManager.class, |
| 38 | + ShadowNotificationManagerCompat.class |
| 39 | + }, |
| 40 | + sdk = 26 |
| 41 | +) |
| 42 | +@RunWith(RobolectricTestRunner.class) |
| 43 | +public class HMSDataMessageReceivedIntegrationTestsRunner { |
| 44 | + @SuppressLint("StaticFieldLeak") |
| 45 | + private static Activity blankActivity; |
| 46 | + private static ActivityController<BlankActivity> blankActivityController; |
| 47 | + |
| 48 | + private static final String ALERT_TEST_MESSAGE_BODY = "Test Message body"; |
| 49 | + |
| 50 | + @BeforeClass // Runs only once, before any tests |
| 51 | + public static void setUpClass() throws Exception { |
| 52 | + ShadowLog.stream = System.out; |
| 53 | + TestHelpers.beforeTestSuite(); |
| 54 | + StaticResetHelper.saveStaticValues(); |
| 55 | + } |
| 56 | + |
| 57 | + @Before |
| 58 | + public void beforeEachTest() throws Exception { |
| 59 | + TestHelpers.beforeTestInitAndCleanup(); |
| 60 | + |
| 61 | + ShadowOSUtils.supportsHMS(true); |
| 62 | + |
| 63 | + blankActivityController = Robolectric.buildActivity(BlankActivity.class).create(); |
| 64 | + blankActivity = blankActivityController.get(); |
| 65 | + } |
| 66 | + |
| 67 | + private static @NonNull String helperBasicOSPayload() throws JSONException { |
| 68 | + return new JSONObject() {{ |
| 69 | + put(PAYLOAD_OS_ROOT_CUSTOM, new JSONObject() {{ |
| 70 | + put(PAYLOAD_OS_NOTIFICATION_ID, UUID.randomUUID().toString()); |
| 71 | + }}); |
| 72 | + put("alert", ALERT_TEST_MESSAGE_BODY); |
| 73 | + }}.toString(); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void nullData_shouldNotThrow() { |
| 78 | + NotificationPayloadProcessorHMS.processDataMessageReceived(blankActivity, null); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void blankData_shouldNotThrow() { |
| 83 | + NotificationPayloadProcessorHMS.processDataMessageReceived(blankActivity, ""); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void basicPayload_shouldDisplayNotification() throws JSONException { |
| 88 | + blankActivityController.pause(); |
| 89 | + NotificationPayloadProcessorHMS.processDataMessageReceived(blankActivity, helperBasicOSPayload()); |
| 90 | + assertEquals(ALERT_TEST_MESSAGE_BODY, ShadowRoboNotificationManager.getLastShadowNotif().getBigText()); |
| 91 | + } |
| 92 | + |
| 93 | + // NOTE: More tests can be added but they would be duplicated with GenerateNotificationRunner |
| 94 | + // In 4.0.0 or later these should be written in a reusable way between HMS, FCM, and ADM |
| 95 | +} |
0 commit comments