ImageView包含drawable

时间:2016-08-10 08:07:25

标签: android android-espresso hamcrest

我已经从his medium post实施了Daniele Bottilo的Drawable匹配器。

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

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

它不起作用,IDE警告我

  谓词中的

not(... guava.base.Predicate)无法应用于(org.hamcrest.Matcher)

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

1 个答案:

答案 0 :(得分:7)

我已经在Medium上回答了你,但我也会在这里发表回复;在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;
    }
    ...
}

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

相关问题