Issue
I have CardView with RecyclerView child. Every item in RecyclerView is a word with its translation. I want to test deletion of particular item in RecyclerView. It is important for me to be independent on position of item, because I test in different conditions (with zero, or plenty of items with possibility of different sorting... position doesn't make sense for me). Therefore I wrote Matcher to find view based on data in ViewHolder.
public static Matcher<RecyclerView.ViewHolder> withWordAndTranslation(String word, String translation) {
checkNotNull(word);
checkNotNull(translation);
return new BoundedMatcher<RecyclerView.ViewHolder, WordsViewHolder>(WordsViewHolder.class) {
@Override
protected boolean matchesSafely(WordsViewHolder holder) {
return holder.wordTxt.getText().toString().equalsIgnoreCase(word)
&& holder.translationTxt.getText().toString().equalsIgnoreCase(translation);
}
@Override
public void describeTo(Description description) {
description.appendText("view holder with word, translation: " + word + ", " + translation);
}
};
}
So far, so good. Assuming an item with "test_word" and "test_translation" in its TextViews is added to RecyclerView, I get appropriate view and use click action on it:
onView(allOf(withId(R.id.wordsRecyclerView), withParent(withId(R.id.cardView)), isDisplayed()))
.perform(actionOnHolderItem(withWordAndTranslation("test_word", "test_translation"), click()));
FYI it gives this item selection. I click delete button which job is to delete selected item:
onView(allOf(withId(R.id.action_delete), isDisplayed())).perform(click());
and I really want to check if item ("test_word, test_translation) still exists on RecyclerView. But I dunno how. Any ideas/solutions?
Solution
The Espresso documentation recommends that you get the Adapter and iterate over it. Take the example code and replace AdapterView with RecyclerView.
Answered By - sebokopter
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.