工作流应该如下所示:
- 活动开始
- 进度条可见
- 网络请求启动(空闲资源已注册,因此Espresso知道如何等待它)。
- 隐藏进度条
- 显示来自网络的文本。
到目前为止,我已经为步骤1、3、5编写了断言,并且它完美地工作:
onView(withText("foo 1"))
.check(matches(isDisplayed()));
问题是,我不知道如何让Espresso在请求发出之前和之后验证进度条的可见性。
考虑以下onCreate()
方法:
super.onCreate(...);
setContentView(...);
showProgressBar(true);
apiClient.getStuff(new Callback() {
public void onSuccess() {
showProgressBar(false);
}
});
我已经尝试了以下方法,但它没有起作用:
// Activity is launched at this point.
activityRule.launchActivity(new Intent());
// Up to this point, the request has been fired and response was
// returned, so the progress bar is now GONE.
onView(withId(R.id.progress_bar))
.check(matches(isDisplayed()));
onView(withId(R.id.progress_bar))
.check(matches(not(isDisplayed())));
这种情况发生的原因是,由于客户端被注册为空闲资源,espresso将等待它再次空闲之前运行第一个onView(...progressbar...)...
,所以我需要一种方法让espresso知道在进入空闲状态之前运行它。
编辑:这也不起作用:
idlingResource.registerIdleTransitionCallback(new IdlingResource.ResourceCallback() {
@Override
public void onTransitionToIdle() {
onView(withId(R.id.progress_bar))
.check(matches(isDisplayed()));
}
});