Tuesday, April 17, 2012

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 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?


Source: Tips4all

4 comments:

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

    ReplyDelete
  2. 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 creation of mock implementations.

    ReplyDelete
  3. 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.

    ReplyDelete
  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 to the subject activity and run the tests using Android JUnit Test.

    ReplyDelete