1package com.xtremelabs.robolectric.matchers;
2
3import android.widget.ImageView;
4import org.hamcrest.Description;
5import org.hamcrest.Factory;
6import org.hamcrest.Matcher;
7import org.junit.internal.matchers.TypeSafeMatcher;
8
9import static com.xtremelabs.robolectric.Robolectric.shadowOf;
10
11public 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