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