Please check out this tutorial that I found. Seems to work pretty well https://medium.com/@dbottillo/android-ui-test-espresso-matcher-for-imageview-1a28c832626f#.4snjg8frw
Here is a summary for pasta copies; -)
public class DrawableMatcher extends TypeSafeMatcher<View> { private final int expectedId; String resourceName; public DrawableMatcher(int expectedId) { super(View.class); this.expectedId = expectedId; } @Override protected boolean matchesSafely(View target) { if (!(target instanceof ImageView)){ return false; } ImageView imageView = (ImageView) target; if (expectedId < 0){ return imageView.getDrawable() == null; } Resources resources = target.getContext().getResources(); Drawable expectedDrawable = resources.getDrawable(expectedId); resourceName = resources.getResourceEntryName(expectedId); if (expectedDrawable == null) { return false; } Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap(); Bitmap otherBitmap = ((BitmapDrawable) expectedDrawable).getBitmap(); return bitmap.sameAs(otherBitmap); } @Override public void describeTo(Description description) { description.appendText("with drawable from resource id: "); description.appendValue(expectedId); if (resourceName != null) { description.appendText("["); description.appendText(resourceName); description.appendText("]"); } } }
Remember that this only works when your Drawable is BitmapDrawable . If you also have VectorDrawable or another Drawable , you should check this ( imageView.getDrawable() instanceOf XXXDrawable ) and get a bitmap from it. In addition, you have some simple Drawable where you have only one color or you can compare.
To get a VectorDrawable bitmap, for example, you have to draw a VectorDrawable on the canvas and save it in a bitmap (I had some problems drawing VectorDrawable). If you have a StateListDrawable, you can get Drawable from the selected state and repeat the if instanceOf cascade. For other types of Drawable, I have no experience, sorry!
wolle
source share