TestUtilsMatchers.java revision ee9519c17254b5e992164ff278173c4b2c7c5fce
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.support.v7.testutils;
18
19import android.database.sqlite.SQLiteCursor;
20import android.graphics.drawable.Drawable;
21import android.support.annotation.ColorInt;
22import android.support.test.espresso.matcher.BoundedMatcher;
23import android.support.v4.view.TintableBackgroundView;
24import android.text.TextUtils;
25import android.view.View;
26import android.widget.CheckedTextView;
27import android.widget.ImageView;
28import junit.framework.Assert;
29import org.hamcrest.Description;
30import org.hamcrest.Matcher;
31import org.hamcrest.TypeSafeMatcher;
32
33public class TestUtilsMatchers {
34    /**
35     * Returns a matcher that matches <code>ImageView</code>s which have drawable flat-filled
36     * with the specific color.
37     */
38    public static Matcher drawable(@ColorInt final int color) {
39        return new BoundedMatcher<View, ImageView>(ImageView.class) {
40            private String failedComparisonDescription;
41
42            @Override
43            public void describeTo(final Description description) {
44                description.appendText("with drawable of color: ");
45
46                description.appendText(failedComparisonDescription);
47            }
48
49            @Override
50            public boolean matchesSafely(final ImageView view) {
51                Drawable drawable = view.getDrawable();
52                if (drawable == null) {
53                    return false;
54                }
55
56                // One option is to check if we have a ColorDrawable and then call getColor
57                // but that API is v11+. Instead, we call our helper method that checks whether
58                // all pixels in a Drawable are of the same specified color.
59                try {
60                    TestUtils.assertAllPixelsOfColor("", drawable, view.getWidth(),
61                            view.getHeight(), true, color, true);
62                    // If we are here, the color comparison has passed.
63                    failedComparisonDescription = null;
64                    return true;
65                } catch (Throwable t) {
66                    // If we are here, the color comparison has failed.
67                    failedComparisonDescription = t.getMessage();
68                    return false;
69                }
70            }
71        };
72    }
73
74    /**
75     * Returns a matcher that matches <code>CheckedTextView</code>s which are in checked state.
76     */
77    public static Matcher isCheckedTextView() {
78        return new BoundedMatcher<View, CheckedTextView>(CheckedTextView.class) {
79            private String failedDescription;
80
81            @Override
82            public void describeTo(final Description description) {
83                description.appendText("checked text view: ");
84
85                description.appendText(failedDescription);
86            }
87
88            @Override
89            public boolean matchesSafely(final CheckedTextView view) {
90                if (view.isChecked()) {
91                    return true;
92                }
93
94                failedDescription = "not checked";
95                return false;
96            }
97        };
98    }
99
100    /**
101     * Returns a matcher that matches <code>CheckedTextView</code>s which are in checked state.
102     */
103    public static Matcher isNonCheckedTextView() {
104        return new BoundedMatcher<View, CheckedTextView>(CheckedTextView.class) {
105            private String failedDescription;
106
107            @Override
108            public void describeTo(final Description description) {
109                description.appendText("non checked text view: ");
110
111                description.appendText(failedDescription);
112            }
113
114            @Override
115            public boolean matchesSafely(final CheckedTextView view) {
116                if (!view.isChecked()) {
117                    return true;
118                }
119
120                failedDescription = "checked";
121                return false;
122            }
123        };
124    }
125
126    /**
127     * Returns a matcher that matches data entry in <code>SQLiteCursor</code> that has
128     * the specified text in the specified column.
129     */
130    public static Matcher<Object> withCursorItemContent(final String columnName,
131            final String expectedText) {
132        return new BoundedMatcher<Object, SQLiteCursor>(SQLiteCursor.class) {
133            @Override
134            public void describeTo(Description description) {
135                description.appendText("doesn't match " + expectedText);
136            }
137
138            @Override
139            protected boolean matchesSafely(SQLiteCursor cursor) {
140                return TextUtils.equals(expectedText,
141                        cursor.getString(cursor.getColumnIndex(columnName)));
142            }
143        };
144    }
145
146    /**
147     * Returns a matcher that matches Views which implement TintableBackgroundView.
148     */
149    public static Matcher<View> isTintableBackgroundView() {
150        return new TypeSafeMatcher<View>() {
151            @Override
152            public void describeTo(Description description) {
153                description.appendText("is TintableBackgroundView");
154            }
155
156            @Override
157            public boolean matchesSafely(View view) {
158                return TintableBackgroundView.class.isAssignableFrom(view.getClass());
159            }
160        };
161    }
162
163}
164