1394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov/* 2394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov * Copyright (C) 2015 The Android Open Source Project 3394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov * 4394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov * Licensed under the Apache License, Version 2.0 (the "License"); 5394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov * you may not use this file except in compliance with the License. 6394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov * You may obtain a copy of the License at 7394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov * 8394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov * http://www.apache.org/licenses/LICENSE-2.0 9394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov * 10394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov * Unless required by applicable law or agreed to in writing, software 11394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov * distributed under the License is distributed on an "AS IS" BASIS, 12394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov * See the License for the specific language governing permissions and 14394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov * limitations under the License. 15394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov */ 16394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov 17394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikovpackage android.support.design.testutils; 18394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov 1986d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Korenimport static android.support.test.espresso.matcher.ViewMatchers.isDisplayingAtLeast; 2086d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren 21394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikovimport android.support.annotation.Nullable; 22394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikovimport android.support.design.widget.TabLayout; 23394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikovimport android.support.test.espresso.UiController; 24394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikovimport android.support.test.espresso.ViewAction; 2586d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Korenimport android.support.test.espresso.matcher.ViewMatchers; 26394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikovimport android.support.v4.view.ViewPager; 27394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikovimport android.view.View; 28394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov 2986d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Korenimport org.hamcrest.Matcher; 30394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov 31394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikovpublic class TabLayoutActions { 32394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov /** 33394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov * Wires <code>TabLayout</code> to <code>ViewPager</code> content. 34394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov */ 35394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov public static ViewAction setupWithViewPager(final @Nullable ViewPager viewPager) { 36394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov return new ViewAction() { 37394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov @Override 38394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov public Matcher<View> getConstraints() { 39394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov return isDisplayingAtLeast(90); 40394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov } 41394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov 42394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov @Override 43394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov public String getDescription() { 44394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov return "Setup with ViewPager content"; 45394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov } 46394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov 47394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov @Override 48394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov public void perform(UiController uiController, View view) { 49394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov uiController.loopMainThreadUntilIdle(); 50394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov 51394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov TabLayout tabLayout = (TabLayout) view; 52394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov tabLayout.setupWithViewPager(viewPager); 53394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov 54394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov uiController.loopMainThreadUntilIdle(); 55394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov } 56394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov }; 57394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov } 58e2f344dcf51e299fce191eee0712cd45a30cc931Kirill Grouchnikov 59e2f344dcf51e299fce191eee0712cd45a30cc931Kirill Grouchnikov /** 603cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes * Wires <code>TabLayout</code> to <code>ViewPager</code> content. 613cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes */ 623cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes public static ViewAction setupWithViewPager(final @Nullable ViewPager viewPager, 633cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes final boolean autoRefresh) { 643cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes return new ViewAction() { 653cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes @Override 663cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes public Matcher<View> getConstraints() { 673cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes return isDisplayingAtLeast(90); 683cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes } 693cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes 703cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes @Override 713cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes public String getDescription() { 723cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes return "Setup with ViewPager content"; 733cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes } 743cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes 753cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes @Override 763cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes public void perform(UiController uiController, View view) { 773cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes uiController.loopMainThreadUntilIdle(); 783cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes 793cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes TabLayout tabLayout = (TabLayout) view; 803cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes tabLayout.setupWithViewPager(viewPager, autoRefresh); 813cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes 823cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes uiController.loopMainThreadUntilIdle(); 833cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes } 843cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes }; 853cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes } 863cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes 873cbbf6cc4d8124ae40181b5ce38c7a89e749ff0fChris Banes /** 88e2f344dcf51e299fce191eee0712cd45a30cc931Kirill Grouchnikov * Selects the specified tab in the <code>TabLayout</code>. 89e2f344dcf51e299fce191eee0712cd45a30cc931Kirill Grouchnikov */ 90e2f344dcf51e299fce191eee0712cd45a30cc931Kirill Grouchnikov public static ViewAction selectTab(final int tabIndex) { 91e2f344dcf51e299fce191eee0712cd45a30cc931Kirill Grouchnikov return new ViewAction() { 92e2f344dcf51e299fce191eee0712cd45a30cc931Kirill Grouchnikov @Override 93e2f344dcf51e299fce191eee0712cd45a30cc931Kirill Grouchnikov public Matcher<View> getConstraints() { 94e2f344dcf51e299fce191eee0712cd45a30cc931Kirill Grouchnikov return isDisplayingAtLeast(90); 95e2f344dcf51e299fce191eee0712cd45a30cc931Kirill Grouchnikov } 96e2f344dcf51e299fce191eee0712cd45a30cc931Kirill Grouchnikov 97e2f344dcf51e299fce191eee0712cd45a30cc931Kirill Grouchnikov @Override 98e2f344dcf51e299fce191eee0712cd45a30cc931Kirill Grouchnikov public String getDescription() { 99c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov return "Selects tab"; 100e2f344dcf51e299fce191eee0712cd45a30cc931Kirill Grouchnikov } 101e2f344dcf51e299fce191eee0712cd45a30cc931Kirill Grouchnikov 102e2f344dcf51e299fce191eee0712cd45a30cc931Kirill Grouchnikov @Override 103e2f344dcf51e299fce191eee0712cd45a30cc931Kirill Grouchnikov public void perform(UiController uiController, View view) { 104e2f344dcf51e299fce191eee0712cd45a30cc931Kirill Grouchnikov uiController.loopMainThreadUntilIdle(); 105e2f344dcf51e299fce191eee0712cd45a30cc931Kirill Grouchnikov 106e2f344dcf51e299fce191eee0712cd45a30cc931Kirill Grouchnikov TabLayout tabLayout = (TabLayout) view; 107e2f344dcf51e299fce191eee0712cd45a30cc931Kirill Grouchnikov tabLayout.getTabAt(tabIndex).select(); 108e2f344dcf51e299fce191eee0712cd45a30cc931Kirill Grouchnikov 109e2f344dcf51e299fce191eee0712cd45a30cc931Kirill Grouchnikov uiController.loopMainThreadUntilIdle(); 110e2f344dcf51e299fce191eee0712cd45a30cc931Kirill Grouchnikov } 111e2f344dcf51e299fce191eee0712cd45a30cc931Kirill Grouchnikov }; 112e2f344dcf51e299fce191eee0712cd45a30cc931Kirill Grouchnikov } 113c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov 114c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov /** 115c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov * Sets the specified tab mode in the <code>TabLayout</code>. 116c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov */ 117c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov public static ViewAction setTabMode(final int tabMode) { 118c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov return new ViewAction() { 119c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov @Override 120c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov public Matcher<View> getConstraints() { 121c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov return isDisplayingAtLeast(90); 122c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov } 123c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov 124c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov @Override 125c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov public String getDescription() { 126c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov return "Sets tab mode"; 127c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov } 128c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov 129c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov @Override 130c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov public void perform(UiController uiController, View view) { 131c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov uiController.loopMainThreadUntilIdle(); 132c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov 133c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov TabLayout tabLayout = (TabLayout) view; 134c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov tabLayout.setTabMode(tabMode); 135c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov 136c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov uiController.loopMainThreadUntilIdle(); 137c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov } 138c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov }; 139c7f2aad754af4373c32fcab613de0ba164ada2a2Kirill Grouchnikov } 14086d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren 14186d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren /** 14286d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren * Calls <code>setScrollPosition(position, positionOffset, true)</code> on the 14386d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren * <code>TabLayout</code> 14486d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren */ 14586d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren public static ViewAction setScrollPosition(final int position, final float positionOffset) { 14686d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren return new ViewAction() { 14786d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren 14886d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren @Override 14986d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren public Matcher<View> getConstraints() { 15086d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren return ViewMatchers.isAssignableFrom(TabLayout.class); 15186d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren } 15286d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren 15386d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren @Override 15486d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren public String getDescription() { 15586d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren return "setScrollPosition(" + position + ", " + positionOffset + ", true)"; 15686d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren } 15786d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren 15886d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren @Override 15986d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren public void perform(UiController uiController, View view) { 16086d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren TabLayout tabs = (TabLayout) view; 16186d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren tabs.setScrollPosition(position, positionOffset, true); 16286d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren uiController.loopMainThreadUntilIdle(); 16386d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren } 16486d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren }; 16586d6a88c4d98bf6f125d4a514004bdf86a2a215aJonathan Koren } 166394c4272d0e220cf7af236665eecaf99dfed25b1Kirill Grouchnikov} 167