如何检查特定RecyclerView项目上的View是否可见?

时间:2016-09-25 14:59:15

标签: android unit-testing android-espresso

我是使用浓缩咖啡的新手。我无法使用此代码获得我想测试的内容:

    onData(withId(R.id.relativelayout_tag))
        .inAdapterView(withId(R.id.recyclerview_tag_list))
        .onChildView(withId(R.id.imageview_tag))
        .atPosition(1)
        .check(matches(isDisplayed()));

R.id.imageview_tagR.id.relativelayout_tag的孩子。 R.id.relativelayout_tag保存适配器项的全部内容。 R.id.recyclerview_tag_list是我RecyclerView的名称,我在其上分配了特定的RecyclerView Adapter

这是一个非常非常基本的测试。以下是用户程序:

  1. 选择,特定RecyclerView上的第一项(我真的不在乎什么 文字在视图上)。另外,不建议使用视图文本来标识第一个项目。 我不关心适配器项目的内容,甚至在某些视图上放置一个独特的标签。
  2. 在选择时,指示器视图(显示该项目的ImageView) 被选中)应该出现。
  3. 非常基本和简单。使用Espresso为这个基本用户故​​事编写测试是如此困难。当我运行该特定测试时,它始终无法说明:

    Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:
    (is assignable from class: class android.widget.AdapterView and is displayed on the screen to the user)
    Target view: "RecyclerView{id=2131624115, res-name=recyclerview_tag_list, visibility=VISIBLE, width=480, height=1032, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, child-count=15}"
    

    由于列表已经可见,因此没有意义。我甚至可以运行这个测试:

        onView(withId(R.id.recyclerview_tag_list))
                .perform(RecyclerViewActions
                .actionOnItemAtPosition(1, click()));
    

    这是完整的测试:

    @Test
    public void shouldTagToggleSelected()
    {
        onView(withId(R.id.recyclerview_tag_list))
                .perform(RecyclerViewActions
                .actionOnItemAtPosition(1, click()));
    
        onData(withId(R.id.relativelayout_tag))
            .inAdapterView(withId(R.id.recyclerview_tag_list))
            .onChildView(withId(R.id.imageview_tag))
            .atPosition(1)
            .check(matches(isDisplayed()));
    
        //onView(withId(R.id.imageview_tag))
        //        .check(matches(isDisplayed()));
    }
    

    如果指标视图的可见性设置为visible 仅适用于该特定商品(或我选择的任何商品),我想测试一下。

    有什么想法?也许我非常想念一些东西。

    非常感谢!

2 个答案:

答案 0 :(得分:5)

onData不适用于RecyclerView,因为RecyclerView未延伸AdapterView

您需要使用onView来进行断言。如果它是回收者视图中的第一个项目,那么你可以使用类似这个匹配器的东西来做出断言:

public static Matcher<View> withViewAtPosition(final int position, final Matcher<View> itemMatcher) {
        return new BoundedMatcher<View, RecyclerView>(RecyclerView.class) {
            @Override
            public void describeTo(Description description) {
                itemMatcher.describeTo(description);
            }

            @Override
            protected boolean matchesSafely(RecyclerView recyclerView) {
                final RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(position);
                return viewHolder != null && itemMatcher.matches(viewHolder.itemView);
            }
        };
}

用法如下:

    onView(withId(R.id.recyclerview_tag_list))
            .check(matches(withViewAtPosition(1, hasDescendant(allOf(withId(R.id.imageview_tag), isDisplayed())))));

请记住,如果您的ViewHolder尚未布局,此匹配器将失败。如果是这种情况,您需要使用RecyclerViewActions滚动到ViewHolder。如果在使用匹配器之前单击测试中的项目,则无需滚动。

答案 1 :(得分:1)

检查不可见的元素线性布局时出现异常错误。要解决它,我必须添加withFailureHandler,如下所示:

class MyTest {

        boolean isViewVisible = true;

        public void test1(){

           onView(withRecyclerView(R.id.recycler_view)
                .atPositionOnView(index, R.id.linear_layout))
                .withFailureHandler(new FailureHandler() {
                    @Override
                    public void handle(Throwable error, Matcher<View> viewMatcher){

                        isViewVisible = false;                            
                    }
                })
          .check(isVisible());

          //wait for failure handler to finish
          try {
            Thread.sleep(1000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }

          if(isViewVisible){

              //do something
          }
        }
}

class MyTestUtil {

    public static RecyclerViewMatcher withRecyclerView(final int recyclerViewId) {

        return new RecyclerViewMatcher(recyclerViewId);
    }

    public static ViewAssertion isVisible() {

        return new ViewAssertion() {
            public void check(View view, NoMatchingViewException noView) {
                assertThat(view, new VisibilityMatcher(View.VISIBLE));
            }
        };
    }
}


public class RecyclerViewMatcher {
    private final int recyclerViewId;

    public RecyclerViewMatcher(int recyclerViewId) {
        this.recyclerViewId = recyclerViewId;
    }

    public Matcher<View> atPosition(final int position) {
        return atPositionOnView(position, -1);
    }

    public Matcher<View> atPositionOnView(final int position, final int targetViewId) {

        return new TypeSafeMatcher<View>() {
            Resources resources = null;
            View childView;

            public void describeTo(Description description) {
                String idDescription = Integer.toString(recyclerViewId);
                if (this.resources != null) {
                    try {
                        idDescription = this.resources.getResourceName(recyclerViewId);
                    } catch (Resources.NotFoundException var4) {
                        idDescription = String.format("%s (resource name not found)",
                                new Object[] { Integer.valueOf
                                        (recyclerViewId) });
                    }
                }

                description.appendText("with id: " + idDescription);
            }

            public boolean matchesSafely(View view) {

                this.resources = view.getResources();

                if (childView == null) {
                    RecyclerView recyclerView =
                            (RecyclerView) view.getRootView().findViewById(recyclerViewId);
                    if (recyclerView != null && recyclerView.getId() == recyclerViewId) {
                        childView = recyclerView.findViewHolderForAdapterPosition(position).itemView;
                    }
                    else {
                        return false;
                    }
                }

                if (targetViewId == -1) {
                    return view == childView;
                } else {
                    View targetView = childView.findViewById(targetViewId);
                    return view == targetView;
                }

            }
        };
    }
}