I'm trying to write a test to check if my ViewGroup has the right background. I'm using Espresso and Hamcrest for the test.
This is the xml file I used for the background of my ViewGroup(RelativeLayout).
<?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>
Attempt 1
I followed the code from programcreek.com and I get GradientDrawable
after getCurrent
is called on both expectedDrawable
and actualDrawable
. Since GradientDrawable
isn't covered in the example, I tried adding a few code hoping it would work.
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;
}
this is the error I get.
java.lang.IllegalArgumentException: width and height must be > 0
So, I logged the value of gradientDrawable.getIntrinsicWidth()
to see what's going on and found that it was -1
.
Attempt 2
Then, I tried changing those StateListDrawable
(s) directly into Bitmap
(s). I get the same error as attempt 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;
}
Attempt 3
If I simply use equals
or ==
on either StateListDrawable
or GradientDrawable
, drawables from the same xml file do not match.
android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with drawable ' doesn't match the selected view.