首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用espresso测试矢量绘图

如何使用espresso测试矢量绘图
EN

Stack Overflow用户
提问于 2019-06-18 03:51:57
回答 1查看 1.7K关注 0票数 2

我有一个矢量绘图,我想测试使用espresso。

代码语言:javascript
复制
  <TextView
                android:id="@+id/text_video_call"
                style="@style/UserDetails.TextView.Actions"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="24dp"
                android:drawableTop="@drawable/ic_video_24dp"
                android:text="@string/msg_video_call"
                android:visibility="gone"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@+id/text_message"
                app:layout_constraintTop_toBottomOf="@+id/text_username"
                tools:visibility="visible" />

向量可绘制: ic_video_24dp

代码语言:javascript
复制
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="20"
    android:viewportHeight="20">

    <path
        android:fillColor="#1d74f5"
        android:fillType="evenOdd"
        android:pathData="M16.538,13.564l-3.388,-1.09v-2.07l3.394,-1.085 -0.006,4.245zM11.696,14.56L3.454,14.56L3.454,8.32h8.242v6.24zM17.428,8.107c0.362,0.261 0.57,0.69 0.57,1.176v4.312c0,0.487 -0.209,0.914 -0.57,1.175a1.37,1.37 0,0 1,-0.808 0.254c-0.164,0 -0.331,-0.026 -0.498,-0.08l-2.972,-0.956L13.15,16L2,16L2,6.88h11.15v2.01l2.973,-0.956c0.468,-0.15 0.943,-0.087 1.305,0.173zM4.424,5.44L4.424,4h6.302v1.44L4.424,5.44z" />
</vector>

有没有什么方法可以用espresso测试一个矢量图形?任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2019-06-18 03:56:46

最好不要比较位图,而是遵循以下答案的建议:https://stackoverflow.com/a/14474954/1396068

当设置图像视图的可绘制时,也将可绘制的ID存储在setTag(R.drawable.your_drawable).的标签中,然后使用Espresso的withTagValue(equalTo(R.drawable.your_drawable))匹配器检查正确的标签。

请检查我找到的这个教程。看起来https://medium.com/@dbottillo/android-ui-test-espresso-matcher-for-imageview-1a28c832626f#.4snjg8frw运行得很好

以下是复制粘贴的摘要;-)

代码语言:javascript
复制
public class DrawableMatcher extends TypeSafeMatcher<View> {

    private final int expectedId;
    String resourceName;

    public DrawableMatcher(int expectedId) {
        super(View.class);
        this.expectedId = expectedId;
    }

    @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);
        resourceName = resources.getResourceEntryName(expectedId);

        if (expectedDrawable == null) {
            return false;
        }

        Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
        Bitmap otherBitmap = ((BitmapDrawable) expectedDrawable).getBitmap();
        return bitmap.sameAs(otherBitmap);
    }


    @Override
    public void describeTo(Description description) {
        description.appendText("with drawable from resource id: ");
        description.appendValue(expectedId);
        if (resourceName != null) {
            description.appendText("[");
            description.appendText(resourceName);
            description.appendText("]");
        }
    }
}

请注意,这只适用于当您的绘图是一个BitmapDrawable。如果您也有VectorDrawable或其他可绘制的,则必须检查此(imageView.getDrawable() instanceOf XXXDrawable)并从中获取位图。除了你有一种简单的绘图,你只有一种颜色,或者你可以比较。

为了获得VectorDrawable的位图,您必须将VectorDrawable绘制到画布并将其保存为位图。如果你有一个StateListDrawable,你可以得到所选状态的Drawable,并重复你的If级联实例。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56638025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档