Android Espresso matches BitmapDrawables with a different hue

Is there a way to compare two Bitmapsthat are wrapped BitmapDrawable.

The comparison should not end if the sizes do not match, but they must match the pixels and color Bitmap

I'm not sure how the native part of Android draws Bitmap, because it sameAsreturns true, even if the color of the hue is different.

If the size is different, I can create a scaled one Bitmapfrom another and compare it with.

In my source code I use DrawableCompat.setTintc ImageViews Drawable, and in the test code I load Drawablefrom resources and align it in the same way.

Any ideas? I would like to have a test that checks the original Drawableof ImageViewand the color also based on clicking on it or not.

NOTE 1: My drawings are white and I use a hue to set the color. Cyclic pixels for Bitmapsdo not work, because at this moment they are white, most likely, on the Android side, the hue color is used when drawing.

NOTE 2: The use compile 'com.android.support:palette-v7:21.0.0'and Palette.from(bitmap).generate();does not help either because the palette has returned 0 samples, so it can not get out there no color information.

This is my current match:

public static Matcher<View> withDrawable(final Drawable d) {
    return new BoundedMatcher<View, ImageView>(ImageView.class) {

        @Override
        public boolean matchesSafely(ImageView iv) {
            if (d == null) {
                return iv.getDrawable() == null;
            } else if (iv.getDrawable() == null) {
                return false;
            }

            if (d instanceof BitmapDrawable && iv.getDrawable() instanceof BitmapDrawable) {
                BitmapDrawable d1 = (BitmapDrawable) d;
                BitmapDrawable d2 = (BitmapDrawable) iv.getDrawable();

                Bitmap b1 = d1.getBitmap();
                Bitmap b2 = d2.getBitmap();

                return b1.sameAs(b2);
            }

            return iv.getDrawable().getConstantState().equals(d.getConstantState());
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("with drawable: ");
        }
    };
}

Thanks.

+4
source share

Source: https://habr.com/ru/post/1609917/


All Articles