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 17 package android.widget.listview.arrowscroll; 18 19 import android.test.ActivityInstrumentationTestCase; 20 import android.view.KeyEvent; 21 import android.view.View; 22 import android.widget.ListView; 23 import android.widget.listview.ListOfThinItems; 24 25 import androidx.test.filters.LargeTest; 26 import androidx.test.filters.MediumTest; 27 import androidx.test.filters.Suppress; 28 29 public class ListOfThinItemsTest extends ActivityInstrumentationTestCase<ListOfThinItems> { 30 private ListView mListView; 31 ListOfThinItemsTest()32 public ListOfThinItemsTest() { 33 super("com.android.frameworks.coretests", ListOfThinItems.class); 34 } 35 36 @Override setUp()37 protected void setUp() throws Exception{ 38 super.setUp(); 39 mListView = getActivity().getListView(); 40 } 41 42 @MediumTest 43 @Suppress // Failing. testPreconditions()44 public void testPreconditions() { 45 assertNotNull(mListView); 46 assertTrue("need item height less than fading edge length", 47 mListView.getSelectedView().getHeight() < mListView.getVerticalFadingEdgeLength()); 48 assertTrue("need items off screen", 49 mListView.getChildCount() < mListView.getAdapter().getCount()); 50 } 51 52 @LargeTest testScrollToBottom()53 public void testScrollToBottom() { 54 // focus the listview 55 getActivity().runOnUiThread(() -> mListView.requestFocus()); 56 getInstrumentation().waitForIdleSync(); 57 58 final int numItems = mListView.getAdapter().getCount(); 59 final int listBottom = mListView.getHeight() - mListView.getListPaddingBottom(); 60 for (int i = 0; i < numItems; i++) { 61 assertEquals("wrong selection at position " + i, 62 i, mListView.getSelectedItemPosition()); 63 final int bottomFadingEdge = listBottom - mListView.getVerticalFadingEdgeLength(); 64 final View lastChild = mListView.getChildAt(mListView.getChildCount() - 1); 65 final int lastVisiblePosition = lastChild.getId(); 66 67 68 int bottomThreshold = (lastVisiblePosition < mListView.getAdapter().getCount() - 1) ? 69 bottomFadingEdge : listBottom; 70 71 String prefix = "after " + i + " down presses, "; 72 73 assertTrue(prefix + "selected item is below bottom threshold (fading edge or bottom as " + 74 "appropriate)", 75 mListView.getSelectedView().getBottom() <= bottomThreshold); 76 assertTrue(prefix + "first item in list must be at very top or just above", 77 mListView.getChildAt(0).getTop() <= 0); 78 assertTrue(prefix + "last item in list should be at very bottom or just below", 79 lastChild.getBottom() >= listBottom); 80 sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); 81 } 82 } 83 84 @LargeTest testScrollToTop()85 public void testScrollToTop() { 86 // focus the listview 87 getActivity().runOnUiThread(() -> mListView.requestFocus()); 88 getInstrumentation().waitForIdleSync(); 89 90 final int numItems = mListView.getAdapter().getCount(); 91 92 for (int i = 0; i < numItems - 1; i++) { 93 sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); 94 } 95 assertEquals("should have moved to last position", 96 numItems - 1, mListView.getSelectedItemPosition()); 97 98 int listTop = mListView.getListPaddingTop(); 99 final int listBottom = mListView.getHeight() - mListView.getListPaddingBottom(); 100 101 for (int i = 0; i < numItems; i++) { 102 int expectedPostion = numItems - (i + 1); 103 assertEquals("wrong selection at position " + expectedPostion, 104 expectedPostion, mListView.getSelectedItemPosition()); 105 final int topFadingEdge = listTop + mListView.getVerticalFadingEdgeLength(); 106 final View firstChild = mListView.getChildAt(0); 107 final View lastChild = mListView.getChildAt(mListView.getChildCount() - 1); 108 final int firstVisiblePosition = firstChild.getId(); 109 110 111 int topThreshold = (firstVisiblePosition > 0) ? 112 topFadingEdge : listTop; 113 114 String prefix = "after " + i + " up presses, "; 115 116 assertTrue(prefix + "selected item is above top threshold (fading edge or top as " + 117 "appropriate)", 118 mListView.getSelectedView().getTop() >= topThreshold); 119 assertTrue(prefix + "first item in list must be at very top or just above", 120 firstChild.getTop() <= 0); 121 assertTrue(prefix + "last item in list should be at very bottom or just below", 122 lastChild.getBottom() >= listBottom); 123 sendKeys(KeyEvent.KEYCODE_DPAD_UP); 124 } 125 } 126 } 127