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