1/*
2 * Copyright (C) 2016 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.content.res.ColorStateList;
20import android.graphics.PorterDuff;
21import android.support.test.espresso.UiController;
22import android.support.test.espresso.ViewAction;
23import android.support.v4.view.ViewCompat;
24import android.view.View;
25import android.widget.TextView;
26import org.hamcrest.Matcher;
27
28import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
29import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
30import static android.support.test.espresso.matcher.ViewMatchers.isDisplayingAtLeast;
31import static org.hamcrest.core.AllOf.allOf;
32
33public class TestUtilsActions {
34    /**
35     * Sets layout direction on the view.
36     */
37    public static ViewAction setLayoutDirection(final int layoutDirection) {
38        return new ViewAction() {
39            @Override
40            public Matcher<View> getConstraints() {
41                return isDisplayed();
42            }
43
44            @Override
45            public String getDescription() {
46                return "set layout direction";
47            }
48
49            @Override
50            public void perform(UiController uiController, View view) {
51                uiController.loopMainThreadUntilIdle();
52
53                ViewCompat.setLayoutDirection(view, layoutDirection);
54
55                uiController.loopMainThreadUntilIdle();
56            }
57        };
58    }
59
60    /**
61     * Sets text appearance on {@code TextView}.
62     */
63    public static ViewAction setTextAppearance(final int resId) {
64        return new ViewAction() {
65            @Override
66            public Matcher<View> getConstraints() {
67                return allOf(isDisplayingAtLeast(90), isAssignableFrom(TextView.class));
68            }
69
70            @Override
71            public String getDescription() {
72                return "TextView set text appearance";
73            }
74
75            @Override
76            public void perform(UiController uiController, View view) {
77                uiController.loopMainThreadUntilIdle();
78
79                TextView textView = (TextView) view;
80                textView.setTextAppearance(textView.getContext(), resId);
81
82                uiController.loopMainThreadUntilIdle();
83            }
84        };
85    }
86
87    /**
88     * Sets the passed color state list as the background layer on a {@link View} with
89     * {@link ViewCompat#setBackgroundTintList(View, ColorStateList)} API.
90     */
91    public static ViewAction setBackgroundTintListViewCompat(final ColorStateList colorStateList) {
92        return new ViewAction() {
93            @Override
94            public Matcher<View> getConstraints() {
95                return isDisplayed();
96            }
97
98            @Override
99            public String getDescription() {
100                return "set background tint list";
101            }
102
103            @Override
104            public void perform(UiController uiController, View view) {
105                uiController.loopMainThreadUntilIdle();
106
107                ViewCompat.setBackgroundTintList(view, colorStateList);
108
109                uiController.loopMainThreadUntilIdle();
110            }
111        };
112    }
113
114    /**
115     * Sets the passed mode as the background tint mode on a {@link View} with
116     * {@link ViewCompat#setBackgroundTintMode(View, PorterDuff.Mode)} API.
117     */
118    public static ViewAction setBackgroundTintModeViewCompat(final PorterDuff.Mode tintMode) {
119        return new ViewAction() {
120            @Override
121            public Matcher<View> getConstraints() {
122                return isDisplayed();
123            }
124
125            @Override
126            public String getDescription() {
127                return "set background tint mode";
128            }
129
130            @Override
131            public void perform(UiController uiController, View view) {
132                uiController.loopMainThreadUntilIdle();
133
134                ViewCompat.setBackgroundTintMode(view, tintMode);
135
136                uiController.loopMainThreadUntilIdle();
137            }
138        };
139    }
140
141    public static ViewAction setEnabled(final boolean enabled) {
142        return new ViewAction() {
143            @Override
144            public Matcher<View> getConstraints() {
145                return isDisplayed();
146            }
147
148            @Override
149            public String getDescription() {
150                return "set enabled";
151            }
152
153            @Override
154            public void perform(UiController uiController, View view) {
155                uiController.loopMainThreadUntilIdle();
156
157                view.setEnabled(enabled);
158
159                uiController.loopMainThreadUntilIdle();
160            }
161        };
162    }
163}
164