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.v4.testutils;
18
19import android.graphics.drawable.Drawable;
20import android.support.annotation.DrawableRes;
21import android.support.annotation.Nullable;
22import android.support.annotation.StringRes;
23import android.support.annotation.StyleRes;
24import android.support.test.espresso.UiController;
25import android.support.test.espresso.ViewAction;
26import android.support.v4.widget.TextViewCompat;
27import android.view.View;
28import android.widget.TextView;
29import org.hamcrest.Matcher;
30
31import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
32
33public class TextViewActions {
34    /**
35     * Sets max lines count on <code>TextView</code>.
36     */
37    public static ViewAction setMaxLines(final int maxLines) {
38        return new ViewAction() {
39            @Override
40            public Matcher<View> getConstraints() {
41                return isAssignableFrom(TextView.class);
42            }
43
44            @Override
45            public String getDescription() {
46                return "TextView set max lines";
47            }
48
49            @Override
50            public void perform(UiController uiController, View view) {
51                uiController.loopMainThreadUntilIdle();
52
53                TextView textView = (TextView) view;
54                textView.setMaxLines(maxLines);
55
56                uiController.loopMainThreadUntilIdle();
57            }
58        };
59    }
60
61    /**
62     * Sets min lines count on <code>TextView</code>.
63     */
64    public static ViewAction setMinLines(final int minLines) {
65        return new ViewAction() {
66            @Override
67            public Matcher<View> getConstraints() {
68                return isAssignableFrom(TextView.class);
69            }
70
71            @Override
72            public String getDescription() {
73                return "TextView set min lines";
74            }
75
76            @Override
77            public void perform(UiController uiController, View view) {
78                uiController.loopMainThreadUntilIdle();
79
80                TextView textView = (TextView) view;
81                textView.setMinLines(minLines);
82
83                uiController.loopMainThreadUntilIdle();
84            }
85        };
86    }
87
88    /**
89     * Sets text content on <code>TextView</code>.
90     */
91    public static ViewAction setText(final @StringRes int stringResId) {
92        return new ViewAction() {
93            @Override
94            public Matcher<View> getConstraints() {
95                return isAssignableFrom(TextView.class);
96            }
97
98            @Override
99            public String getDescription() {
100                return "TextView set text";
101            }
102
103            @Override
104            public void perform(UiController uiController, View view) {
105                uiController.loopMainThreadUntilIdle();
106
107                TextView textView = (TextView) view;
108                textView.setText(stringResId);
109
110                uiController.loopMainThreadUntilIdle();
111            }
112        };
113    }
114
115    /**
116     * Sets text appearance on <code>TextView</code>.
117     */
118    public static ViewAction setTextAppearance(final @StyleRes int styleResId) {
119        return new ViewAction() {
120            @Override
121            public Matcher<View> getConstraints() {
122                return isAssignableFrom(TextView.class);
123            }
124
125            @Override
126            public String getDescription() {
127                return "TextView set text appearance";
128            }
129
130            @Override
131            public void perform(UiController uiController, View view) {
132                uiController.loopMainThreadUntilIdle();
133
134                TextView textView = (TextView) view;
135                TextViewCompat.setTextAppearance(textView, styleResId);
136
137                uiController.loopMainThreadUntilIdle();
138            }
139        };
140    }
141
142    /**
143     * Sets compound drawables on <code>TextView</code>.
144     */
145    public static ViewAction setCompoundDrawablesRelative(final @Nullable Drawable start,
146            final @Nullable Drawable top, final @Nullable Drawable end,
147            final @Nullable Drawable bottom) {
148        return new ViewAction() {
149            @Override
150            public Matcher<View> getConstraints() {
151                return isAssignableFrom(TextView.class);
152            }
153
154            @Override
155            public String getDescription() {
156                return "TextView set compound drawables";
157            }
158
159            @Override
160            public void perform(UiController uiController, View view) {
161                uiController.loopMainThreadUntilIdle();
162
163                TextView textView = (TextView) view;
164                TextViewCompat.setCompoundDrawablesRelative(textView, start, top, end, bottom);
165
166                uiController.loopMainThreadUntilIdle();
167            }
168        };
169    }
170
171    /**
172     * Sets compound drawables on <code>TextView</code>.
173     */
174    public static ViewAction setCompoundDrawablesRelativeWithIntrinsicBounds(
175            final @Nullable Drawable start, final @Nullable Drawable top,
176            final @Nullable Drawable end, final @Nullable Drawable bottom) {
177        return new ViewAction() {
178            @Override
179            public Matcher<View> getConstraints() {
180                return isAssignableFrom(TextView.class);
181            }
182
183            @Override
184            public String getDescription() {
185                return "TextView set compound drawables";
186            }
187
188            @Override
189            public void perform(UiController uiController, View view) {
190                uiController.loopMainThreadUntilIdle();
191
192                TextView textView = (TextView) view;
193                TextViewCompat.setCompoundDrawablesRelativeWithIntrinsicBounds(
194                        textView, start, top, end, bottom);
195
196                uiController.loopMainThreadUntilIdle();
197            }
198        };
199    }
200
201    /**
202     * Sets compound drawables on <code>TextView</code>.
203     */
204    public static ViewAction setCompoundDrawablesRelativeWithIntrinsicBounds(
205            final @DrawableRes int start, final @DrawableRes int top, final @DrawableRes int end,
206            final @DrawableRes int bottom) {
207        return new ViewAction() {
208            @Override
209            public Matcher<View> getConstraints() {
210                return isAssignableFrom(TextView.class);
211            }
212
213            @Override
214            public String getDescription() {
215                return "TextView set compound drawables";
216            }
217
218            @Override
219            public void perform(UiController uiController, View view) {
220                uiController.loopMainThreadUntilIdle();
221
222                TextView textView = (TextView) view;
223                TextViewCompat.setCompoundDrawablesRelativeWithIntrinsicBounds(
224                        textView, start, top, end, bottom);
225
226                uiController.loopMainThreadUntilIdle();
227            }
228        };
229    }
230}
231