ListWithEditTextHeaderTest.java revision 158d6b70ac2f0c8fd7dafe0f865112090fb31699
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.test.ActivityInstrumentationTestCase2;
20import android.test.FlakyTest;
21import android.test.TouchUtils;
22import android.test.suitebuilder.annotation.LargeTest;
23import android.test.suitebuilder.annotation.MediumTest;
24import android.view.KeyEvent;
25import android.view.View;
26import android.widget.AbsListView;
27import android.widget.ListView;
28import android.widget.listview.ListWithEditTextHeader;
29
30public class ListWithEditTextHeaderTest extends ActivityInstrumentationTestCase2<ListWithEditTextHeader> {
31    private ListView mListView;
32
33    public ListWithEditTextHeaderTest() {
34        super(ListWithEditTextHeader.class);
35    }
36
37    @Override
38    protected void setUp() throws Exception {
39        super.setUp();
40        mListView = getActivity().getListView();
41    }
42
43    @MediumTest
44    public void testPreconditions() {
45        assertTrue("listview.getItemsCanFocus()", mListView.getItemsCanFocus());
46        assertFalse("out of touch-mode", mListView.isInTouchMode());
47        assertEquals("header view count", 1, mListView.getHeaderViewsCount());
48        assertTrue("header does not have focus", mListView.getChildAt(0).isFocused());
49    }
50
51    @FlakyTest(tolerance=2)
52    @LargeTest
53    public void testClickingHeaderKeepsFocus() {
54        TouchUtils.clickView(this, mListView.getChildAt(0));
55        assertTrue("header does not have focus", mListView.getChildAt(0).isFocused());
56        assertEquals("something is selected", AbsListView.INVALID_POSITION, mListView.getSelectedItemPosition());
57    }
58
59    @LargeTest
60    public void testClickingHeaderWhenOtherItemHasFocusGivesHeaderFocus() {
61        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
62        assertEquals("selected position", 1, mListView.getSelectedItemPosition());
63        TouchUtils.clickView(this, mListView.getChildAt(0));
64        assertTrue("header does not have focus", mListView.getChildAt(0).isFocused());
65        assertEquals("something is selected", AbsListView.INVALID_POSITION, mListView.getSelectedItemPosition());
66    }
67
68    @LargeTest
69    public void testScrollingDoesNotDetachHeaderViewFromWindow() {
70        View header = mListView.getChildAt(0);
71        assertNotNull("header is not attached to a window (?!)", header.getWindowToken());
72
73        // Scroll header off the screen and back onto the screen
74        int numItemsOnScreen = mListView.getChildCount();
75        for (int i = 0; i < numItemsOnScreen; i++) {
76            sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
77        }
78        for (int i = 0; i < numItemsOnScreen; i++) {
79            sendKeys(KeyEvent.KEYCODE_DPAD_UP);
80        }
81
82        // Make sure the header was not accidentally left detached from its window
83        assertNotNull("header has lost its window", header.getWindowToken());
84    }
85}
86