Espresso testing that ImageView contains a drawable
Asked Answered
H

1

6

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?

Herring answered 10/8, 2016 at 8:7 Comment(0)
C
12

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 :)

Cittern answered 11/8, 2016 at 12:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.