Skip to content

Instantly share code, notes, and snippets.

@mapyo
Created November 12, 2015 23:47
Show Gist options
  • Save mapyo/95664309e00aade60770 to your computer and use it in GitHub Desktop.
Save mapyo/95664309e00aade60770 to your computer and use it in GitHub Desktop.
Espresso
public static class ImageViewAssert extends AbstractAssert<ImageViewAssert, ImageView> {
public ImageViewAssert(ImageView actual) {
super(actual, ImageViewAssert.class);
}
public static ImageViewAssert assertThat(ImageView actual) {
return new ImageViewAssert(actual);
}
public ImageViewAssert hasDrawable(Drawable drawable) {
isNotNull();
Drawable actualDrawable = actual.getDrawable();
if (sameBitmap(actualDrawable, drawable)) {
failWithMessage("error");
}
return this;
}
public ImageViewAssert hasDrawable(int resId) {
isNotNull();
Context context = InstrumentationRegistry.getTargetContext();
Drawable drawable = ((BitmapDrawable) context.getResources().getDrawable(resId));
Drawable actualDrawable = actual.getDrawable();
if (!sameBitmap(actualDrawable, drawable)) {
failWithMessage("error");
}
return this;
}
private boolean sameBitmap(Drawable actual, Drawable expected) {
Bitmap actualBitmap = ((BitmapDrawable) actual).getBitmap();
Bitmap expectedBitmap = ((BitmapDrawable) expected).getBitmap();
return actualBitmap.sameAs(expectedBitmap);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment