Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/javax/swing/JLabel/4138746/JLabelMnemonicsTest.java
66646 views
1
/*
2
* Copyright (c) 2001, 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.GridLayout;
25
import java.awt.Robot;
26
import java.awt.event.FocusAdapter;
27
import java.awt.event.FocusEvent;
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.AtomicInteger;
35
import java.util.stream.Collectors;
36
import javax.swing.AbstractButton;
37
import javax.swing.JButton;
38
import javax.swing.JFrame;
39
import javax.swing.JLabel;
40
import javax.swing.JPanel;
41
import javax.swing.SwingUtilities;
42
import javax.swing.UIManager;
43
import javax.swing.UIManager.LookAndFeelInfo;
44
import javax.swing.UnsupportedLookAndFeelException;
45
46
import static javax.swing.UIManager.getInstalledLookAndFeels;
47
48
/*
49
* @test
50
* @key headful
51
* @bug 4138746
52
* @summary This testcase tests RFE-4138746 request, verifies the case-sensitive
53
* setting of Mnemonics to AbstractButton & JLabel.
54
* @run main JLabelMnemonicsTest
55
*/
56
public class JLabelMnemonicsTest {
57
58
private static JButton button1;
59
private static JButton button2;
60
private static JLabel label1;
61
private static JLabel label2;
62
private static JPanel panel;
63
private static Robot robot;
64
private static boolean result;
65
private static CountDownLatch focusGainedLatch;
66
private static JFrame frame;
67
68
public static void main(String[] args) throws Exception {
69
final boolean isMac =
70
System.getProperty("os.name").toLowerCase().contains("os x");
71
72
robot = new Robot();
73
robot.setAutoWaitForIdle(true);
74
robot.setAutoDelay(200);
75
List<String> lafs = Arrays.stream(getInstalledLookAndFeels())
76
.map(LookAndFeelInfo::getClassName)
77
.collect(Collectors.toList());
78
for (final String laf : lafs) {
79
try {
80
result = true;
81
focusGainedLatch = new CountDownLatch(1);
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
// Verifier 1: Verifies if getDisplayedMnemonicIndex() returns the
95
// right index set with setDisplayedMnemonicIndex method for JButton
96
if (getDisplayedMnemonicIndex(button1) == 5 &&
97
getDisplayedMnemonicIndex(button2) == 0) {
98
System.out.println("Verifier 1 Passed");
99
} else {
100
System.out.println(
101
"Verifier 1 Failed, testing JButton failed");
102
result = false;
103
}
104
105
// Verifier 2: Verifies that, on setting displayedMnemonicIndex to
106
// -1, the component can be still accessed with the mnemonic set
107
if (isMac) {
108
hitKeys(KeyEvent.VK_ALT, KeyEvent.VK_CONTROL,
109
KeyEvent.VK_V);
110
} else {
111
hitKeys(KeyEvent.VK_ALT, KeyEvent.VK_V);
112
}
113
if (focusGainedLatch.await(3, TimeUnit.SECONDS)) {
114
System.out.println("Verifier 2 Passed");
115
} else {
116
System.out.println(
117
"Verifier 2 Failed, Waited too long, but Button3 " +
118
"has not yet gained focus in " + laf);
119
result = false;
120
}
121
122
// Verifier 3: Testing JLabel: Verifies if
123
// getDisplayedMnemonicIndex() returns the right
124
// index set with setDisplayedMnemonicIndex method for JLabel
125
if (getDisplayedMnemonicIndex(label1) == 5 &&
126
getDisplayedMnemonicIndex(label2) == 0) {
127
System.out.println("Verifier 3 Passed");
128
} else {
129
System.out.println("Verifier 3, testing JLabel Failed");
130
result = false;
131
}
132
133
if (result) {
134
System.out.println("Test Passed in " + laf);
135
} else {
136
throw new RuntimeException(
137
"Test Failed, as one or more verifiers failed in " +
138
laf);
139
}
140
} finally {
141
SwingUtilities.invokeAndWait(JLabelMnemonicsTest::disposeFrame);
142
}
143
}
144
}
145
146
private static int getDisplayedMnemonicIndex(JLabel jLabel)
147
throws Exception {
148
final AtomicInteger index = new AtomicInteger();
149
SwingUtilities.invokeAndWait(
150
() -> index.set(jLabel.getDisplayedMnemonicIndex()));
151
return index.get();
152
}
153
154
private static int getDisplayedMnemonicIndex(AbstractButton button)
155
throws Exception {
156
final AtomicInteger index = new AtomicInteger();
157
SwingUtilities.invokeAndWait(
158
() -> index.set(button.getDisplayedMnemonicIndex()));
159
return index.get();
160
}
161
162
private static void hitKeys(int... keys) {
163
for (int key : keys) {
164
robot.keyPress(key);
165
}
166
167
for (int i = keys.length - 1; i >= 0; i--) {
168
robot.keyRelease(keys[i]);
169
}
170
}
171
172
private static void createUI() {
173
frame = new JFrame();
174
panel = new JPanel();
175
panel.setLayout(new GridLayout(2, 3));
176
177
button1 = new JButton("Save As");
178
button1.setMnemonic(KeyEvent.VK_A);
179
button1.setDisplayedMnemonicIndex(5);
180
panel.add(button1);
181
182
button2 = new JButton("save AS");
183
button2.setMnemonic(KeyEvent.VK_S);
184
panel.add(button2);
185
186
JButton button3 = new JButton("Save As");
187
button3.setMnemonic(KeyEvent.VK_V);
188
button3.setDisplayedMnemonicIndex(-1);
189
panel.add(button3);
190
button3.addFocusListener(new FocusAdapter() {
191
public void focusGained(FocusEvent fe) {
192
System.out.println("FocusGained on Button3");
193
focusGainedLatch.countDown();
194
}
195
});
196
197
label1 = new JLabel("Save As");
198
label1.setDisplayedMnemonic(KeyEvent.VK_A);
199
label1.setDisplayedMnemonicIndex(5);
200
panel.add(label1);
201
202
label2 = new JLabel("save AS");
203
label2.setDisplayedMnemonic(KeyEvent.VK_S);
204
panel.add(label2);
205
206
frame.add(panel);
207
frame.setLocationRelativeTo(null);
208
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
209
frame.pack();
210
frame.setVisible(true);
211
}
212
213
private static boolean setLookAndFeel(String lafName) {
214
try {
215
UIManager.setLookAndFeel(lafName);
216
} catch (UnsupportedLookAndFeelException ignored) {
217
System.out.println("Ignoring Unsupported L&F: " + lafName);
218
return false;
219
} catch (ClassNotFoundException | InstantiationException
220
| IllegalAccessException e) {
221
throw new RuntimeException(e);
222
}
223
return true;
224
}
225
226
private static void disposeFrame() {
227
if (frame != null) {
228
frame.dispose();
229
frame = null;
230
}
231
}
232
233
}
234
235