liudonghua123 / android-test-kit

Automatically exported from code.google.com/p/android-test-kit
0 stars 0 forks source link

Add withDrawable to ViewMatchers #94

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago

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

GoogleCodeExporter commented 9 years ago
The link doesn't work, btw.

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

GoogleCodeExporter commented 9 years ago
fixed link — https://gist.github.com/AlexKorovyansky/bb9e832bcb2ee86e4475

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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
There is no good way to compare drawable instances across all API levels.

Original comment by nkors...@google.com on 5 May 2015 at 6:00