Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/javax/swing/JButton/4659800/SpaceKeyActivatesButton.java
66646 views
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.Robot;
25
import java.awt.event.FocusAdapter;
26
import java.awt.event.FocusEvent;
27
import java.awt.event.KeyEvent;
28
import java.util.Arrays;
29
import java.util.List;
30
import java.util.concurrent.CountDownLatch;
31
import java.util.concurrent.TimeUnit;
32
import java.util.concurrent.atomic.AtomicBoolean;
33
import javax.swing.JButton;
34
import javax.swing.JFrame;
35
import javax.swing.JPanel;
36
import javax.swing.SwingUtilities;
37
import javax.swing.UIManager;
38
import javax.swing.UnsupportedLookAndFeelException;
39
40
41
import static java.util.stream.Collectors.toList;
42
43
/*
44
* @test
45
* @key headful
46
* @bug 8281738
47
* @summary Check whether pressing <Space> key generates
48
* ActionEvent on focused Button or not.
49
* @run main SpaceKeyActivatesButton
50
*/
51
public class SpaceKeyActivatesButton {
52
53
private static volatile boolean buttonPressed;
54
private static JFrame frame;
55
private static JButton focusedButton;
56
private static CountDownLatch buttonGainedFocusLatch;
57
58
public static void main(String[] s) throws Exception {
59
runTest();
60
}
61
62
public static void runTest() throws Exception {
63
Robot robot = new Robot();
64
robot.setAutoDelay(100);
65
robot.setAutoWaitForIdle(true);
66
67
List<String> lafs = Arrays.stream(UIManager.getInstalledLookAndFeels())
68
.map(laf -> laf.getClassName())
69
.collect(toList());
70
for (String laf : lafs) {
71
buttonGainedFocusLatch = new CountDownLatch(1);
72
try {
73
buttonPressed = false;
74
System.out.println("Testing laf : " + laf);
75
AtomicBoolean lafSetSuccess = new AtomicBoolean(false);
76
SwingUtilities.invokeAndWait(() -> {
77
lafSetSuccess.set(setLookAndFeel(laf));
78
// Call createUI() only if setting laf succeeded
79
if (lafSetSuccess.get()) {
80
createUI();
81
}
82
});
83
// If setting laf failed, then just get next laf and continue
84
if (!lafSetSuccess.get()) {
85
continue;
86
}
87
robot.waitForIdle();
88
89
// Wait until the button2 gains focus.
90
if (!buttonGainedFocusLatch.await(3, TimeUnit.SECONDS)) {
91
throw new RuntimeException("Test Failed, waited too long, " +
92
"but the button can't gain focus for laf : " + laf);
93
}
94
95
robot.keyPress(KeyEvent.VK_SPACE);
96
robot.keyRelease(KeyEvent.VK_SPACE);
97
98
if (buttonPressed) {
99
System.out.println("Test Passed for laf : " + laf);
100
} else {
101
throw new RuntimeException("Test Failed, button not pressed for laf : " + laf);
102
}
103
104
} finally {
105
SwingUtilities.invokeAndWait(SpaceKeyActivatesButton::disposeFrame);
106
}
107
}
108
109
}
110
111
private static boolean setLookAndFeel(String lafName) {
112
try {
113
UIManager.setLookAndFeel(lafName);
114
} catch (UnsupportedLookAndFeelException ignored) {
115
System.out.println("Ignoring Unsupported laf : " + lafName);
116
return false;
117
} catch (ClassNotFoundException | InstantiationException
118
| IllegalAccessException e) {
119
throw new RuntimeException(e);
120
}
121
return true;
122
}
123
124
private static void createUI() {
125
frame = new JFrame();
126
JPanel panel = new JPanel();
127
panel.add(new JButton("Button1"));
128
focusedButton = new JButton("Button2");
129
focusedButton.addActionListener(e -> buttonPressed = true);
130
focusedButton.addFocusListener(new FocusAdapter() {
131
@Override
132
public void focusGained(FocusEvent e) {
133
buttonGainedFocusLatch.countDown();
134
}
135
});
136
panel.add(focusedButton);
137
138
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
139
frame.add(panel);
140
frame.pack();
141
frame.setLocationRelativeTo(null);
142
frame.setVisible(true);
143
focusedButton.requestFocusInWindow();
144
}
145
146
private static void disposeFrame() {
147
if (frame != null) {
148
frame.dispose();
149
frame = null;
150
}
151
}
152
}
153
154