mercredi 31 mars 2021

Espresso - checking recyclerview position for specific image

I'm having a hard time figuring out how to test whether or not a certain image is being show on a position in a recyclerview using espresso. I have a list of people, and when one of them is selected, I'm showing a selected indicator around his image in the recyclerview. So I want to check that, for instance, position 0 has this indicator showing. What I'm trying is this:

fun test(position: Int, @DrawableRes res: Int): ViewInteraction {
    return onView(withId(recyclerViewId)).check(matches(hasItemAtPosition(position, hasBackground(res))))
}

 private fun hasItemAtPosition(position: Int, matcher: Matcher<View>): Matcher<View> {
    return object : BoundedMatcher<View, RecyclerView>(RecyclerView::class.java) {

        override fun describeTo(description: Description?) {
            description?.appendText("has item at position $position : ")
            matcher.describeTo(description)
        }

        override fun matchesSafely(recyclerView: RecyclerView): Boolean {
            val viewHolder = recyclerView.findViewHolderForAdapterPosition(position)
                ?: return false
            return matcher.matches(viewHolder.itemView)
        }
    }
}

This code works fine if I do it with withText rather than withBackground and match the text of the item.

The error I get looks like this:

androidx.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'has item at position 0 : has background with drawable ID: 2131231310' doesn't match the selected view.
    Expected: has item at position 0 : has background with drawable ID: 2131231310

I'm kind of new to espresso and testing in general, so hoping someone has any suggestions.

Aucun commentaire:

Enregistrer un commentaire