1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

EspressoでRecyclerViewの中身を比較

Posted at

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)))
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?