The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
Neal Nguyen | 1d3165f | 2010-01-12 13:26:10 -0800 | [diff] [blame] | 17 | package android.widget.scroll; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | import android.test.ActivityInstrumentationTestCase; |
| 20 | import android.test.suitebuilder.annotation.LargeTest; |
| 21 | import android.test.suitebuilder.annotation.MediumTest; |
Evan Rosky | 91d6a55 | 2017-07-27 10:22:15 -0700 | [diff] [blame] | 22 | import android.test.suitebuilder.annotation.Suppress; |
| 23 | import android.view.KeyEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.widget.Button; |
| 25 | import android.widget.LinearLayout; |
| 26 | import android.widget.ScrollView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | |
| 28 | |
| 29 | public class ScrollViewButtonsAndLabelsTest |
| 30 | extends ActivityInstrumentationTestCase<ScrollViewButtonsAndLabels> { |
| 31 | |
| 32 | private ScrollView mScrollView; |
| 33 | private LinearLayout mLinearLayout; |
| 34 | private int mScreenBottom; |
| 35 | private int mScreenTop; |
| 36 | |
| 37 | public ScrollViewButtonsAndLabelsTest() { |
Neal Nguyen | 1d3165f | 2010-01-12 13:26:10 -0800 | [diff] [blame] | 38 | super("com.android.frameworks.coretests", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | ScrollViewButtonsAndLabels.class); |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public void setUp() throws Exception { |
| 44 | super.setUp(); |
| 45 | mScrollView = getActivity().getScrollView(); |
| 46 | mLinearLayout = getActivity().getLinearLayout(); |
| 47 | |
| 48 | int origin[] = {0, 0}; |
| 49 | mScrollView.getLocationOnScreen(origin); |
| 50 | mScreenTop = origin[1]; |
| 51 | mScreenBottom = origin[1] + mScrollView.getHeight(); |
| 52 | } |
| 53 | |
| 54 | @MediumTest |
Abodunrinwa Toki | f901776 | 2015-07-31 18:13:30 -0700 | [diff] [blame] | 55 | @Suppress // Failing. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | public void testPreconditions() { |
| 57 | assertTrue("vertical fading edge width needs to be non-zero for this " |
| 58 | + "test to be worth anything", |
| 59 | mScrollView.getVerticalFadingEdgeLength() > 0); |
| 60 | } |
| 61 | |
| 62 | // moving down to something off screen should move the element |
| 63 | // onto the screen just above the vertical fading edge |
| 64 | @LargeTest |
| 65 | public void testArrowScrollDownOffScreenVerticalFadingEdge() { |
| 66 | |
| 67 | int offScreenIndex = findFirstButtonOffScreenTop2Bottom(); |
| 68 | Button firstButtonOffScreen = getActivity().getButton(offScreenIndex); |
| 69 | |
Evan Rosky | 91d6a55 | 2017-07-27 10:22:15 -0700 | [diff] [blame] | 70 | getActivity().runOnUiThread(() -> getActivity().getButton(0).requestFocus()); |
| 71 | getInstrumentation().waitForIdleSync(); |
| 72 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | for (int i = 0; i < offScreenIndex; i++) { |
| 74 | sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); |
| 75 | } |
| 76 | getInstrumentation().waitForIdleSync(); |
| 77 | assertTrue(firstButtonOffScreen.hasFocus()); |
| 78 | |
| 79 | assertTrue("the button we've moved to off screen must not be the last " |
| 80 | + "button in the scroll view for this test to work (since we " |
| 81 | + "are expecting the fading edge to be there).", |
| 82 | offScreenIndex < getActivity().getNumButtons()); |
| 83 | |
| 84 | // now we are at the first button off screen |
| 85 | int buttonLoc[] = {0, 0}; |
| 86 | firstButtonOffScreen.getLocationOnScreen(buttonLoc); |
| 87 | int buttonBottom = buttonLoc[1] + firstButtonOffScreen.getHeight(); |
| 88 | |
| 89 | int verticalFadingEdgeLength = mScrollView |
| 90 | .getVerticalFadingEdgeLength(); |
| 91 | assertEquals("bottom of button should be verticalFadingEdgeLength " |
| 92 | + "above the bottom of the screen", |
| 93 | buttonBottom, mScreenBottom - verticalFadingEdgeLength); |
| 94 | } |
| 95 | |
| 96 | // there should be no offset for vertical fading edge |
| 97 | // if the item is the last one on screen |
| 98 | @LargeTest |
| 99 | public void testArrowScrollDownToBottomElementOnScreen() { |
| 100 | |
| 101 | int numGroups = getActivity().getNumButtons(); |
| 102 | Button lastButton = getActivity().getButton(numGroups - 1); |
| 103 | |
| 104 | assertEquals("button needs to be at the very bottom of the layout for " |
| 105 | + "this test to work", |
| 106 | mLinearLayout.getHeight(), lastButton.getBottom()); |
| 107 | |
| 108 | // move down to last button |
| 109 | for (int i = 0; i < numGroups; i++) { |
| 110 | sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); |
| 111 | } |
| 112 | getInstrumentation().waitForIdleSync(); |
| 113 | assertTrue("last button should have focus", lastButton.hasFocus()); |
| 114 | |
| 115 | int buttonLoc[] = {0, 0}; |
| 116 | lastButton.getLocationOnScreen(buttonLoc); |
| 117 | int buttonBottom = buttonLoc[1] + lastButton.getHeight(); |
| 118 | assertEquals("button should be at very bottom of screen", |
| 119 | mScreenBottom, buttonBottom); |
| 120 | } |
| 121 | |
| 122 | @LargeTest |
| 123 | public void testArrowScrollUpOffScreenVerticalFadingEdge() { |
| 124 | // get to bottom button |
| 125 | int numGroups = goToBottomButton(); |
| 126 | |
| 127 | // go up to first off screen button |
| 128 | int offScreenIndex = findFirstButtonOffScreenBottom2Top(); |
| 129 | Button offScreenButton = getActivity().getButton(offScreenIndex); |
| 130 | int clicksToOffScreenIndex = numGroups - offScreenIndex - 1; |
| 131 | for (int i = 0; i < clicksToOffScreenIndex; i++) { |
| 132 | sendKeys(KeyEvent.KEYCODE_DPAD_UP); |
| 133 | } |
| 134 | getInstrumentation().waitForIdleSync(); |
| 135 | assertTrue("we want to be at offScreenButton", offScreenButton.hasFocus()); |
| 136 | |
| 137 | // top should take into account fading edge |
| 138 | int buttonLoc[] = {0, 0}; |
| 139 | offScreenButton.getLocationOnScreen(buttonLoc); |
| 140 | assertEquals("top should take into account fading edge", |
| 141 | mScreenTop + mScrollView.getVerticalFadingEdgeLength(), buttonLoc[1]); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | @LargeTest |
| 146 | public void testArrowScrollUpToTopElementOnScreen() { |
| 147 | // get to bottom button |
| 148 | int numButtons = goToBottomButton(); |
| 149 | |
| 150 | // go back to the top |
| 151 | for (int i = 0; i < numButtons; i++) { |
| 152 | sendKeys(KeyEvent.KEYCODE_DPAD_UP); |
| 153 | } |
| 154 | getInstrumentation().waitForIdleSync(); |
| 155 | |
| 156 | Button topButton = getActivity().getButton(0); |
| 157 | assertTrue("should be back at top button", topButton.hasFocus()); |
| 158 | |
| 159 | |
| 160 | int buttonLoc[] = {0, 0}; |
| 161 | topButton.getLocationOnScreen(buttonLoc); |
| 162 | assertEquals("top of top button should be at top of screen; no need to take" |
| 163 | + " into account vertical fading edge.", |
| 164 | mScreenTop, buttonLoc[1]); |
| 165 | } |
| 166 | |
| 167 | private int goToBottomButton() { |
| 168 | int numButtons = getActivity().getNumButtons(); |
| 169 | Button lastButton = getActivity().getButton(numButtons - 1); |
| 170 | |
| 171 | for (int i = 0; i < numButtons; i++) { |
| 172 | sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); |
| 173 | } |
| 174 | getInstrumentation().waitForIdleSync(); |
| 175 | assertTrue("we want to be at the last button", lastButton.hasFocus()); |
| 176 | return numButtons; |
| 177 | } |
| 178 | |
| 179 | // search from top to bottom for the first button off screen |
| 180 | private int findFirstButtonOffScreenTop2Bottom() { |
| 181 | int origin[] = {0, 0}; |
| 182 | mScrollView.getLocationOnScreen(origin); |
| 183 | int screenHeight = mScrollView.getHeight(); |
| 184 | |
| 185 | for (int i = 0; i < getActivity().getNumButtons(); i++) { |
| 186 | |
| 187 | int buttonLoc[] = {0, 0}; |
| 188 | Button button = getActivity().getButton(i); |
| 189 | button.getLocationOnScreen(buttonLoc); |
| 190 | |
| 191 | if (buttonLoc[1] - origin[1] > screenHeight) { |
| 192 | return i; |
| 193 | } |
| 194 | } |
| 195 | fail("couldn't find first button off screen"); |
| 196 | return -1; // this won't execute, but the compiler needs it |
| 197 | } |
| 198 | |
| 199 | private int findFirstButtonOffScreenBottom2Top() { |
| 200 | int origin[] = {0, 0}; |
| 201 | mScrollView.getLocationOnScreen(origin); |
| 202 | |
| 203 | for (int i = getActivity().getNumButtons() - 1; i >= 0; i--) { |
| 204 | |
| 205 | int buttonLoc[] = {0, 0}; |
| 206 | Button button = getActivity().getButton(i); |
| 207 | button.getLocationOnScreen(buttonLoc); |
| 208 | |
| 209 | if (buttonLoc[1] < 0) { |
| 210 | return i; |
| 211 | } |
| 212 | } |
| 213 | fail("couldn't find first button off screen"); |
| 214 | return -1; // this won't execute, but the compiler needs it |
| 215 | } |
| 216 | |
| 217 | } |