Espresso 不匹配具有许多字符的矢量绘图 - ImageDrawable(AppCompatResource)
Espresso Doesn't Match Vector Drawables With Many Characters - ImageDrawable(AppCompatResource)
我正在尝试使用 Espresso 断言 Android 中的图像视图中是否显示了正确的图像。这些图像以 SVG 格式提供,并使用 Android Studio 转换器直接转换为矢量可绘制对象。我的自定义匹配器对某些图像进行了成功测试,但对其他图像却没有。如果我交换 SVG XML 文件,那么结果会与现在失败的文件相反,这让我怀疑这可能与检查 SVG 本身有关,可能是由于某些路径的字符数量当我 delete some lines
包含比其他字符更多的字符时它可以工作但不确定如何正确修复它。
这是有效的 SVG
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="80dp"
android:height="80dp"
android:viewportWidth="80"
android:viewportHeight="80">
<path
android:pathData="M37.2,37.1m-34.85,0a34.85,34.85 0,1 1,69.7 0a34.85,34.85 0,1 1,-69.7 0"
android:fillColor="#fff"/>
<path
android:pathData="M49.22,19.23h2.67A4.79,4.79 0,0 1,56.67 24V53.39a4.79,4.79 0,0 1,-4.78 4.77H22.51a4.79,4.79 0,0 1,-4.78 -4.77V24a4.79,4.79 0,0 1,4.78 -4.77h3.2"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M45.37,24.93a8.9,8.9 0,1 1,-8.9 -8.9A8.9,8.9 0,0 1,45.37 24.93Z"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M35.88,25.46L40.08,21.72"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
</vector>
但这是 SVG 不起作用
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="80dp"
android:height="80dp"
android:viewportWidth="80"
android:viewportHeight="80">
<path
android:pathData="M37.12,37.28m-34.85,0a34.85,34.85 0,1 1,69.7 0a34.85,34.85 0,1 1,-69.7 0"
android:fillColor="#fff"/>
<path
android:pathData="M34.37,44.93h8.46a3,3 0,0 0,3 -3h0a3,3 0,0 0,-3 -3H34.37"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M34.37,51h8.46a3,3 0,0 0,3 -3h0a3,3 0,0 0,-3 -3H34.37"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M28.6,57H27.54A16.27,16.27 0,0 1,19.62 55h0"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M34.37,51H41.1a3,3 0,0 1,3 3h0a3,3 0,0 1,-3 3H34.37"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M47.31,38.87h8a3.09,3.09 0,0 0,3.11 -2.81,3 3,0 0,0 -0.88,-2.36 3,3 0,0 0,-2.14 -0.89H30.84A1.06,1.06 0,0 1,30.09 31l6.07,-6.07a3.09,3.09 0,0 0,0.19 -4.21,3 3,0 0,0 -4.41,-0.13L18.19,34.33c-6.2,6.21 -6.43,16 1.43,20.65"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M54.91,43.54s3.42,4 3.42,7.05a3.42,3.42 0,0 1,-6.84 0c0,-3 3.42,-7.05 3.42,-7.05"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M54.8,48.13a5.3,5.3 0,0 0,-0.52 2,1.79 1.79,0 0,0 0.79,1.5"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M54.91,35.39L54.91,35.84"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M54.91,25.3L54.91,29.03"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M34.37,51a3,3 0,0 1,-3 -3h0a3,3 0,0 1,3 -3"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M34.37,57a3,3 0,0 1,-3 -3h0a3,3 0,0 1,3 -3"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M34.37,44.93a3,3 0,0 1,-3 -3h0a3,3 0,0 1,3 -3"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M58.13,18.56v4.6A2.14,2.14 0,0 1,56 25.3H53.83a2.14,2.14 0,0 1,-2.14 -2.14v-4.6"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
</vector>
这是我的自定义匹配器
public class EspressoTestsMatchers {
public Matcher<View> withDrawable(final int resourceId) {
return new DrawableMatcher(resourceId);
}
public Matcher<View> noDrawable() {
return new DrawableMatcher(-1);
}
}
public class DrawableMatcher extends TypeSafeMatcher<View> {
private final int expectedId;
public DrawableMatcher(int resourceId) {
super(View.class);
this.expectedId = resourceId;
}
@Override
protected boolean matchesSafely(View target) {
if (!(target instanceof ImageView)) {
return false;
}
ImageView imageView = (ImageView) target;
if (expectedId < 0) {
return imageView.getDrawable() == null;
}
Resources resources = target.getContext().getResources();
Drawable expectedDrawable = resources.getDrawable(expectedId);
if (expectedDrawable == null) {
return false;
}
Bitmap bitmap = getBitmap(imageView.getDrawable());
Bitmap otherBitmap = getBitmap(expectedDrawable);
return bitmap.sameAs(otherBitmap);
}
private Bitmap getBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
@Override
public void describeTo(Description description) {
}
}
这样调用
onView(withId(R.id.rv_metrics)).perform(actionOnItemAtPosition(0, click()));
onView(withId(R.id.iv_type)).check(matches(new EspressoTestsMatchers().withDrawable(R.drawable.ic_metrics_dialog_weight)));
非常感谢。
更新
还尝试将 SVG 转换为位图,然后才进行断言,但出于某种原因,获得 item drawable
仍然无法使断言通过。
BackgroundDrawableMatcher2(Drawable expectedDrawable) {
super(View.class);
mExpectedDrawable = expectedDrawable;
}
@Override
protected boolean matchesSafely(View item) {
Drawable drawable = ContextCompat.getDrawable(getTargetContext(), R.drawable.ic_metrics_dialog_weight); // Hardcoding this makes the assertion work
ImageView imageView = (ImageView) item;
return getBitmap2(imageView.getDrawable()).sameAs(getBitmap2(mExpectedDrawable)); // Getting the item this way instead doesn't though :(
}
为了让我的测试通过
,这些是需要注释掉的行
出于某种原因,将代码从 setImageDrawable(AppCompatResources)
更改为 setImageResource
已解决问题。
//ivDialogType.setImageDrawable(AppCompatResources.getDrawable(Objects.requireNonNull(getContext()), R.drawable.ic_metrics_dialog_weight));
ivDialogType.setImageResource(R.drawable.ic_metrics_dialog_weight);
或者为图像设置 tag
可能是一个不错的选择,也是 SO 问题。
我正在尝试使用 Espresso 断言 Android 中的图像视图中是否显示了正确的图像。这些图像以 SVG 格式提供,并使用 Android Studio 转换器直接转换为矢量可绘制对象。我的自定义匹配器对某些图像进行了成功测试,但对其他图像却没有。如果我交换 SVG XML 文件,那么结果会与现在失败的文件相反,这让我怀疑这可能与检查 SVG 本身有关,可能是由于某些路径的字符数量当我 delete some lines
包含比其他字符更多的字符时它可以工作但不确定如何正确修复它。
这是有效的 SVG
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="80dp"
android:height="80dp"
android:viewportWidth="80"
android:viewportHeight="80">
<path
android:pathData="M37.2,37.1m-34.85,0a34.85,34.85 0,1 1,69.7 0a34.85,34.85 0,1 1,-69.7 0"
android:fillColor="#fff"/>
<path
android:pathData="M49.22,19.23h2.67A4.79,4.79 0,0 1,56.67 24V53.39a4.79,4.79 0,0 1,-4.78 4.77H22.51a4.79,4.79 0,0 1,-4.78 -4.77V24a4.79,4.79 0,0 1,4.78 -4.77h3.2"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M45.37,24.93a8.9,8.9 0,1 1,-8.9 -8.9A8.9,8.9 0,0 1,45.37 24.93Z"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M35.88,25.46L40.08,21.72"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
</vector>
但这是 SVG 不起作用
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="80dp"
android:height="80dp"
android:viewportWidth="80"
android:viewportHeight="80">
<path
android:pathData="M37.12,37.28m-34.85,0a34.85,34.85 0,1 1,69.7 0a34.85,34.85 0,1 1,-69.7 0"
android:fillColor="#fff"/>
<path
android:pathData="M34.37,44.93h8.46a3,3 0,0 0,3 -3h0a3,3 0,0 0,-3 -3H34.37"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M34.37,51h8.46a3,3 0,0 0,3 -3h0a3,3 0,0 0,-3 -3H34.37"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M28.6,57H27.54A16.27,16.27 0,0 1,19.62 55h0"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M34.37,51H41.1a3,3 0,0 1,3 3h0a3,3 0,0 1,-3 3H34.37"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M47.31,38.87h8a3.09,3.09 0,0 0,3.11 -2.81,3 3,0 0,0 -0.88,-2.36 3,3 0,0 0,-2.14 -0.89H30.84A1.06,1.06 0,0 1,30.09 31l6.07,-6.07a3.09,3.09 0,0 0,0.19 -4.21,3 3,0 0,0 -4.41,-0.13L18.19,34.33c-6.2,6.21 -6.43,16 1.43,20.65"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M54.91,43.54s3.42,4 3.42,7.05a3.42,3.42 0,0 1,-6.84 0c0,-3 3.42,-7.05 3.42,-7.05"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M54.8,48.13a5.3,5.3 0,0 0,-0.52 2,1.79 1.79,0 0,0 0.79,1.5"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M54.91,35.39L54.91,35.84"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M54.91,25.3L54.91,29.03"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M34.37,51a3,3 0,0 1,-3 -3h0a3,3 0,0 1,3 -3"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M34.37,57a3,3 0,0 1,-3 -3h0a3,3 0,0 1,3 -3"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M34.37,44.93a3,3 0,0 1,-3 -3h0a3,3 0,0 1,3 -3"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
<path
android:pathData="M58.13,18.56v4.6A2.14,2.14 0,0 1,56 25.3H53.83a2.14,2.14 0,0 1,-2.14 -2.14v-4.6"
android:strokeWidth="1.97"
android:fillColor="#00000000"
android:strokeColor="#89d3dd"
android:strokeLineCap="round"/>
</vector>
这是我的自定义匹配器
public class EspressoTestsMatchers {
public Matcher<View> withDrawable(final int resourceId) {
return new DrawableMatcher(resourceId);
}
public Matcher<View> noDrawable() {
return new DrawableMatcher(-1);
}
}
public class DrawableMatcher extends TypeSafeMatcher<View> {
private final int expectedId;
public DrawableMatcher(int resourceId) {
super(View.class);
this.expectedId = resourceId;
}
@Override
protected boolean matchesSafely(View target) {
if (!(target instanceof ImageView)) {
return false;
}
ImageView imageView = (ImageView) target;
if (expectedId < 0) {
return imageView.getDrawable() == null;
}
Resources resources = target.getContext().getResources();
Drawable expectedDrawable = resources.getDrawable(expectedId);
if (expectedDrawable == null) {
return false;
}
Bitmap bitmap = getBitmap(imageView.getDrawable());
Bitmap otherBitmap = getBitmap(expectedDrawable);
return bitmap.sameAs(otherBitmap);
}
private Bitmap getBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
@Override
public void describeTo(Description description) {
}
}
这样调用
onView(withId(R.id.rv_metrics)).perform(actionOnItemAtPosition(0, click()));
onView(withId(R.id.iv_type)).check(matches(new EspressoTestsMatchers().withDrawable(R.drawable.ic_metrics_dialog_weight)));
非常感谢。
更新
还尝试将 SVG 转换为位图,然后才进行断言,但出于某种原因,获得 item drawable
仍然无法使断言通过。
BackgroundDrawableMatcher2(Drawable expectedDrawable) {
super(View.class);
mExpectedDrawable = expectedDrawable;
}
@Override
protected boolean matchesSafely(View item) {
Drawable drawable = ContextCompat.getDrawable(getTargetContext(), R.drawable.ic_metrics_dialog_weight); // Hardcoding this makes the assertion work
ImageView imageView = (ImageView) item;
return getBitmap2(imageView.getDrawable()).sameAs(getBitmap2(mExpectedDrawable)); // Getting the item this way instead doesn't though :(
}
为了让我的测试通过
,这些是需要注释掉的行出于某种原因,将代码从 setImageDrawable(AppCompatResources)
更改为 setImageResource
已解决问题。
//ivDialogType.setImageDrawable(AppCompatResources.getDrawable(Objects.requireNonNull(getContext()), R.drawable.ic_metrics_dialog_weight));
ivDialogType.setImageResource(R.drawable.ic_metrics_dialog_weight);
或者为图像设置 tag
可能是一个不错的选择,也是