Search in sources :

Example 46 with OSTestInAppMessageInternal

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

the class InAppMessageIntegrationTests method testInAppMessageDisplayMultipleTimes_RemoveTrigger.

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

        {
            add(message);
        }
    });
    // Init OneSignal with IAM with redisplay
    OneSignalInit();
    threadAndTaskWait();
    // Because trigger doesn't exist IAM will be shown immediately
    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());
    // 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);
    OneSignal.addTrigger("test_1", 2);
    // Wait for the delay between redisplay
    time.advanceSystemTimeBy(DELAY);
    assertEquals(0, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
    // Remove trigger, IAM should display again
    OneSignal.removeTriggerForKey("test_1");
    // Check that IAM is queue for redisplay
    assertEquals(1, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
}
Also used : OSTestInAppMessageInternal(com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal) Test(org.junit.Test)

Example 47 with OSTestInAppMessageInternal

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

the class InAppMessageIntegrationTests method testInAppMessageDisplayMultipleTimes.

@Test
public void testInAppMessageDisplayMultipleTimes() 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 IAM with redisplay
    OneSignalInit();
    threadAndTaskWait();
    // Add trigger to make IAM display
    OneSignal.addTrigger("test_1", 2);
    assertEquals(1, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
    // Check impression request
    int requestSize = ShadowOneSignalRestClient.requests.size();
    ShadowOneSignalRestClient.Request iamImpressionRequest = ShadowOneSignalRestClient.requests.get(requestSize - 1);
    assertEquals("in_app_messages/" + message.getMessageId() + "/impression", iamImpressionRequest.url);
    // 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);
    // Set same trigger, should display again
    OneSignal.addTrigger("test_1", 2);
    assertEquals(1, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
    // Check impression request is sent again
    int requestSizeAfterRedisplay = ShadowOneSignalRestClient.requests.size();
    ShadowOneSignalRestClient.Request iamImpressionRequestAfterRedisplay = ShadowOneSignalRestClient.requests.get(requestSizeAfterRedisplay - 1);
    assertEquals("in_app_messages/" + message.getMessageId() + "/impression", iamImpressionRequestAfterRedisplay.url);
    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 : ShadowOneSignalRestClient(com.onesignal.ShadowOneSignalRestClient) OSTestInAppMessageInternal(com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal) SuppressLint(android.annotation.SuppressLint) Test(org.junit.Test)

Example 48 with OSTestInAppMessageInternal

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

the class InAppMessageIntegrationTests method testOnIAMActionSendsOutcome_afterDismiss_usingOutcomesV2.

@Test
public void testOnIAMActionSendsOutcome_afterDismiss_usingOutcomesV2() throws Exception {
    // Enable IAM v2
    preferences = new MockOSSharedPreferences();
    preferences.saveBool(preferences.getPreferencesName(), preferences.getOutcomesV2KeyName(), true);
    trackerFactory = new OSTrackerFactory(preferences, new MockOSLog(), time);
    sessionManager = new MockSessionManager(OneSignal_getSessionListener(), trackerFactory, new MockOSLog());
    OneSignal_setSharedPreferences(preferences);
    OneSignal_setTrackerFactory(trackerFactory);
    OneSignal_setSessionManager(sessionManager);
    final OSTestInAppMessageInternal message = InAppMessagingHelpers.buildTestMessageWithSingleTrigger(OSTriggerKind.CUSTOM, "test_1", OSTestTrigger.OSTriggerOperator.EQUAL_TO.toString(), 2);
    setMockRegistrationResponseWithMessages(new ArrayList<OSTestInAppMessageInternal>() {

        {
            add(message);
        }
    });
    // 1. Init OneSignal
    OneSignalInit();
    threadAndTaskWait();
    // Enable influence outcomes
    trackerFactory.saveInfluenceParams(new OneSignalPackagePrivateHelper.RemoteOutcomeParams());
    final OSInAppMessageAction[] lastAction = new OSInAppMessageAction[1];
    OneSignal.setInAppMessageClickHandler(new OneSignal.OSInAppMessageClickHandler() {

        @Override
        public void inAppMessageClicked(OSInAppMessageAction result) {
            lastAction[0] = result;
            // Ensure we are on the main thread when running the callback, since the app developer
            // will most likely need to update UI.
            assertMainThread();
        }
    });
    threadAndTaskWait();
    // Add trigger to make IAM display
    OneSignal.addTrigger("test_1", 2);
    assertEquals(1, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
    OneSignalPackagePrivateHelper.onMessageActionOccurredOnMessage(message, new JSONObject() {

        {
            put("id", "button_id_123");
            put("name", "my_click_name");
        }
    });
    // Ensure we fire public callback that In-App was clicked.
    assertEquals(lastAction[0].getClickName(), "my_click_name");
    OneSignalPackagePrivateHelper.dismissCurrentMessage();
    OneSignal.sendOutcome("test1");
    try {
        // Ensure outcome is sent but with INDIRECT influence from IAM
        assertMeasureOnV2AtIndex(5, "test1", null, null, new JSONArray().put(message.getMessageId()), new JSONArray());
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
Also used : MockOSLog(com.onesignal.MockOSLog) OneSignal(com.onesignal.OneSignal) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) MockOSSharedPreferences(com.onesignal.MockOSSharedPreferences) OneSignalPackagePrivateHelper(com.onesignal.OneSignalPackagePrivateHelper) OSTestInAppMessageInternal(com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal) OSTrackerFactory(com.onesignal.influence.data.OSTrackerFactory) JSONObject(org.json.JSONObject) MockSessionManager(com.onesignal.MockSessionManager) OSInAppMessageAction(com.onesignal.OSInAppMessageAction) Test(org.junit.Test)

Example 49 with OSTestInAppMessageInternal

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

the class InAppMessagingUnitTests method testBuiltMessageRedisplayLimit.

@Test
public void testBuiltMessageRedisplayLimit() throws JSONException {
    OSTestInAppMessageInternal message = InAppMessagingHelpers.buildTestMessageWitRedisplay(LIMIT, DELAY);
    for (int i = 0; i < LIMIT; i++) {
        assertTrue(message.getRedisplayStats().shouldDisplayAgain());
        message.getRedisplayStats().incrementDisplayQuantity();
    }
    message.getRedisplayStats().incrementDisplayQuantity();
    assertFalse(message.getRedisplayStats().shouldDisplayAgain());
}
Also used : OSTestInAppMessageInternal(com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal) SuppressLint(android.annotation.SuppressLint) Test(org.junit.Test)

Example 50 with OSTestInAppMessageInternal

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

the class InAppMessagingUnitTests method testBuiltMessageRedisplayCLickId.

@Test
public void testBuiltMessageRedisplayCLickId() throws JSONException {
    OSTestInAppMessageInternal message = InAppMessagingHelpers.buildTestMessageWitRedisplay(LIMIT, DELAY);
    assertTrue(message.getClickedClickIds().isEmpty());
    assertTrue(message.isClickAvailable(IAM_CLICK_ID));
    message.addClickId(IAM_CLICK_ID);
    message.clearClickIds();
    assertTrue(message.getClickedClickIds().isEmpty());
    message.addClickId(IAM_CLICK_ID);
    message.addClickId(IAM_CLICK_ID);
    assertEquals(1, message.getClickedClickIds().size());
    assertFalse(message.isClickAvailable(IAM_CLICK_ID));
    OSTestInAppMessageInternal messageWithoutDisplay = InAppMessagingHelpers.buildTestMessageWithSingleTrigger(OSTriggerKind.SESSION_TIME, null, OSTriggerOperator.GREATER_THAN_OR_EQUAL_TO.toString(), 3);
    messageWithoutDisplay.addClickId(IAM_CLICK_ID);
    assertFalse(messageWithoutDisplay.isClickAvailable(IAM_CLICK_ID));
}
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