1 package com.xtremelabs.robolectric.matchers; 2 3 import android.widget.ImageView; 4 import org.hamcrest.Description; 5 import org.hamcrest.Factory; 6 import org.hamcrest.Matcher; 7 import org.junit.internal.matchers.TypeSafeMatcher; 8 9 import static com.xtremelabs.robolectric.Robolectric.shadowOf; 10 11 public class HasResourceMatcher extends TypeSafeMatcher<ImageView> { 12 private int expectedResourceId; 13 private Integer actualResourceId; 14 15 public HasResourceMatcher(int expectedResourceId) { 16 this.expectedResourceId = expectedResourceId; 17 } 18 19 @Override 20 public boolean matchesSafely(ImageView actual) { 21 if (actual == null) { 22 return false; 23 } 24 25 actualResourceId = shadowOf(actual).getResourceId(); 26 27 return actualResourceId == expectedResourceId; 28 } 29 30 @Override 31 public void describeTo(Description description) { 32 if (actualResourceId == null) { 33 description.appendText("actual view was null"); 34 } else { 35 description.appendText("[" + actualResourceId + "]"); 36 description.appendText(" to equal "); 37 description.appendText("[" + expectedResourceId + "]"); 38 } 39 } 40 41 @Factory 42 public static Matcher<ImageView> hasResource(int expectedResourceId) { 43 return new HasResourceMatcher(expectedResourceId); 44 } 45 46 } 47