gpt4 book ai didi

android - Espresso 不匹配具有许多字符的矢量绘图 - ImageDrawable(AppCompatResource)

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:10:45 26 4
gpt4 key购买 nike

我正在尝试使用 Espresso 断言在 Android 中的 ImageView 上是否显示了正确的图像。图像以 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 :(
}

为了使我的测试通过

,这些是需要 注释掉的行

enter image description here

最佳答案

出于某种原因,将代码从 setImageDrawable(AppCompatResources) 更改为 setImageResource 已经解决了这个问题。

//ivDialogType.setImageDrawable(AppCompatResources.getDrawable(Objects.requireNonNull(getContext()), R.drawable.ic_metrics_dialog_weight));
ivDialogType.setImageResource(R.drawable.ic_metrics_dialog_weight);

或者为图像设置一个标签可能是一个不错的选择,也是this的一个好选择。所以问题。

关于android - Espresso 不匹配具有许多字符的矢量绘图 - ImageDrawable(AppCompatResource),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52968631/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com