Jul 24, 2017 · 2 min read
Thanks! Daniele Bottillo. That matcher is good but not enought, if you have another target different than *ImageView* or If you are asking for *src* and not the *background*.
In those other cases I implemented this matcher hopes it helps:
@Override
protected boolean matchesSafely(View target) {
Bitmap backgroundBitmap = null;
Bitmap resourceBitmap = null;
Class<?> clazz = target.getClass();
if (clazz == ImageView.class) {
ImageView image = (ImageView) target;
if (expectedId < 0) {
return image.getBackground() == null;
}
resourceBitmap = drawableToBitmap(image.getDrawable());
backgroundBitmap = drawableToBitmap(image.getBackground());
}
if (clazz == AppCompatImageView.class) {
AppCompatImageView image = (AppCompatImageView) target;
if (expectedId < 0) {
return image.getBackground() == null;
}
resourceBitmap = drawableToBitmap(image.getDrawable());
backgroundBitmap = drawableToBitmap(image.getBackground());
}
if(clazz == AppCompatImageButton.class){
AppCompatImageButton image = (AppCompatImageButton) target;
if (expectedId < 0) {
return image.getDrawable() == null;
}
resourceBitmap = drawableToBitmap(image.getDrawable());
backgroundBitmap = drawableToBitmap(image.getBackground());
}
if (clazz == ImageView.class) {
ImageView image = (ImageView) target;
if (expectedId < 0) {
return image.getBackground() == null;
}
resourceBitmap = drawableToBitmap(image.getDrawable());
backgroundBitmap = drawableToBitmap(image.getBackground());
}
if(clazz == FloatingActionMenu.class){
FloatingActionMenu image = (FloatingActionMenu) target;
if (expectedId < 0) {
return image.getMenuIconView() == null;
}
resourceBitmap = drawableToBitmap(image.getMenuIconView().getDrawable());
backgroundBitmap = drawableToBitmap(image.getMenuIconView().getBackground());
}
if(clazz == FloatingActionButton.class){
FloatingActionButton image = (FloatingActionButton) target;
if (expectedId < 0) {
return image.getDrawable() == null;
}
resourceBitmap = drawableToBitmap(image.getDrawable());
backgroundBitmap = drawableToBitmap(image.getBackground());
}
Resources resources = target.getContext().getResources();
Drawable expectedDrawable = resources.getDrawable(expectedId);
resourceName = resources.getResourceEntryName(expectedId);
if (expectedDrawable == null) {
return false;
}
Bitmap otherBitmap = drawableToBitmap(expectedDrawable);
return (resourceBitmap != null && resourceBitmap.sameAs(otherBitmap)) ||
(backgroundBitmap != null && backgroundBitmap.sameAs(otherBitmap));
}the class drawableToBitmap is kind of like *getBitmap(Drawable drawable)*, take a look:
public static Bitmap drawableToBitmap (Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if(bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}
if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
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;
}