Skip to content

Instantly share code, notes, and snippets.

@huuphuoc1396
Created October 8, 2020 04:46
Show Gist options
  • Save huuphuoc1396/1d7a868895caa270753a54584292e62f to your computer and use it in GitHub Desktop.
Save huuphuoc1396/1d7a868895caa270753a54584292e62f to your computer and use it in GitHub Desktop.
RecyclerView testing - Using assert item count of RecyclerView adapter
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.test.espresso.NoMatchingViewException
import androidx.test.espresso.ViewAssertion
import androidx.test.espresso.matcher.ViewMatchers.assertThat
import org.hamcrest.Matcher
import org.hamcrest.Matchers.`is`
/**
* Using assert item count of RecyclerView adapter
*/
class RecyclerViewItemCountAssertion private constructor(
private val matcher: Matcher<Int>
) : ViewAssertion {
override fun check(view: View?, noViewFoundException: NoMatchingViewException?) {
if (noViewFoundException != null) {
throw noViewFoundException
}
val recyclerView: RecyclerView? = view as? RecyclerView
val adapter = recyclerView?.adapter
assertThat(adapter?.itemCount, matcher)
}
companion object {
fun withItemCount(expectedCount: Int): RecyclerViewItemCountAssertion {
return withItemCount(`is`(expectedCount))
}
fun withItemCount(matcher: Matcher<Int>): RecyclerViewItemCountAssertion {
return RecyclerViewItemCountAssertion(matcher)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment