浓缩咖啡测试,ImageView包含一个drawable

Nic*_*oso 3 android hamcrest android-espresso

我从他的中篇文章中实现了Daniele Bottilo的Drawable matcher .

现在我想用它来测试我的图像视图是不是空的.我试过这个:

onView(withId(R.id.image)) 
        .check( matches( not(noDrawable()) ) );
Run Code Online (Sandbox Code Playgroud)

它不起作用,IDE警告我

谓词中没有(... guava.base.Predicate)不能应用于(org.hamcrest.Matcher)

我是Espresso的新手,并没有成功地成功获得Google的答案.在我应该使用的另一个包装中是否有"不",或者我在这里做错了什么?

Dan*_*llo 7

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

public static Matcher<View> hasDrawable() {
    return new DrawableMatcher(DrawableMatcher.ANY);
}
Run Code Online (Sandbox Code Playgroud)

在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;
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

实际上我想我应该根据你的要求更新我的帖子!hasDrawable()匹配器可能很有用:)