aboutsummaryrefslogtreecommitdiff
path: root/v1/src/main/java/com/xtremelabs/robolectric/matchers/HasResourceMatcher.java
blob: 12928e684c477a7f44fe9b2fddf93b3f388da60f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.xtremelabs.robolectric.matchers;

import android.widget.ImageView;
import org.hamcrest.Description;
import org.hamcrest.Factory;
import org.hamcrest.Matcher;
import org.junit.internal.matchers.TypeSafeMatcher;

import static com.xtremelabs.robolectric.Robolectric.shadowOf;

public class HasResourceMatcher extends TypeSafeMatcher<ImageView> {
    private int expectedResourceId;
    private Integer actualResourceId;

    public HasResourceMatcher(int expectedResourceId) {
        this.expectedResourceId = expectedResourceId;
    }

    @Override
    public boolean matchesSafely(ImageView actual) {
        if (actual == null) {
            return false;
        }

        actualResourceId = shadowOf(actual).getResourceId();

        return actualResourceId == expectedResourceId;
    }

    @Override
    public void describeTo(Description description) {
        if (actualResourceId == null) {
            description.appendText("actual view was null");
        } else {
            description.appendText("[" + actualResourceId + "]");
            description.appendText(" to equal ");
            description.appendText("[" + expectedResourceId + "]");
        }
    }

    @Factory
    public static Matcher<ImageView> hasResource(int expectedResourceId) {
        return new HasResourceMatcher(expectedResourceId);
    }

}