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