Skip to content

Instantly share code, notes, and snippets.

@romainpiel
Last active August 13, 2019 08:31
  • Star 32 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
Star You must be signed in to star a gist
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);
}
}
@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