ListFocusableTest.java revision 1d3165f10b12165f02b7015ac1a817c5f60e6399
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;
18
19import android.test.ActivityInstrumentationTestCase;
20import android.test.suitebuilder.annotation.MediumTest;
21import android.widget.ListView;
22import android.widget.ListAdapter;
23import android.widget.ArrayAdapter;
24
25public class ListFocusableTest extends ActivityInstrumentationTestCase<ListTopGravity> {
26    private ListTopGravity mActivity;
27    private ListView mListView;
28
29    public ListFocusableTest() {
30        super("com.android.frameworks.coretests", ListTopGravity.class);
31    }
32
33    @Override
34    protected void setUp() throws Exception {
35        super.setUp();
36
37        mActivity = getActivity();
38        mListView = getActivity().getListView();
39    }
40
41    @MediumTest
42    public void testPreconditions() {
43        assertNotNull(mActivity);
44        assertNotNull(mListView);
45
46        // First item should be selected
47        assertEquals(0, mListView.getSelectedItemPosition());
48    }
49
50    @MediumTest
51    public void testAdapterFull() {
52        setFullAdapter();
53        assertTrue(mListView.isFocusable());
54    }
55
56    @MediumTest
57    public void testAdapterEmpty() {
58        setEmptyAdapter();
59        assertFalse(mListView.isFocusable());
60    }
61
62    @MediumTest
63    public void testAdapterNull() {
64        setNullAdapter();
65        assertFalse(mListView.isFocusable());
66    }
67
68    @MediumTest
69    public void testAdapterFullSetFocusable() {
70        assertTrue(mListView.isFocusable());
71
72        setFocusable();
73        assertTrue(mListView.isFocusable());
74    }
75
76    @MediumTest
77    public void testAdapterFullSetNonFocusable() {
78        assertTrue(mListView.isFocusable());
79
80        setNonFocusable();
81        assertFalse(mListView.isFocusable());
82    }
83
84    @MediumTest
85    public void testAdapterEmptySetFocusable() {
86        setEmptyAdapter();
87        assertFalse(mListView.isFocusable());
88
89        setFocusable();
90        assertFalse(mListView.isFocusable());
91    }
92
93    @MediumTest
94    public void testAdapterEmptySetNonFocusable() {
95        setEmptyAdapter();
96        assertFalse(mListView.isFocusable());
97
98        setNonFocusable();
99        assertFalse(mListView.isFocusable());
100    }
101
102    @MediumTest
103    public void testAdapterNullSetFocusable() {
104        setNullAdapter();
105        assertFalse(mListView.isFocusable());
106
107        setFocusable();
108        assertFalse(mListView.isFocusable());
109    }
110
111    @MediumTest
112    public void testAdapterNullSetNonFocusable() {
113        setNullAdapter();
114        assertFalse(mListView.isFocusable());
115
116        setNonFocusable();
117        assertFalse(mListView.isFocusable());
118    }
119
120    @MediumTest
121    public void testFocusableSetAdapterFull() {
122        assertTrue(mListView.isFocusable());
123
124        setFullAdapter();
125        assertTrue(mListView.isFocusable());
126    }
127
128    @MediumTest
129    public void testNonFocusableSetAdapterFull() {
130        assertTrue(mListView.isFocusable());
131
132        setNonFocusable();
133        assertFalse(mListView.isFocusable());
134
135        setFullAdapter();
136        assertFalse(mListView.isFocusable());
137    }
138
139    @MediumTest
140    public void testFocusableSetAdapterEmpty() {
141        assertTrue(mListView.isFocusable());
142
143        setEmptyAdapter();
144        assertFalse(mListView.isFocusable());
145    }
146
147    @MediumTest
148    public void testNonFocusableSetAdapterEmpty() {
149        assertTrue(mListView.isFocusable());
150
151        setNonFocusable();
152        assertFalse(mListView.isFocusable());
153
154        setEmptyAdapter();
155        assertFalse(mListView.isFocusable());
156    }
157
158    @MediumTest
159    public void testFocusableSetAdapterNull() {
160        assertTrue(mListView.isFocusable());
161
162        setNullAdapter();
163        assertFalse(mListView.isFocusable());
164    }
165
166    @MediumTest
167    public void testNonFocusableSetAdapterNull() {
168        assertTrue(mListView.isFocusable());
169
170        setNonFocusable();
171        assertFalse(mListView.isFocusable());
172
173        setNullAdapter();
174        assertFalse(mListView.isFocusable());
175    }
176
177    private ListAdapter createFullAdapter() {
178        return new ArrayAdapter<String>(mActivity, android.R.layout.simple_list_item_1,
179                new String[] { "Android", "Robot" });
180    }
181
182    private ListAdapter createEmptyAdapter() {
183        return new ArrayAdapter<String>(mActivity, android.R.layout.simple_list_item_1,
184                new String[] { });
185    }
186
187
188    private void setFullAdapter() {
189        mActivity.runOnUiThread(new Runnable() {
190            public void run() {
191                mListView.setAdapter(createFullAdapter());
192            }
193        });
194        getInstrumentation().waitForIdleSync();
195    }
196
197    private void setEmptyAdapter() {
198        mActivity.runOnUiThread(new Runnable() {
199            public void run() {
200                mListView.setAdapter(createEmptyAdapter());
201            }
202        });
203        getInstrumentation().waitForIdleSync();
204    }
205
206    private void setNullAdapter() {
207        mActivity.runOnUiThread(new Runnable() {
208            public void run() {
209                mListView.setAdapter(null);
210            }
211        });
212        getInstrumentation().waitForIdleSync();
213    }
214
215    private void setFocusable() {
216        mActivity.runOnUiThread(new Runnable() {
217            public void run() {
218                mListView.setFocusable(true);
219            }
220        });
221        getInstrumentation().waitForIdleSync();
222    }
223
224    private void setNonFocusable() {
225        mActivity.runOnUiThread(new Runnable() {
226            public void run() {
227                mListView.setFocusable(false);
228            }
229        });
230        getInstrumentation().waitForIdleSync();
231    }
232}
233