Espresso - Check RecyclerView items are ordered correctly

后端 未结 5 2096
一个人的身影
一个人的身影 2021-01-05 03:18

How to go about checking whether RecyclerView items are displayed in the correct order using Espresso? I\'m trying to test it checking it by the text for the title of each e

5条回答
  •  佛祖请我去吃肉
    2021-01-05 04:07

    If you want to match a matcher on a position in RecyclerView, then you can try to create a custom Matcher:

    public static Matcher hasItemAtPosition(int position, Matcher matcher) {
        return new BoundedMatcher(RecyclerView.class) {
    
            @Override public void describeTo(Description description) {
                description.appendText("has item: ");
                matcher.describeTo(description);
                description.appendText(" at position: " + position);
            }
    
            @Override protected boolean matchesSafely(RecyclerView view) {
                RecyclerView.Adapter adapter = view.getAdapter();
                int type = adapter.getItemViewType(position);
                RecyclerView.ViewHolder holder = adapter.createViewHolder(view, type);
                adapter.onBindViewHolder(holder, position);
                return matcher.matches(holder.itemView);
            }
        };
    }
    

    And you can use it for example:

    onView(withId(R.id.rv_metrics)).check(matches(0, hasDescendant(withText("Weight")))))
    

提交回复
热议问题