【发布时间】:2011-08-30 05:28:32
【问题描述】:
为了对涉及 Android 状态栏的代码使用测试驱动的开发原则,我需要编写测试来验证是否已达到预期状态。例如,像下面这样的小单元测试可以让我验证我打算发布的通知是否实际显示:
public class NotificationTest extends AndroidTestCase {
public void testCanNotify() {
// setup SUT
NotificationManager mgr = (NotificationManager) getContext().getSystemService(
Context.NOTIFICATION_SERVICE);
Notification notif = new Notification(android.R.drawable.stat_notify_error, "ticker", 0);
PendingIntent intent = PendingIntent.getActivity(getContext(), 0, null, 0);
notif.setLatestEventInfo(getContext().getApplicationContext(), "title", "text", intent);
int id = 123;
// exercise SUT
mgr.notify(id, notif);
// verify result
// QUESTION: HERE I WOULD LIKE TO SAY:
// assertTrue(mgr.isShowing(id));
// teardown
mgr.cancel(id);
}
}
因此,如示例所示,通知管理器本身似乎没有任何诊断方法,例如 isShowing(id) 或 get(id) 可用于测试中的“验证”步骤。
我看过优秀的 Robotium 工具包,但它们非常具体地测试单个应用程序,因此它们不包括通知。
有人知道解决办法吗?
【问题讨论】: