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 static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
20
21import android.content.res.ColorStateList;
22import android.graphics.drawable.Drawable;
23import android.support.annotation.IdRes;
24import android.support.annotation.Nullable;
25import android.support.design.widget.BottomNavigationView;
26import android.support.test.espresso.UiController;
27import android.support.test.espresso.ViewAction;
28import android.view.View;
29
30import org.hamcrest.Matcher;
31
32
33public class BottomNavigationViewActions {
34    /**
35     * Sets item icon tint list on the content of the bottom navigation view.
36     */
37    public static ViewAction setItemIconTintList(@Nullable final ColorStateList tint) {
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 item icon tint list";
47            }
48
49            @Override
50            public void perform(UiController uiController, View view) {
51                uiController.loopMainThreadUntilIdle();
52
53                BottomNavigationView navigationView = (BottomNavigationView) view;
54                navigationView.setItemIconTintList(tint);
55
56                uiController.loopMainThreadUntilIdle();
57            }
58        };
59    }
60
61    /**
62     * Sets icon for the menu item of the navigation view.
63     */
64    public static ViewAction setIconForMenuItem(@IdRes final int menuItemId,
65            final Drawable iconDrawable) {
66        return new ViewAction() {
67            @Override
68            public Matcher<View> getConstraints() {
69                return isDisplayed();
70            }
71
72            @Override
73            public String getDescription() {
74                return "Set menu item icon";
75            }
76
77            @Override
78            public void perform(UiController uiController, View view) {
79                uiController.loopMainThreadUntilIdle();
80
81                BottomNavigationView navigationView = (BottomNavigationView) view;
82                navigationView.getMenu().findItem(menuItemId).setIcon(iconDrawable);
83
84                uiController.loopMainThreadUntilIdle();
85            }
86        };
87    }
88}
89