Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JList/6462008/bug6462008.java
41154 views
1
/*
2
* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @key headful
27
* @bug 6462008
28
* @summary Tests that mouse/keyboard work properly on JList with lead < 0 or > list.getModel().getSize()
29
* @author Shannon Hickey
30
* @run main bug6462008
31
*/
32
import java.awt.*;
33
import java.awt.event.*;
34
import javax.swing.*;
35
import java.util.*;
36
37
public class bug6462008 {
38
39
private static final int DONT_CARE = -2;
40
private static int anchorLead;
41
private static boolean isAquaLAF;
42
private static int controlKey;
43
private static JList list;
44
private static Robot robot;
45
private static JFrame frame;
46
47
public static void main(String[] args) throws Exception {
48
try {
49
robot = new Robot();
50
robot.setAutoDelay(100);
51
52
isAquaLAF = "Aqua".equals(UIManager.getLookAndFeel().getID());
53
controlKey = isAquaLAF ? KeyEvent.VK_META : KeyEvent.VK_CONTROL;
54
55
SwingUtilities.invokeAndWait(new Runnable() {
56
57
@Override
58
public void run() {
59
createAndShowGUI();
60
}
61
});
62
63
robot.waitForIdle();
64
robot.delay(1000);
65
66
setAnchorLead(-1);
67
robot.waitForIdle();
68
69
testListSelection();
70
71
setAnchorLead(100);
72
robot.waitForIdle();
73
74
testListSelection();
75
} finally {
76
if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
77
}
78
}
79
80
public static void testListSelection() throws Exception {
81
82
// Space
83
robot.keyPress(KeyEvent.VK_SPACE);
84
robot.keyRelease(KeyEvent.VK_SPACE);
85
86
robot.waitForIdle();
87
checkSelection();
88
resetList();
89
robot.waitForIdle();
90
91
// Control + Space
92
robot.keyPress(KeyEvent.VK_CONTROL);
93
robot.keyPress(KeyEvent.VK_SPACE);
94
robot.keyRelease(KeyEvent.VK_SPACE);
95
robot.keyRelease(KeyEvent.VK_CONTROL);
96
97
robot.waitForIdle();
98
checkSelection();
99
resetList();
100
robot.waitForIdle();
101
102
// Shift + Space
103
robot.keyPress(KeyEvent.VK_SHIFT);
104
robot.keyPress(KeyEvent.VK_SPACE);
105
robot.keyRelease(KeyEvent.VK_SPACE);
106
robot.keyRelease(KeyEvent.VK_SHIFT);
107
108
robot.waitForIdle();
109
checkSelection();
110
resetList();
111
robot.waitForIdle();
112
113
// Control + Shift + Space
114
robot.keyPress(KeyEvent.VK_CONTROL);
115
robot.keyPress(KeyEvent.VK_SHIFT);
116
robot.keyPress(KeyEvent.VK_SPACE);
117
robot.keyRelease(KeyEvent.VK_SPACE);
118
robot.keyRelease(KeyEvent.VK_SHIFT);
119
robot.keyRelease(KeyEvent.VK_CONTROL);
120
121
robot.waitForIdle();
122
checkSelection();
123
resetList();
124
robot.waitForIdle();
125
126
127
// Control + A Multiple Selection
128
129
robot.keyPress(controlKey);
130
robot.keyPress(KeyEvent.VK_A);
131
robot.keyRelease(KeyEvent.VK_A);
132
robot.keyRelease(controlKey);
133
134
robot.waitForIdle();
135
checkSelectionAL(-1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
136
resetList();
137
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
138
robot.waitForIdle();
139
140
// Control + A Single Selection
141
robot.keyPress(controlKey);
142
robot.keyPress(KeyEvent.VK_A);
143
robot.keyRelease(KeyEvent.VK_A);
144
robot.keyRelease(controlKey);
145
146
robot.waitForIdle();
147
checkSelectionAL(0, 0, 0);
148
resetList();
149
setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
150
setSelectionInterval(5, 5);
151
robot.waitForIdle();
152
153
154
// Control + A Selection interval (5, 5)
155
robot.keyPress(controlKey);
156
robot.keyPress(KeyEvent.VK_A);
157
robot.keyRelease(KeyEvent.VK_A);
158
robot.keyRelease(controlKey);
159
160
robot.waitForIdle();
161
checkSelection(5);
162
resetList();
163
robot.waitForIdle();
164
165
// Page Down
166
// Not applicable for the Aqua L&F
167
if (!isAquaLAF) {
168
robot.keyPress(KeyEvent.VK_PAGE_DOWN);
169
robot.keyRelease(KeyEvent.VK_PAGE_DOWN);
170
171
robot.waitForIdle();
172
checkSelection(9, 9, 9);
173
resetList();
174
robot.waitForIdle();
175
}
176
177
// Shift + Page Down
178
/*
179
* We really want to use robot here, but there seems to be a bug in AWT's
180
* robot implementation (see 6463168). For now, we'll invoke the action
181
* directly instead. When the bug is fixed, we'll use the following four
182
* lines instead:
183
* robot.keyPress(KeyEvent.VK_SHIFT);
184
* robot.keyPress(KeyEvent.VK_PAGE_DOWN);
185
* robot.keyRelease(KeyEvent.VK_PAGE_DOWN);
186
* robot.keyRelease(KeyEvent.VK_SHIFT);
187
*/
188
189
scrollDownExtendSelection();
190
191
robot.waitForIdle();
192
checkSelection(0, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
193
resetList();
194
robot.waitForIdle();
195
196
// Down
197
robot.keyPress(KeyEvent.VK_DOWN);
198
robot.keyRelease(KeyEvent.VK_DOWN);
199
200
robot.waitForIdle();
201
checkSelectionAL(0, 0, 0);
202
resetList();
203
robot.waitForIdle();
204
205
// L
206
robot.keyPress(KeyEvent.VK_L);
207
robot.keyRelease(KeyEvent.VK_L);
208
209
robot.waitForIdle();
210
checkSelectionAL(0, 0, 0);
211
resetList();
212
robot.waitForIdle();
213
214
// Click item 4
215
Point p = clickItem4();
216
robot.mouseMove(p.x, p.y);
217
robot.mousePress(InputEvent.BUTTON1_MASK);
218
robot.mouseRelease(InputEvent.BUTTON1_MASK);
219
220
221
robot.waitForIdle();
222
checkSelectionAL(4, 4, 4);
223
resetList();
224
robot.waitForIdle();
225
226
227
// Control + Click item 4
228
robot.keyPress(controlKey);
229
p = clickItem4();
230
robot.mouseMove(p.x, p.y);
231
robot.mousePress(InputEvent.BUTTON1_MASK);
232
robot.mouseRelease(InputEvent.BUTTON1_MASK);
233
robot.keyRelease(controlKey);
234
235
236
robot.waitForIdle();
237
checkSelectionAL(4, 4, 4);
238
resetList();
239
robot.waitForIdle();
240
241
// Shift + Click item 4
242
robot.keyPress(KeyEvent.VK_SHIFT);
243
p = clickItem4();
244
robot.mouseMove(p.x, p.y);
245
robot.mousePress(InputEvent.BUTTON1_MASK);
246
robot.mouseRelease(InputEvent.BUTTON1_MASK);
247
robot.keyRelease(KeyEvent.VK_SHIFT);
248
249
250
robot.waitForIdle();
251
checkSelectionAL(0, 4, 0, 1, 2, 3, 4);
252
resetList();
253
robot.waitForIdle();
254
255
256
// Control + Shift + Click item 4
257
robot.keyPress(controlKey);
258
robot.keyPress(KeyEvent.VK_SHIFT);
259
p = clickItem4();
260
robot.mouseMove(p.x, p.y);
261
robot.mousePress(InputEvent.BUTTON1_MASK);
262
robot.mouseRelease(InputEvent.BUTTON1_MASK);
263
robot.keyRelease(KeyEvent.VK_SHIFT);
264
robot.keyRelease(controlKey);
265
266
robot.waitForIdle();
267
checkSelectionAL(0, 4);
268
resetList();
269
robot.waitForIdle();
270
}
271
272
private static DefaultListModel getModel() {
273
DefaultListModel listModel = new DefaultListModel();
274
for (int i = 0; i < 10; i++) {
275
listModel.addElement("List Item " + i);
276
}
277
return listModel;
278
}
279
280
private static Point clickItem4() throws Exception {
281
282
final Point[] result = new Point[1];
283
SwingUtilities.invokeAndWait(new Runnable() {
284
285
@Override
286
public void run() {
287
Rectangle r = list.getCellBounds(4, 4);
288
Point p = new Point(r.x + r.width / 2, r.y + r.height / 2);
289
SwingUtilities.convertPointToScreen(p, list);
290
result[0] = p;
291
}
292
});
293
294
return result[0];
295
}
296
297
private static void resetList() throws Exception {
298
SwingUtilities.invokeAndWait(new Runnable() {
299
300
@Override
301
public void run() {
302
list.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
303
list.getSelectionModel().clearSelection();
304
setAnchorLeadNonThreadSafe();
305
}
306
});
307
}
308
309
private static void scrollDownExtendSelection() throws Exception {
310
SwingUtilities.invokeAndWait(new Runnable() {
311
312
@Override
313
public void run() {
314
list.getActionMap().get("scrollDownExtendSelection").
315
actionPerformed(new ActionEvent(list,
316
ActionEvent.ACTION_PERFORMED, null));
317
}
318
});
319
}
320
321
private static void setSelectionMode(final int selectionMode) throws Exception {
322
SwingUtilities.invokeAndWait(new Runnable() {
323
324
@Override
325
public void run() {
326
list.getSelectionModel().setSelectionMode(selectionMode);
327
setAnchorLeadNonThreadSafe();
328
}
329
});
330
}
331
332
private static void setSelectionInterval(final int index0, final int index1) throws Exception {
333
SwingUtilities.invokeAndWait(new Runnable() {
334
335
@Override
336
public void run() {
337
list.getSelectionModel().setSelectionInterval(index0, index1);
338
setAnchorLeadNonThreadSafe();
339
}
340
});
341
}
342
343
private static void setAnchorLead(final int anchorLeadValue) throws Exception {
344
SwingUtilities.invokeAndWait(new Runnable() {
345
346
@Override
347
public void run() {
348
anchorLead = anchorLeadValue;
349
setAnchorLeadNonThreadSafe();
350
}
351
});
352
}
353
354
private static void setAnchorLeadNonThreadSafe() {
355
list.getSelectionModel().setAnchorSelectionIndex(anchorLead);
356
((DefaultListSelectionModel) list.getSelectionModel()).moveLeadSelectionIndex(anchorLead);
357
}
358
359
private static void createAndShowGUI() {
360
frame = new JFrame("bug6462008");
361
frame.setSize(200, 500);
362
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
363
364
list = new JList(getModel());
365
JPanel panel = new JPanel(new BorderLayout());
366
panel.add(list);
367
frame.getContentPane().add(panel);
368
369
frame.setVisible(true);
370
frame.setLocationRelativeTo(null);
371
}
372
373
private static void checkSelection(int... sels) throws Exception {
374
checkSelectionAL(DONT_CARE, DONT_CARE, sels);
375
}
376
377
private static void checkSelectionAL(final int anchor, final int lead, final int... sels) throws Exception {
378
SwingUtilities.invokeAndWait(new Runnable() {
379
380
@Override
381
public void run() {
382
checkSelectionNonThreadSafe(anchor, lead, sels);
383
}
384
});
385
}
386
387
private static void checkSelectionNonThreadSafe(int anchor, int lead, int... sels) {
388
ListSelectionModel lsm = list.getSelectionModel();
389
390
int actualAnchor = lsm.getAnchorSelectionIndex();
391
int actualLead = lsm.getLeadSelectionIndex();
392
393
if (anchor != DONT_CARE && actualAnchor != anchor) {
394
throw new RuntimeException("anchor is " + actualAnchor + ", should be " + anchor);
395
}
396
397
if (lead != DONT_CARE && actualLead != lead) {
398
throw new RuntimeException("lead is " + actualLead + ", should be " + lead);
399
}
400
401
Arrays.sort(sels);
402
boolean[] checks = new boolean[list.getModel().getSize()];
403
for (int i : sels) {
404
checks[i] = true;
405
}
406
407
int index0 = Math.min(lsm.getMinSelectionIndex(), 0);
408
int index1 = Math.max(lsm.getMaxSelectionIndex(), list.getModel().getSize() - 1);
409
410
for (int i = index0; i <= index1; i++) {
411
if (lsm.isSelectedIndex(i)) {
412
if (i < 0 || i >= list.getModel().getSize() || !checks[i]) {
413
throw new RuntimeException(i + " is selected when it should not be");
414
}
415
} else if (i >= 0 && i < list.getModel().getSize() && checks[i]) {
416
throw new RuntimeException(i + " is supposed to be selected");
417
}
418
}
419
}
420
}
421
422