[Qiita] AndroidのUIテストでBitmapDrawableとColorDrawableをチェックする

※この記事は以前Qiitaに投稿されていた古い記事です

はじめに

AndroidのUIテストでDrawableをチェックしたかったのですが、デフォルトではDrawableのMatcherが無いようなので、以下の記事を参考にMatcherを作成してチェックしてみました。
Android UI Test — Espresso Matcher for ImageView
https://medium.com/@dbottillo/android-ui-test-espresso-matcher-for-imageview-1a28c832626f#.qv4pjg197
また、上記の記事ではBitmapDrawableしかチェックすることができなかったので、ColorDrawableもチェックできるようなMatcherを作ってみました。

BitmapDrawableMatcher

Matcherの作成

まず、以下の記事の一番下にあるDrawableMatcherのクラスを作成します(ColorDrawableのMatcherと分けるためにクラス名をBitmapDrawableMatcherに変えました)。
Android UI Test — Espresso Matcher for ImageView
https://medium.com/@dbottillo/android-ui-test-espresso-matcher-for-imageview-1a28c832626f#.qv4pjg197
次に、以下のようなメソッドを作成します。
public class DrawableMatcher {
    public static Matcher<View> withBitmapDrawable(final int resourceId) {
        return new BitmapDrawableMatcher(resourceId);
    }
}

使い方

引数にDrawableのResourceIdを渡して、checkするだけです。
import static <パッケージ名>.DrawableMatcher.withBitmapDrawable;

onView(withId(R.id.image_view)).check(matches(withBitmapDrawable(R.drawable.ic_image_view)));

ColorDrawableMatcher

記事のDrawableMatcherを参考に、ColorDrawableMatcherを作成してみました。
やっていることとしては、targetのViewからBackgroundを取得して、そのBackgroundのColorと一致するかどうかを見ています。

Matcherの作成

public class ColorDrawableMatcher extends TypeSafeMatcher<View> {
    private final int expectedId;
    private String resourceName;

    public ColorDrawableMatcher(int expectedId) {
        super(View.class);
        this.expectedId = expectedId;
    }

    @Override
    protected boolean matchesSafely(View target) {
        if (expectedId < 0){
            return false;
        }
        Resources resources = target.getContext().getResources();
        Drawable expectedDrawable = ContextCompat.getDrawable(target.getContext(), expectedId);
        resourceName = resources.getResourceEntryName(expectedId);

        if (expectedDrawable == null) {
            return false;
        }

        int color = ((ColorDrawable) target.getBackground()).getColor();
        int otherColor = ((ColorDrawable) expectedDrawable).getColor();
        return color == otherColor;
    }

    @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("]");
        }
    }
}
public class DrawableMatcher {

    /** 省略 */

    public static Matcher<View> withColorDrawable(final int resourceId) {
        return new ColorDrawableMatcher(resourceId);
    }
}

使い方

引数にcolorのResourceIdを渡して、checkするだけです。
import static <パッケージ名>.DrawableMatcher.withColorDrawable;



onView(withId(R.id.view)).check(matches(withColorDrawable(android.R.color.white)));

おわりに

Matcherを作るのは始めてでしたが、無事に目的のテストを行うことができたので良かったです。
以上、おつかれさまでした。

コメント

人気の投稿

[Qiita] Google Playのクローズドベータ版テストでメールアドレスを指定して公開する

[Qiita] Androidの実機でPCのlocalhostに接続したり、Webページの要素を検証する

[Qiita] AppCompatのAlertDialogのテーマカラーを変える