【问题标题】:Espresso testing that ImageView contains a drawableEspresso 测试 ImageView 是否包含可绘制对象
【发布时间】:2016-08-10 08:07:25
【问题描述】:
我已经从his medium post 实现了 Daniele Bottilo 的 Drawable 匹配器。
现在我想用它来测试我的图像视图是否为空。我试过这个:
onView(withId(R.id.image))
.check( matches( not(noDrawable()) ) );
它不起作用,IDE 警告我
Predicates 中的 not(...guava.base.Predicate) 不能应用于 (org.hamcrest.Matcher)
我是 Espresso 的新手,还没有成功地通过 Google 找到答案。我应该使用的另一个包中是否有“不”,或者我在这里做错了什么?
【问题讨论】:
标签:
android
android-espresso
hamcrest
【解决方案1】:
我已经在 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() 匹配器很有用:)