FileDocCategorySizeDatePackage
ListOfButtonsTest.javaAPI DocAndroid 5.1 API5381Thu Mar 12 22:22:12 GMT 2015android.widget.focus

ListOfButtonsTest

public class ListOfButtonsTest extends android.test.ActivityInstrumentationTestCase2
Tests that focus works as expected when navigating into and out of a {@link ListView} that has buttons in it.

Fields Summary
private android.widget.ListAdapter
mListAdapter
private android.widget.Button
mButtonAtTop
private android.widget.ListView
mListView
Constructors Summary
public ListOfButtonsTest()

        super(ListOfButtons.class);
    
Methods Summary
public voidTODO_testNavigateThroughAllButtonsAndBack()


        String[] labels = getActivity().getLabels();
        for (int i = 0; i < labels.length; i++) {
            String label = labels[i];
            sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
            getInstrumentation().waitForIdleSync();

            String indexInfo = "index: " + i + ", label: " + label;

            assertTrue(indexInfo, mListView.hasFocus());
            
            Button button = (Button) mListView.getSelectedView();
            assertNotNull(indexInfo, button);
            assertEquals(indexInfo, label, button.getText().toString());
            assertTrue(indexInfo, button.hasFocus());
        }

        // pressing down again shouldn't matter; make sure last item keeps focus
        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);


        for (int i = labels.length - 1; i >= 0; i--) {
            String label = labels[i];

            String indexInfo = "index: " + i + ", label: " + label;

            assertTrue(indexInfo, mListView.hasFocus());

            Button button = (Button) mListView.getSelectedView();
            assertNotNull(indexInfo, button);
            assertEquals(indexInfo, label, button.getText().toString());
            assertTrue(indexInfo, button.hasFocus());

            sendKeys(KeyEvent.KEYCODE_DPAD_UP);
            getInstrumentation().waitForIdleSync();
        }

        assertTrue("button at top should have focus back",
                mButtonAtTop.hasFocus());
        assertFalse(mListView.hasFocus());
    
protected voidsetUp()

        super.setUp();

        ListOfButtons a = getActivity();
        getInstrumentation().waitForIdleSync();
        mListAdapter = a.getListAdapter();
        mButtonAtTop = (Button) a.findViewById(R.id.button);
        mListView = a.getListView();
    
public voidtestGoInAndOutOfListWithItemsFocusable()


        sendKeys(KeyEvent.KEYCODE_DPAD_UP);

        assertTrue(mButtonAtTop.hasFocus());

        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);

        final String firstButtonLabel = getActivity().getLabels()[0];
        final Button firstButton = (Button) mListView.getSelectedView();

        assertTrue(firstButton.isFocused());
        assertEquals(firstButtonLabel, firstButton.getText());

        sendKeys(KeyEvent.KEYCODE_DPAD_UP);
        assertTrue(mButtonAtTop.isFocused());

        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
        assertTrue(firstButton.isFocused());

        sendKeys(KeyEvent.KEYCODE_DPAD_UP);
        assertTrue(mButtonAtTop.isFocused());

        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
        assertTrue(firstButton.isFocused());
    
public voidtestNavigateToButtonAbove()

        sendKeys(KeyEvent.KEYCODE_DPAD_UP);

        assertTrue(mButtonAtTop.hasFocus());        
        assertFalse(mListView.hasFocus());
    
public voidtestNavigateToSecondItem()

        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);

        assertTrue(mListView.hasFocus());

        View childOne = mListView.getChildAt(1);
        assertNotNull(childOne);
        assertEquals(childOne, mListView.getFocusedChild());
        assertTrue(childOne.hasFocus());
    
public voidtestNavigateUpAboveAndBackOut()

        sendKeys(KeyEvent.KEYCODE_DPAD_UP);
        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);

        assertFalse("button at top should have focus back",
                mButtonAtTop.hasFocus());
        assertTrue(mListView.hasFocus());
    
public voidtestPreconditions()

        assertNotNull(mListAdapter);
        assertNotNull(mButtonAtTop);
        assertNotNull(mListView);

        assertFalse(mButtonAtTop.hasFocus());
        assertTrue(mListView.hasFocus());
        assertEquals("expecting 0 index to be selected",
                0, mListView.getSelectedItemPosition());