【问题标题】:Espresso broadcasting intents manuallyEspresso 手动广播意图
【发布时间】:2016-12-22 20:36:24
【问题描述】:

我正在为一项功能编写浓缩咖啡测试,该功能使用广播接收器过滤Intent.ACTION_TIME_TICK 操作。本质上,onReceive(..) 由系统每分钟触发一次,我的 UI 可能会更新,也可能不会更新。问题是,我的 UI 测试现在必须休眠 1 分钟,以确保 onReceive(..) 在执行语句之前至少触发一次,假设 onReceive(..) 已触发 UI 更新。

我想摆脱睡眠代码并代表系统手动触发 onReceive(..),但 Android 文档使用 Intent.ACTION_TIME_TICK 是不可能的。

这是一个受保护的意图,只能由系统发送。

是否有可能实现我想要的或者完全不同的方法?

【问题讨论】:

    标签: android android-intent broadcastreceiver automated-tests android-espresso


    【解决方案1】:

    最终引用了正在运行的最顶部的活动,并广播消息本身。我之所以能够做到这一点,是因为实现存在于所有活动扩展的基础活动中。方法getCurrentActivity()的实现可以在here找到。

    public static void triggerACTION_TICK_BROADCAST() {
        Activity activity = getCurrentActivity();
        if (activity != null) {
            if (activity instanceof BaseActivity) {
                BaseActivity baseActivity = (BaseActivity) activity;
                BroadcastReceiver tickReceiver = baseActivity.getTickReceiver();
                if (tickReceiver != null) {
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_TIME_TICK);
                    tickReceiver.onReceive(InstrumentationRegistry.getContext(), intent);
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      相关资源
      最近更新 更多