Skip to content

Commit 66b2c3b

Browse files
Manukumar V Saivanov-jdk
Manukumar V S
authored andcommittedFeb 4, 2022
8280948: [TESTBUG] Write a regression test for JDK-4659800
Reviewed-by: aivanov
1 parent 7207f2a commit 66b2c3b

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (c) 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.event.KeyEvent;
25+
import java.awt.Robot;
26+
import java.util.Arrays;
27+
import java.util.List;
28+
29+
import javax.swing.JButton;
30+
import javax.swing.JFrame;
31+
import javax.swing.JPanel;
32+
import javax.swing.SwingUtilities;
33+
import javax.swing.UIManager;
34+
import javax.swing.UnsupportedLookAndFeelException;
35+
36+
import static java.util.stream.Collectors.toList;
37+
38+
/*
39+
* @test
40+
* @key headful
41+
* @requires (os.family == "windows")
42+
* @bug 4659800
43+
* @summary Check whether pressing <Enter> key generates
44+
* ActionEvent on focused Button or not. This is applicable only for
45+
* WindowsLookAndFeel and WindowsClassicLookAndFeel.
46+
* @run main EnterKeyActivatesButton
47+
*/
48+
public class EnterKeyActivatesButton {
49+
private static volatile boolean buttonPressed;
50+
private static JFrame frame;
51+
52+
public static void main(String[] s) throws Exception {
53+
runTest();
54+
}
55+
56+
private static void setLookAndFeel(String lafName) {
57+
try {
58+
UIManager.setLookAndFeel(lafName);
59+
} catch (UnsupportedLookAndFeelException ignored) {
60+
System.out.println("Ignoring Unsupported L&F: " + lafName);
61+
} catch (ClassNotFoundException | InstantiationException
62+
| IllegalAccessException e) {
63+
throw new RuntimeException(e);
64+
}
65+
}
66+
67+
private static void disposeFrame() {
68+
if (frame != null) {
69+
frame.dispose();
70+
frame = null;
71+
}
72+
}
73+
74+
private static void createUI() {
75+
frame = new JFrame();
76+
JPanel panel = new JPanel();
77+
JButton focusedButton = new JButton("Button1");
78+
focusedButton.addActionListener(e -> buttonPressed = true);
79+
panel.add(focusedButton);
80+
panel.add(new JButton("Button2"));
81+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
82+
frame.add(panel);
83+
frame.pack();
84+
frame.setLocationRelativeTo(null);
85+
frame.setVisible(true);
86+
focusedButton.requestFocusInWindow();
87+
}
88+
89+
public static void runTest() throws Exception {
90+
Robot robot = new Robot();
91+
robot.setAutoDelay(100);
92+
// Consider only Windows and Windows Classic LnFs.
93+
List<String> winlafs = Arrays.stream(UIManager.getInstalledLookAndFeels())
94+
.filter(laf -> laf.getName().startsWith("Windows"))
95+
.map(laf -> laf.getClassName())
96+
.collect(toList());
97+
98+
for (String laf : winlafs) {
99+
try {
100+
buttonPressed = false;
101+
System.out.println("Testing L&F: " + laf);
102+
SwingUtilities.invokeAndWait(() -> {
103+
setLookAndFeel(laf);
104+
createUI();
105+
});
106+
107+
robot.waitForIdle();
108+
robot.keyPress(KeyEvent.VK_ENTER);
109+
robot.keyRelease(KeyEvent.VK_ENTER);
110+
robot.waitForIdle();
111+
112+
if (buttonPressed) {
113+
System.out.println("Test Passed for L&F: " + laf);
114+
} else {
115+
throw new RuntimeException("Test Failed, button not pressed for L&F: " + laf);
116+
}
117+
118+
} finally {
119+
SwingUtilities.invokeAndWait(EnterKeyActivatesButton::disposeFrame);
120+
}
121+
}
122+
123+
}
124+
}

0 commit comments

Comments
 (0)
Please sign in to comment.