Created
October 8, 2020 04:46
-
-
Save huuphuoc1396/1d7a868895caa270753a54584292e62f to your computer and use it in GitHub Desktop.
RecyclerView testing - Using assert item count of RecyclerView adapter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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