首页 > 解决方案 > Android Espresso:如何检查 EditText 的“drawableStart”?

问题描述

我有我的 xml 布局:

<EditText
            android:id="@+id/passwordEditText"
            android:layout_width="0dp"
            android:layout_height="45dp"
            android:layout_marginBottom="20dp"
            android:drawableStart="@drawable/ic_sign_in_password"
            android:drawablePadding="15dp"
            android:hint="@string/password"
android:textSize="15sp"/>

我想编写EditText有的 Espresso测试检查android:drawableStart="@drawable/ic_sign_in_password".

我怎么能做到这一点?

标签: android-espresso

解决方案


创建一个sameBitmap比较 2 个可绘制对象的辅助方法。

private static boolean sameBitmap(Drawable actualDrawable, Drawable expectedDrawable) {
    if (actualDrawable == null || expectedDrawable == null) {
        return false;
    }

    if (actualDrawable instanceof StateListDrawable && expectedDrawable instanceof StateListDrawable) {
        actualDrawable = actualDrawable.getCurrent();
        expectedDrawable = expectedDrawable.getCurrent();
    }
    if (actualDrawable instanceof BitmapDrawable) {
        Bitmap bitmap = ((BitmapDrawable) actualDrawable).getBitmap();
        Bitmap otherBitmap = ((BitmapDrawable) expectedDrawable).getBitmap();
        return bitmap.sameAs(otherBitmap);
    }

    if (actualDrawable instanceof VectorDrawable ||
            actualDrawable instanceof VectorDrawableCompat ||
            actualDrawable instanceof GradientDrawable) {
        Rect drawableRect = actualDrawable.getBounds();
        Bitmap bitmap = Bitmap.createBitmap(drawableRect.width(), drawableRect.height(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        actualDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        actualDrawable.draw(canvas);

        Bitmap otherBitmap = Bitmap.createBitmap(drawableRect.width(), drawableRect.height(), Bitmap.Config.ARGB_8888);
        Canvas otherCanvas = new Canvas(otherBitmap);
        expectedDrawable.setBounds(0, 0, otherCanvas.getWidth(), otherCanvas.getHeight());
        expectedDrawable.draw(otherCanvas);
        return bitmap.sameAs(otherBitmap);
    }
    return false;
}

然后,创建一个匹配器来检查相关的可绘制对象。在这里,它只验证可绘制的开始,但如果您想验证可绘制的结束或顶部或底部,您可以自己扩展它:

private static Matcher<View> withRelativeDrawables(int expectedDrawableStart) {
    return new TypeSafeMatcher<View>() {
        @Override
        protected boolean matchesSafely(View item) {
            if (item instanceof TextView) {
                TextView textView = (TextView) item;
                //get an array of 4 relative drawables. The first one is drawable start
                Drawable[] relativeDrawables = textView.getCompoundDrawablesRelative();

                Drawable expectedDrawableStart = ContextCompat.getDrawable(context, expectedDrawableStart);
                return sameBitmap(relativeDrawables[0], expectedDrawableStart);
            }
            return false;
        }

        @Override
        public void describeTo(Description description) {

        }
    };
} 

然后如下使用它:

onView(withId(R.id.passwordEditText)).check(matches(withRelativeDrawables(R.drawable.ic_sign_in_password)));

推荐阅读