Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

isIdleNow() is returning true, but a message indicating that the resource has transitioned from busy to idle was never sent

I'm trying to write a test in Espresso for my Android application, and I have a problem with Idle. If my isIdleNow() looks like this

public boolean isIdleNow() {
    return true;
}

then the message is sent. But if IsIdleNow() has to wait for some conditions (it doesn't return true at start) then the message isn't sent.

My code:

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;
}}

and the test:

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()));
}}
like image 292
maciej Avatar asked Feb 23 '15 13:02

maciej


1 Answers

You have to notify callback that you are on the way to idle state, something like that:

@Override
public boolean isIdleNow() {   
    if (!imageView.isShown()){ 
        if (callback != null) {
            callback.onTransitionToIdle();
        }
        return true;
    }
    return false;
}
like image 113
denys Avatar answered Sep 23 '22 19:09

denys