Issue
I have an espresso test I'm writing for a feature, and the feature uses a broadcast receiver filtering the Intent.ACTION_TIME_TICK action. Essentially, the onReceive(..) is triggered every minute by the system, and my UI may or may not update. Problem is, my UI test right now has to sleep for 1 minute to ensure the onReceive(..) has triggered at least once before executing a statement assuming the onReceive(..) has triggered the UI update.
I want to get rid of the sleep code and manually trigger the onReceive(..) on behalf of the system, but Android documents this is not possible with Intent.ACTION_TIME_TICK.
This is a protected intent that can only be sent by the system.
Is it possible to achieve what I want or perhaps a different approach entirely?
Solution
Ended up referencing the top most activity running, and broadcasting the message itself. I was able to do this because the implementation lives in the base activity which all activities extend from. The method getCurrentActivity()
implementation can be found 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);
}
}
}
}
Answered By - Ryhan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.