Search in sources :

Example 1 with Duration

use of org.awaitility.Duration in project providence by morimekta.

the class QueuedFileMessageWriterTest method testSeparator.

@Test
public void testSeparator() throws IOException {
    setDefaultPollDelay(new Duration(10, TimeUnit.MILLISECONDS));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IOMessageWriter target = new IOMessageWriter(baos, new BinarySerializer());
    QueuedMessageWriter writer = new QueuedMessageWriter(target);
    assertThat(writer.separator(), is(0));
    writer.close();
    assertThat(baos.size(), is(0));
}
Also used : Duration(org.awaitility.Duration) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) Test(org.junit.Test)

Example 2 with Duration

use of org.awaitility.Duration in project OneSignal-Android-SDK by OneSignal.

the class GenerateNotificationRunner method testRemoteNotificationReceivedHandler_notificationProcessingProperties.

@Test
@Config(shadows = { ShadowOneSignal.class, ShadowGenerateNotification.class })
public void testRemoteNotificationReceivedHandler_notificationProcessingProperties() throws Exception {
    // 1. Setup correct notification extension service class
    startRemoteNotificationReceivedHandlerService("com.test.onesignal.GenerateNotificationRunner$" + "RemoteNotificationReceivedHandler_notificationReceivedProperties");
    // 2. Add app context and setup the established notification extension service
    OneSignal.initWithContext(ApplicationProvider.getApplicationContext());
    OneSignal_setupNotificationServiceExtension();
    final boolean[] callbackEnded = { false };
    OneSignalPackagePrivateHelper.ProcessBundleReceiverCallback processBundleReceiverCallback = new OneSignalPackagePrivateHelper.ProcessBundleReceiverCallback() {

        public void onBundleProcessed(OneSignalPackagePrivateHelper.ProcessedBundleResult processedResult) {
            assertNotNull(processedResult);
            // 3. Test that WorkManager begins processing the notification
            assertTrue(processedResult.isProcessed());
            callbackEnded[0] = true;
        }
    };
    FCMBroadcastReceiver_processBundle(blankActivity, getBaseNotifBundle(), processBundleReceiverCallback);
    Awaitility.await().atMost(new Duration(3, TimeUnit.SECONDS)).pollInterval(new Duration(100, TimeUnit.MILLISECONDS)).untilAsserted(() -> {
        assertTrue(callbackEnded[0]);
    });
    // 4. Receive a notification with all data fields used
    FCMBroadcastReceiver_processBundle(blankActivity, getBundleWithAllOptionsSet());
    threadAndTaskWait();
    // 5. Evaluate the notification received within the NotificationProcessingHandler
    OSNotificationReceivedEvent receivedEvent = RemoteNotificationReceivedHandler_notificationReceivedProperties.notification;
    OSNotification notification = receivedEvent.getNotification();
    assertEquals("Test H", notification.getTitle());
    assertEquals("Test B", notification.getBody());
    assertEquals("9764eaeb-10ce-45b1-a66d-8f95938aaa51", notification.getNotificationId());
    assertEquals(0, notification.getLockScreenVisibility());
    assertEquals("FF0000FF", notification.getSmallIconAccentColor());
    assertEquals("703322744261", notification.getFromProjectNumber());
    assertEquals("FFFFFF00", notification.getLedColor());
    assertEquals("big_picture", notification.getBigPicture());
    assertEquals("large_icon", notification.getLargeIcon());
    assertEquals("small_icon", notification.getSmallIcon());
    assertEquals("test_sound", notification.getSound());
    assertEquals("You test $[notif_count] MSGs!", notification.getGroupMessage());
    assertEquals("http://google.com", notification.getLaunchURL());
    assertEquals(10, notification.getPriority());
    assertEquals("a_key", notification.getCollapseId());
    assertEquals("id1", notification.getActionButtons().get(0).getId());
    assertEquals("button1", notification.getActionButtons().get(0).getText());
    assertEquals("ic_menu_share", notification.getActionButtons().get(0).getIcon());
    assertEquals("id2", notification.getActionButtons().get(1).getId());
    assertEquals("button2", notification.getActionButtons().get(1).getText());
    assertEquals("ic_menu_send", notification.getActionButtons().get(1).getIcon());
    assertEquals("test_image_url", notification.getBackgroundImageLayout().getImage());
    assertEquals("FF000000", notification.getBackgroundImageLayout().getTitleTextColor());
    assertEquals("FFFFFFFF", notification.getBackgroundImageLayout().getBodyTextColor());
    JSONObject additionalData = notification.getAdditionalData();
    assertEquals("myValue", additionalData.getString("myKey"));
    assertEquals("nValue", additionalData.getJSONObject("nested").getString("nKey"));
    // 6. Make sure the notification id is not -1 (not restoring)
    assertThat(RemoteNotificationReceivedHandler_notificationReceivedProperties.notificationId, not(-1));
    // 7. Test a basic notification without anything special
    FCMBroadcastReceiver_processBundle(blankActivity, getBaseNotifBundle());
    threadAndTaskWait();
    assertFalse(ShadowOneSignal.messages.contains("Error assigning"));
    // 8. Test that a notification is still displayed if the developer's code in onNotificationProcessing throws an Exception
    RemoteNotificationReceivedHandler_notificationReceivedProperties.throwInAppCode = true;
    FCMBroadcastReceiver_processBundle(blankActivity, getBaseNotifBundle("NewUUID1"));
    threadAndTaskWait();
    // 9. Make sure 3 notifications have been posted
    assertTrue(ShadowOneSignal.messages.contains("remoteNotificationReceived throw an exception"));
    Map<Integer, PostedNotification> postedNotifs = ShadowRoboNotificationManager.notifications;
    assertEquals(3, postedNotifs.size());
}
Also used : BigInteger(java.math.BigInteger) JSONObject(org.json.JSONObject) OSNotificationReceivedEvent(com.onesignal.OSNotificationReceivedEvent) Duration(org.awaitility.Duration) OSNotification(com.onesignal.OSNotification) OneSignalPackagePrivateHelper(com.onesignal.OneSignalPackagePrivateHelper) PostedNotification(com.onesignal.ShadowRoboNotificationManager.PostedNotification) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 3 with Duration

use of org.awaitility.Duration in project OneSignal-Android-SDK by OneSignal.

the class GenerateNotificationRunner method testNotificationProcessingAndForegroundHandler_completeNotCalled_callsForegroundHandler.

@Test
@Config(shadows = { ShadowGenerateNotification.class, ShadowTimeoutHandler.class })
public void testNotificationProcessingAndForegroundHandler_completeNotCalled_callsForegroundHandler() throws Exception {
    // 1. Setup correct notification extension service class
    startRemoteNotificationReceivedHandlerService("com.test.onesignal.GenerateNotificationRunner$" + "RemoteNotificationReceivedHandler_notificationReceivedCompleteNotCalled");
    // 2. Init OneSignal
    OneSignal.setAppId("b2f7f966-d8cc-11e4-bed1-df8f05be55ba");
    OneSignal.initWithContext(blankActivity);
    OneSignal.setNotificationWillShowInForegroundHandler(notificationReceivedEvent -> {
        lastForegroundNotificationReceivedEvent = notificationReceivedEvent;
        OneSignal.onesignalLog(OneSignal.LOG_LEVEL.DEBUG, "Calling OSNotificationReceivedEvent complete from setNotificationWillShowInForegroundHandler test handler");
        // Call complete to end without waiting default 30 second timeout
        notificationReceivedEvent.complete(notificationReceivedEvent.getNotification());
    });
    threadAndTaskWait();
    blankActivityController.resume();
    threadAndTaskWait();
    // Mock timeout to 1, given that we are not calling complete inside RemoteNotificationReceivedHandler we depend on the timeout complete
    ShadowTimeoutHandler.setMockDelayMillis(1);
    // 3. Receive a notification in foreground
    FCMBroadcastReceiver_processBundle(blankActivity, getBaseNotifBundle());
    threadAndTaskWait();
    // 4. Make sure service was called
    assertNotNull(lastServiceNotificationReceivedEvent);
    // 5. Make sure the last OSNotificationReceivedEvent is not null
    assertNotNull(lastForegroundNotificationReceivedEvent);
    // Notification save is done on the OSTimeoutHandler, we wait until the notification is saved
    Awaitility.await().atMost(new Duration(3, TimeUnit.SECONDS)).pollInterval(new Duration(100, TimeUnit.MILLISECONDS)).untilAsserted(() -> {
        // 6. Make sure 1 notification exists in DB
        assertNotificationDbRecords(1);
    });
}
Also used : Duration(org.awaitility.Duration) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 4 with Duration

use of org.awaitility.Duration in project OneSignal-Android-SDK by OneSignal.

the class InAppMessageIntegrationTests method testAfterLastInAppTimeIsDisplayedOncePerSession.

@Test
public void testAfterLastInAppTimeIsDisplayedOncePerSession() throws Exception {
    final OSTestInAppMessageInternal message1 = InAppMessagingHelpers.buildTestMessageWithSingleTriggerAndRedisplay(OSTriggerKind.SESSION_TIME, null, OSTestTrigger.OSTriggerOperator.GREATER_THAN.toString(), 0.05, 10, 0);
    ArrayList<ArrayList<OSTestTrigger>> triggers2 = new ArrayList<ArrayList<OSTestTrigger>>() {

        {
            add(new ArrayList<OSTestTrigger>() {

                {
                    add(InAppMessagingHelpers.buildTrigger(OSTriggerKind.SESSION_TIME, null, OSTestTrigger.OSTriggerOperator.GREATER_THAN.toString(), 0.1));
                    add(InAppMessagingHelpers.buildTrigger(OSTriggerKind.TIME_SINCE_LAST_IN_APP, null, OSTestTrigger.OSTriggerOperator.GREATER_THAN.toString(), 0.05));
                }
            });
        }
    };
    final OSTestInAppMessageInternal message2 = InAppMessagingHelpers.buildTestMessageWithMultipleTriggersAndRedisplay(triggers2, 10, 0);
    setMockRegistrationResponseWithMessages(new ArrayList<OSTestInAppMessageInternal>() {

        {
            add(message1);
            add(message2);
        }
    });
    // 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(150, TimeUnit.MILLISECONDS)).pollInterval(new Duration(10, TimeUnit.MILLISECONDS)).untilAsserted(() -> {
        assertEquals(1, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
        try {
            assertEquals(message1.getMessageId(), OneSignalPackagePrivateHelper.getShowingInAppMessageId());
        } catch (NullPointerException e) {
            // Awaitility won't retry if something is thrown, but will if an assert fails.
            fail("Should not throw");
        }
    });
    OneSignalPackagePrivateHelper.dismissCurrentMessage();
    // Second in app should now display
    Awaitility.await().atMost(new Duration(1, TimeUnit.SECONDS)).pollInterval(new Duration(100, TimeUnit.MILLISECONDS)).untilAsserted(() -> {
        assertEquals(1, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
        try {
            assertEquals(message2.getMessageId(), OneSignalPackagePrivateHelper.getShowingInAppMessageId());
        } catch (NullPointerException e) {
            // Awaitility won't retry if something is thrown, but will if an assert fails.
            fail("Should not throw");
        }
    });
    OneSignalPackagePrivateHelper.dismissCurrentMessage();
    // Check that the IAM is not displayed again
    assertEquals(0, OneSignalPackagePrivateHelper.getInAppMessageDisplayQueue().size());
}
Also used : OSTestInAppMessageInternal(com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal) ArrayList(java.util.ArrayList) Duration(org.awaitility.Duration) OSTestTrigger(com.onesignal.OneSignalPackagePrivateHelper.OSTestTrigger) Test(org.junit.Test)

Example 5 with Duration

use of org.awaitility.Duration in project OneSignal-Android-SDK by OneSignal.

the class MainOneSignalClassRunner method testNotificationReceivedWhenAppInFocus.

@Test
@Config(shadows = { ShadowGenerateNotification.class })
public void testNotificationReceivedWhenAppInFocus() throws Exception {
    // 1. Init OneSignal
    OneSignal.setAppId(ONESIGNAL_APP_ID);
    OneSignal.initWithContext(blankActivity);
    OneSignal.setNotificationWillShowInForegroundHandler(notificationReceivedEvent -> {
        androidNotificationId = notificationReceivedEvent.getNotification().getAndroidNotificationId();
        notificationReceivedBody = notificationReceivedEvent.getNotification().getBody();
        notificationReceivedEvent.complete(notificationReceivedEvent.getNotification());
    });
    OneSignal.setNotificationOpenedHandler(getNotificationOpenedHandler());
    // 2. Foreground the app
    blankActivityController.resume();
    threadAndTaskWait();
    Bundle bundle = getBaseNotifBundle();
    final boolean[] callbackEnded = { false };
    OneSignalPackagePrivateHelper.ProcessBundleReceiverCallback processBundleReceiverCallback = new OneSignalPackagePrivateHelper.ProcessBundleReceiverCallback() {

        public void onBundleProcessed(OneSignalPackagePrivateHelper.ProcessedBundleResult processedResult) {
            assertNotNull(processedResult);
            assertTrue(processedResult.isProcessed());
            callbackEnded[0] = true;
        }
    };
    FCMBroadcastReceiver_processBundle(blankActivity, bundle, processBundleReceiverCallback);
    Awaitility.await().atMost(new Duration(3, TimeUnit.SECONDS)).pollInterval(new Duration(100, TimeUnit.MILLISECONDS)).untilAsserted(() -> {
        assertTrue(callbackEnded[0]);
    });
    assertNull(lastNotificationOpenedBody);
    assertEquals("Robo test message", notificationReceivedBody);
    assertNotEquals(0, androidNotificationId);
    // Don't fire for duplicates
    lastNotificationOpenedBody = null;
    notificationReceivedBody = null;
    FCMBroadcastReceiver_processBundle(blankActivity, bundle);
    threadAndTaskWait();
    assertNull(lastNotificationOpenedBody);
    assertNull(notificationReceivedBody);
    lastNotificationOpenedBody = null;
    notificationReceivedBody = null;
    // Test that only NotificationReceivedHandler fires
    bundle = getBaseNotifBundle("UUID2");
    FCMBroadcastReceiver_processBundle(blankActivity, bundle);
    threadAndTaskWait();
    assertNull(lastNotificationOpenedBody);
    assertEquals("Robo test message", notificationReceivedBody);
}
Also used : Bundle(android.os.Bundle) GenerateNotificationRunner.getBaseNotifBundle(com.test.onesignal.GenerateNotificationRunner.getBaseNotifBundle) FCMBroadcastReceiver_processBundle(com.onesignal.OneSignalPackagePrivateHelper.FCMBroadcastReceiver_processBundle) Duration(org.awaitility.Duration) OneSignalPackagePrivateHelper(com.onesignal.OneSignalPackagePrivateHelper) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Aggregations

Duration (org.awaitility.Duration)14 Test (org.junit.Test)14 Config (org.robolectric.annotation.Config)7 OSTestInAppMessageInternal (com.onesignal.OneSignalPackagePrivateHelper.OSTestInAppMessageInternal)4 Bundle (android.os.Bundle)3 OneSignalPackagePrivateHelper (com.onesignal.OneSignalPackagePrivateHelper)3 FCMBroadcastReceiver_processBundle (com.onesignal.OneSignalPackagePrivateHelper.FCMBroadcastReceiver_processBundle)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 BinarySerializer (net.morimekta.providence.serializer.BinarySerializer)3 OneSignalPackagePrivateHelper.createInternalPayloadBundle (com.onesignal.OneSignalPackagePrivateHelper.createInternalPayloadBundle)2 IOException (java.io.IOException)2 UncheckedIOException (java.io.UncheckedIOException)2 ExecutorService (java.util.concurrent.ExecutorService)2 MessageGenerator (net.morimekta.providence.util_internal.MessageGenerator)2 ThrowingRunnable (org.awaitility.core.ThrowingRunnable)2 SuppressLint (android.annotation.SuppressLint)1 OSNotification (com.onesignal.OSNotification)1 OSNotificationReceivedEvent (com.onesignal.OSNotificationReceivedEvent)1 OSTestTrigger (com.onesignal.OneSignalPackagePrivateHelper.OSTestTrigger)1 ShadowOneSignalRestClient (com.onesignal.ShadowOneSignalRestClient)1