EspressoでRecyclerViewの中身を比較
Espressoに標準で用意されていないので、自分でmatherを作る必要があります
- RecyclerViewに用意されているfind系のメソッドにはこのようなものがあります
findChildViewUnder(float x, float y)findContainingItemView(View view)findContainingViewHolder(View view)findViewHolderForAdapterPosition(int position)findViewHolderForItemId(long id)findViewHolderForLayoutPosition(int position)findViewHolderForPosition(int position)
今回はRecyclerViewの findViewHolderForAdapterPosition(int position)を使用します
適宜 使いやすいメソッドを使用して下さい
ViewHolder
class MyViewHolder(v: View) : RecyclerView.ViewHolder(v) {
var title: TextView
...
}
matcherのメソッドを作成
fun hasText(position: Int, text: String) = object : TypeSafeMatcher<View>() {
var innerText: String? = null
override fun describeTo(description: Description) {
description.appendText(String.format(text + "is not equal to %s.", text))
}
override fun matchesSafely(view: View): Boolean {
if (view !is RecyclerView) return false
val holder = view.findViewHolderForAdapterPosition(position) as MyViewHolder
innerText = holder.title.text.toString()
return holder.description.text == text
}
}
使用側
@Test
fun testRecyclerView() {
val position = 0
val text = "hoge"
onView(withId(R.id.recyclerView)).check(matches(hasText(position, text)))
}