我正在尝试编写一个测试来检查我的 ViewGroup 是否具有正确的背景。我正在使用 Espresso 和 Hamcrest 进行测试。
这是我用于 ViewGroup(RelativeLayout) 背景的 xml 文件。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="4dp" />
<solid android:color="@color/gray" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="4dp" />
<solid android:color="@color/white" />
<stroke
android:width="1dp"
android:color="@color/colorPrimaryDark" />
</shape>
</item>
</selector>
尝试 1
我遵循了来自programcreek.com的代码,并且在和. 由于示例中未涵盖,因此我尝试添加一些代码,希望它能起作用。GradientDrawable
getCurrent
expectedDrawable
actualDrawable
GradientDrawable
public static Matcher<View> withBgDrawable(final int expectedResourceId) {
return new TypeSafeMatcher<View>() {
@Override
public boolean matchesSafely(View view) {
Drawable actualDrawable = view.getBackground();
if (expectedResourceId < 0) return actualDrawable == null;
Drawable expectedDrawable = ContextCompat.getDrawable(view.getContext(), expectedResourceId);
if (expectedDrawable == null) return false;
if (actualDrawable instanceof StateListDrawable) {
actualDrawable = actualDrawable.getCurrent();
}
if (expectedDrawable instanceof StateListDrawable) {
expectedDrawable = expectedDrawable.getCurrent();
}
if (expectedDrawable instanceof GradientDrawable) {
return actualDrawable instanceof GradientDrawable && gradientToBitmap((GradientDrawable) expectedDrawable)
.sameAs(gradientToBitmap((GradientDrawable) actualDrawable));
}
throw new IllegalArgumentException("Unsupported drawable: " + actualDrawable);
}
@Override
public void describeTo(Description description) {
description.appendText("with drawable ");
}
};
}
private static Bitmap gradientToBitmap(GradientDrawable gradientDrawable) {
Bitmap bitmap = Bitmap.createBitmap(gradientDrawable.getIntrinsicWidth(), gradientDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
gradientDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
gradientDrawable.draw(canvas);
return bitmap;
}
这是我得到的错误。
java.lang.IllegalArgumentException:宽度和高度必须 > 0
因此,我记录了 的值gradientDrawable.getIntrinsicWidth()
以查看发生了什么,发现它是-1
.
尝试 2
然后,我尝试将那些StateListDrawable
(s)直接更改为Bitmap
(s)。我得到与尝试 1 相同的错误。
public static Matcher<View> withBgDrawable(final int expectedResourceId) {
return new TypeSafeMatcher<View>() {
@Override
public boolean matchesSafely(View view) {
Drawable actualDrawable = view.getBackground();
if (expectedResourceId < 0) return actualDrawable == null;
Drawable expectedDrawable = ContextCompat.getDrawable(view.getContext(), expectedResourceId);
if (expectedDrawable == null) return false;
if (expectedDrawable instanceof StateListDrawable) {
return actualDrawable instanceof StateListDrawable && stateListToBitmap((StateListDrawable) expectedDrawable)
.sameAs(stateListToBitmap((StateListDrawable) actualDrawable));
}
throw new IllegalArgumentException("Unsupported drawable: " + actualDrawable);
}
@Override
public void describeTo(Description description) {
description.appendText("with drawable ");
}
};
}
private static Bitmap stateListToBitmap(StateListDrawable stateListDrawable) {
Bitmap bitmap = Bitmap.createBitmap(stateListDrawable.getIntrinsicWidth(), stateListDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
stateListDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
stateListDrawable.draw(canvas);
return bitmap;
}
尝试 3
如果我只是在equals
or==
上使用StateListDrawable
or GradientDrawable
,则来自同一 xml 文件的可绘制对象不匹配。
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with drawable' 与所选视图不匹配。