Testing background color espresso Android

前端 未结 7 992
灰色年华
灰色年华 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:40

    private fun hasBackgroundColor(colorRes: Int): Matcher {
        Checks.checkNotNull(colorRes)
    
        return object : TypeSafeMatcher() {
            override fun describeTo(description: Description?) {
                description?.appendText("background color: $colorRes")
            }
    
            override fun matchesSafely(item: View?): Boolean {
    
                val context = item?.context
                val textViewColor = (item?.background as ColorDrawable).color
                val expectedColor: Int?
    
                if (Build.VERSION.SDK_INT <= 22) {
                    expectedColor = context?.resources?.getColor(colorRes)
                } else {
                    expectedColor = context?.getColor(colorRes)
                }
    
                return textViewColor == expectedColor
            }
        }
    }
    

提交回复
热议问题