Skip to content

Commit 0bbe02b

Browse files
committed
8282789: Create a regression test for the JTree usecase of JDK-4618767
Backport-of: 83a1c90433343107eaa2a7fa41b9b07f86b6ce19
1 parent 1782c76 commit 0bbe02b

File tree

1 file changed

+226
-0
lines changed

1 file changed

+226
-0
lines changed
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
/*
2+
* Copyright (c) 2002, 2022, 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+
import java.awt.Point;
25+
import java.awt.Rectangle;
26+
import java.awt.Robot;
27+
import java.awt.event.InputEvent;
28+
import java.awt.event.KeyEvent;
29+
import java.util.Arrays;
30+
import java.util.List;
31+
import java.util.concurrent.CountDownLatch;
32+
import java.util.concurrent.TimeUnit;
33+
import java.util.concurrent.atomic.AtomicBoolean;
34+
import java.util.concurrent.atomic.AtomicReference;
35+
import java.util.stream.Collectors;
36+
import javax.swing.JFrame;
37+
import javax.swing.JMenu;
38+
import javax.swing.JMenuBar;
39+
import javax.swing.JMenuItem;
40+
import javax.swing.JTree;
41+
import javax.swing.SwingUtilities;
42+
import javax.swing.UIManager;
43+
import javax.swing.UIManager.LookAndFeelInfo;
44+
import javax.swing.UnsupportedLookAndFeelException;
45+
import javax.swing.event.MenuEvent;
46+
import javax.swing.event.MenuListener;
47+
48+
import static javax.swing.UIManager.getInstalledLookAndFeels;
49+
50+
/*
51+
* @test
52+
* @key headful
53+
* @bug 4618767
54+
* @summary This test confirms that typing a letter while a JTree has focus now makes the selection
55+
* not jump to the item whose text starts with that letter if that typed letter is accompanied
56+
* by modifier keys such as ALT or CTRL(eg: ALT+F).
57+
* @run main JTreeSelectedElementTest
58+
*/
59+
public class JTreeSelectedElementTest {
60+
61+
private static final int FILE_MENU = KeyEvent.VK_F;
62+
private static JFrame frame;
63+
private static JTree tree;
64+
private static Robot robot;
65+
private static CountDownLatch menuSelectedEventLatch;
66+
67+
public static void main(String[] args) throws Exception {
68+
robot = new Robot();
69+
robot.setAutoWaitForIdle(true);
70+
robot.setAutoDelay(200);
71+
72+
final boolean isMac = System.getProperty("os.name")
73+
.toLowerCase()
74+
.contains("os x");
75+
76+
List<String> lafs = Arrays.stream(getInstalledLookAndFeels())
77+
.map(LookAndFeelInfo::getClassName)
78+
.collect(Collectors.toList());
79+
for (final String laf : lafs) {
80+
menuSelectedEventLatch = new CountDownLatch(1);
81+
try {
82+
AtomicBoolean lafSetSuccess = new AtomicBoolean(false);
83+
SwingUtilities.invokeAndWait(() -> {
84+
lafSetSuccess.set(setLookAndFeel(laf));
85+
if (lafSetSuccess.get()) {
86+
createUI();
87+
}
88+
});
89+
if (!lafSetSuccess.get()) {
90+
continue;
91+
}
92+
robot.waitForIdle();
93+
94+
// Select the node named as 'colors'
95+
Point pt = getNodeLocation(1);
96+
robot.mouseMove(pt.x, pt.y);
97+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
98+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
99+
100+
// Assertion check to verify that the selected node is 'colors'
101+
final String elementSelBefore = getCurrentNodeName();
102+
if (!"colors".equals(elementSelBefore)) {
103+
throw new RuntimeException("Test failed for " + laf
104+
+ " as the tree node selected: " + elementSelBefore
105+
+ " is not the expected one 'colors'"
106+
);
107+
}
108+
109+
// Now operate Menu using Mnemonics, different key combinations for different OSes.
110+
// For most OSes it's ALT+F; on macOS it's ALT+CNTRL+F except for Nimbus LaF.
111+
if (isMac && !laf.contains("Nimbus")) {
112+
hitKeys(KeyEvent.VK_ALT, KeyEvent.VK_CONTROL, FILE_MENU);
113+
} else {
114+
hitKeys(KeyEvent.VK_ALT, FILE_MENU);
115+
}
116+
117+
// Wait until the menu got selected.
118+
if (!menuSelectedEventLatch.await(3, TimeUnit.SECONDS)) {
119+
throw new RuntimeException("Waited too long, but can't select menu using mnemonics for " + laf);
120+
}
121+
122+
hitKeys(KeyEvent.VK_ENTER);
123+
124+
String elementSelAfter = getCurrentNodeName();
125+
126+
// As per the fix of BugID 4618767, the tree element selection should not change
127+
if (!elementSelBefore.equals(elementSelAfter)) {
128+
throw new RuntimeException("Test failed for " + laf
129+
+ " as tree.getLastSelectedPathComponent() before: " + elementSelBefore
130+
+ " not same as tree.getLastSelectedPathComponent() after pressing Enter: "
131+
+ elementSelAfter
132+
);
133+
}
134+
135+
System.out.println("Test passed for laf: " + laf);
136+
137+
} finally {
138+
SwingUtilities.invokeAndWait(JTreeSelectedElementTest::disposeFrame);
139+
}
140+
}
141+
}
142+
143+
private static void hitKeys(int... keys) {
144+
for (int key : keys) {
145+
robot.keyPress(key);
146+
}
147+
148+
for (int i = keys.length - 1; i >= 0; i--) {
149+
robot.keyRelease(keys[i]);
150+
}
151+
}
152+
153+
private static String getCurrentNodeName() throws Exception {
154+
AtomicReference<String> nodeName = new AtomicReference<>();
155+
SwingUtilities.invokeAndWait(() -> {
156+
nodeName.set(tree.getLastSelectedPathComponent().toString().trim());
157+
});
158+
return nodeName.get();
159+
}
160+
161+
private static Point getNodeLocation(int rowCount) throws Exception {
162+
AtomicReference<Point> treeNodeLoc = new AtomicReference<>();
163+
SwingUtilities.invokeAndWait(() -> {
164+
final Point locationOnScreen = tree.getLocationOnScreen();
165+
Rectangle rt = tree.getPathBounds(tree.getPathForRow(rowCount));
166+
locationOnScreen.translate(rt.x + rt.width / 2, rt.y + rt.height / 2);
167+
treeNodeLoc.set(locationOnScreen);
168+
});
169+
return treeNodeLoc.get();
170+
}
171+
172+
private static void createUI() {
173+
frame = new JFrame();
174+
tree = new JTree();
175+
JMenu menu = new JMenu("File");
176+
menu.setMnemonic(FILE_MENU);
177+
JMenuItem menuItem = new JMenuItem("Dummy");
178+
menu.add(menuItem);
179+
menu.addMenuListener(new MenuListener() {
180+
@Override
181+
public void menuSelected(MenuEvent e) {
182+
menuSelectedEventLatch.countDown();
183+
}
184+
185+
@Override
186+
public void menuDeselected(MenuEvent e) {
187+
}
188+
189+
@Override
190+
public void menuCanceled(MenuEvent e) {
191+
}
192+
});
193+
194+
JMenuBar menuBar = new JMenuBar();
195+
menuBar.add(menu);
196+
197+
frame.setJMenuBar(menuBar);
198+
frame.setContentPane(tree);
199+
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
200+
frame.pack();
201+
frame.setAlwaysOnTop(true);
202+
frame.setLocationRelativeTo(null);
203+
frame.setVisible(true);
204+
}
205+
206+
private static boolean setLookAndFeel(String lafName) {
207+
try {
208+
UIManager.setLookAndFeel(lafName);
209+
} catch (UnsupportedLookAndFeelException ignored) {
210+
System.out.println("Ignoring Unsupported L&F: " + lafName);
211+
return false;
212+
} catch (ClassNotFoundException | InstantiationException
213+
| IllegalAccessException e) {
214+
throw new RuntimeException(e);
215+
}
216+
return true;
217+
}
218+
219+
private static void disposeFrame() {
220+
if (frame != null) {
221+
frame.dispose();
222+
frame = null;
223+
}
224+
}
225+
226+
}

0 commit comments

Comments
 (0)