1ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov/*
2ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov * Copyright (C) 2016 The Android Open Source Project
3ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov *
4ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov * Licensed under the Apache License, Version 2.0 (the "License");
5ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov * you may not use this file except in compliance with the License.
6ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov * You may obtain a copy of the License at
7ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov *
8ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov *      http://www.apache.org/licenses/LICENSE-2.0
9ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov *
10ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov * Unless required by applicable law or agreed to in writing, software
11ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov * distributed under the License is distributed on an "AS IS" BASIS,
12ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov * See the License for the specific language governing permissions and
14ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov * limitations under the License.
15ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov */
16ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov
17ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikovpackage android.support.v7.testutils;
18ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov
19ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikovimport android.content.res.ColorStateList;
201b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikovimport android.graphics.PorterDuff;
211b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikovimport android.graphics.drawable.Drawable;
221b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikovimport android.support.annotation.DrawableRes;
23ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikovimport android.support.test.espresso.UiController;
24ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikovimport android.support.test.espresso.ViewAction;
25ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikovimport android.support.v4.view.TintableBackgroundView;
26cd5e42a478a6cb4592f87971b7eadd29b47b8007Kirill Grouchnikovimport android.support.v4.view.ViewCompat;
27ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikovimport android.support.v7.widget.AppCompatTextView;
28ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikovimport android.view.View;
29ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikovimport org.hamcrest.Matcher;
30ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov
31ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikovimport static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
32cd5e42a478a6cb4592f87971b7eadd29b47b8007Kirill Grouchnikovimport static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
33ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikovimport static android.support.test.espresso.matcher.ViewMatchers.isDisplayingAtLeast;
34ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikovimport static org.hamcrest.core.AllOf.allOf;
35ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov
36ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikovpublic class AppCompatTintableViewActions {
37ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov    /**
38ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov     * Sets enabled state on a <code>View</code> that implements the
39ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov     * <code>TintableBackgroundView</code> interface.
40ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov     */
41ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov    public static ViewAction setEnabled(final boolean enabled) {
42ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov        return new ViewAction() {
43ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov            @Override
44ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov            public Matcher<View> getConstraints() {
45ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov                return allOf(isDisplayingAtLeast(90), TestUtilsMatchers.isTintableBackgroundView());
46ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov            }
47ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov
48ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov            @Override
49ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov            public String getDescription() {
50ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov                return "set enabled";
51ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov            }
52ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov
53ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov            @Override
54ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov            public void perform(UiController uiController, View view) {
55ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov                uiController.loopMainThreadUntilIdle();
56ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov
57ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov                view.setEnabled(enabled);
58ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov
59ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov                uiController.loopMainThreadUntilIdle();
60ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov            }
61ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov        };
62ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov    }
63ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov
64ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov    /**
65cd5e42a478a6cb4592f87971b7eadd29b47b8007Kirill Grouchnikov     * Sets the passed color state list as the background layer on a {@link View} that
66cd5e42a478a6cb4592f87971b7eadd29b47b8007Kirill Grouchnikov     * implements the {@link TintableBackgroundView} interface.
67ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov     */
68ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov    public static ViewAction setBackgroundTintList(final ColorStateList colorStateList) {
69ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov        return new ViewAction() {
70ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov            @Override
71ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov            public Matcher<View> getConstraints() {
72cd5e42a478a6cb4592f87971b7eadd29b47b8007Kirill Grouchnikov                return allOf(isDisplayed(), TestUtilsMatchers.isTintableBackgroundView());
73ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov            }
74ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov
75ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov            @Override
76ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov            public String getDescription() {
77ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov                return "set background tint list";
78ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov            }
79ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov
80ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov            @Override
81ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov            public void perform(UiController uiController, View view) {
82ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov                uiController.loopMainThreadUntilIdle();
83ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov
84ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov                TintableBackgroundView tintableBackgroundView = (TintableBackgroundView) view;
85ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov                tintableBackgroundView.setSupportBackgroundTintList(colorStateList);
86ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov
87ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov                uiController.loopMainThreadUntilIdle();
88ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov            }
89ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov        };
90ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov    }
911b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov
921b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov    /**
931b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov     * Sets the passed mode as the background tint mode on a <code>View</code> that
941b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov     * implements the <code>TintableBackgroundView</code> interface.
951b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov     */
961b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov    public static ViewAction setBackgroundTintMode(final PorterDuff.Mode tintMode) {
971b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov        return new ViewAction() {
981b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            @Override
991b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            public Matcher<View> getConstraints() {
100cd5e42a478a6cb4592f87971b7eadd29b47b8007Kirill Grouchnikov                return allOf(isDisplayed(), TestUtilsMatchers.isTintableBackgroundView());
1011b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            }
1021b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov
1031b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            @Override
1041b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            public String getDescription() {
1051b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov                return "set background tint mode";
1061b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            }
1071b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov
1081b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            @Override
1091b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            public void perform(UiController uiController, View view) {
1101b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov                uiController.loopMainThreadUntilIdle();
1111b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov
1121b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov                TintableBackgroundView tintableBackgroundView = (TintableBackgroundView) view;
1131b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov                tintableBackgroundView.setSupportBackgroundTintMode(tintMode);
1141b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov
1151b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov                uiController.loopMainThreadUntilIdle();
1161b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            }
1171b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov        };
1181b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov    }
1191b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov
1201b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov    /**
1211b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov     * Sets background drawable on a <code>View</code> that implements the
1221b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov     * <code>TintableBackgroundView</code> interface.
1231b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov     */
1241b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov    public static ViewAction setBackgroundDrawable(final Drawable background) {
1251b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov        return new ViewAction() {
1261b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            @Override
1271b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            public Matcher<View> getConstraints() {
1281b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov                return allOf(TestUtilsMatchers.isTintableBackgroundView());
1291b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            }
1301b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov
1311b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            @Override
1321b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            public String getDescription() {
1331b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov                return "set background drawable";
1341b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            }
1351b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov
1361b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            @Override
1371b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            public void perform(UiController uiController, View view) {
1381b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov                uiController.loopMainThreadUntilIdle();
1391b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov
1401b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov                view.setBackgroundDrawable(background);
1411b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov
1421b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov                uiController.loopMainThreadUntilIdle();
1431b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            }
1441b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov        };
1451b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov    }
1461b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov
1471b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov    /**
1481b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov     * Sets background resource on a <code>View</code> that implements the
1491b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov     * <code>TintableBackgroundView</code> interface.
1501b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov     */
1511b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov    public static ViewAction setBackgroundResource(final @DrawableRes int resId) {
1521b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov        return new ViewAction() {
1531b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            @Override
1541b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            public Matcher<View> getConstraints() {
1551b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov                return allOf(TestUtilsMatchers.isTintableBackgroundView());
1561b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            }
1571b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov
1581b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            @Override
1591b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            public String getDescription() {
1601b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov                return "set background resource";
1611b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            }
1621b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov
1631b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            @Override
1641b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            public void perform(UiController uiController, View view) {
1651b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov                uiController.loopMainThreadUntilIdle();
1661b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov
1671b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov                view.setBackgroundResource(resId);
1681b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov
1691b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov                uiController.loopMainThreadUntilIdle();
1701b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov            }
1711b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov        };
1721b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov    }
1731b81f288853d60e25e870fd522c927fd72f2efb5Kirill Grouchnikov
174ee9519c17254b5e992164ff278173c4b2c7c5fceKirill Grouchnikov}
175