ListButtonsDiagonalAcrossItemsTest.java revision 1d3165f10b12165f02b7015ac1a817c5f60e6399
1/*
2 * Copyright (C) 2008 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.focus;
18
19import android.widget.listview.ListButtonsDiagonalAcrossItems;
20
21import android.test.ActivityInstrumentationTestCase;
22import android.test.suitebuilder.annotation.MediumTest;
23import android.view.FocusFinder;
24import android.view.KeyEvent;
25import android.view.View;
26import android.widget.Button;
27import android.widget.ListView;
28
29/**
30 * Test that ListView will override default behavior of focus searching to
31 * make sure going right and left doesn't change selection
32 */
33public class ListButtonsDiagonalAcrossItemsTest extends ActivityInstrumentationTestCase<ListButtonsDiagonalAcrossItems> {
34
35    private Button mLeftButton;
36    private Button mCenterButton;
37    private Button mRightButton;
38    private ListView mListView;
39
40    public ListButtonsDiagonalAcrossItemsTest() {
41        super("com.android.frameworks.coretests", ListButtonsDiagonalAcrossItems.class);
42    }
43
44    @Override
45    protected void setUp() throws Exception {
46        super.setUp();
47
48        mLeftButton = getActivity().getLeftButton();
49        mCenterButton = getActivity().getCenterButton();
50        mRightButton = getActivity().getRightButton();
51
52        mListView = getActivity().getListView();
53    }
54
55    @MediumTest
56    public void testPreconditions() {
57        final ListView lv = mListView;
58        assertEquals("num children", 3, lv.getChildCount());
59
60        assertEquals("selected position", 0, lv.getSelectedItemPosition());
61        assertTrue("left button focused", mLeftButton.isFocused());
62
63        assertTrue("left left of center",
64                mLeftButton.getRight()
65                        < mCenterButton.getLeft());
66
67        assertTrue("center left of right",
68                mCenterButton.getRight()
69                        < mRightButton.getLeft());
70
71        assertEquals("focus search right from left button should be center button",
72            mCenterButton,
73            FocusFinder.getInstance().findNextFocus(mListView, mLeftButton, View.FOCUS_RIGHT));
74        assertEquals("focus search right from center button should be right button",
75            mRightButton,
76            FocusFinder.getInstance().findNextFocus(mListView, mCenterButton, View.FOCUS_RIGHT));
77        assertEquals("focus search left from centr button should be left button",
78            mLeftButton,
79            FocusFinder.getInstance().findNextFocus(mListView, mCenterButton, View.FOCUS_LEFT));
80    }
81
82    @MediumTest
83    public void testGoingRightDoesNotChangeSelection() {
84        sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT);
85
86        assertEquals("selected position shouldn't have changed",
87                0,
88                mListView.getSelectedItemPosition());
89        assertTrue("left should still be focused", mLeftButton.isFocused());
90    }
91
92    @MediumTest
93    public void testGoingLeftDoesNotChangeSelection() {
94        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
95        assertEquals("list view postion", 1, mListView.getSelectedItemPosition());
96        assertTrue("mCenterButton.isFocused()", mCenterButton.isFocused());
97
98        sendKeys(KeyEvent.KEYCODE_DPAD_LEFT);
99        assertEquals("selected position shouldn't have changed",
100                1,
101                mListView.getSelectedItemPosition());
102        assertTrue("center should still be focused", mCenterButton.isFocused());
103    }
104
105}
106