如何检查两个StateListDrawable是否相等?

时间:2017-11-27 04:08:40

标签: android android-espresso hamcrest xml-drawable statelistdrawable

我正在尝试编写一个测试来检查我的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中的代码,并在GradientDrawablegetCurrent上调用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:width和height必须是&gt; 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==上使用StateListDrawableGradientDrawable,则同一xml文件中的drawable不匹配。

  

android.support.test.espresso.base.DefaultFailureHandler $ AssertionFailedWithCauseError:   &#39; with drawable&#39;与所选视图不匹配。

0 个答案:

没有答案
相关问题