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.design.testutils;
18
19import android.content.res.ColorStateList;
20import android.graphics.drawable.Drawable;
21import android.support.annotation.DrawableRes;
22import android.support.annotation.IdRes;
23import android.support.annotation.LayoutRes;
24import android.support.annotation.NonNull;
25import android.support.annotation.Nullable;
26import android.support.annotation.StyleRes;
27import android.support.design.widget.NavigationView;
28import android.support.test.espresso.UiController;
29import android.support.test.espresso.ViewAction;
30import android.view.LayoutInflater;
31import android.view.View;
32import org.hamcrest.Matcher;
33
34import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
35
36public class NavigationViewActions {
37    /**
38     * Sets item text appearance on the content of the navigation view.
39     */
40    public static ViewAction setItemTextAppearance(final @StyleRes int resId) {
41        return new ViewAction() {
42            @Override
43            public Matcher<View> getConstraints() {
44                return isDisplayed();
45            }
46
47            @Override
48            public String getDescription() {
49                return "Set item text appearance";
50            }
51
52            @Override
53            public void perform(UiController uiController, View view) {
54                uiController.loopMainThreadUntilIdle();
55
56                NavigationView navigationView = (NavigationView) view;
57                navigationView.setItemTextAppearance(resId);
58
59                uiController.loopMainThreadUntilIdle();
60            }
61        };
62    }
63
64    /**
65     * Sets item text color on the content of the navigation view.
66     */
67    public static ViewAction setItemTextColor(final ColorStateList textColor) {
68        return new ViewAction() {
69            @Override
70            public Matcher<View> getConstraints() {
71                return isDisplayed();
72            }
73
74            @Override
75            public String getDescription() {
76                return "Set item text color";
77            }
78
79            @Override
80            public void perform(UiController uiController, View view) {
81                uiController.loopMainThreadUntilIdle();
82
83                NavigationView navigationView = (NavigationView) view;
84                navigationView.setItemTextColor(textColor);
85
86                uiController.loopMainThreadUntilIdle();
87            }
88        };
89    }
90
91    /**
92     * Sets item background on the content of the navigation view.
93     */
94    public static ViewAction setItemBackground(final @Nullable Drawable itemBackground) {
95        return new ViewAction() {
96            @Override
97            public Matcher<View> getConstraints() {
98                return isDisplayed();
99            }
100
101            @Override
102            public String getDescription() {
103                return "Set item background";
104            }
105
106            @Override
107            public void perform(UiController uiController, View view) {
108                uiController.loopMainThreadUntilIdle();
109
110                NavigationView navigationView = (NavigationView) view;
111                navigationView.setItemBackground(itemBackground);
112
113                uiController.loopMainThreadUntilIdle();
114            }
115        };
116    }
117
118    /**
119     * Sets item background on the content of the navigation view.
120     */
121    public static ViewAction setItemBackgroundResource(final @DrawableRes int resId) {
122        return new ViewAction() {
123            @Override
124            public Matcher<View> getConstraints() {
125                return isDisplayed();
126            }
127
128            @Override
129            public String getDescription() {
130                return "Set item background";
131            }
132
133            @Override
134            public void perform(UiController uiController, View view) {
135                uiController.loopMainThreadUntilIdle();
136
137                NavigationView navigationView = (NavigationView) view;
138                navigationView.setItemBackgroundResource(resId);
139
140                uiController.loopMainThreadUntilIdle();
141            }
142        };
143    }
144
145    /**
146     * Sets item icon tint list on the content of the navigation view.
147     */
148    public static ViewAction setItemIconTintList(final @Nullable ColorStateList tint) {
149        return new ViewAction() {
150            @Override
151            public Matcher<View> getConstraints() {
152                return isDisplayed();
153            }
154
155            @Override
156            public String getDescription() {
157                return "Set item icon tint list";
158            }
159
160            @Override
161            public void perform(UiController uiController, View view) {
162                uiController.loopMainThreadUntilIdle();
163
164                NavigationView navigationView = (NavigationView) view;
165                navigationView.setItemIconTintList(tint);
166
167                uiController.loopMainThreadUntilIdle();
168            }
169        };
170    }
171
172    /**
173     * Add the specified view as a header to the navigation view.
174     */
175    public static ViewAction addHeaderView(final @NonNull LayoutInflater inflater,
176            final @LayoutRes int res) {
177        return new ViewAction() {
178            @Override
179            public Matcher<View> getConstraints() {
180                return isDisplayed();
181            }
182
183            @Override
184            public String getDescription() {
185                return "Add header view";
186            }
187
188            @Override
189            public void perform(UiController uiController, View view) {
190                uiController.loopMainThreadUntilIdle();
191
192                NavigationView navigationView = (NavigationView) view;
193                navigationView.addHeaderView(inflater.inflate(res, null, false));
194
195                uiController.loopMainThreadUntilIdle();
196            }
197        };
198    }
199
200    /**
201     * Inflates a view from the specified layout ID and adds it as a header to the navigation view.
202     */
203    public static ViewAction inflateHeaderView(final @LayoutRes int res) {
204        return new ViewAction() {
205            @Override
206            public Matcher<View> getConstraints() {
207                return isDisplayed();
208            }
209
210            @Override
211            public String getDescription() {
212                return "Inflate and add header view";
213            }
214
215            @Override
216            public void perform(UiController uiController, View view) {
217                uiController.loopMainThreadUntilIdle();
218
219                NavigationView navigationView = (NavigationView) view;
220                navigationView.inflateHeaderView(res);
221
222                uiController.loopMainThreadUntilIdle();
223            }
224        };
225    }
226
227    /**
228     * Removes a previously added header view from the navigation view.
229     */
230    public static ViewAction removeHeaderView(final @Nullable View headerView) {
231        return new ViewAction() {
232            @Override
233            public Matcher<View> getConstraints() {
234                return isDisplayed();
235            }
236
237            @Override
238            public String getDescription() {
239                return "Remove header view";
240            }
241
242            @Override
243            public void perform(UiController uiController, View view) {
244                uiController.loopMainThreadUntilIdle();
245
246                NavigationView navigationView = (NavigationView) view;
247                navigationView.removeHeaderView(headerView);
248
249                uiController.loopMainThreadUntilIdle();
250            }
251        };
252    }
253
254    /**
255     * Sets checked item on the navigation view.
256     */
257    public static ViewAction setCheckedItem(final @IdRes int id) {
258        return new ViewAction() {
259            @Override
260            public Matcher<View> getConstraints() {
261                return isDisplayed();
262            }
263
264            @Override
265            public String getDescription() {
266                return "Set checked item";
267            }
268
269            @Override
270            public void perform(UiController uiController, View view) {
271                uiController.loopMainThreadUntilIdle();
272
273                NavigationView navigationView = (NavigationView) view;
274                navigationView.setCheckedItem(id);
275
276                uiController.loopMainThreadUntilIdle();
277            }
278        };
279    }
280
281    /**
282     * Sets icon for the menu item of the navigation view.
283     */
284    public static ViewAction setIconForMenuItem(final @IdRes int menuItemId,
285            final Drawable iconDrawable) {
286        return new ViewAction() {
287            @Override
288            public Matcher<View> getConstraints() {
289                return isDisplayed();
290            }
291
292            @Override
293            public String getDescription() {
294                return "Set menu item icon";
295            }
296
297            @Override
298            public void perform(UiController uiController, View view) {
299                uiController.loopMainThreadUntilIdle();
300
301                NavigationView navigationView = (NavigationView) view;
302                navigationView.getMenu().findItem(menuItemId).setIcon(iconDrawable);
303
304                uiController.loopMainThreadUntilIdle();
305            }
306        };
307    }
308}
309