IncomingPushRunnable implements Runnable { @Override public void run() { Autopilot.automaticTakeOff(context); long airshipWaitTime = isLongRunning ? LONG_AIRSHIP_WAIT_TIME_MS : AIRSHIP_WAIT_TIME_MS; UAirship airship = UAirship.waitForTakeOff(airshipWaitTime); if (airship == null) { Logger.error("Unable to process push, Airship is not ready. Make sure takeOff is called by either using autopilot or by calling takeOff in the application's onCreate method."); return; } if (!message.isAccengagePush() && !message.isAirshipPush()) { Logger.debug("Ignoring push: %s", message); return; } if (checkProvider(airship, providerClass)) { if (isProcessed) { postProcessPush(airship); } else { processPush(airship); } } } private  IncomingPushRunnable(@NonNull Builder builder); @Override void run();  }
@Test public void testDisplayNotification() { when(pushManager.isComponentEnabled()).thenReturn(true); when(pushManager.isPushEnabled()).thenReturn(true); when(pushManager.isOptIn()).thenReturn(true); when(pushManager.isUniqueCanonicalId("testPushID")).thenReturn(true); notificationProvider.notification = createNotification(); notificationProvider.tag = "testNotificationTag"; pushRunnable.run(); verify(notificationManager).notify("testNotificationTag", TEST_NOTIFICATION_ID, notificationProvider.notification); verify(analytics).addEvent(any(PushArrivedEvent.class)); ShadowPendingIntent shadowPendingIntent = Shadows.shadowOf(notificationProvider.notification.contentIntent); assertTrue("The pending intent is an activity intent.", shadowPendingIntent.isActivityIntent()); Intent intent = shadowPendingIntent.getSavedIntent(); assertEquals("The intent action should match.", intent.getAction(), PushManager.ACTION_NOTIFICATION_RESPONSE); assertBundlesEquals("The push message bundles should match.", pushBundle, intent.getExtras().getBundle(PushManager.EXTRA_PUSH_MESSAGE_BUNDLE)); assertEquals("One category should exist.", 1, intent.getCategories().size()); verify(pushListener).onPushReceived(message, true); verify(notificationListener).onNotificationPosted(any(NotificationInfo.class)); }
@Test public void test() { when(pushManager.isComponentEnabled()).thenReturn(true); when(pushManager.isPushEnabled()).thenReturn(true); when(pushManager.isOptIn()).thenReturn(true); when(pushManager.isUniqueCanonicalId("testPushID")).thenReturn(true); pushRunnable = new IncomingPushRunnable.Builder(TestApplication.getApplication()) .setProviderClass("wrong class") .setMessage(new PushMessage(pushBundle)) .setLongRunning(true) .setNotificationManager(notificationManager) .build(); pushRunnable.run(); verifyZeroInteractions(notificationManager); }
@Test public void testInvalidPushProvider() { when(pushManager.isComponentEnabled()).thenReturn(true); when(pushManager.isPushEnabled()).thenReturn(true); when(pushManager.isOptIn()).thenReturn(true); when(pushManager.isUniqueCanonicalId("testPushID")).thenReturn(true); pushRunnable = new IncomingPushRunnable.Builder(TestApplication.getApplication()) .setProviderClass("wrong class") .setMessage(new PushMessage(pushBundle)) .setLongRunning(true) .setNotificationManager(notificationManager) .build(); pushRunnable.run(); verifyZeroInteractions(notificationManager); }
@Test public void testUserNotificationsDisabled() { when(pushManager.isComponentEnabled()).thenReturn(true); when(pushManager.isPushEnabled()).thenReturn(true); when(pushManager.isOptIn()).thenReturn(false); when(pushManager.isUniqueCanonicalId("testPushID")).thenReturn(true); pushRunnable.run(); verify(pushListener).onPushReceived(message, false); }
@Test public void testBackgroundPush() { when(pushManager.isComponentEnabled()).thenReturn(true); when(pushManager.isPushEnabled()).thenReturn(true); when(pushManager.isOptIn()).thenReturn(true); when(pushManager.isUniqueCanonicalId("testPushID")).thenReturn(true); pushRunnable.run(); verify(pushListener).onPushReceived(message, false); }
@Test public void testNotificationProviderException() { when(pushManager.isComponentEnabled()).thenReturn(true); when(pushManager.isPushEnabled()).thenReturn(true); when(pushManager.isOptIn()).thenReturn(true); when(pushManager.isUniqueCanonicalId("testPushID")).thenReturn(true); notificationProvider = new TestNotificationProvider() { @NonNull @Override public NotificationResult onCreateNotification(@NonNull Context context, @NonNull NotificationArguments arguments) { throw new RuntimeException("Unable to create and display notification."); } }; pushRunnable.run(); verify(notificationManager, Mockito.never()).notify(Mockito.anyString(), Mockito.anyInt(), any(Notification.class)); verify(jobDispatcher, Mockito.never()).dispatch(any(JobInfo.class)); }
@Test public void testNotificationProviderSuccess() { notificationProvider.notification = createNotification(); when(pushManager.isComponentEnabled()).thenReturn(true); when(pushManager.isPushEnabled()).thenReturn(true); when(pushManager.isOptIn()).thenReturn(true); when(pushManager.isUniqueCanonicalId("testPushID")).thenReturn(true); pushRunnable.run(); verify(notificationManager).notify(null, TEST_NOTIFICATION_ID, notificationProvider.notification); verify(jobDispatcher, Mockito.never()).dispatch(any(JobInfo.class)); }
@Test public void testAccengageNotificationProvider() { accengageNotificationProvider.notification = createNotification(); when(pushManager.isComponentEnabled()).thenReturn(true); when(pushManager.isPushEnabled()).thenReturn(true); when(pushManager.isOptIn()).thenReturn(true); when(pushManager.isUniqueCanonicalId(null)).thenReturn(true); IncomingPushRunnable pushRunnable = new IncomingPushRunnable.Builder(TestApplication.getApplication()) .setProviderClass(testPushProvider.getClass().toString()) .setMessage(accengageMessage) .setNotificationManager(notificationManager) .setLongRunning(true) .setJobDispatcher(jobDispatcher) .build(); pushRunnable.run(); verify(notificationManager).notify(null, TEST_NOTIFICATION_ID, accengageNotificationProvider.notification); verify(jobDispatcher, Mockito.never()).dispatch(any(JobInfo.class)); }
@Test public void testNotificationProviderResultCancel() { when(pushManager.isComponentEnabled()).thenReturn(true); when(pushManager.isPushEnabled()).thenReturn(true); when(pushManager.isOptIn()).thenReturn(true); when(pushManager.isUniqueCanonicalId("testPushID")).thenReturn(true); notificationProvider = new TestNotificationProvider() { @NonNull @Override public NotificationResult onCreateNotification(@NonNull Context context, @NonNull NotificationArguments arguments) { return NotificationResult.cancel(); } }; displayRunnable.run(); verify(notificationManager, Mockito.never()).notify(Mockito.anyString(), Mockito.anyInt(), any(Notification.class)); verify(jobDispatcher, Mockito.never()).dispatch(any(JobInfo.class)); }
@Test public void testNotificationFactoryResultRetry() { when(pushManager.isComponentEnabled()).thenReturn(true); when(pushManager.isPushEnabled()).thenReturn(true); when(pushManager.isOptIn()).thenReturn(true); when(pushManager.isUniqueCanonicalId("testPushID")).thenReturn(true); notificationProvider = new TestNotificationProvider() { @NonNull @Override public NotificationResult onCreateNotification(@NonNull Context context, @NonNull NotificationArguments arguments) { return NotificationResult.retry(); } }; displayRunnable.run(); verify(notificationManager, Mockito.never()).notify(Mockito.anyString(), Mockito.anyInt(), any(Notification.class)); verify(jobDispatcher).dispatch(any(JobInfo.class)); }
@Test public void testNotificationFactoryResultOK() { when(pushManager.isComponentEnabled()).thenReturn(true); when(pushManager.isPushEnabled()).thenReturn(true); when(pushManager.isOptIn()).thenReturn(true); when(pushManager.isUniqueCanonicalId("testPushID")).thenReturn(true); notificationProvider.notification = createNotification(); displayRunnable.run(); verify(notificationManager).notify(null, TEST_NOTIFICATION_ID, notificationProvider.notification); verify(jobDispatcher, Mockito.never()).dispatch(any(JobInfo.class)); }
@Test public void testNotificationContentIntent() { when(pushManager.isComponentEnabled()).thenReturn(true); when(pushManager.isPushEnabled()).thenReturn(true); when(pushManager.isOptIn()).thenReturn(true); when(pushManager.isUniqueCanonicalId("testPushID")).thenReturn(true); PendingIntent pendingIntent = PendingIntent.getBroadcast(RuntimeEnvironment.application, 1, new Intent(), 0); notificationProvider.notification = createNotification(); notificationProvider.tag = "cool-tag"; notificationProvider.notification.contentIntent = pendingIntent; pushRunnable.run(); ShadowPendingIntent shadowPendingIntent = Shadows.shadowOf(notificationProvider.notification.contentIntent); assertTrue("The pending intent is an activity intent.", shadowPendingIntent.isActivityIntent()); Intent intent = shadowPendingIntent.getSavedIntent(); assertEquals("The intent action should match.", intent.getAction(), PushManager.ACTION_NOTIFICATION_RESPONSE); assertEquals("One category should exist.", 1, intent.getCategories().size()); assertNotNull("The notification content intent is not null.", pendingIntent); assertSame("The notification content intent matches.", pendingIntent, intent.getExtras().get(PushManager.EXTRA_NOTIFICATION_CONTENT_INTENT)); assertSame( "cool-tag", intent.getExtras().get(PushManager.EXTRA_NOTIFICATION_TAG)); assertSame( TEST_NOTIFICATION_ID, intent.getExtras().get(PushManager.EXTRA_NOTIFICATION_ID)); }
@Test public void testNotificationDeleteIntent() { when(pushManager.isComponentEnabled()).thenReturn(true); when(pushManager.isPushEnabled()).thenReturn(true); when(pushManager.isOptIn()).thenReturn(true); when(pushManager.isUniqueCanonicalId("testPushID")).thenReturn(true); PendingIntent pendingIntent = PendingIntent.getBroadcast(RuntimeEnvironment.application, 1, new Intent(), 0); notificationProvider.notification = createNotification(); notificationProvider.notification.deleteIntent = pendingIntent; pushRunnable.run(); ShadowPendingIntent shadowPendingIntent = Shadows.shadowOf(notificationProvider.notification.deleteIntent); assertTrue("The pending intent is broadcast intent.", shadowPendingIntent.isBroadcastIntent()); Intent intent = shadowPendingIntent.getSavedIntent(); assertEquals("The intent action should match.", intent.getAction(), PushManager.ACTION_NOTIFICATION_DISMISSED); assertEquals("One category should exist.", 1, intent.getCategories().size()); assertNotNull("The notification delete intent is not null.", pendingIntent); assertSame("The notification delete intent matches.", pendingIntent, intent.getExtras().get(PushManager.EXTRA_NOTIFICATION_DELETE_INTENT)); }
@Test @Config(sdk = 25) public void testDeliverPushPreOreo() { when(pushManager.isComponentEnabled()).thenReturn(true); when(pushManager.isPushEnabled()).thenReturn(true); when(pushManager.isOptIn()).thenReturn(true); when(pushManager.isUniqueCanonicalId("testPushID")).thenReturn(true); NotificationChannelCompat channelCompat = new NotificationChannelCompat(TEST_NOTIFICATION_CHANNEL_ID, "Test Notification Channel", NotificationManager.IMPORTANCE_HIGH); channelCompat.setSound(Uri.parse("cool: channelCompat.enableVibration(true); channelCompat.enableLights(true); channelCompat.setLightColor(123); when(mockChannelRegistry.getNotificationChannelSync(TEST_NOTIFICATION_CHANNEL_ID)).thenReturn(channelCompat); notificationProvider.notification = createNotification(); pushRunnable.run(); Notification notification = notificationProvider.notification; assertEquals(notification.sound, channelCompat.getSound()); assertEquals(NotificationManager.IMPORTANCE_HIGH, channelCompat.getImportance()); assertEquals(notification.defaults & Notification.DEFAULT_VIBRATE, Notification.DEFAULT_VIBRATE); assertEquals(notification.ledARGB, channelCompat.getLightColor()); }