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