Search in sources :

Example 41 with OSTestInAppMessageInternal

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

the class InAppMessageIntegrationTests method testInAppMessageOnlyReceivesOneClick_onColdRestart.

@Test
public void testInAppMessageOnlyReceivesOneClick_onColdRestart() throws Exception {
    // 1. Init OneSignal
    OneSignalInit();
    threadAndTaskWait();
    // 2. Create an IAM
    final OSTestInAppMessageInternal message = InAppMessagingHelpers.buildTestMessageWithSingleTrigger(OSTriggerKind.SESSION_TIME, null, OSTestTrigger.OSTriggerOperator.NOT_EXISTS.toString(), null);
    // 2. Count IAM as clicked
    JSONObject action = new JSONObject() {

        {
            put("id", IAM_CLICK_ID);
        }
    };
    OneSignalPackagePrivateHelper.onMessageActionOccurredOnMessage(message, action);
    // 3. Cold restart app and re-init OneSignal
    fastColdRestartApp();
    OneSignalInit();
    threadAndTaskWait();
    // 4. Click on IAM again
    OneSignalPackagePrivateHelper.onMessageActionOccurredOnMessage(message, action);
    // Since the app restart and another message shown callback only 1 more request should exist
    // So verify 4 requests exist (3 old and 1 new)
    ShadowOneSignalRestClient.Request mostRecentRequest = ShadowOneSignalRestClient.requests.get(3);
    assertEquals(4, ShadowOneSignalRestClient.requests.size());
    // Now verify the most recent request was not a click request
    boolean isIamClickUrl = mostRecentRequest.url.equals("in_app_messages/" + message.getMessageId() + "/click");
    assertFalse(isIamClickUrl);
}
Also used : ShadowOneSignalRestClient(com.onesignal.ShadowOneSignalRestClient) OSTestInAppMessageInternal(com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal) JSONObject(org.json.JSONObject) Test(org.junit.Test)

Example 42 with OSTestInAppMessageInternal

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

the class InAppMessageIntegrationTests method testInAppMessageClickActionDisabledOutcomes.

@Test
public void testInAppMessageClickActionDisabledOutcomes() throws Exception {
    // 1. Init OneSignal
    OneSignalInit();
    threadAndTaskWait();
    // Disable influence outcomes
    trackerFactory.saveInfluenceParams(new OneSignalPackagePrivateHelper.RemoteOutcomeParams(false, false, false));
    // 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);
        }
    };
    // 3. Send IAM action
    // With unattributed outcomes disable no outcome request should happen
    OneSignalPackagePrivateHelper.onMessageActionOccurredOnMessage(message, action);
    // Requests: Param request + Players Request + Click request
    assertEquals(3, ShadowOneSignalRestClient.requests.size());
    assertEquals("in_app_messages/" + message.getMessageId() + "/click", ShadowOneSignalRestClient.requests.get(2).url);
}
Also used : OSTestInAppMessageInternal(com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) OneSignalPackagePrivateHelper(com.onesignal.OneSignalPackagePrivateHelper) Test(org.junit.Test)

Example 43 with OSTestInAppMessageInternal

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

the class InAppMessageIntegrationTests method testInAppMessageIdTracked.

@Test
public void testInAppMessageIdTracked() 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);
        }
    });
    // For mocking behaviour
    OneSignal_setTrackerFactory(trackerFactory);
    OneSignal_setSessionManager(sessionManager);
    // Init OneSignal IAM with redisplay
    OneSignalInit();
    threadAndTaskWait();
    // Check no influence id saved
    JSONArray lastReceivedIds = trackerFactory.getIAMChannelTracker().getLastReceivedIds();
    assertEquals(0, lastReceivedIds.length());
    // Add trigger to make IAM display
    OneSignal.addTrigger("test_1", 2);
    assertEquals(1, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
    // Check influence id saved
    lastReceivedIds = trackerFactory.getIAMChannelTracker().getLastReceivedIds();
    assertEquals(1, lastReceivedIds.length());
}
Also used : OSTestInAppMessageInternal(com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal) JSONArray(org.json.JSONArray) Test(org.junit.Test)

Example 44 with OSTestInAppMessageInternal

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

the class InAppMessageIntegrationTests method testOnIAMActionSendsOutcome_usingOutcomesV2.

@Test
public void testOnIAMActionSendsOutcome_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();
            OneSignal.sendOutcome("test");
            try {
                // Ensure outcome is sent
                assertMeasureOnV2AtIndex(4, "test", new JSONArray().put(message.getMessageId()), new JSONArray(), null, null);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });
    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");
}
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 45 with OSTestInAppMessageInternal

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

the class InAppMessageIntegrationTests method testInAppMessageOnlyReceivesClickIdOnce.

@Test
public void testInAppMessageOnlyReceivesClickIdOnce() throws Exception {
    // 1. Init OneSignal
    OneSignalInit();
    threadAndTaskWait();
    // 2. Create an IAM
    final OSTestInAppMessageInternal message = InAppMessagingHelpers.buildTestMessageWithSingleTrigger(OSTriggerKind.SESSION_TIME, null, OSTestTrigger.OSTriggerOperator.NOT_EXISTS.toString(), null);
    // 2. Count IAM as clicked
    JSONObject action = new JSONObject() {

        {
            put("id", IAM_CLICK_ID);
        }
    };
    OneSignalPackagePrivateHelper.onMessageActionOccurredOnMessage(message, action);
    // 3. Ensure click is sent
    ShadowOneSignalRestClient.Request iamImpressionRequest = ShadowOneSignalRestClient.requests.get(2);
    assertEquals("in_app_messages/" + message.getMessageId() + "/click", iamImpressionRequest.url);
    assertEquals(3, ShadowOneSignalRestClient.requests.size());
    // 4. Call IAM clicked again, ensure a 2nd network call is not made.
    OneSignalPackagePrivateHelper.onMessageActionOccurredOnMessage(message, action);
    assertEquals(3, ShadowOneSignalRestClient.requests.size());
    // Verify clickId was persisted locally
    Set<String> testClickedMessages = TestOneSignalPrefs.getStringSet(TestOneSignalPrefs.PREFS_ONESIGNAL, TestOneSignalPrefs.PREFS_OS_CLICKED_CLICK_IDS_IAMS, null);
    assertEquals(1, testClickedMessages.size());
}
Also used : ShadowOneSignalRestClient(com.onesignal.ShadowOneSignalRestClient) OSTestInAppMessageInternal(com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal) JSONObject(org.json.JSONObject) 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