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;
18
19import android.widget.listview.ListItemFocusablesClose;
20
21import android.test.ActivityInstrumentationTestCase;
22import android.test.suitebuilder.annotation.MediumTest;
23import android.view.KeyEvent;
24
25public class ListRetainsFocusAcrossLayoutsTest extends ActivityInstrumentationTestCase<ListItemFocusablesClose> {
26
27    public ListRetainsFocusAcrossLayoutsTest() {
28        super("com.android.frameworks.coretests", ListItemFocusablesClose.class);
29    }
30
31    private void requestLayoutOnList() {
32        getActivity().runOnUiThread(new Runnable() {
33            public void run() {
34                getActivity().getListView().requestLayout();
35            }
36        });
37    }
38
39    @MediumTest
40    public void testPreconditions() {
41        assertTrue("top button at position 0 should be focused",
42                getActivity().getChildOfItem(0, 0).isFocused());
43    }
44
45    @MediumTest
46    public void testBottomButtonRetainsFocusAfterLayout() throws Exception {
47
48        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
49
50        assertTrue("bottom botton at position 0 should be focused",
51                getActivity().getChildOfItem(0, 2).isFocused());
52
53        requestLayoutOnList();
54        getInstrumentation().waitForIdleSync();
55
56        assertTrue("bottom botton at position 0 should be focused after layout",
57                getActivity().getChildOfItem(0, 2).isFocused());
58    }
59
60    @MediumTest
61    public void testTopButtonOfSecondPositionRetainsFocusAfterLayout() {
62        sendRepeatedKeys(2, KeyEvent.KEYCODE_DPAD_DOWN);
63
64        assertTrue("top botton at position 1 should be focused",
65                getActivity().getChildOfItem(1, 0).isFocused());
66
67        requestLayoutOnList();
68        getInstrumentation().waitForIdleSync();
69
70        assertTrue("top botton at position 1 should be focused after layout",
71                getActivity().getChildOfItem(1, 0).isFocused());
72
73    }
74
75    @MediumTest
76    public void testBottomButtonOfSecondPositionRetainsFocusAfterLayout() {
77        sendRepeatedKeys(3, KeyEvent.KEYCODE_DPAD_DOWN);
78
79        assertTrue("bottom botton at position 1 should be focused",
80                getActivity().getChildOfItem(1, 2).isFocused());
81
82        requestLayoutOnList();
83        getInstrumentation().waitForIdleSync();
84
85        assertTrue("bottom botton at position 1 should be focused after layout",
86                getActivity().getChildOfItem(1, 2).isFocused());
87    }
88}
89