Search in sources :

Example 21 with OSTestInAppMessageInternal

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

the class InAppMessagingUnitTests method comparativeOperatorTest.

/**
 * Convenience function that saves a local trigger (localValue) for the property name "test_property"
 * then creates an in-app message with a trigger (triggerValue) for the same property name. It
 * then evaluates the message for the given trigger conditions and returns the boolean, which
 * indicates whether or not the message should be shown.
 *
 * For example, we can set up a test where the app has a property value of 3 and we want to
 * test to make sure that if a message has a trigger value of 2 and an operator > that it
 * returns true when evaluated, because 3 > 2
 */
private static boolean comparativeOperatorTest(OSTriggerOperator operator, Object triggerValue, Object localValue) throws JSONException {
    setLocalTriggerValue("test_property", localValue);
    OSTestInAppMessageInternal testMessage = InAppMessagingHelpers.buildTestMessageWithSingleTrigger(OSTriggerKind.CUSTOM, "test_property", operator.toString(), triggerValue);
    return InAppMessagingHelpers.evaluateMessage(testMessage);
}
Also used : OSTestInAppMessageInternal(com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal)

Example 22 with OSTestInAppMessageInternal

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

the class InAppMessageIntegrationTests method testInAppMessageClickActionOutcome.

@Test
public void testInAppMessageClickActionOutcome() 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 outcomes = new JSONArray();
    outcomes.put(new JSONObject() {

        {
            put("name", IAM_OUTCOME_NAME);
        }
    });
    JSONObject action = new JSONObject() {

        {
            put("id", IAM_CLICK_ID);
            put("outcomes", outcomes);
        }
    };
    OneSignalPackagePrivateHelper.onMessageActionOccurredOnMessage(message, action);
    // 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());
    assertFalse(iamOutcomeRequest.payload.has("weight"));
    assertFalse(iamOutcomeRequest.payload.has("direct"));
    assertEquals(IAM_OUTCOME_NAME, iamOutcomeRequest.payload.get("id"));
    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 23 with OSTestInAppMessageInternal

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

the class InAppMessageIntegrationTests method testCachedIAMSharedPreferenceAndSQL_cleanedAfterSixMonths.

@Test
public void testCachedIAMSharedPreferenceAndSQL_cleanedAfterSixMonths() throws Exception {
    final long currentTimeInSeconds = System.currentTimeMillis() / 1_000L;
    // 1. Setup IAMs
    // Create an IAM younger than 6 months
    final OSTestInAppMessageInternal iam1 = InAppMessagingHelpers.buildTestMessage(null);
    iam1.setRedisplayStats(1, currentTimeInSeconds - SIX_MONTHS_TIME_SECONDS + 10);
    String clickId1 = "iam1_click_id_1";
    iam1.addClickId(clickId1);
    TestHelpers.saveIAM(iam1, dbHelper);
    // Create an IAM older than 6 months
    final OSTestInAppMessageInternal iam2 = InAppMessagingHelpers.buildTestMessage(null);
    iam2.setRedisplayStats(1, currentTimeInSeconds - SIX_MONTHS_TIME_SECONDS - 10);
    String clickId2 = "iam2_click_id_1";
    iam2.addClickId(clickId2);
    TestHelpers.saveIAM(iam2, dbHelper);
    // 2. Cache IAMs as dismissed, impressioned, and clicked
    Set<String> messageIds = new HashSet<String>() {

        {
            add(iam1.getMessageId());
            add(iam2.getMessageId());
        }
    };
    TestOneSignalPrefs.saveStringSet(TestOneSignalPrefs.PREFS_ONESIGNAL, TestOneSignalPrefs.PREFS_OS_DISMISSED_IAMS, messageIds);
    TestOneSignalPrefs.saveStringSet(TestOneSignalPrefs.PREFS_ONESIGNAL, TestOneSignalPrefs.PREFS_OS_IMPRESSIONED_IAMS, messageIds);
    Set<String> clickedClickIds = new HashSet<String>() {

        {
            addAll(iam1.getClickedClickIds());
            addAll(iam2.getClickedClickIds());
        }
    };
    TestOneSignalPrefs.saveStringSet(TestOneSignalPrefs.PREFS_ONESIGNAL, TestOneSignalPrefs.PREFS_OS_CLICKED_CLICK_IDS_IAMS, clickedClickIds);
    // 3. Init OneSignal so it attempts to clean IAM cache
    OneSignalInit();
    threadAndTaskWait();
    // 4. Validate all data associated with the 6 month old IAM has been deleted
    Set<String> testDismissedMessages = TestOneSignalPrefs.getStringSet(TestOneSignalPrefs.PREFS_ONESIGNAL, TestOneSignalPrefs.PREFS_OS_DISMISSED_IAMS, null);
    assertEquals(1, testDismissedMessages.size());
    assertTrue(testDismissedMessages.contains(iam1.getMessageId()));
    Set<String> testImpressionedMessages = TestOneSignalPrefs.getStringSet(TestOneSignalPrefs.PREFS_ONESIGNAL, TestOneSignalPrefs.PREFS_OS_IMPRESSIONED_IAMS, null);
    assertEquals(1, testImpressionedMessages.size());
    assertTrue(testImpressionedMessages.contains(iam1.getMessageId()));
    Set<String> testClickedClickIds = TestOneSignalPrefs.getStringSet(TestOneSignalPrefs.PREFS_ONESIGNAL, TestOneSignalPrefs.PREFS_OS_CLICKED_CLICK_IDS_IAMS, null);
    assertEquals(1, testClickedClickIds.size());
    assertTrue(testClickedClickIds.contains(clickId1));
    // 5. Make sure only IAM left is the IAM younger than 6 months
    List<OSTestInAppMessageInternal> savedInAppMessagesAfterInit = TestHelpers.getAllInAppMessages(dbHelper);
    assertEquals(1, savedInAppMessagesAfterInit.size());
    assertEquals(iam1.getMessageId(), savedInAppMessagesAfterInit.get(0).getMessageId());
}
Also used : OSTestInAppMessageInternal(com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 24 with OSTestInAppMessageInternal

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

the class InAppMessageIntegrationTests method testMessagesDoNotDisplayPastEndTime.

@Test
public void testMessagesDoNotDisplayPastEndTime() throws Exception {
    final OSTestInAppMessageInternal message = InAppMessagingHelpers.buildTestMessageWithEndTime(OSTriggerKind.CUSTOM, "test_key", OSTestTrigger.OSTriggerOperator.EQUAL_TO.toString(), 3, true);
    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(0, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
    // Make sure no IAM is showing
    assertFalse(OneSignalPackagePrivateHelper.isInAppMessageShowing());
}
Also used : OSTestInAppMessageInternal(com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal) Test(org.junit.Test)

Example 25 with OSTestInAppMessageInternal

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

the class InAppMessageIntegrationTests method testInAppMessageRedisplayCacheCleaning.

@Test
public void testInAppMessageRedisplayCacheCleaning() throws Exception {
    final long currentTimeInSeconds = System.currentTimeMillis() / 1000;
    final OSTestInAppMessageInternal inAppMessage = InAppMessagingHelpers.buildTestMessageWithSingleTriggerAndRedisplay(OSTriggerKind.CUSTOM, "test_saved", OneSignalPackagePrivateHelper.OSTestTrigger.OSTriggerOperator.EQUAL_TO.toString(), 2, LIMIT, DELAY);
    String firstID = inAppMessage.getMessageId() + "_test";
    inAppMessage.setMessageId(firstID);
    inAppMessage.getRedisplayStats().setLastDisplayTime(currentTimeInSeconds - SIX_MONTHS_TIME_SECONDS + 1);
    TestHelpers.saveIAM(inAppMessage, dbHelper);
    inAppMessage.getRedisplayStats().setLastDisplayTime(currentTimeInSeconds - SIX_MONTHS_TIME_SECONDS - 1);
    inAppMessage.setMessageId(inAppMessage.getMessageId() + "1");
    TestHelpers.saveIAM(inAppMessage, dbHelper);
    List<OSTestInAppMessageInternal> savedInAppMessages = TestHelpers.getAllInAppMessages(dbHelper);
    assertEquals(2, savedInAppMessages.size());
    final OSTestInAppMessageInternal message1 = InAppMessagingHelpers.buildTestMessageWithSingleTriggerAndRedisplay(OSTriggerKind.CUSTOM, "test_1", OSTestTrigger.OSTriggerOperator.EQUAL_TO.toString(), 2, LIMIT, DELAY);
    final OSTestInAppMessageInternal message2 = InAppMessagingHelpers.buildTestMessageWithSingleTriggerAndRedisplay(OSTriggerKind.CUSTOM, "test_2", OSTestTrigger.OSTriggerOperator.EQUAL_TO.toString(), 2, LIMIT, DELAY);
    setMockRegistrationResponseWithMessages(new ArrayList<OSTestInAppMessageInternal>() {

        {
            add(message1);
            add(message2);
        }
    });
    // Init OneSignal with IAM with redisplay
    OneSignalInit();
    threadAndTaskWait();
    List<OSTestInAppMessageInternal> savedInAppMessagesAfterInit = TestHelpers.getAllInAppMessages(dbHelper);
    // Message with old display time should be removed
    assertEquals(1, savedInAppMessagesAfterInit.size());
    assertEquals(firstID, savedInAppMessagesAfterInit.get(0).getMessageId());
}
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