测试背景色浓咖啡 Android

Testing background color espresso Android

是否可以检查背景颜色是否与浓缩咖啡的给定颜色相匹配?

更新:

我做了一个自定义匹配器,类似于@Irfan 的建议,谢谢!

public static Matcher<Object> backgroundShouldHaveColor(int expectedColor) {
    return buttondShouldHaveBackgroundColor(equalTo(expectedColor));
}
private static Matcher<Object> buttonShouldHaveBackgroundColor(final Matcher<Integer> expectedObject) {
    final int[] color = new int[1];
    return new BoundedMatcher<Object, Button>( Button.class) {
        @Override
        public boolean matchesSafely(final Button actualObject) {

            color[0] =((ColorDrawable) actualObject.getBackground()).getColor();


            if( expectedObject.matches(color[0])) {
                return true;
            } else {
                return false;
            }
        }
        @Override
        public void describeTo(final Description description) {
            // Should be improved!
            description.appendText("Color did not match "+color[0]);
        }
    };
}

我不确定,但我们可以检索某些元素的颜色,例如按钮和文本视图`

Button button = (Button) findViewById(R.id.my_button);
Drawable buttonBackground = button.getBackground();

你可以这样试试

ColorDrawable b_color = (ColorDrawable) button.getBackground();

然后

int color = b_color.getColor();
if (color == R.color.green) {
    log("color is green");
}

希望这能让你入门。

在我的测试中,我有以下匹配器用于测试 EditText 颜色:

public static Matcher<View> withTextColor(final int color) {
    Checks.checkNotNull(color);
    return new BoundedMatcher<View, EditText>(EditText.class) {
        @Override
        public boolean matchesSafely(EditText warning) {
            return color == warning.getCurrentTextColor();
        }
        @Override
        public void describeTo(Description description) {
            description.appendText("with text color: ");
        }
    };
}

用法是:

onView(withId(R.id.password_edittext)).check(matches(withTextColor(Color.RED)));

这是我在测试中使用的

public static Matcher<View> withTextColor(final int color) {
    Checks.checkNotNull(color);
        return new BoundedMatcher<View, TextView>(TextView.class) {
            @Override
            public boolean matchesSafely(TextView textView) {
                return ContextCompat.getColor(getTargetContext(),color)==textView.getCurrentTextColor();
            }
            @Override
            public void describeTo(Description description) {
                description.appendText("with text color: ");
            }
        };
}

并将其命名为

onView(withId(R.id.price_value)).check(匹配(withText Color(R.color.black)));

另一种检查 TextView 文本颜色的方法是通过 hasTextColor(int color),因为它直接来自 Espresso

import static android.support.test.espresso.matcher.ViewMatchers.hasTextColor;

onView(withId(R.id.anyTextView)).check(matches(hasTextColor(R.color.red)));

请注意,此方法目前处于 Espresso 3.0.1 的 Beta 阶段

private fun hasBackgroundColor(colorRes: Int): Matcher<View> {
    Checks.checkNotNull(colorRes)

    return object : TypeSafeMatcher<View>() {
        override fun describeTo(description: Description?) {
            description?.appendText("background color: $colorRes")
        }

        override fun matchesSafely(item: View?): Boolean {

            val context = item?.context
            val textViewColor = (item?.background as ColorDrawable).color
            val expectedColor: Int?

            if (Build.VERSION.SDK_INT <= 22) {
                expectedColor = context?.resources?.getColor(colorRes)
            } else {
                expectedColor = context?.getColor(colorRes)
            }

            return textViewColor == expectedColor
        }
    }
}

可以测试颜色。我创建了一个方法来测试 TextView 背景颜色,如下所示。另请注意,我正在传递颜色资源。

 private fun withBackgroundColor(@ColorInt color: Int): Matcher<View> {
        Checks.checkNotNull(color)
        return object : BoundedMatcher<View, TextView>(TextView::class.java) {
            override fun describeTo(description: Description?) {
                description?.appendText("TextView background color to be $color")
            }

            override fun matchesSafely(item: TextView?): Boolean {
                val backgroundColor = item?.background as ColorDrawable
                val colorDrawable = ColorDrawable(ContextCompat.getColor(item.context, color))
                return colorDrawable.color == backgroundColor.color
            }

        }
    }

我从颜色资源创建 ColorDrawable 对象并从该对象获取颜色。

比较没有 ColorDrawable 的资源的颜色不会通过。希望对你有帮助。

我来晚了,但这可能对其他人有帮助

    public static Matcher<View> matchesBackgroundColor(final int expectedResourceId) {
    return new BoundedMatcher<View, View>(View.class) {
        int actualColor;
        int expectedColor;
        String message;

        @Override
        protected boolean matchesSafely(View item) {
            if (item.getBackground() == null) {
                message = item.getId() + " does not have a background";
                return false;
            }
            Resources resources = item.getContext().getResources();
            expectedColor = ResourcesCompat.getColor(resources, expectedResourceId, null);

            try {
                actualColor = ((ColorDrawable) item.getBackground()).getColor();
            }
            catch (Exception e) {
                actualColor = ((GradientDrawable) item.getBackground()).getColor().getDefaultColor();
            }
            finally {
                if (actualColor == expectedColor) {
                    Timber.i("Success...: Expected Color " + String.format("#%06X", (0xFFFFFF & expectedColor))
                            + " Actual Color " + String.format("#%06X", (0xFFFFFF & actualColor)));
                }
            }
            return actualColor == expectedColor;
        }
        @Override
        public void describeTo(final Description description) {
            if (actualColor != 0) { message = "Background color did not match: Expected "
                    +  String.format("#%06X", (0xFFFFFF & expectedColor))
                    + " was " + String.format("#%06X", (0xFFFFFF & actualColor));
            }
            description.appendText(message);
        }
    };
}

我还以十六进制格式记录了预期和实际颜色,这可能会有帮助。

用法:onView(withId(R.id.viewId)).check(matches(matchesBackgroundColor(R.color.colorId)));

在 Kotlin 中使用 SAM 转换更简洁优雅的方式

onView(withId(R.id.tv_login_result)).check{
            view, _ ->
        val actualColor = ((view as MaterialTextView).background as 
                           ColorDrawable).color
        assertEquals(Color.RED, actualColor)
    }