Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android-Espresso: With R.color.xxx can't check textView text color

here my colors.xml

<color name="color_primary">#29c9b9</color>

Here my xml

<TextView
            android:id="@+id/registerTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"           
            android:text="@string/register"
            android:textAllCaps="true"
            android:textColor="@color/color_primary"/>

So I want to write Espresso test check that in TextView android:textColor="@color/color_primary"

So here my test:

    @Test
    public void registerTextViewTextColor() {
        onView(withId(R.id.registerTextView)).check(matches(withTextColor(Color.parseColor("#29c9b9"))));
}

Here matcher:

public static Matcher<View> withTextColor(final int expectedId) {
        return new BoundedMatcher<View, TextView>(TextView.class) {

            @Override
            protected boolean matchesSafely(TextView textView) {
                return expectedId == textView.getCurrentTextColor();
            }

            @Override
            public void describeTo(Description description) {
                description.appendText("with text color: ");
                description.appendValue(expectedId);
            }
        };
    }

And test work fine.

So I change my test to use color_primary:

@Test
public void registerTextViewTextColor() {
  onView(withId(R.id.registerTextView)).check(matches(withTextColor(R.color.color_primary)));}

But now I get error:

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with text color: <2131099686>' doesn't match the selected view.
Expected: with text color: <2131099686>
Got: "AppCompatTextView{id=2131296512, res-name=registerTextView, visibility=VISIBLE, width=180, height=53, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, layout-params=android.support.constraint.ConstraintLayout$LayoutParams@9e165b3, tag=null, root-is-layout-requested=false, has-input-connection=false, x=451.0, y=1508.0, text=Register, input-type=0, ime-target=false, has-links=false}"
like image 357
Alexei Avatar asked Jun 13 '18 08:06

Alexei


1 Answers

IF you want to extract the int out of the color resource your view matcher should look like this:

public static Matcher<View> withTextColor(final int expectedId) {
        return new BoundedMatcher<View, TextView>(TextView.class) {

            @Override
            protected boolean matchesSafely(TextView textView) {
                int colorId = ContextCompat.getColor(textView.getContext(), expectedId);
                return textView.getCurrentTextColor() == colorId;
            }

            @Override
            public void describeTo(Description description) {
                description.appendText("with text color: ");
                description.appendValue(expectedId);
            }
        };
    }

And then this should give the desired result:

onView(withId(R.id.registerTextView)).check(matches(withTextColor(R.color.color_primary)))
like image 105
Anatolii Avatar answered Jan 04 '23 17:01

Anatolii