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