'Android Espresso test for background color of a MaterialButton

Previously had a android.widget.Button. Button has been changed to type com.google.android.material.button.MaterialButton.

Espresso test for background color is like below:

@Test
    public void checkLoginButton() {
        onView(withId(R.id.login_button))
                .check(matches(hasBackgroundColor(ContextCompat.getColor(
                        mLoginActivityRule.getActivity(), R.color.colorPrimaryDarkBlue))));
    }

hadBackgroundColor(int expectedColor) is the custom matcher below:

public static Matcher<View> hasBackgroundColor(final int expectedColor) {
        return new BoundedMatcher<View, Button>(Button.class) {

            int mExpectedColor = expectedColor;
            int mActualColor = 0;

            @Override
            public boolean matchesSafely(Button button) {
                mActualColor = ((RippleDrawable) button.getBackground()).getEffectColor().getDefaultColor();
                return mActualColor == mExpectedColor;
            }

            @Override
            public void describeTo(Description description) {
                description.appendText("Expected color " + mExpectedColor + " did not match with"
                        + " actual color " + mActualColor);
            }
        };

    }

The code currently throws the error below:

java.lang.NoSuchMethodError: No virtual method getEffectColor()Landroid/content/res/ColorStateList; in class Landroid/graphics/drawable/RippleDrawable; or its super classes (declaration of 'android.graphics.drawable.RippleDrawable' appears in /system/framework/framework.jar)
at nl.energieplanner.intake.activity.LoginActivityTest$2.matchesSafely(LoginActivityTest.java:248)
at nl.energieplanner.intake.activity.LoginActivityTest$2.matchesSafely(LoginActivityTest.java:241)
at androidx.test.espresso.matcher.BoundedMatcher.matches(BoundedMatcher.java:7)
at androidx.test.espresso.matcher.ViewMatchers.assertThat(ViewMatchers.java:3)
at androidx.test.espresso.assertion.ViewAssertions$MatchesViewAssertion.check(ViewAssertions.java:11)
at androidx.test.espresso.ViewInteraction$SingleExecutionViewAssertion.check(ViewInteraction.java:2)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:12)
at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:1)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Here's also the button's layout, if needed.

<com.google.android.material.button.MaterialButton
            android:id="@+id/login_button"
            style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:minHeight="70dp"
            android:padding="16dp"
            android:text="@string/action_sign_in"
            android:textSize="20sp" />

Thank you!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source