使用具有多个活动的长者preSSO闲置资源 [英] Using Espresso idling resource with multiple activities
问题描述
我有启动secondActivity,凡在secondActivity我有一个加载对话框(不AsyncTask的)一个firstActivity,我需要让长者preSSO等到对话框消失,它与测试继续之前。
我在哪里必须实现IdlingResource?我怎样才能使它等待dismissDialog()函数?
I have a firstActivity that launches the secondActivity, where in the secondActivity I have a loading Dialog (not AsyncTask), and I need to make Espresso wait until the dialog disappears before it continues with the test.
Where do I have to implement the IdlingResource? How can I make it wait for the dismissDialog() function?
下面是我一直试图做的:
Here is what I've tried to do:
class DocumentLoadingIdlingResource implements IdlingResource {
private ResourceCallback callback;
@Override
public String getName() {
return "Documnet loading idling resource";
}
@Override
public boolean isIdleNow() {
Activity activity;
try {
activity = getCurrentActivity();
} catch (Throwable e) {
return false;
}
if(activity.getClass().getName().equals(EditorActivity.class.getName())
&& activity.loadingDialogShowing() == false) {
return false;
}
return true;
}
@Override
public void registerIdleTransitionCallback(ResourceCallback callback) {
this.callback = callback;
}
}
Activity getCurrentActivity() throws Throwable {
getInstrumentation().waitForIdleSync();
final Activity[] activity = new Activity[1];
runTestOnUiThread(new Runnable() {
@Override
public void run() {
java.util.Collection<Activity> activites = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED);
activity[0] = com.google.common.collect.Iterables.getOnlyElement(activites);
}});
return activity[0];
}
本类是在测试类实现的。
This class is implemented in the test class.
推荐答案
在这里有几个问题:
- 您isIdleNow()调用getCurrentActivity()这就要求waitForIdleSync()和runTestOnUiThread()。 isIdleNow的Javadoc说:ES preSSO将总是调用从主线程这种方法,因此它应该是非阻塞并立即返回。因此,这将不是就是工作,但你可以直接从isIdleNow调用getActivitiesInStage。
- 您的另一个问题是,你存储参考ResourceCallback但从未援引onTransitionToIdle,你也应该允许一个以上的ResourceCallback的可能性正在注册和呼叫onTransitionToIdle所有的回调。
- Your isIdleNow() calls getCurrentActivity() which calls waitForIdleSync() and runTestOnUiThread(). isIdleNow Javadoc says: "Espresso will always call this method from the main thread, therefore it should be non-blocking and return immediately." So this won't work as is, but you could call getActivitiesInStage directly from isIdleNow.
- Your other issue is that you store the reference to ResourceCallback but never invoke onTransitionToIdle, also you should allow for the possibility of more than one ResourceCallback being registered and call onTransitionToIdle on all of the callbacks.
您可以执行以下操作:
- 复制/粘贴<一个href=\"https://android-test-kit.google$c$c.com/git/es$p$psso/lib/src/main/java/com/google/android/apps/common/testing/ui/es$p$psso/IdlingResource.java\"相对=nofollow> IdlingResource 到您的应用作为com.mycompany.IdlingResource。
- 然后让你的活动实现该接口,并确保调用onTransitionToIdle时,对话框消失,使当且仅当对话框显示出一定isIdleNow返回false。
- 在您的测试code,写一个包装com.mycompany.IdlingResource,并把它变成一个长者preSSO IdlingResource并注册了IdlingResourceAdapter即以E preSSO。
- Copy/Paste IdlingResource into your app as com.mycompany.IdlingResource.
- Then have your Activity implement that interface and make sure to call onTransitionToIdle when the dialog goes away and make sure isIdleNow returns false iff the dialog is showing.
- In your test code, write a "IdlingResourceAdapter" that wraps com.mycompany.IdlingResource and turns it into an Espresso IdlingResource and register that with Espresso.
这将是一次简单本期已实现:的 HTTPS://$c$c.google.com/p/android-test-kit/issues/detail ID = 71
This will be simpler once this issue is implemented: https://code.google.com/p/android-test-kit/issues/detail?id=71
这篇关于使用具有多个活动的长者preSSO闲置资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!