I'm trying to write my own viewMatcher testing the background color of my Button
so I'm trying to implement: Testing background color espresso Android
here's my code and how I call it
fun withBackColor(color: Int): Matcher<View?> {
Checks.checkNotNull(color)
return object : BoundedMatcher<View?, Button>(Button::class.java) {
override fun matchesSafely(button: Button): Boolean {
val col = (button.background as ColorDrawable).color
return color == col
}
override fun describeTo(description: org.hamcrest.Description) {
description.appendText("with text color: ")
}
}
}
onView(withId(R.id.continueBtn)).check(matches(withBackColor(R.color.custom_color_slider_widget_unselected)))
when I run this code I get: java.lang.ClassCastException: android.graphics.drawable.RippleDrawable cannot be cast to android.graphics.drawable.ColorDrawable
This is my button:
<Button
android:id="@+id/continueBtn"
style="@style/ButtonGreyStyle"
android:layout_marginBottom="25dp"
android:text="@string/btn_continue"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
The way it's set is in the on create of the fragment even though the button gets created in an activity launched before
continue_btn?.setBackgroundColor(
ContextCompat.getColor(
requireActivity(),
R.color.custom_color_slider_widget_unselected
)
)
The style property of the color
<color name="custom_color_slider_widget_unselected">#33EBF5F9</color>
I searched google and found this
Cannot be cast to android.graphics.drawable.ColorDrawable
but didn't find any link to something I could be doing wrong
EDIT:
override fun matchesSafely(button: Button): Boolean {
val col = button.background
val state: ConstantState? = col.constantState
return try {
val colorField: Field = state?.javaClass!!.getDeclaredField("mColor")
colorField.isAccessible = true
val colorState: ColorStateList = colorField.get(state) as ColorStateList
val rippleColor: Int = colorState.defaultColor
color == rippleColor
} catch (e: NoSuchFieldException) {
e.printStackTrace()
false
} catch (e: IllegalAccessException) {
e.printStackTrace()
false
}
}
I'm now trying to get the color of the RippleDrawable without a cast to ColorDrawable but i get the NoSuchFieldException
Accessing hidden field Landroid/graphics/drawable/RippleDrawable$RippleState;->mColor:Landroid/content/res/ColorStateList; (max-target-r, reflection, denied) 11-16 14:10:21.227 19473 19473 W System.err: java.lang.NoSuchFieldException: No field mColor in class Landroid/graphics/drawable/RippleDrawable$RippleState; (declaration of 'android.graphics.drawable.RippleDrawable$RippleState' appears in /system/framework/framework.jar)