skyisle / android-test-kit

Automatically exported from code.google.com/p/android-test-kit

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add withDrawable to ViewMatchers

GoogleCodeExporter opened this issue · comments

In a lot of test cases we need to check, that ImageView/ImageButton has correct 
drawable associated with in it.

I wasn't able to find solution inside standard Espresso's ViewMatchers. So I've 
created custom ViewMatcher — 
https://gist.github.com/korovyansk/bb9e832bcb2ee86e4475.

What about add it to standard Espresso's ViewMatchers?

Original issue reported on code.google.com by Alex.Kor...@gmail.com on 15 Aug 2014 at 2:35

The link doesn't work, btw.

Original comment by vale...@google.com on 23 Oct 2014 at 9:55

fixed link — https://gist.github.com/AlexKorovyansky/bb9e832bcb2ee86e4475

Original comment by Alex.Kor...@gmail.com on 2 Nov 2014 at 2:01

Original comment by vale...@google.com on 23 Oct 2014 at 9:55

  • Changed state: Accepted
I'm not sure whether testing getConstantState equality is the right approach. 

I added the following test for your matcher:

  public void testWithDrawable() {
    ImageView iv = new ImageView(getInstrumentation().getTargetContext());
    iv.setImageResource(android.R.drawable.zoom_plate);
    Matcher<View> goodMatcher = withDrawable(android.R.drawable.zoom_plate);
    assertThat(goodMatcher.toString(), not(containsString("zoom_plate")));
    assertThat(goodMatcher.matches(iv), is(true));  <<<<<<<<<<<<<<<<<<<< FAILS HERE
    assertThat(goodMatcher.toString(), containsString("zoom_plate"));
    assertThat(withDrawable(android.R.drawable.star_on).matches(iv), is(false));
    assertThat(withDrawable(-1).matches(iv), is(false));
  }

It fails at the line:
return drawable.getConstantState().equals(resourcesDrawable.getConstantState());

Looking at a couple of implementation of ConstantState (e.g. BitmapState and 
VectorDrawableState), they don't implement the equals method, so it would just 
fall back to obj and fail. Do you have a test that works?

Original comment by vale...@google.com on 26 Nov 2014 at 10:40

Yes, it works here — 

@SmallTest
    public void testLogout() throws Exception {
        onView(withId(R.id.custom_actionbar_action_button))
                .check(matches(withDrawable(R.drawable.ic_logout)));
    }

Where drawable.ic_logout is based on png resource image.

Original comment by Alex.Kor...@gmail.com on 27 Nov 2014 at 8:54