Can I test status bar notifications using Android's testing framework?
Asked Answered
W

8

35

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?

Walsh answered 28/10, 2010 at 17:12 Comment(1)
There's a test framework out there named robotium, but I don't know if it supports notifications. But in case the standard junit lib doesn't support it, you can check there maybe. code.google.com/p/robotiumOutward
U
6

Maybe not the answer you are looking for, but as usual, the solution is to:

  • Create an interface abstracting the notification functionality.
  • Create a default implementation that delegates to the system notification API.
  • When running tests replace (or decorate) the default implementation with a mock implementation that supports testing.

This can be simplified with the following technology:

  • Dependency injection frameworks (such as RoboGuice) to streamline the choice of implementation.
  • Mocking library (such as EasyMock) to automate the creation of mock implementations.
Ubiquitarian answered 9/1, 2012 at 6:10 Comment(0)
A
4

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.

Abbott answered 1/4, 2012 at 19:18 Comment(0)
L
2

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

Lordinwaiting answered 6/4, 2011 at 21:48 Comment(0)
C
2

UI Testing

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.

Cordate answered 5/1, 2013 at 7:20 Comment(0)
L
2

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.

Lundberg answered 8/6, 2016 at 15:55 Comment(0)
D
1

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.

Disinclined answered 8/2, 2012 at 12:55 Comment(0)
P
0

Check out NotificationListenerService:

"A service that receives calls from the system when new notifications are posted or removed, or their ranking changed."

https://developer.android.com/reference/android/service/notification/NotificationListenerService.html

Pleione answered 7/9, 2015 at 1:48 Comment(0)
B
-1

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.

Bankrupt answered 12/12, 2013 at 9:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.