I have a class that sends a status bar notification in Android. I can't find a way to test whether the notification was sent or not, which makes it very hard to write any kind of useful unit test.
Does anyone have a solution for this?
I have a class that sends a status bar notification in Android. I can't find a way to test whether the notification was sent or not, which makes it very hard to write any kind of useful unit test.
Does anyone have a solution for this?
Maybe not the answer you are looking for, but as usual, the solution is to:
This can be simplified with the following technology:
Try to create a Mock object by extending NotificationManager and override the notify()
methods. The overridden functions can be asserted. In your test case(s), inject the Mock into the subject activity and run the tests using Android JUnit Test.
Robotium cannot interact with the notification bar. You are restricted to one application. Check FAQ for robotium, there is mention of this: http://code.google.com/p/robotium/wiki/QuestionsAndAnswers
This is what you want I think. The uiautomator.
Works only on Android 4.1 or above. You need run the sample code first maybe.
You can write a piece of code to perform the pull-down-status-bar action and then do your things. This is only UI testing. You can't test your data with uiautomator.
Hope this would help.
You can use an emulator, just for this kind of test, with Android Marshmallow (API 23).
For example:
public void testCheckNotification_2() throws IOException, InterruptedException, GcmException
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
GcmResponse gcmResponse = endPointImp.sendGcmMulticastRequestImp(getGcmMultiRequest());
assertThat(gcmResponse.getSuccess(), is(1));
Thread.sleep(2000);
assertThat(mManager.getActiveNotifications().length, is(1));
StatusBarNotification barNotification = mManager.getActiveNotifications()[0];
assertThat(barNotification.getId(), is(INCIDENCIA.getTitleRsc()));
// We check the pending intent.
PendingIntent pendingIntent = barNotification.getNotification().contentIntent;
assertThat(pendingIntent.getCreatorPackage(), is(GcmRequest.PACKAGE_DIDEKINDROID));
}
}
The important part is the call to the NotificationManager (mManager) to check that it has active notifications after sending a multicast message to the Google FCM http endpoint.
Solved a similar kind of situation by storing notification detail in data store before sending and marking as acknowledged whenever user enters into application through notification bar. As robotium is restricted to application boundaries it can't access system data hence tested the behavior using junit/jmockit based unit test.
Check out NotificationListenerService:
"A service that receives calls from the system when new notifications are posted or removed, or their ranking changed."
Just to save others some time: a thing that looks like it should detect notifications, but doesn't, is trying to create a PendingIntent with the NO_CREATE flag.
Since attempts to create a PendingIntent with the same values as an existing notification, but with the NO_CREATE flag, should return null, it seemed that would tell whether a notification already had one or not.
Sadly, this doesn't seem to be reliable.
© 2022 - 2024 — McMap. All rights reserved.