Is it possible to check if the background color matches a given color with espresso?
Update:
I made a custom matcher, similar to what @Irfa
It is possible to to test on color. I created a method to test on TextView
background color as below. Also notice that am passing color resource.
private fun withBackgroundColor(@ColorInt color: Int): Matcher {
Checks.checkNotNull(color)
return object : BoundedMatcher(TextView::class.java) {
override fun describeTo(description: Description?) {
description?.appendText("TextView background color to be $color")
}
override fun matchesSafely(item: TextView?): Boolean {
val backgroundColor = item?.background as ColorDrawable
val colorDrawable = ColorDrawable(ContextCompat.getColor(item.context, color))
return colorDrawable.color == backgroundColor.color
}
}
}
I create ColorDrawable
object from color resource and get color from the object.
Comparing color from resource without the ColorDrawable
does not pass. Hope this helps you.