如何在浓缩咖啡中获取文本视图的实际颜色值
How to get actual color value of text view in espresso
我正在尝试验证 android 应用中文本视图的颜色。
healthHistoryPage.allergies.check(匹配(hasTextColor(android.R.color.black))))
我收到一条错误消息:
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'has color with ID 17170444' 与所选视图不匹配。
预期:具有 ID 为 android 的颜色:color/black
但是我在错误消息的任何地方都看不到实际颜色值是多少。如果我无法访问源代码,有没有办法获取实际颜色值。
看这里修改后的原版hasTextColor匹配器:https://android.googlesource.com/platform/frameworks/testing/+/android-support-test/espresso/core/src/main/java/android/support/test/espresso/matcher/ViewMatchers.java
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
注释修改了颜色十六进制代码
这将记录类似于以下内容的错误:
...
Expected: has color with ID com.appham.sandbox:color/colorAccent and
color code: #FF4081
...
我正在尝试验证 android 应用中文本视图的颜色。
healthHistoryPage.allergies.check(匹配(hasTextColor(android.R.color.black))))
我收到一条错误消息:
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'has color with ID 17170444' 与所选视图不匹配。
预期:具有 ID 为 android 的颜色:color/black
但是我在错误消息的任何地方都看不到实际颜色值是多少。如果我无法访问源代码,有没有办法获取实际颜色值。
看这里修改后的原版hasTextColor匹配器:https://android.googlesource.com/platform/frameworks/testing/+/android-support-test/espresso/core/src/main/java/android/support/test/espresso/matcher/ViewMatchers.java
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
注释修改了颜色十六进制代码
这将记录类似于以下内容的错误:
... Expected: has color with ID com.appham.sandbox:color/colorAccent and color code: #FF4081 ...