Android Espresso:如何检查EditText的“drawableStart”?

时间:2018-06-12 12:19:22

标签: android-espresso

我有我的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"/>

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

我怎么能这样做?

3 个答案:

答案 0 :(得分:2)

创建一个辅助方法sameBitmap,比较2个drawables。

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;
}

然后,创建一个检查相对drawable的匹配器。在这里,它仅验证可绘制的开始,但如果您想验证可绘制的末端或顶部或底部,则可以自行扩展:

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)));

答案 1 :(得分:1)

haroldolivieri 代码的简单版本,仅支持 TextViews 上的非着色复合可绘制对象:

private fun withDrawable(@DrawableRes id: Int, drawablePosition: TextDrawablePosition) =
    object : TypeSafeMatcher<View>() {
        override fun describeTo(description: Description) {
            description.appendText("TextView with compound drawable at position $drawablePosition same as drawable with id $id")
        }

        override fun matchesSafely(view: View): Boolean {
            if (view !is TextView) {
                return false
            }

            val expectedBitmap = view.context.getDrawable(id)?.toBitmap()
            return view.compoundDrawables[drawablePosition.ordinal].toBitmap().sameAs(expectedBitmap)
        }
    }

enum class TextDrawablePosition { LEFT, TOP, RIGHT, BOTTOM }

你可以这样使用它: onView(withId(R.id.my_view_id)).check(matches(withDrawable(R.drawable.my_drawable, LEFT)))

答案 2 :(得分:0)

基于此article,我做了一个小的更改,以便能够检查compoundDrawables内的任何TextView

fun withDrawable(
    @DrawableRes id: Int,
    @ColorRes tint: Int? = null,
    tintMode: PorterDuff.Mode = PorterDuff.Mode.SRC_IN,
    drawablePosition: TextDrawablePosition = TextDrawablePosition.Left
) = object :
    TypeSafeMatcher<View>() {
    override fun describeTo(description: Description) {
        description.appendText("View with drawable same as drawable with id $id")
        tint?.let { description.appendText(", tint color id: $tint, mode: $tintMode") }
    }

    override fun matchesSafely(view: View): Boolean {
        val context = view.context
        val tintColor = tint?.toColor(context)
        val expectedBitmap = context.getDrawable(id)?.tinted(tintColor, tintMode)?.toBitmap()

        return when (view) {
            is TextView -> view.compoundDrawables[drawablePosition.ordinal].toBitmap().sameAs(expectedBitmap)
            is ImageView -> view.drawable.toBitmap().sameAs(expectedBitmap)
            else -> false
        }
    }
}

enum class TextDrawablePosition {
    Left, Top, Right, Bottom
}