Android-Espresso: с R.color.xxx невозможно проверить цвет текста textView. - PullRequest
0 голосов
/ 13 июня 2018

здесь мой 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 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}"

1 Ответ

0 голосов
/ 13 июня 2018

ЕСЛИ вы хотите извлечь int из цветового ресурса, ваш сопоставитель видов должен выглядеть следующим образом:

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)))
...