Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JList/BasicListTest.java
41149 views
1
/*
2
* Copyright (c) 2017, 2018, 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
* @test
25
* @bug 8191639 8196443
26
* @key headful
27
* @summary Verifies no NPE is thrown when pageup/down is pressed in a JList
28
* @run main BasicListTest
29
*/
30
31
import java.awt.Point;
32
import java.awt.Rectangle;
33
import java.awt.Robot;
34
import java.awt.event.InputEvent;
35
import java.awt.event.KeyEvent;
36
import javax.swing.JFrame;
37
import javax.swing.JList;
38
import javax.swing.JScrollPane;
39
import javax.swing.SwingUtilities;
40
import javax.swing.UIManager;
41
import javax.swing.UnsupportedLookAndFeelException;
42
43
class MyList extends JList {
44
// I need this to be able to unselect when clicking outside list content
45
@Override
46
public int locationToIndex(final Point location) {
47
final int n = super.locationToIndex(location);
48
//return n;
49
final Rectangle q = getCellBounds(n, n);
50
return q != null && q.contains(location)?n:-1;
51
}
52
}
53
54
public class BasicListTest {
55
private static void initComponents() {
56
f = new JFrame();
57
jScrollPane1 = new JScrollPane();
58
list1 = new MyList();
59
60
f.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
61
62
list1.setModel(new javax.swing.AbstractListModel() {
63
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
64
@Override
65
public int getSize() { return strings.length; }
66
@Override
67
public Object getElementAt(int i) { return strings[i]; }
68
});
69
jScrollPane1.setViewportView(list1);
70
71
f.getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
72
73
f.pack();
74
f.setVisible(true);
75
p = list1.getLocationOnScreen();
76
}
77
78
private static void setLookAndFeel(final UIManager.LookAndFeelInfo laf) {
79
try {
80
UIManager.setLookAndFeel(laf.getClassName());
81
} catch (ClassNotFoundException | InstantiationException |
82
UnsupportedLookAndFeelException | IllegalAccessException e) {
83
throw new RuntimeException(e);
84
}
85
}
86
87
public static void main(String args[]) throws Exception {
88
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
89
try {
90
SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
91
System.out.println("Test for LookAndFeel " + laf.getClassName());
92
SwingUtilities.invokeAndWait(() -> {
93
initComponents();
94
});
95
System.out.println("Test passed for LookAndFeel " + laf.getClassName());
96
} catch (Exception e) {
97
throw new RuntimeException(e);
98
}
99
100
Robot robot = new Robot();
101
robot.setAutoDelay(200);
102
robot.mouseMove(p.x, p.y);
103
robot.waitForIdle();
104
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
105
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
106
robot.waitForIdle();
107
robot.keyPress(KeyEvent.VK_PAGE_DOWN);
108
robot.keyRelease(KeyEvent.VK_PAGE_DOWN);
109
110
try {
111
Thread.sleep(1000);
112
} catch (InterruptedException ex) {
113
}
114
SwingUtilities.invokeAndWait(() -> {
115
f.dispose();
116
});
117
}
118
}
119
120
private static JScrollPane jScrollPane1;
121
private static MyList list1;
122
private static Point p;
123
private static JFrame f;
124
125
}
126
127