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

标签 android svg android-espresso android-vectordrawable

我正在尝试使用 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/

相关文章:

android-espresso - 使用 espresso 处理由 AutoCompleteTextView 填充的选项

android - Espresso 中错误线程的 Realm 访问

javascript - 使用 Dagre d3 缩放后重新渲染 HTML

android - 如何将文件保存在手机内存中?

Android:OutofMemoryError:位图大小超出 VM 预算,我看不到任何理由

android - 是否可以在 Android Studio 中重命名模拟器?

javascript - 计算质心 d3

css - svg 填充在 Firefox 中不起作用

android - 检查回收站中位置的文本

android - 没有 ContentProvider 的 CursorLoader 使用