Espresso 测试 ImageView 包含可绘制对象

Espresso testing that ImageView contains a drawable

我已经实现了来自 his medium post 的 Daniele Bottilo 的 Drawable 匹配器。

现在我想用它来测试我的图像视图是否为空。我试过这个:

onView(withId(R.id.image)) 
        .check( matches( not(noDrawable()) ) );

它不起作用,IDE 警告我

not(...guava.base.Predicate) in Predicates cannot be applied to (org.hamcrest.Matcher)

我是 Espresso 的新手,还没有成功 Google 找到答案。我应该使用的另一个包中是否有 'Not',或者我在这里做错了什么?

我已经在 Medium 上回复了你,但我也会 post 我的回复在这里;在 EspressoTestsMatchers 中我会添加:

public static Matcher<View> hasDrawable() {
    return new DrawableMatcher(DrawableMatcher.ANY);
}

在 DrawableMatcher 中你可以做这样的事情:

static final int EMPTY = -1;
static final int ANY = -2;

@Override
protected boolean matchesSafely(View target) {
    ...
    ImageView imageView = (ImageView) target;
    if (expectedId == EMPTY){
        return imageView.getDrawable() == null;
    }
    if (expectedId == ANY){
        return imageView.getDrawable() != null;
    }
    ...
}

实际上,我认为我应该根据您的要求更新我的 post! hasDrawable() 匹配器很有用:)