Skip to content

Instantly share code, notes, and snippets.

@yfujiki
Last active January 21, 2019 16:03
Show Gist options
  • Select an option

  • Save yfujiki/2d78a442d2158e436ee3040db62a91c2 to your computer and use it in GitHub Desktop.

Select an option

Save yfujiki/2d78a442d2158e436ee3040db62a91c2 to your computer and use it in GitHub Desktop.
Custom matcher for Espresso to check whether the background shape has a border or not
//
// Usage :
// onView(withId(R.id...)
// .check(
// ViewAssertions.matches(
// BackgroundViewMatchers.backgroundHasBorder()
// )
// )
// )
//
// onView(withId(R.id...)
// .check(
// not(
// ViewAssertions.matches(
// BackgroundViewMatchers.backgroundHasBorder()
// )
// )
// )
// )
//
object BackgroundViewMatchers {
fun backgroundHasBorder(): Matcher<View> {
return object : TypeSafeMatcher<View>(View::class.java!!) {
override fun describeTo(description: Description) {
description.appendText("to have a border")
}
override fun matchesSafely(foundView: View): Boolean {
val gradientDrawable = (foundView.background as? GradientDrawable)
if (gradientDrawable == null) {
print("Background is not a gradient")
return false
}
val field = (GradientDrawable::class.java.getDeclaredField("mStrokePaint"))
field.isAccessible = true
val stroke = field.get(gradientDrawable)
return stroke != null
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment