测试背景颜色浓缩咖啡Android

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

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

public static Matcher backgroundShouldHaveColor(int expectedColor) { return buttondShouldHaveBackgroundColor(equalTo(expectedColor)); } private static Matcher buttonShouldHaveBackgroundColor(final Matcher expectedObject) { final int[] color = new int[1]; return new BoundedMatcher( 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 withTextColor(final int color) { Checks.checkNotNull(color); return new BoundedMatcher(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 withTextColor(final int color) { Checks.checkNotNull(color); return new BoundedMatcher(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))检查(匹配(withTextColor(R.color.black)))。

另一种检查TextView文本颜色的方法可以是直接来自Espresso的 hasTextColor(int color)

 import static android.support.test.espresso.matcher.ViewMatchers.hasTextColor; onView(withId(R.id.anyTextView)).check(matches(hasTextColor(R.color.red))); 

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

 private fun hasBackgroundColor(colorRes: Int): Matcher { Checks.checkNotNull(colorRes) return object : TypeSafeMatcher() { 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 } } } 

考虑到您有颜色列表,请创建它们的数组:

 < ?xml version="1.0" encoding="utf-8"?>  #000000 #FFFFFF  @color/black @color/white   

然后你得到这个数组并循环它:

 int[] colors = context.getResources().getIntArray(R.array.colors); for (int i = 0; i < colors.length; i++) { // Compare your color with 'colors[i]' }