Skip to content

Commit 3564fca

Browse files
committed
Unit test for display notif with NSE and foreground handler
When the NSE calls `complete` with `notification.mutableCopy()` rather than the original notification, and the foreground handler is also set, the notification will not display. This test is added to replicate that behavior. We will address the bug in the following commit to get the test to pass.
1 parent 2e1fd4a commit 3564fca

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

OneSignalSDK/unittest/src/test/java/com/test/onesignal/GenerateNotificationRunner.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,6 +1983,64 @@ public void remoteNotificationReceived(Context context, OSNotificationReceivedEv
19831983
}
19841984
}
19851985

1986+
@Test
1987+
@Config(shadows = { ShadowGenerateNotification.class })
1988+
public void testNotificationProcessingAndForegroundHandler_callCompleteWithMutableNotification_displays() throws Exception {
1989+
// 1. Setup correct notification extension service class
1990+
startRemoteNotificationReceivedHandlerService(
1991+
RemoteNotificationReceivedHandler_notificationReceivedCallCompleteWithMutableNotification
1992+
.class
1993+
.getName()
1994+
);
1995+
1996+
// 2. Init OneSignal
1997+
OneSignal.setAppId("b2f7f966-d8cc-11e4-bed1-df8f05be55ba");
1998+
OneSignal.initWithContext(blankActivity);
1999+
OneSignal.setNotificationWillShowInForegroundHandler(notificationReceivedEvent -> {
2000+
lastForegroundNotificationReceivedEvent = notificationReceivedEvent;
2001+
2002+
// Call complete to end without waiting default 30 second timeout
2003+
notificationReceivedEvent.complete(notificationReceivedEvent.getNotification());
2004+
});
2005+
threadAndTaskWait();
2006+
2007+
blankActivityController.resume();
2008+
threadAndTaskWait();
2009+
2010+
// 3. Receive a notification in foreground
2011+
FCMBroadcastReceiver_processBundle(blankActivity, getBaseNotifBundle());
2012+
threadAndTaskWait();
2013+
2014+
// 4. Make sure service was called
2015+
assertNotNull(lastServiceNotificationReceivedEvent);
2016+
2017+
// 5. Make sure foreground handler was called
2018+
assertNotNull(lastForegroundNotificationReceivedEvent);
2019+
2020+
// 6. Make sure running on main thread check is called, this is only called for showing the notification
2021+
assertTrue(ShadowGenerateNotification.isRunningOnMainThreadCheckCalled());
2022+
2023+
// 7. Check badge count to represent the notification is displayed
2024+
assertEquals(1, ShadowBadgeCountUpdater.lastCount);
2025+
}
2026+
2027+
/**
2028+
* @see #testNotificationProcessingAndForegroundHandler_callCompleteWithMutableNotification_displays
2029+
*/
2030+
public static class RemoteNotificationReceivedHandler_notificationReceivedCallCompleteWithMutableNotification implements OneSignal.OSRemoteNotificationReceivedHandler {
2031+
2032+
@Override
2033+
public void remoteNotificationReceived(final Context context, OSNotificationReceivedEvent receivedEvent) {
2034+
lastServiceNotificationReceivedEvent = receivedEvent;
2035+
2036+
OSNotification notification = receivedEvent.getNotification();
2037+
OSMutableNotification mutableNotification = notification.mutableCopy();
2038+
2039+
// Complete is called to end NotificationProcessingHandler
2040+
receivedEvent.complete(mutableNotification);
2041+
}
2042+
}
2043+
19862044
@Test
19872045
@Config(shadows = { ShadowGenerateNotification.class })
19882046
public void testNotificationWillShowInForegroundHandlerIsCallWhenReceivingNotificationInForeground() throws Exception {

0 commit comments

Comments
 (0)