TestUtilsMatchers.java revision 21f78eb90b3217aa5cf69c3ffd359506468b55f4
1244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov/*
2244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov * Copyright (C) 2015 The Android Open Source Project
3244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov *
4244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov * Licensed under the Apache License, Version 2.0 (the "License");
5244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov * you may not use this file except in compliance with the License.
6244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov * You may obtain a copy of the License at
7244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov *
8244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov *      http://www.apache.org/licenses/LICENSE-2.0
9244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov *
10244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov * Unless required by applicable law or agreed to in writing, software
11244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov * distributed under the License is distributed on an "AS IS" BASIS,
12244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov * See the License for the specific language governing permissions and
14244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov * limitations under the License.
15244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov */
16244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
17244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovpackage android.support.v7.testutils;
18244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
191d8515a3b783e9b091bea2891606b466515b4fddKirill Grouchnikovimport android.database.sqlite.SQLiteCursor;
20244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport android.graphics.drawable.Drawable;
21244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport android.support.annotation.ColorInt;
22244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport android.support.test.espresso.matcher.BoundedMatcher;
23ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikovimport android.support.v4.view.TintableBackgroundView;
241d8515a3b783e9b091bea2891606b466515b4fddKirill Grouchnikovimport android.text.TextUtils;
25244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport android.view.View;
262d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikovimport android.widget.CheckedTextView;
27244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport android.widget.ImageView;
28ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikovimport junit.framework.Assert;
29244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport org.hamcrest.Description;
30244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovimport org.hamcrest.Matcher;
31ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikovimport org.hamcrest.TypeSafeMatcher;
32244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
3321f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikovimport java.util.List;
3421f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov
35244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikovpublic class TestUtilsMatchers {
36244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    /**
37244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov     * Returns a matcher that matches <code>ImageView</code>s which have drawable flat-filled
38244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov     * with the specific color.
39244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov     */
40244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    public static Matcher drawable(@ColorInt final int color) {
41244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        return new BoundedMatcher<View, ImageView>(ImageView.class) {
42244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            private String failedComparisonDescription;
43244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
44244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            @Override
45244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            public void describeTo(final Description description) {
46244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                description.appendText("with drawable of color: ");
47244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
48244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                description.appendText(failedComparisonDescription);
49244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            }
50244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
51244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            @Override
52244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            public boolean matchesSafely(final ImageView view) {
53244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                Drawable drawable = view.getDrawable();
54244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                if (drawable == null) {
55244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                    return false;
56244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                }
57244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov
58244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                // One option is to check if we have a ColorDrawable and then call getColor
59244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                // but that API is v11+. Instead, we call our helper method that checks whether
60244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                // all pixels in a Drawable are of the same specified color.
61244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                try {
62244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                    TestUtils.assertAllPixelsOfColor("", drawable, view.getWidth(),
631b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov                            view.getHeight(), true, color, 0, true);
64244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                    // If we are here, the color comparison has passed.
65244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                    failedComparisonDescription = null;
66244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                    return true;
67244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                } catch (Throwable t) {
68244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                    // If we are here, the color comparison has failed.
69244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                    failedComparisonDescription = t.getMessage();
70244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                    return false;
71244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov                }
72244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov            }
73244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov        };
74244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov    }
752d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
762d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    /**
772d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov     * Returns a matcher that matches <code>CheckedTextView</code>s which are in checked state.
782d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov     */
792d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    public static Matcher isCheckedTextView() {
802d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        return new BoundedMatcher<View, CheckedTextView>(CheckedTextView.class) {
812d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            private String failedDescription;
822d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
832d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            @Override
842d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            public void describeTo(final Description description) {
852d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                description.appendText("checked text view: ");
862d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
872d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                description.appendText(failedDescription);
882d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            }
892d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
902d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            @Override
912d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            public boolean matchesSafely(final CheckedTextView view) {
922d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                if (view.isChecked()) {
932d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                    return true;
942d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                }
952d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
962d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                failedDescription = "not checked";
972d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                return false;
982d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            }
992d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        };
1002d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    }
1012d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
1022d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    /**
1032d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov     * Returns a matcher that matches <code>CheckedTextView</code>s which are in checked state.
1042d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov     */
1052d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    public static Matcher isNonCheckedTextView() {
1062d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        return new BoundedMatcher<View, CheckedTextView>(CheckedTextView.class) {
1072d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            private String failedDescription;
1082d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
1092d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            @Override
1102d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            public void describeTo(final Description description) {
1112d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                description.appendText("non checked text view: ");
1122d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
1132d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                description.appendText(failedDescription);
1142d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            }
1152d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
1162d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            @Override
1172d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            public boolean matchesSafely(final CheckedTextView view) {
1182d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                if (!view.isChecked()) {
1192d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                    return true;
1202d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                }
1212d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov
1222d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                failedDescription = "checked";
1232d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov                return false;
1242d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov            }
1252d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov        };
1262d18d3a4b92369971ed446cf54d98fb0fc55646aKirill Grouchnikov    }
1271d8515a3b783e9b091bea2891606b466515b4fddKirill Grouchnikov
1281d8515a3b783e9b091bea2891606b466515b4fddKirill Grouchnikov    /**
1291d8515a3b783e9b091bea2891606b466515b4fddKirill Grouchnikov     * Returns a matcher that matches data entry in <code>SQLiteCursor</code> that has
1301d8515a3b783e9b091bea2891606b466515b4fddKirill Grouchnikov     * the specified text in the specified column.
1311d8515a3b783e9b091bea2891606b466515b4fddKirill Grouchnikov     */
1321d8515a3b783e9b091bea2891606b466515b4fddKirill Grouchnikov    public static Matcher<Object> withCursorItemContent(final String columnName,
1331d8515a3b783e9b091bea2891606b466515b4fddKirill Grouchnikov            final String expectedText) {
1341d8515a3b783e9b091bea2891606b466515b4fddKirill Grouchnikov        return new BoundedMatcher<Object, SQLiteCursor>(SQLiteCursor.class) {
1351d8515a3b783e9b091bea2891606b466515b4fddKirill Grouchnikov            @Override
1361d8515a3b783e9b091bea2891606b466515b4fddKirill Grouchnikov            public void describeTo(Description description) {
1371d8515a3b783e9b091bea2891606b466515b4fddKirill Grouchnikov                description.appendText("doesn't match " + expectedText);
1381d8515a3b783e9b091bea2891606b466515b4fddKirill Grouchnikov            }
1391d8515a3b783e9b091bea2891606b466515b4fddKirill Grouchnikov
1401d8515a3b783e9b091bea2891606b466515b4fddKirill Grouchnikov            @Override
1411d8515a3b783e9b091bea2891606b466515b4fddKirill Grouchnikov            protected boolean matchesSafely(SQLiteCursor cursor) {
1421d8515a3b783e9b091bea2891606b466515b4fddKirill Grouchnikov                return TextUtils.equals(expectedText,
1431d8515a3b783e9b091bea2891606b466515b4fddKirill Grouchnikov                        cursor.getString(cursor.getColumnIndex(columnName)));
1441d8515a3b783e9b091bea2891606b466515b4fddKirill Grouchnikov            }
1451d8515a3b783e9b091bea2891606b466515b4fddKirill Grouchnikov        };
1461d8515a3b783e9b091bea2891606b466515b4fddKirill Grouchnikov    }
147ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov
148ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov    /**
149ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov     * Returns a matcher that matches Views which implement TintableBackgroundView.
150ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov     */
151ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov    public static Matcher<View> isTintableBackgroundView() {
152ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov        return new TypeSafeMatcher<View>() {
153ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov            @Override
154ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov            public void describeTo(Description description) {
155ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov                description.appendText("is TintableBackgroundView");
156ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov            }
157ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov
158ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov            @Override
159ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov            public boolean matchesSafely(View view) {
160ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov                return TintableBackgroundView.class.isAssignableFrom(view.getClass());
161ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov            }
162ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov        };
163ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov    }
164ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov
16521f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov    /**
16621f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov     * Returns a matcher that matches lists of float values that fall into the specified range.
16721f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov     */
16821f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov    public static Matcher<List<Float>> inRange(final float from, final float to) {
16921f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov        return new TypeSafeMatcher<List<Float>>() {
17021f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov            private String mFailedDescription;
17121f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov
17221f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov            @Override
17321f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov            public void describeTo(Description description) {
17421f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                description.appendText(mFailedDescription);
17521f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov            }
17621f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov
17721f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov            @Override
17821f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov            protected boolean matchesSafely(List<Float> item) {
17921f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                int itemCount = item.size();
18021f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov
18121f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                for (int i = 0; i < itemCount; i++) {
18221f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                    float curr = item.get(i);
18321f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov
18421f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                    if ((curr < from) || (curr > to)) {
18521f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                        mFailedDescription = "Value #" + i + ":" + curr + " should be between " +
18621f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                                from + " and " + to;
18721f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                        return false;
18821f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                    }
18921f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                }
19021f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov
19121f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                return true;
19221f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov            }
19321f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov        };
19421f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov    }
19521f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov
19621f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov    /**
19721f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov     * Returns a matcher that matches lists of float values that are in ascending order.
19821f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov     */
19921f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov    public static Matcher<List<Float>> inAscendingOrder() {
20021f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov        return new TypeSafeMatcher<List<Float>>() {
20121f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov            private String mFailedDescription;
20221f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov
20321f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov            @Override
20421f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov            public void describeTo(Description description) {
20521f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                description.appendText(mFailedDescription);
20621f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov            }
20721f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov
20821f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov            @Override
20921f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov            protected boolean matchesSafely(List<Float> item) {
21021f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                int itemCount = item.size();
21121f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov
21221f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                if (itemCount >= 2) {
21321f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                    for (int i = 0; i < itemCount - 1; i++) {
21421f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                        float curr = item.get(i);
21521f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                        float next = item.get(i + 1);
21621f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov
21721f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                        if (curr > next) {
21821f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                            mFailedDescription = "Values should increase between #" + i +
21921f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                                    ":" + curr + " and #" + (i + 1) + ":" + next;
22021f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                            return false;
22121f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                        }
22221f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                    }
22321f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                }
22421f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov
22521f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                return true;
22621f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov            }
22721f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov        };
22821f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov    }
22921f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov
23021f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov    /**
23121f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov     * Returns a matcher that matches lists of float values that are in descending order.
23221f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov     */
23321f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov    public static Matcher<List<Float>> inDescendingOrder() {
23421f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov        return new TypeSafeMatcher<List<Float>>() {
23521f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov            private String mFailedDescription;
23621f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov
23721f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov            @Override
23821f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov            public void describeTo(Description description) {
23921f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                description.appendText(mFailedDescription);
24021f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov            }
24121f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov
24221f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov            @Override
24321f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov            protected boolean matchesSafely(List<Float> item) {
24421f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                int itemCount = item.size();
24521f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov
24621f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                if (itemCount >= 2) {
24721f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                    for (int i = 0; i < itemCount - 1; i++) {
24821f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                        float curr = item.get(i);
24921f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                        float next = item.get(i + 1);
25021f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov
25121f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                        if (curr < next) {
25221f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                            mFailedDescription = "Values should decrease between #" + i +
25321f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                                    ":" + curr + " and #" + (i + 1) + ":" + next;
25421f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                            return false;
25521f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                        }
25621f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                    }
25721f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                }
25821f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov
25921f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov                return true;
26021f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov            }
26121f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov        };
26221f78eb90b3217aa5cf69c3ffd359506468b55f4Kirill Grouchnikov    }
263244abf1fee3fe4fab72a1d8925407e29219940beKirill Grouchnikov}
264