Amaro
Amaro

Reputation: 499

Android Espresso is waiting rxjava .delay from observable

I have a server call made with Retrofit + RxJava that i want to test its behaviour on screen.

The goal is to have a loading image set before the call be performed and, after getting the results, hide the loading image and show the data.

I tried to setup the mock using the "delay" method from the Observable class, so Espresso can find the image. That's the code i used:

Observable<AccountDetails> observable = Observable.just(details)
            .delay(5, TimeUnit.SECONDS)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io());
doReturn(observable).when(mScope).requestAccounts();

performServerCall();

onView(withId(R.id.panel_loading)).check(matches(isDisplayed()));

After running the test, i realized that the Espresso is actually waiting the delay set on Observable, before actually executing the check (isDisplayed). That way it will only check after the info is loaded and the loading image is gone.

Is that the normal behaviour for RxJava/Espresso?

Is there a better way achieve this?

Upvotes: 0

Views: 910

Answers (1)

jeprubio
jeprubio

Reputation: 18002

There must be an animation in the R.id.panel_loading that is being executed.

When there is an animation in the UI thread espresso waits until it is finished.

I had the same problem and I did a ViewAction to disable the animations for my custom loadings, here is the code:

public static ViewAction disableAnimations() {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return isAssignableFrom(CustomLoading.class);
        }

        @Override
        public String getDescription() {
            return "Disable animations";
        }

        @Override
        public void perform(UiController uiController, View view) {
            CustomLoading loading = (CustomLoading) view;
            loading.setAnimations(false);
        }
    };
}

and I call it this way before pressing the button that shows the loading and so the tests do not wait:

onView(withId(R.id.panel_loading)).perform(disableAnimations());

If the panel_loading is not the one that makes the animation something else must be.

Hope this helps.

Upvotes: 1

Related Questions