gpt4 book ai didi

android-espresso - 安卓 Espresso : How check "drawableStart" of EditText?

转载 作者:行者123 更新时间:2023-12-04 01:55:44 24 4
gpt4 key购买 nike

我有我的 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 测试检查 EditTextandroid:drawableStart="@drawable/ic_sign_in_password".

我该怎么做?

最佳答案

创建一个辅助方法 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)));

关于android-espresso - 安卓 Espresso : How check "drawableStart" of EditText?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50817066/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com