AssertJについてはこの辺参照
- http://joel-costigliola.github.io/assertj/index.html
- http://qiita.com/petitviolet/items/c95255bf3e1ccd9766c3
Custom assertionを作るにあたってはこちらを参考にさせて頂いた
作ったもの
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);
}
}
その他
もともと、assertj-android
でhasDrawable
というメソッドは用意されてたんだけど、xmlのsrcにセットしてて、それを比較する場合に上手く動かなかったので、Bitmapで比較するようにした。