Created
July 23, 2018 22:31
-
-
Save ryansgot/e68d48947f957d81981135cd9b900e34 to your computer and use it in GitHub Desktop.
Espresso testing Drawables with color filters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DrawableMatcher extends TypeSafeMatcher<View> { | |
public interface Extractor { | |
@NonNull Drawable extract(@NonNull View v); | |
} | |
private String mReason; | |
@DrawableRes private final int mDrawableId; | |
private ColorFilter mExpectedColorFilter; | |
private Extractor mExtractor; | |
DrawableMatcher(@DrawableRes int drawableId, @Nullable ColorFilter expectedColorFilter, Extractor extractor) { | |
mDrawableId = drawableId; | |
mExpectedColorFilter = expectedColorFilter; | |
mExtractor = extractor; | |
} | |
@Override | |
protected boolean matchesSafely(View target) { | |
if (!(target instanceof ImageView) && mExtractor == null) { | |
mReason = "view " + target.getId() + " is not an " + ImageView.class.getSimpleName() + ". Pass in a custom Extractor in order to use this matcher."; | |
return false; | |
} | |
Drawable actualDrawable = mExtractor == null ? defaultExtract(target) : mExtractor.extract(target); | |
if (mDrawableId < 0 && actualDrawable != null) { | |
mReason = "expected no drawable for view " + target.getId() + ", but has one"; | |
return false; | |
} | |
Drawable expectedDrawable = ViewTestUtil.drawableById(target.getContext(), mDrawableId); | |
if (expectedDrawable == null) { | |
mReason = "drawable with id " + mDrawableId + " does not exist"; | |
return false; | |
} | |
expectedDrawable.setColorFilter(mExpectedColorFilter); | |
if (!ViewTestUtil.getBitmap(expectedDrawable).sameAs(ViewTestUtil.getBitmap(actualDrawable))) { | |
mReason = "expected and actual bitmaps do not match"; | |
return false; | |
} | |
return true; | |
} | |
@Override | |
public void describeTo(Description description) { | |
description.appendText(mReason == null ? "" : mReason); | |
} | |
private static Drawable defaultExtract(View target) { | |
ImageView targetImageView = (ImageView) target; | |
return targetImageView.getDrawable(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class ViewTestUtil { | |
public static Bitmap getBitmap(Drawable drawable) { | |
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); | |
Canvas canvas = new Canvas(bitmap); | |
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); | |
drawable.draw(canvas); | |
return bitmap; | |
} | |
public static Drawable drawableById(Context context, @DrawableRes int id) { | |
final Drawable drawable = ContextCompat.getDrawable(context, id); | |
return Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP | |
? (DrawableCompat.wrap(drawable)).mutate() | |
: drawable; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment