Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso testing that ImageView contains a drawable

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?

like image 307
Nick Cardoso Avatar asked Aug 10 '16 08:08

Nick Cardoso


1 Answers

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

like image 159
Daniele Bottillo Avatar answered Nov 09 '22 10:11

Daniele Bottillo