Skip to content

Instantly share code, notes, and snippets.

@romainpiel
Last active August 13, 2019 08:31
Show Gist options
  • Star 32 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save romainpiel/ec10302a4687171a5e1a to your computer and use it in GitHub Desktop.
Save romainpiel/ec10302a4687171a5e1a to your computer and use it in GitHub Desktop.
Source for https://medium.com/p/3f6f4179652e - "RecyclerView and espresso, a complicated story"
RecyclerViewInteraction.<Item>onRecyclerView(withId(R.id.recyclerview))
.withItems(items)
.check(new ItemViewAssertion<Item>() {
@Override
public void check(Item item, View view, NoMatchingViewException e) {
matches(hasDescendant(withText(item.getDisplayName())))
.check(view, e);
}
});
import android.support.test.espresso.NoMatchingViewException;
import android.support.test.espresso.PerformException;
import android.support.test.espresso.ViewAssertion;
import android.support.test.espresso.util.HumanReadables;
import android.support.v7.widget.RecyclerView;
import android.view.View;
public class RecyclerItemViewAssertion<A> implements ViewAssertion {
private int position;
private A item;
private ItemViewAssertion<A> itemViewAssertion;
public RecyclerItemViewAssertion(int position, A item, ItemViewAssertion<A> itemViewAssertion) {
this.position = position;
this.item = item;
this.itemViewAssertion = itemViewAssertion;
}
@Override
public final void check(View view, NoMatchingViewException e) {
RecyclerView recyclerView = (RecyclerView) view;
RecyclerView.ViewHolder viewHolderForPosition = recyclerView.findViewHolderForLayoutPosition(position);
if (viewHolderForPosition == null) {
throw (new PerformException.Builder())
.withActionDescription(toString())
.withViewDescription(HumanReadables.describe(view))
.withCause(new IllegalStateException("No view holder at position: " + position))
.build();
} else {
View viewAtPosition = viewHolderForPosition.itemView;
itemViewAssertion.check(item, viewAtPosition, e);
}
}
}
import android.support.test.espresso.NoMatchingViewException;
import android.view.View;
import org.hamcrest.Matcher;
import java.util.List;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.contrib.RecyclerViewActions.scrollToPosition;
public class RecyclerViewInteraction<A> {
private Matcher<View> viewMatcher;
private List<A> items;
private RecyclerViewInteraction(Matcher<View> viewMatcher) {
this.viewMatcher = viewMatcher;
}
public static <A> RecyclerViewInteraction<A> onRecyclerView(Matcher<View> viewMatcher) {
return new RecyclerViewInteraction<>(viewMatcher);
}
public RecyclerViewInteraction<A> withItems(List<A> items) {
this.items = items;
return this;
}
public RecyclerViewInteraction<A> check(ItemViewAssertion<A> itemViewAssertion) {
for (int i = 0; i < items.size(); i++) {
onView(viewMatcher)
.perform(scrollToPosition(i))
.check(new RecyclerItemViewAssertion<>(i, items.get(i), itemViewAssertion));
}
return this;
}
public interface ItemViewAssertion<A> {
void check(A item, View view, NoMatchingViewException e);
}
}
@ryansgot
Copy link

ryansgot commented Jun 4, 2016

@sughimura

It is indeed useful code, and it worked for me straight away. The key concept that is not explained in this code is "What is an Item?" In this case, an Item is just a placeholder for whatever class you're using to contain the information that populates the view. More concretely, suppose you have 10 items in your recycler view and those 10 items are supposed to have child TextViews with the their corresponding numbers. Then you could write an assertion like this:

RecyclerViewInteraction.<String>onRecyclerView(withId(R.id.recycler_id))
                .withItems(Arrays.asList(new String[] {"1", "2", "3", "4", "5", "6", "7", '8", "9", "10"}))
                .check(new RecyclerViewInteraction.ItemViewAssertion<String>() {
                    @Override
                    public void check(String item, View view, NoMatchingViewException e) {
                        matches(hasDescendant(withText(item))).check(view, e);
                    }
                });

This would scroll the RecyclerView to each position and then check that the item at that position has a child view with the text in the corresponding position of the list that was passed into the withItems method above.

@seanwdas
Copy link

Can you please show the implementation of these with a little of details?

@rjoncontract
Copy link

@romainpiel nice medium article. BTW, what is the license on this code snippet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment