我尝试在espresso中为我的Android应用程序编写一些测试,我遇到了空闲问题。如果我的isIdleNow()看起来像这样
public boolean isIdleNow() {
return true;
}
消息已发送。但是如果IsIdleNow()必须等待某些条件(它在启动时不返回true),则不发送消息。我的代码
public class ImageViewIdlingResource implements IdlingResource {
private ImageView imageView=null;
ResourceCallback callback;
public ImageViewIdlingResource(ImageView imageView2){
this.imageView = imageView2;
}
@Override
public String getName() {
return "ImageViewIdlingResource";
}
@Override
public boolean isIdleNow() {
return !imageView.isShown();
}
@Override
public void registerIdleTransitionCallback(ResourceCallback resourceCallback) {
this.callback = resourceCallback;
}}
并测试
public class EspressoTest extends ActivityInstrumentationTestCase2<SplashScreen> {
public EspressoTest() {
super(SplashScreen.class);
}
@Override
public void setUp() throws Exception {
super.setUp();
getActivity();
ImageViewIdlingResource imageResource = new ImageViewIdlingResource((ImageView)getActivity().findViewById(R.id.splashscreen_icon));
Espresso.registerIdlingResources(imageResource);
}
public void testListGoesOverTheFold() throws InterruptedException {
onView(withId(R.id.button)).check(matches(isDisplayed()));
}}
答案 0 :(得分:6)
你必须通知回调你正在进入空闲状态,就像那样:
@Override
public boolean isIdleNow() {
if (!imageView.isShown()){
if (callback != null) {
callback.onTransitionToIdle();
}
return true;
}
return false;
}