可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Is it possible to check if the background color matches a given color with espresso?
I made a custom matcher, similar to what @Irfan suggested, thanks!
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]);
}
};
}
回答1:
I am not sure about that but we can retrieve the color of some of elements like button and text views `
Button button = (Button) findViewById(R.id.my_button);
Drawable buttonBackground = button.getBackground();
and you can try like this
ColorDrawable b_color = (ColorDrawable) button.getBackground();
and then
int color = b_color.getColor();
if (color == R.color.green) {
log("color is green");
}
Hope this will get you started.
回答2:
In my tests I have the following matcher for testing EditText
color:
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: ");
}
};
}
And usage is :
onView(withId(R.id.password_edittext)).check(matches(withTextColor(Color.RED)));
回答3:
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
}
}
}
回答4:
Here is what i use in my tests
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: ");
}
};
}
and call it as
onView(withId(R.id.price_value)).check(matches(withTextColor(R.color.black)));
回答5:
Another approach to check TextView text's color could be via hasTextColor(int color)
as it comes directly from Espresso.
import static android.support.test.espresso.matcher.ViewMatchers.hasTextColor;
onView(withId(R.id.anyTextView)).check(matches(hasTextColor(R.color.red)));
Be aware that this method is currently in Beta for Espresso 3.0.1
回答6:
It is possible to to test on color. I created a method to test on TextView
background color as below. Also notice that am passing color resource.
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
}
}
}
I create ColorDrawable
object from color resource and get color from the object.
Comparing color from resource without the ColorDrawable
does not pass. Hope this helps you.