如果在Activity的onCreate中启动了长时间运行的任务,则Espresso空转资源无法正常工作吗? [英] Espresso idling resource not working if long running task is started in Activity's onCreate?
问题描述
如果在Activity的onCreate中启动了长时间运行的任务,如何使用意式浓缩咖啡空闲资源?
How to use espresso idling resource if long running task is started in Activity's onCreate ?
我已经创建了一个自定义的IdlingResource,如果通过click事件触发了长时间的异步方法调用,则可以正常工作,但是只要在Acitivty的onCreate方法中调用该方法,它就会中断.
I have created a custom IdlingResource and it is working fine if the long async method call is triggered by click event, but breaks whenever the it is called in Acitivty's onCreate method.
示例:
public void onBtnClick(){
setIdle(true); // This works fine, our tests wait until setIdle(false) is called
doSomeBackgroundTask();
}
public void onDone(){
setResourceIdle(false);
setIdle(false);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setIdle(true); // This doesn't work, our tests won't wait
doSomeBackgroundTask();
}
有什么办法可以克服这种情况吗?
Any ideas to overcome this situation?
推荐答案
在活动onCreate之前尝试注册您的idlingResource.
Try registering your idlingResource before activity onCreate.
简单的@Before
方法就足够了.
如果您使用的是浓缩咖啡规则,则还可以实现自己的ActivityRule并覆盖beforeActivityLaunched()
方法
You can also implement your own ActivityRule and override beforeActivityLaunched()
method if you are using espresso-rules
或者,如果您想在Application oncreate之前执行任何代码,则需要实现自己的testrunner并覆盖callApplicationOnCreate()
Or if you want to execute any code before Application oncreate you need to implement your own testrunner and override callApplicationOnCreate()
此处显示测试规则的示例: ActivityTestRule-如何在应用程序的onCreate之前调用代码
Example of the test rule is shown here: ActivityTestRule - how to call code before Application's onCreate
这篇关于如果在Activity的onCreate中启动了长时间运行的任务,则Espresso空转资源无法正常工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!