ListInterleaveFocusablesTest.java revision 1d3165f10b12165f02b7015ac1a817c5f60e6399
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.LargeTest;
21import android.test.suitebuilder.annotation.MediumTest;
22import android.widget.ListView;
23import android.view.KeyEvent;
24import android.view.View;
25
26import android.widget.listview.ListInterleaveFocusables;
27import android.util.ListUtil;
28
29public class ListInterleaveFocusablesTest extends ActivityInstrumentationTestCase<ListInterleaveFocusables> {
30    private ListView mListView;
31    private ListUtil mListUtil;
32
33    public ListInterleaveFocusablesTest() {
34        super("com.android.frameworks.coretests", ListInterleaveFocusables.class);
35    }
36
37    @Override
38    protected void setUp() throws Exception {
39        super.setUp();
40
41        mListView = getActivity().getListView();
42        mListUtil = new ListUtil(mListView, getInstrumentation());
43    }
44
45    @LargeTest
46    public void testPreconditions() {
47        assertEquals(7, mListView.getChildCount());
48        assertTrue(mListView.getChildAt(1).isFocusable());
49        assertTrue(mListView.getChildAt(3).isFocusable());
50        assertTrue(mListView.getChildAt(6).isFocusable());
51    }
52
53    @MediumTest
54    public void testGoingFromUnFocusableSelectedToFocusable() {
55        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
56
57        assertEquals("selected item position", 1, mListView.getSelectedItemPosition());
58        assertSelectedViewFocus(true);
59    }
60
61    // go down from an item that isn't focusable, make sure it finds the focusable
62    // below (instead of above).  this exposes a (now fixed) bug where the focus search
63    // was not starting from the right spot
64    @MediumTest
65    public void testGoingDownFromUnFocusableSelectedToFocusableWithOtherFocusableAbove() {
66        mListUtil.setSelectedPosition(2);
67        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
68        assertEquals("selected item position", 3, mListView.getSelectedItemPosition());
69        assertSelectedViewFocus(true);
70    }
71
72    // same, but going up
73    @MediumTest
74    public void testGoingUpFromUnFocusableSelectedToFocusableWithOtherFocusableAbove() {
75        mListUtil.setSelectedPosition(2);
76        sendKeys(KeyEvent.KEYCODE_DPAD_UP);
77        assertEquals("selected item position", 1, mListView.getSelectedItemPosition());
78        assertSelectedViewFocus(true);
79    }
80
81    /**
82     * Go down from a focusable when there is a focusable below, but it is more than
83     * one item away; make sure it won't give that item focus because it is too far away.
84     */
85    @MediumTest
86    public void testGoingDownFromFocusableToUnfocusableWhenFocusableIsBelow() {
87        mListUtil.setSelectedPosition(3);
88        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
89        assertEquals("selected item position", 4, mListView.getSelectedItemPosition());
90        assertSelectedViewFocus(false);
91    }
92
93    // same but going up
94    @MediumTest
95    public void testGoingUpFromFocusableToUnfocusableWhenFocusableIsBelow() {
96        mListUtil.setSelectedPosition(6);
97        sendKeys(KeyEvent.KEYCODE_DPAD_UP);
98        assertEquals("selected item position", 5, mListView.getSelectedItemPosition());
99        assertSelectedViewFocus(false);
100    }
101
102    public void assertSelectedViewFocus(boolean isFocused) {
103        final View view = mListView.getSelectedView();
104        assertEquals("selected view focused", isFocused, view.isFocused());
105        assertEquals("selected position's isSelected should be the inverse "
106                + "of it having focus", !isFocused, view.isSelected());
107    }
108
109}
110