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 17package android.widget.listview.arrowscroll; 18 19import android.test.ActivityInstrumentationTestCase; 20import android.test.suitebuilder.annotation.MediumTest; 21import android.view.KeyEvent; 22import android.view.View; 23import android.view.ViewGroup; 24import android.widget.Button; 25import android.widget.ListView; 26import android.widget.listview.ListItemFocusablesFarApart; 27 28public class ListItemFocusablesFarApartTest extends ActivityInstrumentationTestCase<ListItemFocusablesFarApart> { 29 private ListView mListView; 30 private int mListTop; 31 private int mListBottom; 32 33 public ListItemFocusablesFarApartTest() { 34 super("com.android.frameworks.coretests", ListItemFocusablesFarApart.class); 35 } 36 37 @Override 38 protected void setUp() throws Exception { 39 super.setUp(); 40 mListView = getActivity().getListView(); 41 mListTop = mListView.getListPaddingTop(); 42 mListBottom = mListView.getHeight() - mListView.getListPaddingBottom(); 43 } 44 45 /** 46 * Get the child of a list item. 47 * @param listIndex The index of the currently visible items 48 * @param index The index of the child. 49 */ 50 public View getChildOfItem(int listIndex, int index) { 51 return ((ViewGroup) mListView.getChildAt(listIndex)).getChildAt(index); 52 53 } 54 55 public int getTopOfChildOfItem(int listIndex, int index) { 56 ViewGroup listItem = (ViewGroup) mListView.getChildAt(listIndex); 57 View child = listItem.getChildAt(index); 58 return child.getTop() + listItem.getTop(); 59 } 60 61 public int getBottomOfChildOfItem(int listIndex, int index) { 62 ViewGroup listItem = (ViewGroup) mListView.getChildAt(listIndex); 63 View child = listItem.getChildAt(index); 64 return child.getBottom() + listItem.getTop(); 65 } 66 67 @MediumTest 68 public void testPreconditions() { 69 assertNotNull(mListView); 70 assertEquals("should only be one visible list item", 71 1, mListView.getChildCount()); 72 int topOfFirstButton = getTopOfChildOfItem(0, 0); 73 int topOfSecondButton = getTopOfChildOfItem(0, 2); 74 assertTrue("second button should be more than max scroll away from first", 75 topOfSecondButton - topOfFirstButton > mListView.getMaxScrollAmount()); 76 } 77 78 79 @MediumTest 80 public void testPanWhenNextFocusableTooFarDown() { 81 82 int expectedTop = mListView.getChildAt(0).getTop(); 83 84 final Button topButton = (Button) getChildOfItem(0, 0); 85 86 int counter = 0; 87 while(getTopOfChildOfItem(0, 2) > mListBottom) { 88 // just to make sure we never end up with an infinite loop 89 if (counter > 5) fail("couldn't reach next button within " + counter + " downs"); 90 91 if (getBottomOfChildOfItem(0, 0) < mListTop) { 92 assertFalse("after " + counter + " downs, top button not visible, should not have focus", 93 topButton.isFocused()); 94 assertFalse("after " + counter + " downs, neither top button nor botom button visible, nothng within first list " + 95 "item should have focus", mListView.getChildAt(0).hasFocus()); 96 } else { 97 assertTrue("after " + counter + " downs, top button still visible, should have focus", 98 topButton.isFocused()); 99 } 100 101 assertEquals("after " + counter + " downs, " + 102 "should have panned by max scroll amount", 103 expectedTop, mListView.getChildAt(0).getTop()); 104 105 sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); 106 expectedTop -= mListView.getMaxScrollAmount(); 107 counter++; 108 } 109 110 // at this point, the second button is visible on screen. 111 // it should have focus 112 assertTrue("second button should have focus", 113 getChildOfItem(0, 2).isFocused()); 114 } 115 116} 117