开发者

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

I have a class that sends a status bar notification in Android. I can't find a way to test whether the notificati开发者_如何学Goon 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:

  • 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.


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


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.


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."

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜