Skip to content

Instantly share code, notes, and snippets.

@eyeahs
Forked from romainpiel/MyTest.java
Created May 10, 2016 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eyeahs/a10be83fe649db089cc094411021aa3d to your computer and use it in GitHub Desktop.
Save eyeahs/a10be83fe649db089cc094411021aa3d 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment