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.focus; 18 19 import android.widget.focus.ListOfButtons; 20 import com.android.frameworks.coretests.R; 21 22 import android.test.ActivityInstrumentationTestCase2; 23 import android.test.suitebuilder.annotation.MediumTest; 24 import android.widget.ListAdapter; 25 import android.widget.Button; 26 import android.widget.ListView; 27 import android.view.KeyEvent; 28 import android.view.View; 29 30 /** 31 * Tests that focus works as expected when navigating into and out of 32 * a {@link ListView} that has buttons in it. 33 */ 34 public class ListOfButtonsTest extends ActivityInstrumentationTestCase2<ListOfButtons> { 35 36 private ListAdapter mListAdapter; 37 private Button mButtonAtTop; 38 39 private ListView mListView; 40 41 public ListOfButtonsTest() { 42 super(ListOfButtons.class); 43 } 44 45 @Override 46 protected void setUp() throws Exception { 47 super.setUp(); 48 49 ListOfButtons a = getActivity(); 50 getInstrumentation().waitForIdleSync(); 51 mListAdapter = a.getListAdapter(); 52 mButtonAtTop = (Button) a.findViewById(R.id.button); 53 mListView = a.getListView(); 54 } 55 56 @MediumTest 57 public void testPreconditions() { 58 assertNotNull(mListAdapter); 59 assertNotNull(mButtonAtTop); 60 assertNotNull(mListView); 61 62 assertFalse(mButtonAtTop.hasFocus()); 63 assertTrue(mListView.hasFocus()); 64 assertEquals("expecting 0 index to be selected", 65 0, mListView.getSelectedItemPosition()); 66 } 67 68 @MediumTest 69 public void testNavigateToButtonAbove() { 70 sendKeys(KeyEvent.KEYCODE_DPAD_UP); 71 72 assertTrue(mButtonAtTop.hasFocus()); 73 assertFalse(mListView.hasFocus()); 74 } 75 76 @MediumTest 77 public void testNavigateToSecondItem() { 78 sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); 79 80 assertTrue(mListView.hasFocus()); 81 82 View childOne = mListView.getChildAt(1); 83 assertNotNull(childOne); 84 assertEquals(childOne, mListView.getFocusedChild()); 85 assertTrue(childOne.hasFocus()); 86 } 87 88 @MediumTest 89 public void testNavigateUpAboveAndBackOut() { 90 sendKeys(KeyEvent.KEYCODE_DPAD_UP); 91 sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); 92 93 assertFalse("button at top should have focus back", 94 mButtonAtTop.hasFocus()); 95 assertTrue(mListView.hasFocus()); 96 } 97 98 // TODO: this reproduces bug 981791 99 public void TODO_testNavigateThroughAllButtonsAndBack() { 100 101 String[] labels = getActivity().getLabels(); 102 for (int i = 0; i < labels.length; i++) { 103 String label = labels[i]; 104 sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); 105 getInstrumentation().waitForIdleSync(); 106 107 String indexInfo = "index: " + i + ", label: " + label; 108 109 assertTrue(indexInfo, mListView.hasFocus()); 110 111 Button button = (Button) mListView.getSelectedView(); 112 assertNotNull(indexInfo, button); 113 assertEquals(indexInfo, label, button.getText().toString()); 114 assertTrue(indexInfo, button.hasFocus()); 115 } 116 117 // pressing down again shouldn't matter; make sure last item keeps focus 118 sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); 119 120 121 for (int i = labels.length - 1; i >= 0; i--) { 122 String label = labels[i]; 123 124 String indexInfo = "index: " + i + ", label: " + label; 125 126 assertTrue(indexInfo, mListView.hasFocus()); 127 128 Button button = (Button) mListView.getSelectedView(); 129 assertNotNull(indexInfo, button); 130 assertEquals(indexInfo, label, button.getText().toString()); 131 assertTrue(indexInfo, button.hasFocus()); 132 133 sendKeys(KeyEvent.KEYCODE_DPAD_UP); 134 getInstrumentation().waitForIdleSync(); 135 } 136 137 assertTrue("button at top should have focus back", 138 mButtonAtTop.hasFocus()); 139 assertFalse(mListView.hasFocus()); 140 } 141 142 @MediumTest 143 public void testGoInAndOutOfListWithItemsFocusable() { 144 145 sendKeys(KeyEvent.KEYCODE_DPAD_UP); 146 147 assertTrue(mButtonAtTop.hasFocus()); 148 149 sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); 150 151 final String firstButtonLabel = getActivity().getLabels()[0]; 152 final Button firstButton = (Button) mListView.getSelectedView(); 153 154 assertTrue(firstButton.isFocused()); 155 assertEquals(firstButtonLabel, firstButton.getText()); 156 157 sendKeys(KeyEvent.KEYCODE_DPAD_UP); 158 assertTrue(mButtonAtTop.isFocused()); 159 160 sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); 161 assertTrue(firstButton.isFocused()); 162 163 sendKeys(KeyEvent.KEYCODE_DPAD_UP); 164 assertTrue(mButtonAtTop.isFocused()); 165 166 sendKeys(KeyEvent.KEYCODE_DPAD_DOWN); 167 assertTrue(firstButton.isFocused()); 168 } 169 170 171 } 172