Last active
May 11, 2024 15:22
Source for https://medium.com/p/3f6f4179652e - "RecyclerView and espresso, a complicated story"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
@romainpiel nice medium article. BTW, what is the license on this code snippet