Espresso: How to test SwipeRefreshLayout?

前端 未结 1 1900
南旧
南旧 2020-12-30 02:22

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         


        
相关标签:
1条回答
  • 2020-12-30 02:31

    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));
    
    0 讨论(0)
提交回复
热议问题