Search in sources :

Example 26 with OSTestInAppMessageInternal

use of com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal in project OneSignal-Android-SDK by OneSignal.

the class InAppMessageIntegrationTests method testInAppMessageNoDisplayMultipleTimes_Delay.

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

        {
            add(message);
        }
    });
    // Init OneSignal with IAM with redisplay
    OneSignalInit();
    threadAndTaskWait();
    // Add trigger to make IAM display
    OneSignal.addTrigger("test_1", 2);
    assertEquals(1, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
    // Dismiss IAM will make display quantity increase and last display time to change
    OneSignalPackagePrivateHelper.dismissCurrentMessage();
    // 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);
    // Set trigger, will evaluate IAMs again
    OneSignal.addTrigger("test_1", 2);
    // Check that the IAM was not displayed again because time between display is not covered
    assertEquals(1, OneSignalPackagePrivateHelper.getRedisplayInAppMessages().size());
    assertEquals(1, OneSignalPackagePrivateHelper.getRedisplayInAppMessages().get(0).getRedisplayStats().getDisplayQuantity());
    assertEquals(lastDisplayTime, OneSignalPackagePrivateHelper.getRedisplayInAppMessages().get(0).getRedisplayStats().getLastDisplayTime());
}
Also used : OSTestInAppMessageInternal(com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal) Test(org.junit.Test)

Example 27 with OSTestInAppMessageInternal

use of com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal in project OneSignal-Android-SDK by OneSignal.

the class InAppMessageIntegrationTests method testInAppMessageDisplayMultipleTimes_NoTriggers.

@Test
public void testInAppMessageDisplayMultipleTimes_NoTriggers() throws Exception {
    final long currentTimeInSeconds = System.currentTimeMillis() / 1000;
    // Create an IAM
    final OSTestInAppMessageInternal message = InAppMessagingHelpers.buildTestMessageWitRedisplay(LIMIT, DELAY);
    message.getRedisplayStats().setLastDisplayTime(currentTimeInSeconds);
    message.getRedisplayStats().setDisplayQuantity(1);
    message.setDisplayedInSession(true);
    // Save IAM on DB
    TestHelpers.saveIAM(message, dbHelper);
    // Save IAM for dismiss
    TestOneSignalPrefs.saveStringSet(TestOneSignalPrefs.PREFS_ONESIGNAL, TestOneSignalPrefs.PREFS_OS_DISMISSED_IAMS, new HashSet<>(Collections.singletonList(message.getMessageId())));
    // Check IAM was saved correctly
    List<OSTestInAppMessageInternal> savedInAppMessages = TestHelpers.getAllInAppMessages(dbHelper);
    assertEquals(savedInAppMessages.size(), 1);
    assertTrue(savedInAppMessages.get(0).isDisplayedInSession());
    setMockRegistrationResponseWithMessages(new ArrayList<OSTestInAppMessageInternal>() {

        {
            add(message);
        }
    });
    // Change time for delay to be covered
    time.advanceSystemTimeBy(DELAY);
    // Init OneSignal with IAM with redisplay
    OneSignalInit();
    threadAndTaskWait();
    // First init will start a new session, then the IAM should be shown
    assertEquals(1, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
    // Dismiss IAM will make display quantity increase and last display time to change
    OneSignalPackagePrivateHelper.dismissCurrentMessage();
    // Check IAM was removed from queue
    assertEquals(0, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
    // Restart OneSignal
    fastColdRestartApp();
    setMockRegistrationResponseWithMessages(new ArrayList<OSTestInAppMessageInternal>() {

        {
            add(message);
        }
    });
    OneSignalInit();
    threadAndTaskWait();
    // Change time for delay to be covered
    time.advanceSystemTimeBy(DELAY * 2);
    // Add trigger to call evaluateInAppMessage
    OneSignal.addTrigger("test_1", 2);
    // IAM shouldn't display again because It don't have triggers
    assertEquals(0, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
}
Also used : OSTestInAppMessageInternal(com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal) Test(org.junit.Test)

Example 28 with OSTestInAppMessageInternal

use of com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal in project OneSignal-Android-SDK by OneSignal.

the class InAppMessageIntegrationTests method testAfterLastInAppTimeIsDisplayedOncePerSession.

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

        {
            add(new ArrayList<OSTestTrigger>() {

                {
                    add(InAppMessagingHelpers.buildTrigger(OSTriggerKind.SESSION_TIME, null, OSTestTrigger.OSTriggerOperator.GREATER_THAN.toString(), 0.1));
                    add(InAppMessagingHelpers.buildTrigger(OSTriggerKind.TIME_SINCE_LAST_IN_APP, null, OSTestTrigger.OSTriggerOperator.GREATER_THAN.toString(), 0.05));
                }
            });
        }
    };
    final OSTestInAppMessageInternal message2 = InAppMessagingHelpers.buildTestMessageWithMultipleTriggersAndRedisplay(triggers2, 10, 0);
    setMockRegistrationResponseWithMessages(new ArrayList<OSTestInAppMessageInternal>() {

        {
            add(message1);
            add(message2);
        }
    });
    // 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(150, TimeUnit.MILLISECONDS)).pollInterval(new Duration(10, TimeUnit.MILLISECONDS)).untilAsserted(() -> {
        assertEquals(1, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
        try {
            assertEquals(message1.getMessageId(), OneSignalPackagePrivateHelper.getShowingInAppMessageId());
        } catch (NullPointerException e) {
            // Awaitility won't retry if something is thrown, but will if an assert fails.
            fail("Should not throw");
        }
    });
    OneSignalPackagePrivateHelper.dismissCurrentMessage();
    // Second in app should now display
    Awaitility.await().atMost(new Duration(1, TimeUnit.SECONDS)).pollInterval(new Duration(100, TimeUnit.MILLISECONDS)).untilAsserted(() -> {
        assertEquals(1, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
        try {
            assertEquals(message2.getMessageId(), OneSignalPackagePrivateHelper.getShowingInAppMessageId());
        } catch (NullPointerException e) {
            // Awaitility won't retry if something is thrown, but will if an assert fails.
            fail("Should not throw");
        }
    });
    OneSignalPackagePrivateHelper.dismissCurrentMessage();
    // Check that the IAM is not displayed again
    assertEquals(0, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
}
Also used : OSTestInAppMessageInternal(com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal) ArrayList(java.util.ArrayList) Duration(org.awaitility.Duration) OSTestTrigger(com.onesignal.OneSignalPackagePrivateHelper.OSTestTrigger) Test(org.junit.Test)

Example 29 with OSTestInAppMessageInternal

use of com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal in project OneSignal-Android-SDK by OneSignal.

the class InAppMessageIntegrationTests method testDisableInAppMessagingPreventsMessageDisplay.

@Test
public void testDisableInAppMessagingPreventsMessageDisplay() throws Exception {
    final OSTestInAppMessageInternal testMessage = InAppMessagingHelpers.buildTestMessageWithSingleTrigger(OSTriggerKind.CUSTOM, "test_key", OSTestTrigger.OSTriggerOperator.EQUAL_TO.toString(), 3);
    setMockRegistrationResponseWithMessages(new ArrayList<OSTestInAppMessageInternal>() {

        {
            add(testMessage);
        }
    });
    OneSignalInit();
    threadAndTaskWait();
    // the SDK now has the in app message but it cannot be shown yet since the trigger is not valid
    // we will now disable in-app messages
    OneSignal.pauseInAppMessages(true);
    // We will set the trigger. However, since messaging is disabled, the message should not be shown
    OneSignal.addTrigger("test_key", 3);
    // Make sure 1 IAM is in the display queue
    assertEquals(1, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
    // Make sure no IAM is showing
    assertFalse(OneSignalPackagePrivateHelper.isInAppMessageShowing());
}
Also used : OSTestInAppMessageInternal(com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal) Test(org.junit.Test)

Example 30 with OSTestInAppMessageInternal

use of com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal in project OneSignal-Android-SDK by OneSignal.

the class InAppMessageIntegrationTests method testMessagesDoDisplayBeforeEndTime.

@Test
public void testMessagesDoDisplayBeforeEndTime() throws Exception {
    final OSTestInAppMessageInternal message = InAppMessagingHelpers.buildTestMessageWithEndTime(OSTriggerKind.CUSTOM, "test_key", OSTestTrigger.OSTriggerOperator.EQUAL_TO.toString(), 3, false);
    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();
    // We will set the trigger. However, since the end time is in the past the message should not be shown
    OneSignal.addTrigger("test_key", 3);
    // Make no IAMs are in the display queue
    assertEquals(1, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
    // Make sure no IAM is showing
    assertTrue(OneSignalPackagePrivateHelper.isInAppMessageShowing());
}
Also used : OSTestInAppMessageInternal(com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal) Test(org.junit.Test)

Aggregations

OSTestInAppMessageInternal (com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal)50 Test (org.junit.Test)43 JSONObject (org.json.JSONObject)19 ShadowOneSignalRestClient (com.onesignal.ShadowOneSignalRestClient)15 JSONArray (org.json.JSONArray)14 OneSignalPackagePrivateHelper (com.onesignal.OneSignalPackagePrivateHelper)8 ArrayList (java.util.ArrayList)5 SuppressLint (android.annotation.SuppressLint)4 OSTestTrigger (com.onesignal.OneSignalPackagePrivateHelper.OSTestTrigger)4 Duration (org.awaitility.Duration)4 MockOSLog (com.onesignal.MockOSLog)3 MockOSSharedPreferences (com.onesignal.MockOSSharedPreferences)3 MockSessionManager (com.onesignal.MockSessionManager)3 OneSignal (com.onesignal.OneSignal)3 OSTrackerFactory (com.onesignal.influence.data.OSTrackerFactory)3 JSONException (org.json.JSONException)3 Cursor (android.database.Cursor)2 OSInAppMessageAction (com.onesignal.OSInAppMessageAction)2 HashSet (java.util.HashSet)2 ThrowingRunnable (org.awaitility.core.ThrowingRunnable)2