1package com.xtremelabs.robolectric.matchers;
2
3import android.widget.TextView;
4import com.xtremelabs.robolectric.shadows.ShadowTextView;
5import org.hamcrest.Description;
6import org.hamcrest.Factory;
7import org.hamcrest.Matcher;
8import org.junit.internal.matchers.TypeSafeMatcher;
9
10import static com.xtremelabs.robolectric.Robolectric.shadowOf;
11
12public class HasCompoundDrawablesMatcher extends TypeSafeMatcher<TextView> {
13    private String message;
14    private ShadowTextView.CompoundDrawables expectedCompoundDrawables;
15
16    public HasCompoundDrawablesMatcher(int left, int top, int right, int bottom) {
17        expectedCompoundDrawables = new ShadowTextView.CompoundDrawables(left, top, right, bottom);
18    }
19
20    @Override
21    public boolean matchesSafely(TextView actual) {
22        if (actual == null) {
23            message = "actual view was null";
24            return false;
25        }
26
27        ShadowTextView.CompoundDrawables actualCompoundDrawables = shadowOf(actual).getCompoundDrawablesImpl();
28        if (!expectedCompoundDrawables.equals(actualCompoundDrawables)) {
29            message = "[" + actualCompoundDrawables + "] to equal [" + expectedCompoundDrawables + "]";
30            return false;
31        } else {
32            return true;
33        }
34    }
35
36    @Override
37    public void describeTo(Description description) {
38        description.appendText(message);
39    }
40
41    @Factory
42    public static Matcher<TextView> hasCompoundDrawables(int left, int top, int right, int bottom) {
43        return new HasCompoundDrawablesMatcher(left, top, right, bottom);
44    }
45}
46