1/*
2 * Copyright (C) 2015 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.isDisplayingAtLeast;
20
21import android.support.annotation.Nullable;
22import android.support.design.widget.TabLayout;
23import android.support.test.espresso.UiController;
24import android.support.test.espresso.ViewAction;
25import android.support.test.espresso.matcher.ViewMatchers;
26import android.support.v4.view.ViewPager;
27import android.view.View;
28
29import org.hamcrest.Matcher;
30
31public class TabLayoutActions {
32    /**
33     * Wires <code>TabLayout</code> to <code>ViewPager</code> content.
34     */
35    public static ViewAction setupWithViewPager(final @Nullable ViewPager viewPager) {
36        return new ViewAction() {
37            @Override
38            public Matcher<View> getConstraints() {
39                return isDisplayingAtLeast(90);
40            }
41
42            @Override
43            public String getDescription() {
44                return "Setup with ViewPager content";
45            }
46
47            @Override
48            public void perform(UiController uiController, View view) {
49                uiController.loopMainThreadUntilIdle();
50
51                TabLayout tabLayout = (TabLayout) view;
52                tabLayout.setupWithViewPager(viewPager);
53
54                uiController.loopMainThreadUntilIdle();
55            }
56        };
57    }
58
59    /**
60     * Wires <code>TabLayout</code> to <code>ViewPager</code> content.
61     */
62    public static ViewAction setupWithViewPager(final @Nullable ViewPager viewPager,
63            final boolean autoRefresh) {
64        return new ViewAction() {
65            @Override
66            public Matcher<View> getConstraints() {
67                return isDisplayingAtLeast(90);
68            }
69
70            @Override
71            public String getDescription() {
72                return "Setup with ViewPager content";
73            }
74
75            @Override
76            public void perform(UiController uiController, View view) {
77                uiController.loopMainThreadUntilIdle();
78
79                TabLayout tabLayout = (TabLayout) view;
80                tabLayout.setupWithViewPager(viewPager, autoRefresh);
81
82                uiController.loopMainThreadUntilIdle();
83            }
84        };
85    }
86
87    /**
88     * Selects the specified tab in the <code>TabLayout</code>.
89     */
90    public static ViewAction selectTab(final int tabIndex) {
91        return new ViewAction() {
92            @Override
93            public Matcher<View> getConstraints() {
94                return isDisplayingAtLeast(90);
95            }
96
97            @Override
98            public String getDescription() {
99                return "Selects tab";
100            }
101
102            @Override
103            public void perform(UiController uiController, View view) {
104                uiController.loopMainThreadUntilIdle();
105
106                TabLayout tabLayout = (TabLayout) view;
107                tabLayout.getTabAt(tabIndex).select();
108
109                uiController.loopMainThreadUntilIdle();
110            }
111        };
112    }
113
114    /**
115     * Sets the specified tab mode in the <code>TabLayout</code>.
116     */
117    public static ViewAction setTabMode(final int tabMode) {
118        return new ViewAction() {
119            @Override
120            public Matcher<View> getConstraints() {
121                return isDisplayingAtLeast(90);
122            }
123
124            @Override
125            public String getDescription() {
126                return "Sets tab mode";
127            }
128
129            @Override
130            public void perform(UiController uiController, View view) {
131                uiController.loopMainThreadUntilIdle();
132
133                TabLayout tabLayout = (TabLayout) view;
134                tabLayout.setTabMode(tabMode);
135
136                uiController.loopMainThreadUntilIdle();
137            }
138        };
139    }
140
141    /**
142     * Calls <code>setScrollPosition(position, positionOffset, true)</code> on the
143     * <code>TabLayout</code>
144     */
145    public static ViewAction setScrollPosition(final int position, final float positionOffset) {
146        return new ViewAction() {
147
148            @Override
149            public Matcher<View> getConstraints() {
150                return ViewMatchers.isAssignableFrom(TabLayout.class);
151            }
152
153            @Override
154            public String getDescription() {
155                return "setScrollPosition(" + position + ", " + positionOffset + ", true)";
156            }
157
158            @Override
159            public void perform(UiController uiController, View view) {
160                TabLayout tabs = (TabLayout) view;
161                tabs.setScrollPosition(position, positionOffset, true);
162                uiController.loopMainThreadUntilIdle();
163            }
164        };
165    }
166}
167