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.test.espresso.UiController;
21import android.support.test.espresso.ViewAction;
22import android.support.test.espresso.action.CoordinatesProvider;
23import android.support.test.espresso.action.GeneralClickAction;
24import android.support.test.espresso.action.Press;
25import android.support.test.espresso.action.Tap;
26import android.support.v4.view.PagerAdapter;
27import android.support.v4.view.PagerTitleStrip;
28import android.support.v4.view.ViewPager;
29import android.view.View;
30import android.widget.TextView;
31import org.hamcrest.Matcher;
32
33import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
34import static android.support.test.espresso.matcher.ViewMatchers.isDisplayingAtLeast;
35
36public class ViewPagerActions {
37    /**
38     * Moves <code>ViewPager</code> to the right by one page.
39     */
40    public static ViewAction scrollRight() {
41        return new ViewAction() {
42            @Override
43            public Matcher<View> getConstraints() {
44                return isDisplayingAtLeast(90);
45            }
46
47            @Override
48            public String getDescription() {
49                return "ViewPager scroll one page to the right";
50            }
51
52            @Override
53            public void perform(UiController uiController, View view) {
54                uiController.loopMainThreadUntilIdle();
55
56                ViewPager viewPager = (ViewPager) view;
57                int current = viewPager.getCurrentItem();
58                viewPager.setCurrentItem(current + 1, false);
59
60                uiController.loopMainThreadUntilIdle();
61            }
62        };
63    }
64
65    /**
66     * Moves <code>ViewPager</code> to the left by one page.
67     */
68    public static ViewAction scrollLeft() {
69        return new ViewAction() {
70            @Override
71            public Matcher<View> getConstraints() {
72                return isDisplayingAtLeast(90);
73            }
74
75            @Override
76            public String getDescription() {
77                return "ViewPager scroll one page to the left";
78            }
79
80            @Override
81            public void perform(UiController uiController, View view) {
82                uiController.loopMainThreadUntilIdle();
83
84                ViewPager viewPager = (ViewPager) view;
85                int current = viewPager.getCurrentItem();
86                viewPager.setCurrentItem(current - 1, false);
87
88                uiController.loopMainThreadUntilIdle();
89            }
90        };
91    }
92
93    /**
94     * Moves <code>ViewPager</code> to the last page.
95     */
96    public static ViewAction scrollToLast() {
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 "ViewPager scroll to last page";
106            }
107
108            @Override
109            public void perform(UiController uiController, View view) {
110                uiController.loopMainThreadUntilIdle();
111
112                ViewPager viewPager = (ViewPager) view;
113                int size = viewPager.getAdapter().getCount();
114                if (size > 0) {
115                    viewPager.setCurrentItem(size - 1, false);
116                }
117
118                uiController.loopMainThreadUntilIdle();
119            }
120        };
121    }
122
123    /**
124     * Moves <code>ViewPager</code> to the first page.
125     */
126    public static ViewAction scrollToFirst() {
127        return new ViewAction() {
128            @Override
129            public Matcher<View> getConstraints() {
130                return isDisplayingAtLeast(90);
131            }
132
133            @Override
134            public String getDescription() {
135                return "ViewPager scroll to first page";
136            }
137
138            @Override
139            public void perform(UiController uiController, View view) {
140                uiController.loopMainThreadUntilIdle();
141
142                ViewPager viewPager = (ViewPager) view;
143                int size = viewPager.getAdapter().getCount();
144                if (size > 0) {
145                    viewPager.setCurrentItem(0, false);
146                }
147
148                uiController.loopMainThreadUntilIdle();
149            }
150        };
151    }
152
153    /**
154     * Moves <code>ViewPager</code> to specific page.
155     */
156    public static ViewAction scrollToPage(final int page) {
157        return new ViewAction() {
158            @Override
159            public Matcher<View> getConstraints() {
160                return isDisplayingAtLeast(90);
161            }
162
163            @Override
164            public String getDescription() {
165                return "ViewPager move to a specific page";
166            }
167
168            @Override
169            public void perform(UiController uiController, View view) {
170                uiController.loopMainThreadUntilIdle();
171
172                ViewPager viewPager = (ViewPager) view;
173                viewPager.setCurrentItem(page, false);
174
175                uiController.loopMainThreadUntilIdle();
176            }
177        };
178    }
179
180    /**
181     * Sets the specified adapter on <code>ViewPager</code>.
182     */
183    public static ViewAction setAdapter(final @Nullable PagerAdapter adapter) {
184        return new ViewAction() {
185            @Override
186            public Matcher<View> getConstraints() {
187                return isAssignableFrom(ViewPager.class);
188            }
189
190            @Override
191            public String getDescription() {
192                return "ViewPager set adapter";
193            }
194
195            @Override
196            public void perform(UiController uiController, View view) {
197                uiController.loopMainThreadUntilIdle();
198
199                ViewPager viewPager = (ViewPager) view;
200                viewPager.setAdapter(adapter);
201
202                uiController.loopMainThreadUntilIdle();
203            }
204        };
205    }
206
207    /**
208     * Moves <code>ViewPager</code> to specific page.
209     */
210    public static ViewAction notifyAdapterContentChange() {
211        return new ViewAction() {
212            @Override
213            public Matcher<View> getConstraints() {
214                return isAssignableFrom(ViewPager.class);
215            }
216
217            @Override
218            public String getDescription() {
219                return "ViewPager notify on adapter content change";
220            }
221
222            @Override
223            public void perform(UiController uiController, View view) {
224                uiController.loopMainThreadUntilIdle();
225
226                ViewPager viewPager = (ViewPager) view;
227                viewPager.getAdapter().notifyDataSetChanged();
228
229                uiController.loopMainThreadUntilIdle();
230            }
231        };
232    }
233}
234