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.MediumTest;
21import android.view.KeyEvent;
22import android.widget.LinearLayout;
23import android.widget.ListView;
24import android.widget.listview.ListItemFocusablesClose;
25
26public class ListItemFocusablesCloseTest extends ActivityInstrumentationTestCase<ListItemFocusablesClose> {
27    private ListView mListView;
28    private int mListTop;
29    private int mListBottom;
30
31    public ListItemFocusablesCloseTest() {
32        super("com.android.frameworks.coretests", ListItemFocusablesClose.class);
33    }
34
35    @Override
36    protected void setUp() throws Exception{
37        super.setUp();
38        mListView = getActivity().getListView();
39        mListTop = mListView.getListPaddingTop();
40        mListBottom = mListView.getHeight() - mListView.getListPaddingBottom();
41    }
42
43    @MediumTest
44    public void testPreconditions() {
45        assertNotNull(mListView);
46        assertTrue(mListView.getAdapter().areAllItemsEnabled());
47        assertTrue(mListView.getItemsCanFocus());
48        assertEquals(0, mListView.getSelectedItemPosition());
49        final LinearLayout first = (LinearLayout) mListView.getSelectedView();
50        getInstrumentation().waitForIdleSync();
51        assertEquals("first item should be at top of screen",
52                mListView.getListPaddingTop(),
53                first.getTop());
54        assertTrue("first button of first list item should have focus",
55                first.getChildAt(0).isFocused());
56        assertTrue("item should be shorter than list for this test to make sense",
57                first.getHeight() < mListView.getHeight());
58        assertEquals("two items should be on screen",
59                2, mListView.getChildCount());
60        assertTrue("first button of second item should be on screen",
61                getActivity().getChildOfItem(1, 0).getBottom() < mListBottom);
62    }
63
64
65    @MediumTest
66    public void testChangeFocusWithinItem() {
67        final LinearLayout first = (LinearLayout) mListView.getSelectedView();
68        final int topOfFirstItemBefore = first.getTop();
69        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
70        assertTrue("focus should have moved to second button of first item",
71                first.getChildAt(2).isFocused());
72        assertEquals("selection should not have changed",
73                0, mListView.getSelectedItemPosition());
74        assertEquals("list item should not have been shifted",
75                topOfFirstItemBefore, first.getTop());
76
77
78        sendKeys(KeyEvent.KEYCODE_DPAD_UP);
79        assertTrue("focus should have moved back to first button of first item",
80                first.getChildAt(0).isFocused());
81        assertEquals("list item should not have been shifted",
82                topOfFirstItemBefore, first.getTop());
83    }
84
85    @MediumTest
86    public void testMoveDownToButtonInDifferentSelection() {
87        final LinearLayout first = (LinearLayout) mListView.getSelectedView();
88        final int topOfFirstItemBefore = first.getTop();
89        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
90        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
91
92        assertEquals("selection should have moved to second item",
93                1, mListView.getSelectedItemPosition());
94        final LinearLayout selectedItem = (LinearLayout) mListView.getSelectedView();
95        assertTrue("first button of second item should have focus",
96                selectedItem.getChildAt(0).isFocused());
97        assertEquals("list item should not have been shifted",
98                topOfFirstItemBefore, first.getTop());
99    }
100
101    @MediumTest
102    public void testMoveUpToButtonInDifferentSelection() {
103        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
104        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
105        assertEquals(1, mListView.getSelectedItemPosition());
106        assertTrue("first button of second item should have focus",
107                getActivity().getChildOfItem(1, 0).hasFocus());
108
109        sendKeys(KeyEvent.KEYCODE_DPAD_UP);
110        assertEquals("first list item should have selection", 0,
111                mListView.getSelectedItemPosition());
112        assertTrue("second button of first item should have focus",
113                getActivity().getChildOfItem(0, 2).hasFocus());
114    }
115}
116