Có thể kiểm tra xem màu nền có phù hợp với màu đã cho bằng cà phê không?Kiểm tra màu nền cà phê espresso Android
Tôi đã thực hiện một đối sánh tùy chỉnh, tương tự như những gì @Irfan đã đề xuất, cảm ơn!
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]);
}
};
}
Bạn nên thay thế EditText cho TextView, kể từ khi kế thừa đầu tiên kể từ sau và getCurrentTextColor() được thực hiện trong TextView. Tôi đã không thử nghiệm nó, nhưng nó sẽ làm việc cho cả EditText và TextView. – Maragues
Cảm ơn bạn đã có mẹo. – denys
hoặc: withTextColor (ContextCompat.getColor (mActivityTestRule.getActivity(), R.color.your_color)) :) – Ciprian