Search in sources :

Example 1 with OSTestTrigger

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

the class InAppMessagingHelpers method buildTrigger.

public static OSTestTrigger buildTrigger(final OSTriggerKind kind, final String key, final String operator, final Object value) throws JSONException {
    JSONObject triggerJson = new JSONObject() {

        {
            put("id", UUID.randomUUID().toString());
            put("kind", kind.toString());
            put("property", key);
            put("operator", operator);
            put("value", value);
        }
    };
    return new OSTestTrigger(triggerJson);
}
Also used : JSONObject(org.json.JSONObject) OSTestTrigger(com.onesignal.OneSignalPackagePrivateHelper.OSTestTrigger)

Example 2 with OSTestTrigger

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

the class InAppMessageIntegrationTests method testTimedMessageDisplayedAfterAllTriggersValid.

/**
 * If an in-app message should only be shown if (A) session_duration is > 30 seconds and
 * (B) a key/value trigger is set, and it should not set up a timer until all of the non-timer
 * based triggers for that message evaluate to true
 * <p>
 * For this test, a timer should never be scheduled because the key/value 'test_key' trigger
 * will not be set until the session duration has already exceeded the minimum (0.05 seconds)
 */
@Test
public void testTimedMessageDisplayedAfterAllTriggersValid() throws Exception {
    ArrayList<ArrayList<OSTestTrigger>> triggers = new ArrayList<ArrayList<OSTestTrigger>>() {

        {
            add(new ArrayList<OSTestTrigger>() {

                {
                    add(InAppMessagingHelpers.buildTrigger(OSTriggerKind.CUSTOM, "test_key", OSTestTrigger.OSTriggerOperator.EQUAL_TO.toString(), "squirrel"));
                    add(InAppMessagingHelpers.buildTrigger(OSTriggerKind.SESSION_TIME, null, OSTestTrigger.OSTriggerOperator.GREATER_THAN.toString(), 0.01));
                }
            });
        }
    };
    final OSTestInAppMessageInternal message = InAppMessagingHelpers.buildTestMessageWithMultipleTriggers(triggers);
    setMockRegistrationResponseWithMessages(new ArrayList<OSTestInAppMessageInternal>() {

        {
            add(message);
        }
    });
    OneSignalInit();
    threadAndTaskWait();
    // no timer should be scheduled since 'test_key' != 'squirrel'
    assertFalse(ShadowDynamicTimer.hasScheduledTimer);
    assertEquals(0, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
    // since we are not actually waiting on any logic to finish, sleeping here is fine
    Thread.sleep(20);
    // the message still should not be displayed
    assertEquals(0, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
    // after setting this trigger the message should be displayed immediately
    OneSignal.addTrigger("test_key", "squirrel");
    threadAndTaskWait();
    // the message should now have been displayed
    assertEquals(1, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
    assertFalse(ShadowDynamicTimer.hasScheduledTimer);
}
Also used : OSTestInAppMessageInternal(com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal) ArrayList(java.util.ArrayList) OSTestTrigger(com.onesignal.OneSignalPackagePrivateHelper.OSTestTrigger) Test(org.junit.Test)

Example 3 with OSTestTrigger

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

the class InAppMessagingUnitTests method testBuiltMessageTrigger.

@Test
public void testBuiltMessageTrigger() {
    OSTestTrigger trigger = (OSTestTrigger) message.triggers.get(0).get(0);
    assertEquals(trigger.kind, OSTriggerKind.SESSION_TIME);
    assertEquals(trigger.operatorType, OSTriggerOperator.GREATER_THAN_OR_EQUAL_TO);
    assertNull(trigger.property);
    assertEquals(trigger.value, 3);
}
Also used : OSTestTrigger(com.onesignal.OneSignalPackagePrivateHelper.OSTestTrigger) Test(org.junit.Test)

Example 4 with OSTestTrigger

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

the class InAppMessagingUnitTests method testShouldTriggerWhen1OutOf3OrsAreMeet.

@Test
public void testShouldTriggerWhen1OutOf3OrsAreMeet() throws JSONException {
    setLocalTriggerValue("prop1", 3);
    ArrayList<ArrayList<OSTestTrigger>> triggers = new ArrayList<ArrayList<OSTestTrigger>>() {

        {
            add(new ArrayList<OSTestTrigger>() {

                {
                    add(InAppMessagingHelpers.buildTrigger(OSTriggerKind.CUSTOM, "prop1", OSTriggerOperator.EQUAL_TO.toString(), 1));
                }
            });
            add(new ArrayList<OSTestTrigger>() {

                {
                    add(InAppMessagingHelpers.buildTrigger(OSTriggerKind.CUSTOM, "prop1", OSTriggerOperator.EQUAL_TO.toString(), 2));
                }
            });
            add(new ArrayList<OSTestTrigger>() {

                {
                    add(InAppMessagingHelpers.buildTrigger(OSTriggerKind.CUSTOM, "prop1", OSTriggerOperator.EQUAL_TO.toString(), 3));
                }
            });
        }
    };
    OSTestInAppMessageInternal testMessage = InAppMessagingHelpers.buildTestMessageWithMultipleTriggers(triggers);
    assertTrue(InAppMessagingHelpers.evaluateMessage(testMessage));
}
Also used : OSTestInAppMessageInternal(com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal) ArrayList(java.util.ArrayList) OSTestTrigger(com.onesignal.OneSignalPackagePrivateHelper.OSTestTrigger) Test(org.junit.Test)

Example 5 with OSTestTrigger

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

the class InAppMessagingUnitTests method testMixedTriggersScheduleTimer.

// This test makes sure that time-based triggers are considered once all non-time-based
// triggers evaluate to true and will set up a timer if needed
@Test
public void testMixedTriggersScheduleTimer() throws JSONException {
    final OSTestTrigger timeBasedTrigger = InAppMessagingHelpers.buildTrigger(OSTriggerKind.SESSION_TIME, null, OSTriggerOperator.GREATER_THAN.toString(), 5.0);
    final OSTestTrigger normalTrigger = InAppMessagingHelpers.buildTrigger(OSTriggerKind.CUSTOM, "prop1", OSTriggerOperator.LESS_THAN_OR_EQUAL_TO.toString(), 3);
    // the time based trigger will be false (but should schedule a timer)
    // while the normal trigger should evaluate to true
    setLocalTriggerValue("prop1", 3);
    ArrayList<ArrayList<OSTestTrigger>> triggers = new ArrayList<ArrayList<OSTestTrigger>>() {

        {
            add(new ArrayList<OSTestTrigger>() {

                {
                    add(timeBasedTrigger);
                    add(normalTrigger);
                }
            });
        }
    };
    OSTestInAppMessageInternal testMessage = InAppMessagingHelpers.buildTestMessageWithMultipleTriggers(triggers);
    assertFalse(InAppMessagingHelpers.evaluateMessage(testMessage));
    assertTrue(ShadowDynamicTimer.hasScheduledTimer);
    assertTrue(roughlyEqualTimerValues(5.0, ShadowDynamicTimer.mostRecentTimerDelaySeconds()));
}
Also used : OSTestInAppMessageInternal(com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal) ArrayList(java.util.ArrayList) OSTestTrigger(com.onesignal.OneSignalPackagePrivateHelper.OSTestTrigger) Test(org.junit.Test)

Aggregations

OSTestTrigger (com.onesignal.OneSignalPackagePrivateHelper.OSTestTrigger)8 Test (org.junit.Test)6 OSTestInAppMessageInternal (com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal)4 ArrayList (java.util.ArrayList)4 JSONObject (org.json.JSONObject)2 Duration (org.awaitility.Duration)1 JSONArray (org.json.JSONArray)1