Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JTree/4927934/bug4927934.java
38854 views
1
/*
2
* Copyright (c) 2013, 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
/* @test
24
@bug 4927934
25
@summary JTree traversal is unlike Native windows tree traversal
26
@author Andrey Pikalev
27
@run main bug4927934
28
*/
29
30
import javax.swing.*;
31
import javax.swing.event.*;
32
import javax.swing.tree.*;
33
import java.awt.*;
34
import java.awt.event.*;
35
import java.lang.reflect.InvocationTargetException;
36
37
public class bug4927934 implements TreeSelectionListener, TreeExpansionListener, FocusListener {
38
39
final static Object listener = new bug4927934();
40
41
static boolean focusGained = false;
42
public static boolean selectionChanged = false;
43
public static boolean treeExpanded = false;
44
public static boolean treeCollapsed = false;
45
46
static JFrame frame;
47
static JTree tree;
48
static Robot robot;
49
50
public static void main(String args[]) throws Exception {
51
UIManager.setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel());
52
53
robot = new Robot();
54
robot.setAutoDelay(50);
55
56
SwingUtilities.invokeAndWait(new Runnable() {
57
public void run() {
58
frame = new JFrame();
59
60
DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
61
createNodes(root);
62
tree = new JTree(root);
63
JScrollPane scrollPane = new JScrollPane(tree);
64
frame.getContentPane().add(scrollPane);
65
66
tree.addFocusListener((FocusListener)listener);
67
tree.addTreeSelectionListener((TreeSelectionListener)listener);
68
tree.addTreeExpansionListener((TreeExpansionListener)listener);
69
70
frame.setSize(300, 300);
71
frame.setVisible(true);
72
}
73
});
74
75
robot.waitForIdle();
76
Thread.sleep(1000);
77
78
SwingUtilities.invokeLater(new Runnable() {
79
public void run() {
80
tree.requestFocus();
81
}
82
});
83
84
synchronized(listener) {
85
if (!focusGained) {
86
System.out.println("waiting focusGained...");
87
try {
88
listener.wait(10000);
89
} catch (InterruptedException e) {
90
e.printStackTrace();
91
}
92
}
93
}
94
95
// GO TO RIGHT
96
selectionChanged = false;
97
hitKey(KeyEvent.VK_RIGHT);
98
robot.waitForIdle();
99
if (!checkSelectionChanged(tree, 0)) {
100
throw new RuntimeException("Root should be selected");
101
}
102
103
selectionChanged = false;
104
hitKey(KeyEvent.VK_RIGHT);
105
robot.waitForIdle();
106
if (!checkSelectionChanged(tree, 1)) {
107
throw new RuntimeException("Node should be selected");
108
}
109
110
treeExpanded = false;
111
hitKey(KeyEvent.VK_RIGHT);
112
robot.waitForIdle();
113
if (!isTreeExpanded()) {
114
throw new RuntimeException("Node should be expanded");
115
}
116
117
selectionChanged = false;
118
hitKey(KeyEvent.VK_RIGHT);
119
robot.waitForIdle();
120
if (!checkSelectionChanged(tree, 2)) {
121
throw new RuntimeException("Leaf1 should be selected");
122
}
123
124
selectionChanged = false;
125
hitKey(KeyEvent.VK_RIGHT);
126
robot.waitForIdle();
127
if (!checkSelectionChanged(tree, 2)) {
128
throw new RuntimeException("Leaf1 should be selected");
129
}
130
131
// GO TO LEFT
132
selectionChanged = false;
133
hitKey(KeyEvent.VK_LEFT);
134
robot.waitForIdle();
135
if (!checkSelectionChanged(tree, 1)) {
136
throw new RuntimeException("Node should be selected");
137
}
138
139
treeCollapsed = false;
140
hitKey(KeyEvent.VK_LEFT);
141
if (!isTreeCollapsed()) {
142
throw new RuntimeException("Node should be collapsed");
143
}
144
145
selectionChanged = false;
146
hitKey(KeyEvent.VK_LEFT);
147
robot.waitForIdle();
148
if (!checkSelectionChanged(tree, 0)) {
149
throw new RuntimeException("Root should be selected");
150
}
151
152
treeCollapsed = false;
153
hitKey(KeyEvent.VK_LEFT);
154
robot.waitForIdle();
155
if (!isTreeCollapsed()) {
156
throw new RuntimeException("Root should be collapsed");
157
}
158
}
159
160
161
synchronized public void focusLost(FocusEvent e) {
162
}
163
164
synchronized public void focusGained(FocusEvent e) {
165
focusGained = true;
166
System.out.println("focusGained");
167
listener.notifyAll();
168
}
169
170
private static void createNodes(DefaultMutableTreeNode root) {
171
DefaultMutableTreeNode node = new DefaultMutableTreeNode("Node");
172
node.add(new DefaultMutableTreeNode("Leaf1"));
173
node.add(new DefaultMutableTreeNode("Leaf2"));
174
root.add(node);
175
root.add(new DefaultMutableTreeNode("Leaf3"));
176
}
177
178
synchronized public void valueChanged(TreeSelectionEvent e) {
179
selectionChanged = true;
180
System.out.println("selectionChanged");
181
notifyAll();
182
}
183
184
synchronized public void treeCollapsed(TreeExpansionEvent e) {
185
System.out.println("treeCollapsed");
186
treeCollapsed = true;
187
notifyAll();
188
}
189
190
synchronized public void treeExpanded(TreeExpansionEvent e) {
191
System.out.println("treeExpanded");
192
treeExpanded = true;
193
notifyAll();
194
}
195
196
private static void hitKey(int key) {
197
System.out.println("key " + key + " pressed");
198
robot.keyPress(key);
199
robot.keyRelease(key);
200
}
201
202
private static boolean checkSelectionChanged(JTree tree, int shouldBeSel) {
203
synchronized(listener) {
204
if (!selectionChanged) {
205
System.out.println("waiting for selectionChanged...");
206
try {
207
listener.wait(5000);
208
} catch (InterruptedException e) {
209
e.printStackTrace();
210
}
211
}
212
}
213
int selRow = tree.getLeadSelectionRow();
214
System.out.println("Selected row: " + selRow);
215
return selRow == shouldBeSel;
216
}
217
218
private static boolean isTreeExpanded() {
219
synchronized(listener) {
220
if (!treeExpanded) {
221
System.out.println("waiting for treeExpanded...");
222
try {
223
listener.wait(5000);
224
} catch (InterruptedException e) {
225
e.printStackTrace();
226
}
227
}
228
}
229
return treeExpanded;
230
}
231
232
private static boolean isTreeCollapsed() {
233
synchronized(listener) {
234
if (!treeCollapsed) {
235
System.out.println("waiting for treeCollapsed...");
236
try {
237
listener.wait(5000);
238
} catch (InterruptedException e) {
239
e.printStackTrace();
240
}
241
}
242
}
243
return treeCollapsed;
244
}
245
}
246
247