Last active
May 11, 2024 15:22
-
-
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"
This file contains 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 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 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); | |
} | |
} |
Can you please show the implementation of these with a little of details?
@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
@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:
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.