Espresso:如何测试SwipeRefreshLayout?

Tho*_*ler 18 testing android android-espresso

我的应用程序在a上进行向下滑动时重新加载数据SwipeRefreshLayout.现在我尝试使用Android Test Kit/Espresso进行测试,如下所示:

onView(withId(R.id.my_refresh_layout)).perform(swipeDown());
Run Code Online (Sandbox Code Playgroud)

不幸的是,这失败了

android.support.test.espresso.PerformException: Error performing 'fast swipe'
on view 'with id: my.app.package:id/my_refresh_layout'.
...
Caused by: java.lang.RuntimeException: Action will not be performed because the target view
does not match one or more of the following constraints:
at least 90 percent of the view's area is displayed to the user.
Target view: "SwipeRefreshLayout{id=2131689751, res-name=my_refresh_layout, visibility=VISIBLE,
width=480, height=672, has-focus=false, has-focusable=true, has-window-focus=true,
is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, 
is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0,
child-count=2}"
Run Code Online (Sandbox Code Playgroud)

当然,布局是可见的,手动滑动有效,但我不确定我做错了什么?布局横跨整个屏幕,因此Espresso 应该可以对其进行一些操作.

Tho*_*ler 45

睡在上面有时会有所帮助.根本原因在于,待刷卡的视图对用户来说只有89%可见,而Espresso的刷卡动作在内部需要90%.因此,解决方案是将滑动操作包装到另一个操作中并手动覆盖这些约束,如下所示:

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);
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

然后可以这样调用:

onView(withId(R.id.my_refresh_layout))
    .perform(withCustomConstraints(swipeDown(), isDisplayingAtLeast(85));
Run Code Online (Sandbox Code Playgroud)