Espresso 手动广播意图
Espresso broadcasting intents manually
我正在为一项功能编写浓缩咖啡测试,该功能使用广播接收器过滤 Intent.ACTION_TIME_TICK 操作。本质上,系统每分钟触发一次 onReceive(..),我的 UI 可能更新也可能不更新。问题是,我的 UI 测试现在必须休眠 1 分钟,以确保 onReceive(..) 在执行语句之前至少触发一次,假设 onReceive(..) 触发了 UI更新。
我想摆脱睡眠代码并代表系统手动触发 onReceive(..),但是 Android 记录了 Intent.ACTION_TIME_TICK 这是不可能的。
This is a protected intent that can only be sent by the system.
是否有可能实现我想要的或者完全不同的方法?
最终引用了最上面的 activity 运行,并广播了消息本身。我之所以能够做到这一点,是因为实现位于所有活动扩展的基础 activity 中。方法 getCurrentActivity()
实现可以在 .
中找到
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);
}
}
}
}
我正在为一项功能编写浓缩咖啡测试,该功能使用广播接收器过滤 Intent.ACTION_TIME_TICK 操作。本质上,系统每分钟触发一次 onReceive(..),我的 UI 可能更新也可能不更新。问题是,我的 UI 测试现在必须休眠 1 分钟,以确保 onReceive(..) 在执行语句之前至少触发一次,假设 onReceive(..) 触发了 UI更新。
我想摆脱睡眠代码并代表系统手动触发 onReceive(..),但是 Android 记录了 Intent.ACTION_TIME_TICK 这是不可能的。
This is a protected intent that can only be sent by the system.
是否有可能实现我想要的或者完全不同的方法?
最终引用了最上面的 activity 运行,并广播了消息本身。我之所以能够做到这一点,是因为实现位于所有活动扩展的基础 activity 中。方法 getCurrentActivity()
实现可以在
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);
}
}
}
}