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.test.suitebuilder.annotation.MediumTest; 21 import android.view.KeyEvent; 22 import android.view.View; 23 import android.widget.ListView; 24 import android.widget.TextView; 25 import android.widget.listview.ListOfItemsShorterThanScreen; 26 27 public class ListOfItemsShorterThanScreenTest 28 extends ActivityInstrumentationTestCase<ListOfItemsShorterThanScreen> { 29 private ListView mListView; 30 private ListOfItemsShorterThanScreen mActivity; 31 32 33 public ListOfItemsShorterThanScreenTest() { 34 super("com.android.frameworks.coretests", ListOfItemsShorterThanScreen.class); 35 } 36 37 protected void setUp() throws Exception { 38 super.setUp(); 39 mActivity = getActivity(); 40 mListView = getActivity().getListView(); 41 } 42 43 @MediumTest 44 public void testPreconditions() { 45 assertEquals(0, mListView.getSelectedItemPosition()); 46 assertTrue(mListView.getChildAt(0).isSelected()); 47 assertEquals(mListView.getListPaddingTop(), mListView.getSelectedView().getTop()); 48 } 49 50 @MediumTest 51 public void testMoveDownToOnScreenNextItem() { 52 final View next = mListView.getChildAt(1); 53 assertFalse(next.isSelected()); 54 final int secondPosition = mListView.getSelectedView().getBottom(); 55 assertEquals("second item should be positioned item height pixels from top.", 56 secondPosition, 57 next.getTop()); 58 59 sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); 60 assertEquals(1, mListView.getSelectedItemPosition()); 61 assertTrue(next.isSelected()); 62 assertEquals("next selected item shouldn't have moved", 63 secondPosition, 64 next.getTop()); 65 } 66 67 @MediumTest 68 public void testMoveUpToOnScreenItem() { 69 // move down one, then back up 70 sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); 71 sendKeys(KeyEvent.KEYCODE_DPAD_UP); 72 assertEquals(0, mListView.getSelectedItemPosition()); 73 } 74 75 @MediumTest 76 public void testMoveDownToItemRequiringScrolling() { 77 final int lastOnScreenItemIndex = mListView.getChildCount() - 1; 78 final View lastItem = mListView.getChildAt(lastOnScreenItemIndex); 79 assertTrue("last item should be partially off screen", 80 lastItem.getBottom() > mListView.getBottom()); 81 assertEquals(mListView.getListPaddingTop(), mListView.getSelectedView().getTop()); 82 83 for (int i = 0; i < lastOnScreenItemIndex; i++) { 84 sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); 85 } 86 87 assertEquals(lastOnScreenItemIndex, mListView.getSelectedItemPosition()); 88 assertEquals( 89 getTopOfBottomFadingEdge(), 90 mListView.getSelectedView().getBottom()); 91 92 // there should be a peeking view 93 94 // the current view isn't the last anymore... 95 assertEquals(mListView.getSelectedView(), mListView.getChildAt(mListView.getChildCount() - 2)); 96 97 // peeking view is now last 98 final TextView view = (TextView) mListView.getChildAt(mListView.getChildCount() - 1); 99 assertEquals(mActivity.getValueAtPosition(lastOnScreenItemIndex + 1), view.getText()); 100 assertFalse(view.isSelected()); 101 } 102 103 @MediumTest 104 public void testMoveUpToItemRequiringScrolling() { 105 // go down to one past last item, then back up to the second item. this will 106 // require scrolling to get it back on screen, and will need a peeking edge 107 108 int numItemsOnScreen = mListView.getChildCount(); 109 for (int i = 0; i < numItemsOnScreen; i++) { 110 sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); 111 } 112 for (int i = 0; i < numItemsOnScreen - 1; i++) { 113 sendKeys(KeyEvent.KEYCODE_DPAD_UP); 114 } 115 116 assertEquals(1, mListView.getSelectedItemPosition()); 117 assertEquals("top should be just below vertical fading edge", 118 mListView.getVerticalFadingEdgeLength() + mListView.getListPaddingTop(), 119 mListView.getSelectedView().getTop()); 120 } 121 122 @MediumTest 123 public void testPressUpWhenAlreadyAtTop() { 124 sendKeys(KeyEvent.KEYCODE_DPAD_UP); 125 126 assertEquals(0, mListView.getSelectedItemPosition()); 127 } 128 129 @MediumTest 130 public void testPressDownWhenAlreadyAtBottom() { 131 final int lastItemPosition = mListView.getAdapter().getCount() - 1; 132 for (int i = 0; i < lastItemPosition; i++) { 133 sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); 134 } 135 assertEquals(lastItemPosition, mListView.getSelectedItemPosition()); 136 137 sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); 138 assertEquals(lastItemPosition, mListView.getSelectedItemPosition()); 139 } 140 141 @MediumTest 142 public void testNoVerticalFadingEdgeWhenMovingToBottom() { 143 final int lastItemPosition = mListView.getAdapter().getCount() - 1; 144 for (int i = 0; i < lastItemPosition; i++) { 145 sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); 146 } 147 assertEquals(lastItemPosition, mListView.getSelectedItemPosition()); 148 149 assertEquals("bottom of last item should be just above padding; no fading edge.", 150 mListView.getHeight() - mListView.getListPaddingBottom(), 151 mListView.getSelectedView().getBottom()); 152 153 } 154 155 // the top of the bottom fading edge takes into account the list padding at the bottom, 156 // and the fading edge size 157 private int getTopOfBottomFadingEdge() { 158 return mListView.getHeight() - (mListView.getVerticalFadingEdgeLength() + mListView.getListPaddingBottom()); 159 } 160 161 162 } 163