1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.widget.expandablelistview;
import android.app.Instrumentation;
import android.test.ActivityInstrumentationTestCase2;
import android.util.ExpandableListScenario;
import android.util.ListUtil;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import junit.framework.Assert;
public class ExpandableListTester {
private final ExpandableListView mExpandableListView;
private final ExpandableListAdapter mAdapter;
private final ListUtil mListUtil;
private final ActivityInstrumentationTestCase2<? extends ExpandableListScenario>
mActivityInstrumentation;
Instrumentation mInstrumentation;
public ExpandableListTester(
ExpandableListView expandableListView,
ActivityInstrumentationTestCase2<? extends ExpandableListScenario>
activityInstrumentation) {
mExpandableListView = expandableListView;
Instrumentation instrumentation = activityInstrumentation.getInstrumentation();
mListUtil = new ListUtil(mExpandableListView, instrumentation);
mAdapter = mExpandableListView.getExpandableListAdapter();
mActivityInstrumentation = activityInstrumentation;
mInstrumentation = mActivityInstrumentation.getInstrumentation();
}
private void expandGroup(final int groupIndex, int flatPosition) {
Assert.assertFalse("Group is already expanded", mExpandableListView
.isGroupExpanded(groupIndex));
mListUtil.arrowScrollToSelectedPosition(flatPosition);
mInstrumentation.waitForIdleSync();
mActivityInstrumentation.sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
mActivityInstrumentation.getInstrumentation().waitForIdleSync();
Assert.assertTrue("Group did not expand " + groupIndex,
mExpandableListView.isGroupExpanded(groupIndex));
}
void testContextMenus() {
// Add a position tester ContextMenu listener to the ExpandableListView
PositionTesterContextMenuListener menuListener = new PositionTesterContextMenuListener();
mExpandableListView.setOnCreateContextMenuListener(menuListener);
int index = 0;
// Scrolling on header elements should trigger an AdapterContextMenu
for (int i=0; i<mExpandableListView.getHeaderViewsCount(); i++) {
// Check group index in context menu
menuListener.expectAdapterContextMenu(i);
// Make sure the group is visible so that getChild finds it
mListUtil.arrowScrollToSelectedPosition(index);
View headerChild = mExpandableListView.getChildAt(index
- mExpandableListView.getFirstVisiblePosition());
mExpandableListView.showContextMenuForChild(headerChild);
mInstrumentation.waitForIdleSync();
Assert.assertNull(menuListener.getErrorMessage(), menuListener.getErrorMessage());
index++;
}
int groupCount = mAdapter.getGroupCount();
for (int groupIndex = 0; groupIndex < groupCount; groupIndex++) {
// Expand group
expandGroup(groupIndex, index);
// Check group index in context menu
menuListener.expectGroupContextMenu(groupIndex);
// Make sure the group is visible so that getChild finds it
mListUtil.arrowScrollToSelectedPosition(index);
View groupChild = mExpandableListView.getChildAt(index
- mExpandableListView.getFirstVisiblePosition());
mExpandableListView.showContextMenuForChild(groupChild);
mInstrumentation.waitForIdleSync();
Assert.assertNull(menuListener.getErrorMessage(), menuListener.getErrorMessage());
index++;
final int childrenCount = mAdapter.getChildrenCount(groupIndex);
for (int childIndex = 0; childIndex < childrenCount; childIndex++) {
// Check child index in context menu
mListUtil.arrowScrollToSelectedPosition(index);
menuListener.expectChildContextMenu(groupIndex, childIndex);
View child = mExpandableListView.getChildAt(index
- mExpandableListView.getFirstVisiblePosition());
mExpandableListView.showContextMenuForChild(child);
mInstrumentation.waitForIdleSync();
Assert.assertNull(menuListener.getErrorMessage(), menuListener.getErrorMessage());
index++;
}
}
// Scrolling on footer elements should trigger an AdapterContextMenu
for (int i=0; i<mExpandableListView.getFooterViewsCount(); i++) {
// Check group index in context menu
menuListener.expectAdapterContextMenu(index);
// Make sure the group is visible so that getChild finds it
mListUtil.arrowScrollToSelectedPosition(index);
View footerChild = mExpandableListView.getChildAt(index
- mExpandableListView.getFirstVisiblePosition());
mExpandableListView.showContextMenuForChild(footerChild);
mInstrumentation.waitForIdleSync();
Assert.assertNull(menuListener.getErrorMessage(), menuListener.getErrorMessage());
index++;
}
// Cleanup: remove the listener we added.
mExpandableListView.setOnCreateContextMenuListener(null);
}
private int expandAGroup() {
final int groupIndex = 2;
final int headerCount = mExpandableListView.getHeaderViewsCount();
Assert.assertTrue("Not enough groups", groupIndex < mAdapter.getGroupCount());
expandGroup(groupIndex, groupIndex + headerCount);
return groupIndex;
}
// This method assumes that NO group is expanded when called
void testConvertionBetweenFlatAndPackedOnGroups() {
final int headerCount = mExpandableListView.getHeaderViewsCount();
for (int i=0; i<headerCount; i++) {
Assert.assertEquals("Non NULL position for header item",
ExpandableListView.PACKED_POSITION_VALUE_NULL,
mExpandableListView.getExpandableListPosition(i));
}
// Test all (non expanded) groups
final int groupCount = mAdapter.getGroupCount();
for (int groupIndex = 0; groupIndex < groupCount; groupIndex++) {
int expectedFlatPosition = headerCount + groupIndex;
long packedPositionForGroup = ExpandableListView.getPackedPositionForGroup(groupIndex);
Assert.assertEquals("Group not found at flat position " + expectedFlatPosition,
packedPositionForGroup,
mExpandableListView.getExpandableListPosition(expectedFlatPosition));
Assert.assertEquals("Wrong flat position for group " + groupIndex,
expectedFlatPosition,
mExpandableListView.getFlatListPosition(packedPositionForGroup));
}
for (int i=0; i<mExpandableListView.getFooterViewsCount(); i++) {
Assert.assertEquals("Non NULL position for header item",
ExpandableListView.PACKED_POSITION_VALUE_NULL,
mExpandableListView.getExpandableListPosition(headerCount + groupCount + i));
}
}
// This method assumes that NO group is expanded when called
void testConvertionBetweenFlatAndPackedOnChildren() {
// Test with an expanded group
final int headerCount = mExpandableListView.getHeaderViewsCount();
final int groupIndex = expandAGroup();
final int childrenCount = mAdapter.getChildrenCount(groupIndex);
for (int childIndex = 0; childIndex < childrenCount; childIndex++) {
int expectedFlatPosition = headerCount + groupIndex + 1 + childIndex;
long childPos = ExpandableListView.getPackedPositionForChild(groupIndex, childIndex);
Assert.assertEquals("Wrong flat position for child ",
childPos,
mExpandableListView.getExpandableListPosition(expectedFlatPosition));
Assert.assertEquals("Wrong flat position for child ",
expectedFlatPosition,
mExpandableListView.getFlatListPosition(childPos));
}
}
// This method assumes that NO group is expanded when called
void testSelectedPositionOnGroups() {
int index = 0;
// Scrolling on header elements should not give a valid selected position.
for (int i=0; i<mExpandableListView.getHeaderViewsCount(); i++) {
mListUtil.arrowScrollToSelectedPosition(index);
Assert.assertEquals("Header item is selected",
ExpandableListView.PACKED_POSITION_VALUE_NULL,
mExpandableListView.getSelectedPosition());
index++;
}
// Check selection on group items
final int groupCount = mAdapter.getGroupCount();
for (int groupIndex = 0; groupIndex < groupCount; groupIndex++) {
mListUtil.arrowScrollToSelectedPosition(index);
Assert.assertEquals("Group item is not selected",
ExpandableListView.getPackedPositionForGroup(groupIndex),
mExpandableListView.getSelectedPosition());
index++;
}
// Scrolling on footer elements should not give a valid selected position.
for (int i=0; i<mExpandableListView.getFooterViewsCount(); i++) {
mListUtil.arrowScrollToSelectedPosition(index);
Assert.assertEquals("Footer item is selected",
ExpandableListView.PACKED_POSITION_VALUE_NULL,
mExpandableListView.getSelectedPosition());
index++;
}
}
// This method assumes that NO group is expanded when called
void testSelectedPositionOnChildren() {
// Test with an expanded group
final int headerCount = mExpandableListView.getHeaderViewsCount();
final int groupIndex = expandAGroup();
final int childrenCount = mAdapter.getChildrenCount(groupIndex);
for (int childIndex = 0; childIndex < childrenCount; childIndex++) {
int childFlatPosition = headerCount + groupIndex + 1 + childIndex;
mListUtil.arrowScrollToSelectedPosition(childFlatPosition);
Assert.assertEquals("Group item is not selected",
ExpandableListView.getPackedPositionForChild(groupIndex, childIndex),
mExpandableListView.getSelectedPosition());
}
}
}
|