Testing background color espresso Android

前端 未结 7 924
灰色年华
灰色年华 2020-12-11 14:50

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

7条回答
  •  自闭症患者
    2020-12-11 15:29

    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.

提交回复
热议问题