/**
* Test if a RecyclerView contain the right amount of elements
*/
fun withListSize(size: Int): Matcher<View> {
return object : TypeSafeMatcher<View>() {
var listSize = 0
public override fun matchesSafely(view: View): Boolean {
if ((view !is RecyclerView)) throw IllegalStateException("You cannot assert withListSize in none RecyclerView View")
listSize = view.adapter.itemCount
return listSize == size
}
override fun describeTo(description: Description) {
description.appendText("ListView should have $size items and currently $listSize items")
}
}
}
这样使用:
var listSize = 0
Espresso.onView(ViewMatchers.withId(R.id.list))
.check(ViewAssertions.matches(object : TypeSafeMatcher<View>() {
public override fun matchesSafely(view: View): Boolean {
if ((view !is RecyclerView)) throw IllegalStateException("You cannot assert withListSize in none RecyclerView View")
listSize = view.adapter.itemCount
return listSize !=0
}
override fun describeTo(description: Description) {
description.appendText("ListView should not be empty")
}
}))
// ADD TO the list
Espresso.onView(ViewMatchers.withId(R.id.list))
.check(ViewAssertions.matches(Matchers.allOf(withListSize(listSize + 1), ViewMatchers.isDisplayed())))
# 1 楼答案
您可以使用如下自定义匹配器在RecyclerListView或ListView中断言元素数:
这样使用: