I've implemented Daniele Bottilo's Drawable matcher from his medium post.
Now I'd like to use it to test that my image view is not empty. I tried this:
onView(withId(R.id.image))
.check( matches( not(noDrawable()) ) );
It does not work, the IDE warns me
not(...guava.base.Predicate) in Predicates cannot be applied to (org.hamcrest.Matcher)
I am new to Espresso and havent managed to Google the answer successfully. Is there a 'Not' in another package that I should be using, or what am I doing wrong here?
I've answered you on Medium already but I will post my reply also here; In EspressoTestsMatchers I would add:
public static Matcher<View> hasDrawable() {
return new DrawableMatcher(DrawableMatcher.ANY);
}
And in the DrawableMatcher you can do something like this:
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;
}
...
}
Actually I think I should update my post with your request! The hasDrawable() matcher can be useful :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With