android-espresso - 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".
我怎么能做到这一点?
解决方案
创建一个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)));
推荐阅读
- c - 为什么到达 MEX 文件的最后一行后返回 Matlab 需要这么长时间?
- symfony - symfony 在规范化实体时找不到支持规范化器
- c - 为什么下面的代码会在字符串末尾执行意外的字母?
- javascript - 链 connect/mapStateToProps/mapDispatchToProps 函数用于 react-redux 中的代码重用
- java - 你如何在java中重复初始化一个新标签?
- java - @MatrixVariable 注解的 pathVar 属性在 Spring 中是如何工作的?
- python - python中的for循环;创建一个包含比例的列表
- matlab - MATLAB 中的属性初始化
- java - 无法加载平铺地图 (Libgdx)
- android - 硬件加速情况下的Android webview截图