Search in sources :

Example 11 with Duration

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

the class GenerateNotificationRunner method testNotificationWillShowInForegroundHandler_notCallCompleteShowsNotificationAfterTimeout.

@Test
@Config(shadows = { ShadowGenerateNotification.class, ShadowTimeoutHandler.class, ShadowNotificationReceivedEvent.class })
public void testNotificationWillShowInForegroundHandler_notCallCompleteShowsNotificationAfterTimeout() throws Exception {
    OneSignal.setLogLevel(OneSignal.LOG_LEVEL.VERBOSE, OneSignal.LOG_LEVEL.NONE);
    // 1. Init OneSignal
    OneSignal.setAppId("b2f7f966-d8cc-11e4-bed1-df8f05be55ba");
    OneSignal.initWithContext(blankActivity);
    OneSignal.setNotificationWillShowInForegroundHandler(notificationReceivedEvent -> {
        callbackCounter++;
        lastForegroundNotificationReceivedEvent = notificationReceivedEvent;
    });
    threadAndTaskWait();
    blankActivityController.resume();
    threadAndTaskWait();
    // Mock timeout to 1, given that we are not calling complete inside NotificationWillShowInForegroundHandler we depend on the timeout complete
    ShadowTimeoutHandler.setMockDelayMillis(1);
    // 2. Receive a notification
    FCMBroadcastReceiver_processBundle(blankActivity, getBaseNotifBundle());
    threadAndTaskWait();
    // 3. Make sure the AppNotificationJob is not null
    assertNotNull(lastForegroundNotificationReceivedEvent);
    // 4. Make sure the callback counter is only fired once for App NotificationWillShowInForegroundHandler
    assertEquals(1, callbackCounter);
    // Complete being call from Notification Receiver Handler thread
    Awaitility.await().atMost(new Duration(5, TimeUnit.SECONDS)).pollInterval(new Duration(500, TimeUnit.MILLISECONDS)).untilAsserted(() -> {
        // 5. Make sure 1 notification exists in DB
        assertNotificationDbRecords(1);
        // 6. Notification not opened then badges should be 1
        assertEquals(1, ShadowBadgeCountUpdater.lastCount);
    });
}
Also used : Duration(org.awaitility.Duration) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 12 with Duration

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

the class GenerateNotificationRunner method testNotificationWillShowInForegroundHandler_silentNotificationNotSavedUntilTimerCompleteIsDone.

@Test
@Config(shadows = { ShadowGenerateNotification.class })
public void testNotificationWillShowInForegroundHandler_silentNotificationNotSavedUntilTimerCompleteIsDone() throws Exception {
    // 1. Init OneSignal
    OneSignal.setAppId("b2f7f966-d8cc-11e4-bed1-df8f05be55ba");
    OneSignal.initWithContext(blankActivity);
    OneSignal.setNotificationWillShowInForegroundHandler(notificationReceivedEvent -> {
        callbackCounter++;
        lastForegroundNotificationReceivedEvent = notificationReceivedEvent;
        new Thread(() -> {
            try {
                // Simulate long work
                Thread.sleep(5000);
                // Check notification is still not saved
                assertNotificationDbRecords(0);
                notificationReceivedEvent.complete(null);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    });
    threadAndTaskWait();
    blankActivityController.resume();
    threadAndTaskWait();
    // 2. Receive a notification
    FCMBroadcastReceiver_processBundle(blankActivity, getBaseNotifBundle());
    threadAndTaskWait();
    // 3. Make sure the AppNotificationJob is not null
    assertNotNull(lastForegroundNotificationReceivedEvent);
    // 4. Make sure the callback counter is only fired once for App NotificationWillShowInForegroundHandler
    assertEquals(1, callbackCounter);
    // 5. Make sure 0 notification exists in DB because complete was not called yet
    assertNotificationDbRecords(0);
    // Complete being call from User thread
    Awaitility.await().atMost(new Duration(10, TimeUnit.SECONDS)).pollInterval(new Duration(500, TimeUnit.MILLISECONDS)).untilAsserted(() -> {
        // Make sure 1 notification exists in DB
        assertNotificationDbRecords(1, true);
    });
}
Also used : Duration(org.awaitility.Duration) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 13 with Duration

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

the class QueuedFileMessageWriterTest method testAFewWrites_serviceCalls.

@Test
@SuppressWarnings("unchecked")
public void testAFewWrites_serviceCalls() throws IOException, InterruptedException {
    setDefaultPollDelay(new Duration(10, TimeUnit.MILLISECONDS));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IOMessageWriter target = new IOMessageWriter(baos, new BinarySerializer());
    QueuedMessageWriter writer = new QueuedMessageWriter(target);
    ExecutorService executorService = Executors.newFixedThreadPool(11);
    for (int i = 0; i < 10; ++i) {
        executorService.submit(() -> {
            MessageGenerator generator = new MessageGenerator();
            for (int j = 0; j < 10; ++j) {
                try {
                    if (j > 0)
                        sleep(1L);
                    PServiceCall tmp = new PServiceCall("test", PServiceCallType.CALL, j, generator.generate(CompactFields.kDescriptor));
                    writer.write(tmp);
                } catch (IOException e) {
                    throw new UncheckedIOException(e.getMessage(), e);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }
    sleep(10L);
    executorService.shutdown();
    assertTrue(executorService.awaitTermination(10, TimeUnit.SECONDS));
    sleep(1L);
    writer.close();
}
Also used : MessageGenerator(net.morimekta.providence.util_internal.MessageGenerator) Duration(org.awaitility.Duration) UncheckedIOException(java.io.UncheckedIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) PServiceCall(net.morimekta.providence.PServiceCall) ExecutorService(java.util.concurrent.ExecutorService) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) Test(org.junit.Test)

Example 14 with Duration

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

the class QueuedFileMessageWriterTest method testAFewWrites.

@Test
@SuppressWarnings("unchecked")
public void testAFewWrites() throws IOException, InterruptedException {
    setDefaultPollDelay(new Duration(10, TimeUnit.MILLISECONDS));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IOMessageWriter target = new IOMessageWriter(baos, new BinarySerializer());
    QueuedMessageWriter writer = new QueuedMessageWriter(target);
    ExecutorService executorService = Executors.newFixedThreadPool(11);
    for (int i = 0; i < 10; ++i) {
        executorService.submit(() -> {
            MessageGenerator generator = new MessageGenerator();
            for (int j = 0; j < 10; ++j) {
                try {
                    if (j > 0)
                        sleep(1L);
                    writer.write(generator.generate(CompactFields.kDescriptor));
                } catch (IOException e) {
                    throw new UncheckedIOException(e.getMessage(), e);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    Thread.currentThread().interrupt();
                }
            }
        });
    }
    sleep(10L);
    executorService.shutdown();
    assertTrue(executorService.awaitTermination(10, TimeUnit.SECONDS));
    sleep(1L);
    writer.close();
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    List<CompactFields> result = MessageStreams.stream(bais, new BinarySerializer(), CompactFields.kDescriptor).collect(Collectors.toList());
    assertThat(result, hasSize(100));
}
Also used : MessageGenerator(net.morimekta.providence.util_internal.MessageGenerator) Duration(org.awaitility.Duration) UncheckedIOException(java.io.UncheckedIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) CompactFields(net.morimekta.test.providence.core.CompactFields) ByteArrayInputStream(java.io.ByteArrayInputStream) ExecutorService(java.util.concurrent.ExecutorService) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) Test(org.junit.Test)

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