android–Espresso与多个角色的矢量抽屉不匹配 –ImageDrawable(AppCompatResource)

android–Espresso与多个角色的矢量抽屉不匹配 –ImageDrawable(AppCompatResource),第1张

概述我试图断言使用Espresso在Android中的图像视图上是否显示正确的图像.图像以SVG形式提供,并使用AndroidStudio转换器直接转换为矢量绘图.我的自定义匹配器已成功测试某些图像,但不适用于其他图像.如果我交换了SVGXML文件,那么结果会与现在失败的那些成功相反,这让我怀疑它可能与检

我试图断言使用Espresso在Android中的图像视图上是否显示正确的图像.图像以SVG形式提供,并使用AndroID Studio转换器直接转换为矢量绘图.我的自定义匹配器已成功测试某些图像,但不适用于其他图像.如果我交换了SVG XML文件,那么结果会与现在失败的那些成功相反,这让我怀疑它可能与检查SVG本身有关,可能是由于我删除的某些路径的字符数量一些行包含的字符多于其他的字符,但不能确定如何正确修复它.

这是有效的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转换为Bitmap而仅在那时进行断言,但由于某种原因,获取项目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);

或者,为图像设置标签可能是一个很好的选择以及this SO问题.

总结

以上是内存溢出为你收集整理的android – Espresso与多个角色的矢量抽屉不匹配 – ImageDrawable(AppCompatResource)全部内容,希望文章能够帮你解决android – Espresso与多个角色的矢量抽屉不匹配 – ImageDrawable(AppCompatResource)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1118575.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存