Search in sources :

Example 36 with OSTestInAppMessageInternal

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

the class InAppMessageIntegrationTests method testInAppMessageClickActionOutcomeWithValue.

@Test
public void testInAppMessageClickActionOutcomeWithValue() throws Exception {
    // 1. Init OneSignal
    OneSignalInit();
    threadAndTaskWait();
    // Enable influence outcomes
    trackerFactory.saveInfluenceParams(new OneSignalPackagePrivateHelper.RemoteOutcomeParams());
    // 2. Create an IAM
    final OSTestInAppMessageInternal message = InAppMessagingHelpers.buildTestMessageWithSingleTrigger(OSTriggerKind.SESSION_TIME, null, OSTestTrigger.OSTriggerOperator.NOT_EXISTS.toString(), null);
    final JSONArray outcomesWithWeight = new JSONArray();
    outcomesWithWeight.put(new JSONObject() {

        {
            put("name", IAM_OUTCOME_NAME);
            put("weight", IAM_OUTCOME_WEIGHT);
        }
    });
    JSONObject actionWithWeight = new JSONObject() {

        {
            put("id", IAM_CLICK_ID);
            put("outcomes", outcomesWithWeight);
        }
    };
    OneSignalPackagePrivateHelper.onMessageActionOccurredOnMessage(message, actionWithWeight);
    // 3. Ensure outcome is sent
    ShadowOneSignalRestClient.Request iamOutcomeRequest = ShadowOneSignalRestClient.requests.get(3);
    assertEquals("outcomes/measure", iamOutcomeRequest.url);
    // Requests: Param request + Players Request + Click request + Outcome Request
    assertEquals(4, ShadowOneSignalRestClient.requests.size());
    assertEquals(IAM_OUTCOME_WEIGHT, iamOutcomeRequest.payload.get("weight"));
    assertEquals(IAM_OUTCOME_NAME, iamOutcomeRequest.payload.get("id"));
    assertFalse(iamOutcomeRequest.payload.has("direct"));
    assertEquals(1, iamOutcomeRequest.payload.get("device_type"));
}
Also used : ShadowOneSignalRestClient(com.onesignal.ShadowOneSignalRestClient) OSTestInAppMessageInternal(com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) OneSignalPackagePrivateHelper(com.onesignal.OneSignalPackagePrivateHelper) Test(org.junit.Test)

Example 37 with OSTestInAppMessageInternal

use of com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal 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 38 with OSTestInAppMessageInternal

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

the class InAppMessageIntegrationTests method testInAppMessageOnlyReceivesOneImpression.

@Test
public void testInAppMessageOnlyReceivesOneImpression() throws Exception {
    // Init OneSignal
    OneSignalInit();
    threadAndTaskWait();
    // Create an IAM
    final OSTestInAppMessageInternal message = InAppMessagingHelpers.buildTestMessageWithSingleTrigger(OSTriggerKind.SESSION_TIME, null, OSTestTrigger.OSTriggerOperator.NOT_EXISTS.toString(), null);
    // Call message shown callback and verify only 3 requests exist (3rd being the iam impression request)
    OneSignalPackagePrivateHelper.onMessageWasShown(message);
    ShadowOneSignalRestClient.Request iamImpressionRequest = ShadowOneSignalRestClient.requests.get(2);
    assertEquals("in_app_messages/" + message.getMessageId() + "/impression", iamImpressionRequest.url);
    assertEquals(3, ShadowOneSignalRestClient.requests.size());
    // Call message shown again and make sure no other requests were made, so the impression tracking exists locally
    OneSignalPackagePrivateHelper.onMessageWasShown(message);
    assertEquals(3, ShadowOneSignalRestClient.requests.size());
    // Verify impressioned messageId was persisted locally
    Set<String> testImpressionedMessages = TestOneSignalPrefs.getStringSet(TestOneSignalPrefs.PREFS_ONESIGNAL, TestOneSignalPrefs.PREFS_OS_IMPRESSIONED_IAMS, null);
    assertEquals(1, testImpressionedMessages.size());
}
Also used : ShadowOneSignalRestClient(com.onesignal.ShadowOneSignalRestClient) OSTestInAppMessageInternal(com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal) Test(org.junit.Test)

Example 39 with OSTestInAppMessageInternal

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

the class InAppMessageIntegrationTests method testMessageDisplayedAfterAddTriggerEqualWithStringVsNumberFloat.

@Test
public void testMessageDisplayedAfterAddTriggerEqualWithStringVsNumberFloat() throws Exception {
    // Set IAM with EQUAL trigger with number value as string
    final OSTestInAppMessageInternal message = InAppMessagingHelpers.buildTestMessageWithSingleTrigger(OSTriggerKind.CUSTOM, "test", OSTestTrigger.OSTriggerOperator.EQUAL_TO.toString(), "5.5");
    setMockRegistrationResponseWithMessages(new ArrayList<OSTestInAppMessageInternal>() {

        {
            add(message);
        }
    });
    OneSignalInit();
    threadAndTaskWait();
    assertEquals(0, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
    // after setting this trigger the message should be displayed immediately
    OneSignal.addTrigger("test", 5.50);
    threadAndTaskWait();
    // the message should now have been displayed
    assertEquals(1, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
    dismissCurrentMessage();
}
Also used : OSTestInAppMessageInternal(com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal) Test(org.junit.Test)

Example 40 with OSTestInAppMessageInternal

use of com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal 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

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