Skip to content Skip to sidebar Skip to footer

Espresso: How To Test SwipeRefreshLayout?

My app reloads data when a down-swipe is done on a SwipeRefreshLayout. Now I try to test this with Android Test Kit / Espresso like this: onView(withId(R.id.my_refresh_layout)).per

Solution 1:

Sleeping over it sometimes helps. The underlying reason was that the to-be-swiped view was only 89% visible to the user, while Espresso's swipe actions internally demand 90%. So the solution is to wrap the swipe action into another action and override these constraints by hand, like this:

public static ViewAction withCustomConstraints(final ViewAction action, final Matcher<View> constraints) {
    return new ViewAction() {
        @Override
        public Matcher<View> getConstraints() {
            return constraints;
        }

        @Override
        public String getDescription() {
            return action.getDescription();
        }

        @Override
        public void perform(UiController uiController, View view) {
            action.perform(uiController, view);
        }
    };
}

This can then be called like this:

onView(withId(R.id.my_refresh_layout))
    .perform(withCustomConstraints(swipeDown(), isDisplayingAtLeast(85)));

Post a Comment for "Espresso: How To Test SwipeRefreshLayout?"