我正在尝试在Android应用中验证文本视图的颜色。
healthHistoryPage.allergies.check(匹配(hasTextColor(android.R.color.black))))
我收到错误说:
android.support.test.espresso.base.DefaultFailureHandler $ AssertionFailedWithCauseError:'ID为17170444的颜色'与所选视图不匹配。
预计:颜色为ID android:color / black
但我没有看到错误消息中的任何地方的实际颜色值是什么。如果我无法访问源代码,有没有办法可以获取实际的颜色值。
答案 0 :(得分:0)
public static Matcher<View> hasTextColor(final int colorResId) {
return new BoundedMatcher<View, TextView>(TextView.class) {
private Context context;
@Override
protected boolean matchesSafely(TextView textView) {
context = textView.getContext();
int textViewColor = textView.getCurrentTextColor();
int expectedColor;
if (Build.VERSION.SDK_INT <= 22) {
expectedColor = context.getResources().getColor(colorResId);
} else {
expectedColor = context.getColor(colorResId);
}
return textViewColor == expectedColor;
}
@Override
public void describeTo(Description description) {
String colorId = String.valueOf(colorResId);
String colorCode = "";
if (context != null) {
colorId = context.getResources().getResourceName(colorResId);
//added get color hex code
@ColorInt int colorInt = context.getResources().getColor(colorResId);
colorCode = String.format("#%06X", (0xFFFFFF & colorInt));
}
description.appendText("has color with ID " + colorId);
//added output color code in error msg
description.appendText(" and color code: " +
colorCode);
}
};
}
注意://added
注释以及颜色十六进制代码的修改
这将记录类似于:
的错误... 预期:具有ID com.appham.sandbox的颜色:color / colorAccent和 颜色代码:#FF4081 ...