Last active
January 21, 2019 16:03
-
-
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
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
| // | |
| // 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