我可以使用 Android 的测试框架测试状态栏通知吗?

发布于 2024-09-30 00:35:20 字数 88 浏览 1 评论 0原文

我有一个在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

§对你不离不弃 2024-10-07 00:35:20

也许不是您正在寻找的答案,但像往常一样,解决方案是:

  • 创建一个抽象通知功能的接口。
  • 创建委托给系统通知 API 的默认实现。
  • 运行测试时,用支持测试的模拟实现替换(或装饰)默认实现。

这可以通过以下技术来简化:

  • 依赖注入框架(例如 RoboGuice)来简化实现的选择。
  • 模拟库(例如 EasyMock)可自动创建模拟实现。

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.
情释 2024-10-07 00:35:20

尝试通过扩展 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.

失与倦" 2024-10-07 00:35:20

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

二手情话 2024-10-07 00:35:20

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.

≈。彩虹 2024-10-07 00:35:20

您可以使用 Android Marshmallow (API 23) 模拟器来进行此类测试。

例如:

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));
    }
}

重要的部分是在向 Google FCM http 端点发送多播消息后调用NotificationManager (mManager) 以检查其是否具有活动通知。

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.

征棹 2024-10-07 00:35:20

通过在发送之前将通知详细信息存储在数据存储中并在用户通过通知栏进入应用程序时标记为已确认来解决类似的情况。
由于 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.

烟酒忠诚 2024-10-07 00:35:20

查看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

孤独岁月 2024-10-07 00:35:20

只是为了节省其他人一些时间:看起来应该检测通知但实际上没有检测到的事情是尝试使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文