Search in sources :

Example 6 with Duration

use of org.awaitility.Duration in project OneSignal-Android-SDK by OneSignal.

the class InAppMessageIntegrationTests method testLiquidIAMDisplayWaitsForGetTags.

@Test
public void testLiquidIAMDisplayWaitsForGetTags() throws Exception {
    final OSTestInAppMessageInternal message = InAppMessagingHelpers.buildTestMessageWithSingleTriggerAndLiquid(OSTriggerKind.CUSTOM, "test_1", OSTestTrigger.OSTriggerOperator.EQUAL_TO.toString(), 2);
    setMockRegistrationResponseWithMessages(new ArrayList<OSTestInAppMessageInternal>() {

        {
            add(message);
        }
    });
    // Init OneSignal IAM with redisplay
    OneSignalInit();
    threadAndTaskWait();
    // Add trigger to make IAM display
    OneSignal.addTrigger("test_1", 2);
    assertEquals(1, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
    // Wait for both getTags and get IAM HTML to be called
    Awaitility.await().atMost(new Duration(150, TimeUnit.MILLISECONDS)).pollInterval(new Duration(10, TimeUnit.MILLISECONDS)).until(() -> ShadowOneSignalRestClient.requests.size() == 4);
    int requestSize = ShadowOneSignalRestClient.requests.size();
    ShadowOneSignalRestClient.Request getTagsRequest = ShadowOneSignalRestClient.requests.get(requestSize - 1);
    assertEquals("players/" + "df8f05be55ba-b2f7f966-d8cc-11e4-bed1" + "?app_id=" + "b2f7f966-d8cc-11e4-bed1-df8f05be55ba", getTagsRequest.url);
    // Runnable for get tags is run
    threadAndTaskWait();
    // Runnable for webView is run from background thread to main thread
    threadAndTaskWait();
    ShadowOneSignalRestClient.Request lastRequest = ShadowOneSignalRestClient.requests.get(ShadowOneSignalRestClient.requests.size() - 1);
    while (!lastRequest.url.equals("in_app_messages/" + message.getMessageId() + "/impression")) {
        // Check impression request by waiting until animationEnd
        threadAndTaskWait();
        lastRequest = ShadowOneSignalRestClient.requests.get(ShadowOneSignalRestClient.requests.size() - 1);
    }
    assertEquals("in_app_messages/" + message.getMessageId() + "/impression", lastRequest.url);
}
Also used : ShadowOneSignalRestClient(com.onesignal.ShadowOneSignalRestClient) OSTestInAppMessageInternal(com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal) Duration(org.awaitility.Duration) SuppressLint(android.annotation.SuppressLint) Test(org.junit.Test)

Example 7 with Duration

use of org.awaitility.Duration in project OneSignal-Android-SDK by OneSignal.

the class InAppMessageIntegrationTests method testInAppMessageDisplayMultipleTimes_sessionDurationTrigger.

@Test
public void testInAppMessageDisplayMultipleTimes_sessionDurationTrigger() throws Exception {
    final OSTestInAppMessageInternal message = InAppMessagingHelpers.buildTestMessageWithSingleTriggerAndRedisplay(OSTriggerKind.SESSION_TIME, "", OSTestTrigger.OSTriggerOperator.GREATER_THAN.toString(), 0.05, LIMIT, DELAY);
    setMockRegistrationResponseWithMessages(new ArrayList<OSTestInAppMessageInternal>() {

        {
            add(message);
        }
    });
    // Init OneSignal IAM with redisplay
    OneSignalInit();
    threadAndTaskWait();
    // No schedule should happen, IAM should evaluate to true
    Awaitility.await().atMost(new Duration(1_000, TimeUnit.MILLISECONDS)).pollInterval(new Duration(10, TimeUnit.MILLISECONDS)).untilAsserted(new ThrowingRunnable() {

        @Override
        public void run() throws Exception {
            assertEquals(1, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
        }
    });
    // After IAM is added to display queue we now need to wait until it is shown
    Awaitility.await().atMost(new Duration(1_000, TimeUnit.MILLISECONDS)).pollInterval(new Duration(10, TimeUnit.MILLISECONDS)).untilAsserted(new ThrowingRunnable() {

        @Override
        public void run() throws Exception {
            assertTrue(OneSignalPackagePrivateHelper.isInAppMessageShowing());
        }
    });
    // Dismiss IAM will make display quantity increase and last display time to change
    OneSignalPackagePrivateHelper.dismissCurrentMessage();
    // Check IAMs was removed from queue
    assertEquals(0, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
    // Check if data after dismiss is set correctly
    assertEquals(1, OneSignalPackagePrivateHelper.getRedisplayInAppMessages().size());
    assertEquals(1, OneSignalPackagePrivateHelper.getRedisplayInAppMessages().get(0).getRedisplayStats().getDisplayQuantity());
    long lastDisplayTime = OneSignalPackagePrivateHelper.getRedisplayInAppMessages().get(0).getRedisplayStats().getLastDisplayTime();
    assertTrue(lastDisplayTime > 0);
    // Change time for delay to be covered
    time.advanceSystemTimeBy(DELAY);
    fastColdRestartApp();
    setMockRegistrationResponseWithMessages(new ArrayList<OSTestInAppMessageInternal>() {

        {
            add(message);
        }
    });
    // Init OneSignal IAM with redisplay
    OneSignalInit();
    threadAndTaskWait();
    // No schedule should happen since session time period is very small, should evaluate to true on first run
    // Wait for redisplay logic
    Awaitility.await().atMost(new Duration(1_000, TimeUnit.MILLISECONDS)).pollInterval(new Duration(10, TimeUnit.MILLISECONDS)).untilAsserted(new ThrowingRunnable() {

        @Override
        public void run() {
            assertEquals(1, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
        }
    });
    OneSignalPackagePrivateHelper.dismissCurrentMessage();
    // Check IAMs was removed from queue
    assertEquals(0, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
    // Check if data after dismiss is set correctly
    assertEquals(1, OneSignalPackagePrivateHelper.getRedisplayInAppMessages().size());
    assertEquals(2, OneSignalPackagePrivateHelper.getRedisplayInAppMessages().get(0).getRedisplayStats().getDisplayQuantity());
    assertTrue(OneSignalPackagePrivateHelper.getRedisplayInAppMessages().get(0).getRedisplayStats().getLastDisplayTime() - lastDisplayTime >= DELAY);
}
Also used : OSTestInAppMessageInternal(com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal) Duration(org.awaitility.Duration) JSONException(org.json.JSONException) ThrowingRunnable(org.awaitility.core.ThrowingRunnable) Test(org.junit.Test)

Example 8 with Duration

use of org.awaitility.Duration in project OneSignal-Android-SDK by OneSignal.

the class GenerateNotificationRunner method testNotificationReceived_duplicatesInShortTime.

@Test
@Config(shadows = { ShadowGenerateNotification.class })
public void testNotificationReceived_duplicatesInShortTime() throws Exception {
    // 1. Init OneSignal
    OneSignal.setAppId(ONESIGNAL_APP_ID);
    OneSignal.initWithContext(blankActivity);
    OneSignal.setNotificationWillShowInForegroundHandler(notificationReceivedEvent -> {
        androidNotificationId = notificationReceivedEvent.getNotification().getAndroidNotificationId();
        notificationReceivedBody = notificationReceivedEvent.getNotification().getBody();
    // Not call complete to test duplicate arriving before notification processing is completed
    });
    OneSignal.setNotificationOpenedHandler(getNotificationOpenedHandler());
    // 2. Foreground the app
    blankActivityController.resume();
    threadAndTaskWait();
    final boolean[] callbackEnded = { false };
    OneSignalPackagePrivateHelper.ProcessBundleReceiverCallback processBundleReceiverCallback = new OneSignalPackagePrivateHelper.ProcessBundleReceiverCallback() {

        public void onBundleProcessed(OneSignalPackagePrivateHelper.ProcessedBundleResult processedResult) {
            assertNotNull(processedResult);
            assertTrue(processedResult.isProcessed());
            callbackEnded[0] = true;
        }
    };
    Bundle bundle = getBaseNotifBundle();
    FCMBroadcastReceiver_processBundle(blankActivity, bundle, processBundleReceiverCallback);
    Awaitility.await().atMost(new Duration(3, TimeUnit.SECONDS)).pollInterval(new Duration(100, TimeUnit.MILLISECONDS)).untilAsserted(() -> {
        assertTrue(callbackEnded[0]);
    });
    assertNull(lastNotificationOpenedBody);
    assertEquals("Robo test message", notificationReceivedBody);
    assertNotEquals(0, androidNotificationId);
    // Don't fire for duplicates
    lastNotificationOpenedBody = null;
    notificationReceivedBody = null;
    FCMBroadcastReceiver_processBundle(blankActivity, bundle);
    threadAndTaskWait();
    assertNull(lastNotificationOpenedBody);
    assertNull(notificationReceivedBody);
    lastNotificationOpenedBody = null;
    notificationReceivedBody = null;
    // Test that only NotificationReceivedHandler fires
    bundle = getBaseNotifBundle("UUID2");
    FCMBroadcastReceiver_processBundle(blankActivity, bundle);
    threadAndTaskWait();
    assertNull(lastNotificationOpenedBody);
    assertEquals("Robo test message", notificationReceivedBody);
}
Also used : Bundle(android.os.Bundle) FCMBroadcastReceiver_processBundle(com.onesignal.OneSignalPackagePrivateHelper.FCMBroadcastReceiver_processBundle) OneSignalPackagePrivateHelper.createInternalPayloadBundle(com.onesignal.OneSignalPackagePrivateHelper.createInternalPayloadBundle) Duration(org.awaitility.Duration) OneSignalPackagePrivateHelper(com.onesignal.OneSignalPackagePrivateHelper) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 9 with Duration

use of org.awaitility.Duration in project OneSignal-Android-SDK by OneSignal.

the class GenerateNotificationRunner method testNotificationProcessing_completeNotCalled.

@Test
@Config(shadows = { ShadowGenerateNotification.class, ShadowTimeoutHandler.class })
public void testNotificationProcessing_completeNotCalled() throws Exception {
    // 1. Setup correct notification extension service class
    startRemoteNotificationReceivedHandlerService("com.test.onesignal.GenerateNotificationRunner$" + "RemoteNotificationReceivedHandler_notificationReceivedCompleteNotCalled");
    // 2. Add app context and setup the established notification extension service
    OneSignal.initWithContext(ApplicationProvider.getApplicationContext());
    OneSignal_setupNotificationServiceExtension();
    // Mock timeout to 1, given that we are not calling complete inside RemoteNotificationReceivedHandler we depend on the timeout complete
    ShadowTimeoutHandler.setMockDelayMillis(1);
    // 3. Receive a notification
    Bundle bundle = getBaseNotifBundle();
    FCMBroadcastReceiver_processBundle(blankActivity, bundle);
    threadAndTaskWait();
    // 4. Make sure service was called
    assertNotNull(lastServiceNotificationReceivedEvent);
    // Notification save is done on the OSTimeoutHandler, we wait until the notification is saved
    Awaitility.await().atMost(new Duration(3, TimeUnit.SECONDS)).pollInterval(new Duration(100, TimeUnit.MILLISECONDS)).untilAsserted(() -> {
        // 5. Make sure 1 notification exists in DB
        assertNotificationDbRecords(1);
    });
}
Also used : Bundle(android.os.Bundle) FCMBroadcastReceiver_processBundle(com.onesignal.OneSignalPackagePrivateHelper.FCMBroadcastReceiver_processBundle) OneSignalPackagePrivateHelper.createInternalPayloadBundle(com.onesignal.OneSignalPackagePrivateHelper.createInternalPayloadBundle) Duration(org.awaitility.Duration) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 10 with Duration

use of org.awaitility.Duration in project OneSignal-Android-SDK by OneSignal.

the class InAppMessageIntegrationTests method testTimedMessageIsDisplayedOncePerSession.

@Test
public void testTimedMessageIsDisplayedOncePerSession() throws Exception {
    final OSTestInAppMessageInternal message = InAppMessagingHelpers.buildTestMessageWithSingleTriggerAndRedisplay(OSTriggerKind.SESSION_TIME, null, OSTestTrigger.OSTriggerOperator.GREATER_THAN.toString(), 0.05, 10, 0);
    setMockRegistrationResponseWithMessages(new ArrayList<OSTestInAppMessageInternal>() {

        {
            add(message);
        }
    });
    // the SDK should read the message from registration JSON, set up a timer, and once
    // the timer fires the message should get shown.
    OneSignalInit();
    threadAndTaskWait();
    // wait until the timer fires after 50ms and make sure the message gets displayed
    // we already have tests to make sure that the timer is actually being scheduled
    // for the correct amount of time, so all we are doing here is checking to
    // make sure the message actually gets displayed once the timer fires
    Awaitility.await().atMost(new Duration(1_000, TimeUnit.MILLISECONDS)).pollInterval(new Duration(10, TimeUnit.MILLISECONDS)).until(() -> OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size() == 1);
    // After IAM is added to display queue we now need to wait until it is shown
    Awaitility.await().atMost(new Duration(1_000, TimeUnit.MILLISECONDS)).pollInterval(new Duration(10, TimeUnit.MILLISECONDS)).untilAsserted(new ThrowingRunnable() {

        @Override
        public void run() {
            assertTrue(OneSignalPackagePrivateHelper.isInAppMessageShowing());
        }
    });
    OneSignalPackagePrivateHelper.dismissCurrentMessage();
    // Check that the IAM is not displayed again
    Awaitility.await().atMost(new Duration(1_000, TimeUnit.MILLISECONDS)).pollInterval(new Duration(10, TimeUnit.MILLISECONDS)).until(() -> OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size() == 0);
}
Also used : OSTestInAppMessageInternal(com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal) Duration(org.awaitility.Duration) ThrowingRunnable(org.awaitility.core.ThrowingRunnable) Test(org.junit.Test)

Aggregations

Duration (org.awaitility.Duration)14 Test (org.junit.Test)14 Config (org.robolectric.annotation.Config)7 OSTestInAppMessageInternal (com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal)4 Bundle (android.os.Bundle)3 OneSignalPackagePrivateHelper (com.onesignal.OneSignalPackagePrivateHelper)3 FCMBroadcastReceiver_processBundle (com.onesignal.OneSignalPackagePrivateHelper.FCMBroadcastReceiver_processBundle)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 BinarySerializer (net.morimekta.providence.serializer.BinarySerializer)3 OneSignalPackagePrivateHelper.createInternalPayloadBundle (com.onesignal.OneSignalPackagePrivateHelper.createInternalPayloadBundle)2 IOException (java.io.IOException)2 UncheckedIOException (java.io.UncheckedIOException)2 ExecutorService (java.util.concurrent.ExecutorService)2 MessageGenerator (net.morimekta.providence.util_internal.MessageGenerator)2 ThrowingRunnable (org.awaitility.core.ThrowingRunnable)2 SuppressLint (android.annotation.SuppressLint)1 OSNotification (com.onesignal.OSNotification)1 OSNotificationReceivedEvent (com.onesignal.OSNotificationReceivedEvent)1 OSTestTrigger (com.onesignal.OneSignalPackagePrivateHelper.OSTestTrigger)1 ShadowOneSignalRestClient (com.onesignal.ShadowOneSignalRestClient)1