我可以使用 Android 的测试框架测试状态栏通知吗?
我有一个在 Android 中发送状态栏通知的类。我找不到方法来测试通知是否已发送,这使得编写任何有用的单元测试变得非常困难。
有人有解决方案吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
也许不是您正在寻找的答案,但像往常一样,解决方案是:
这可以通过以下技术来简化:
Maybe not the answer you are looking for, but as usual, the solution is to:
This can be simplified with the following technology:
尝试通过扩展 NotificationManager 来创建 Mock 对象并覆盖
notify()
方法。可以断言被覆盖的函数。在您的测试用例中,将 Mock 注入主题活动并使用 Android JUnit Test 运行测试。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 无法与通知栏交互。您仅限于一项应用程序。查看 Robotium 的常见问题解答,其中提到了这一点:
http://code.google.com/p/robotium/wiki/QuestionsAndAnswers
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 测试
我想这就是你想要的。 ui 自动化器。
仅适用于 Android 4.1 或更高版本。也许您需要先运行示例代码。
您可以编写一段代码来执行下拉状态栏操作,然后执行您的操作。这只是 UI 测试。您无法使用 uiautomator 测试您的数据。
希望这会有所帮助。
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.
您可以使用 Android Marshmallow (API 23) 模拟器来进行此类测试。
例如:
重要的部分是在向 Google FCM http 端点发送多播消息后调用NotificationManager (mManager) 以检查其是否具有活动通知。
You can use an emulator, just for this kind of test, with Android Marshmallow (API 23).
For example:
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.
通过在发送之前将通知详细信息存储在数据存储中并在用户通过通知栏进入应用程序时标记为已确认来解决类似的情况。
由于 Robotium 仅限于应用程序边界,因此它无法访问系统数据,因此使用基于 junit/jmockit 的单元测试来测试行为。
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.
查看NotificationListenerService:
“当新通知发布或删除或者其排名发生变化时接收系统调用的服务。”
https://developer.android.com/reference/android/service/notification /NotificationListenerService.html
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
只是为了节省其他人一些时间:看起来应该检测通知但实际上没有检测到的事情是尝试使用 NO_CREATE 标志创建一个 PendingIntent。
由于尝试创建与现有通知具有相同值但带有 NO_CREATE 标志的 PendingIntent 应该返回 null,因此似乎可以判断通知是否已经有一个通知。
遗憾的是,这似乎并不可靠。
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.