Android-Espresso: 使用 R.color.xxx 无法检查 textView 文本颜色

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

这是我的 colors.xml

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

这是我的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"/>

所以我想在 TextView 中编写 Espresso 测试检查 android:textColor="@color/color_primary"

所以这是我的测试:

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

这里是匹配器:

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);
            }
        };
    }

并且测试工作正常。

所以我将测试更改为使用 color_primary:

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

但现在我得到错误:

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}"

如果你想从颜色资源中提取整数,你的视图匹配器应该如下所示:

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);
            }
        };
    }

然后这应该会给出想要的结果:

onView(withId(R.id.registerTextView)).check(matches(withTextColor(R.color.color_primary)))

这对我来说非常有用,可用于检查文本颜色。 onView(withText("你的文本")).check(匹配(withTextColor(Color.parseColor("#282828"))));

注意#282828 是颜色代码应该是正确的。