Search in sources :

Example 1 with ThrowingRunnable

use of org.awaitility.core.ThrowingRunnable in project apollo by ctripcorp.

the class DefaultConfigTest method testGetIntPropertyMultipleTimesWithShortExpireTime.

@Test
public void testGetIntPropertyMultipleTimesWithShortExpireTime() throws Exception {
    final String someKey = "someKey";
    final Integer someValue = 2;
    final Integer someDefaultValue = -1;
    MockInjector.setInstance(ConfigUtil.class, new MockConfigUtilWithShortExpireTime());
    // set up config repo
    someProperties = mock(Properties.class);
    when(someProperties.getProperty(someKey)).thenReturn(String.valueOf(someValue));
    when(configRepository.getConfig()).thenReturn(someProperties);
    final DefaultConfig defaultConfig = new DefaultConfig(someNamespace, configRepository);
    assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue));
    assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue));
    verify(someProperties, times(1)).getProperty(someKey);
    await().atMost(500, TimeUnit.MILLISECONDS).untilAsserted(new ThrowingRunnable() {

        @Override
        public void run() throws Throwable {
            assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue));
            assertEquals(someValue, defaultConfig.getIntProperty(someKey, someDefaultValue));
            verify(someProperties, times(2)).getProperty(someKey);
        }
    });
}
Also used : OrderedProperties(com.ctrip.framework.apollo.util.OrderedProperties) Properties(java.util.Properties) ThrowingRunnable(org.awaitility.core.ThrowingRunnable) Test(org.junit.Test)

Example 2 with ThrowingRunnable

use of org.awaitility.core.ThrowingRunnable 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 3 with ThrowingRunnable

use of org.awaitility.core.ThrowingRunnable 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

ThrowingRunnable (org.awaitility.core.ThrowingRunnable)3 Test (org.junit.Test)3 OSTestInAppMessageInternal (com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal)2 Duration (org.awaitility.Duration)2 OrderedProperties (com.ctrip.framework.apollo.util.OrderedProperties)1 Properties (java.util.Properties)1 JSONException (org.json.JSONException)1