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 */
16package android.support.v4.view;
17
18import android.support.coreui.test.R;
19
20import static android.support.test.espresso.Espresso.onView;
21import static android.support.test.espresso.action.ViewActions.click;
22import static android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA;
23import static android.support.test.espresso.matcher.ViewMatchers.withId;
24import static android.support.test.espresso.matcher.ViewMatchers.withText;
25import static android.support.v4.view.ViewPagerActions.clickBetweenTwoTitles;
26import static android.support.v4.view.ViewPagerActions.scrollRight;
27import static android.support.v4.view.ViewPagerActions.scrollToPage;
28import static org.hamcrest.Matchers.allOf;
29import static org.junit.Assert.assertEquals;
30
31/**
32 * Provides assertions that depend on the interactive nature of <code>PagerTabStrip</code>.
33 */
34public class ViewPagerWithTabStripTest extends BaseViewPagerTest<ViewPagerWithTabStripActivity> {
35    public ViewPagerWithTabStripTest() {
36        super(ViewPagerWithTabStripActivity.class);
37    }
38
39    @Override
40    protected Class getStripClass() {
41        return PagerTabStrip.class;
42    }
43
44    @Override
45    protected void assertStripInteraction(boolean smoothScroll) {
46        // The following block tests that ViewPager page selection changes on clicking titles of
47        // various tabs as PagerTabStrip is interactive
48
49        // Click the tab title for page #0 and verify that we're still on page #0
50        onView(allOf(isDescendantOfA(withId(R.id.titles)), withText("Red"))).perform(click());
51        assertEquals("Click tab #0 on tab #0", 0, mViewPager.getCurrentItem());
52
53        // Click the tab title for page #1 and verify that we're on page #1
54        onView(allOf(isDescendantOfA(withId(R.id.titles)), withText("Green"))).perform(click());
55        assertEquals("Click tab #1 on tab #0", 1, mViewPager.getCurrentItem());
56
57        // Click the tab title for page #0 and verify that we're on page #0
58        onView(allOf(isDescendantOfA(withId(R.id.titles)), withText("Red"))).perform(click());
59        assertEquals("Click tab #0 on tab #1", 0, mViewPager.getCurrentItem());
60
61        // Go back to page #1
62        onView(withId(R.id.pager)).perform(scrollRight(smoothScroll));
63
64        // Click the tab title for page #1 and verify that we're still on page #1
65        onView(allOf(isDescendantOfA(withId(R.id.titles)), withText("Green"))).perform(click());
66        assertEquals("Click tab #1 on tab #1", 1, mViewPager.getCurrentItem());
67
68        // Click the tab title for page #2 and verify that we're on page #2
69        onView(allOf(isDescendantOfA(withId(R.id.titles)), withText("Blue"))).perform(click());
70        assertEquals("Click tab #2 on tab #1", 2, mViewPager.getCurrentItem());
71
72        // The following block tests that ViewPager page selection changes on clicking in
73        // between titles of tabs as that functionality is exposed by PagerTabStrip
74
75        // Scroll back to page #0
76        onView(withId(R.id.pager)).perform(scrollToPage(0, smoothScroll));
77
78        // Click between titles of page #0 and page #1 and verify that we're on page #1
79        onView(withId(R.id.titles)).perform(clickBetweenTwoTitles("Red", "Green"));
80        assertEquals("Click in between tabs #0 and #1 on tab #0", 1, mViewPager.getCurrentItem());
81
82        // Click between titles of page #0 and page #1 and verify that we're on page #0
83        onView(withId(R.id.titles)).perform(clickBetweenTwoTitles("Red", "Green"));
84        assertEquals("Click in between tabs #0 and #1 on tab #1", 0, mViewPager.getCurrentItem());
85
86        // Go back to page #1
87        onView(withId(R.id.pager)).perform(scrollRight(smoothScroll));
88
89        // Click between titles of page #1 and page #2 and verify that we're on page #2
90        onView(withId(R.id.titles)).perform(clickBetweenTwoTitles("Green", "Blue"));
91        assertEquals("Click in between tabs #1 and #2 on tab #1", 2, mViewPager.getCurrentItem());
92    }
93}
94