Last active
March 25, 2017 16:24
-
-
Save ViksaaSkool/d23a7b2868fc9142bff0f4a69273995b to your computer and use it in GitHub Desktop.
DrawableMatcher for medium article https://is.gd/Ru0XVF
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> { | |
private final int expectedId; | |
private String resourceName; | |
private int isAnimatedDrawable = -1; | |
private int randomFrame = 0; | |
public DrawableMatcher(int expectedId) { | |
super(View.class); | |
this.expectedId = expectedId; | |
} | |
public DrawableMatcher(int expectedId, int isAnimatedDrawable) { | |
super(View.class); | |
this.expectedId = expectedId; | |
this.isAnimatedDrawable = isAnimatedDrawable; | |
} | |
@Override | |
protected boolean matchesSafely(View target) { | |
boolean result = false; | |
Resources resources = target.getContext().getResources(); | |
resourceName = resources.getResourceEntryName(expectedId); | |
Bitmap objectBitmap = null; | |
Bitmap expectedBitmap = null; | |
Random random = new Random(); | |
//if it's imageView | |
if (target instanceof ImageView) { | |
ImageView imageView = (ImageView) target; | |
if (expectedId < 0) { | |
return imageView.getDrawable() == null; | |
} | |
if (isAnimatedDrawable != DrawableUtils.ANIMATED_DRAWABLE) { | |
Drawable expectedDrawable = resources.getDrawable(expectedId); | |
if (expectedDrawable == null) { | |
return false; | |
} | |
objectBitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap(); | |
expectedBitmap = ((BitmapDrawable) expectedDrawable).getBitmap(); | |
result = objectBitmap.sameAs(expectedBitmap); | |
} else { | |
AnimationDrawable expectedDrawable = (AnimationDrawable) resources.getDrawable(expectedId); | |
if (expectedDrawable == null) { | |
return false; | |
} | |
//get a random frame | |
randomFrame = random.nextInt(expectedDrawable.getNumberOfFrames()); | |
AnimationDrawable objectAnimationDrawable = ((AnimationDrawable) imageView.getDrawable()); | |
Drawable drawable0 = objectAnimationDrawable.getFrame(randomFrame); | |
objectBitmap = ((BitmapDrawable) drawable0).getBitmap(); | |
Drawable drawable1 = expectedDrawable.getFrame(randomFrame); | |
expectedBitmap = ((BitmapDrawable) drawable1).getBitmap(); | |
result = objectBitmap.sameAs(expectedBitmap); | |
} | |
} | |
//if it's Button | |
else if (target instanceof Button) { | |
Button button = (Button) target; | |
if (isAnimatedDrawable == DrawableUtils.ANIMATED_DRAWABLE) { | |
AnimationDrawable expectedDrawable = (AnimationDrawable) resources.getDrawable(expectedId); | |
if (expectedDrawable == null) { | |
return false; | |
} | |
randomFrame = random.nextInt(expectedDrawable.getNumberOfFrames()); | |
Drawable drawable0 = expectedDrawable.getFrame(randomFrame); | |
AnimationDrawable buttonBackground = ((AnimationDrawable) button.getBackground()); | |
Drawable drawable1 = buttonBackground.getFrame(randomFrame); | |
objectBitmap = ((BitmapDrawable) drawable0).getBitmap(); | |
expectedBitmap = ((BitmapDrawable) drawable1).getBitmap(); | |
// if (buttonBackground.isRunning()) | |
result = objectBitmap.sameAs(expectedBitmap); | |
} | |
} | |
return result; | |
} | |
@Override | |
public void describeTo(Description description) { | |
description.appendText("with drawable from resource id: "); | |
description.appendValue(expectedId); | |
if(isAnimatedDrawable == DrawableUtils.ANIMATED_DRAWABLE){ | |
description.appendText("frame chosen for matching: "); | |
description.appendValue(randomFrame); | |
} | |
if (resourceName != null) { | |
description.appendText("["); | |
description.appendText(resourceName); | |
description.appendText("]"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment