Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JRadioButton/8033699/bug8033699.java
41153 views
1
/*
2
* Copyright (c) 2014, 2019, 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
/*
25
* @test
26
* @key headful
27
* @library ../../regtesthelpers
28
* @build Util
29
* @bug 8033699 8154043 8167160 8208640 8226892
30
* @summary Incorrect radio button behavior when pressing tab key
31
* @run main bug8033699
32
*/
33
import java.awt.KeyboardFocusManager;
34
import java.awt.Robot;
35
import java.awt.event.ActionListener;
36
import java.awt.event.KeyEvent;
37
import java.util.logging.Level;
38
import java.util.logging.Logger;
39
import javax.swing.BorderFactory;
40
import javax.swing.BoxLayout;
41
import javax.swing.ButtonGroup;
42
import javax.swing.JButton;
43
import javax.swing.JFrame;
44
import javax.swing.JPanel;
45
import javax.swing.JRadioButton;
46
import javax.swing.SwingUtilities;
47
import javax.swing.UIManager;
48
import javax.swing.UnsupportedLookAndFeelException;
49
50
public class bug8033699 {
51
52
private static JFrame mainFrame;
53
private static Robot robot;
54
private static JButton btnStart;
55
private static JButton btnEnd;
56
private static JButton btnMiddle;
57
private static JRadioButton radioBtn1;
58
private static JRadioButton radioBtn2;
59
private static JRadioButton radioBtn3;
60
private static JRadioButton radioBtnSingle;
61
62
public static void main(String args[]) throws Throwable {
63
SwingUtilities.invokeAndWait(() -> {
64
changeLAF();
65
createAndShowGUI();
66
});
67
68
robot = new Robot();
69
Thread.sleep(100);
70
71
robot.setAutoDelay(100);
72
73
// tab key test grouped radio button
74
runTest1();
75
76
// tab key test non-grouped radio button
77
runTest2();
78
79
// shift tab key test grouped and non grouped radio button
80
runTest3();
81
82
// left/up key test in grouped radio button
83
runTest4();
84
85
// down/right key test in grouped radio button
86
runTest5();
87
88
// tab from radio button in group to next component in the middle of button group layout
89
runTest6();
90
91
// tab to radio button in group from component in the middle of button group layout
92
runTest7();
93
94
// down key circle back to first button in grouped radio button
95
runTest8();
96
97
// Verify that ActionListener is called when a RadioButton is selected using arrow key.
98
runTest9();
99
100
SwingUtilities.invokeAndWait(() -> mainFrame.dispose());
101
}
102
103
private static void changeLAF() {
104
String currentLAF = UIManager.getLookAndFeel().toString();
105
System.out.println(currentLAF);
106
currentLAF = currentLAF.toLowerCase();
107
if (currentLAF.contains("nimbus")) {
108
try {
109
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
110
} catch (Exception ex) {
111
ex.printStackTrace();
112
}
113
}
114
}
115
116
private static void createAndShowGUI() {
117
mainFrame = new JFrame("Bug 8033699 - 8 Tests for Grouped/Non Group Radio Buttons");
118
btnStart = new JButton("Start");
119
btnEnd = new JButton("End");
120
btnMiddle = new JButton("Middle");
121
122
JPanel box = new JPanel();
123
box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
124
box.setBorder(BorderFactory.createTitledBorder("Grouped Radio Buttons"));
125
radioBtn1 = new JRadioButton("A");
126
radioBtn2 = new JRadioButton("B");
127
radioBtn3 = new JRadioButton("C");
128
129
ButtonGroup btnGrp = new ButtonGroup();
130
btnGrp.add(radioBtn1);
131
btnGrp.add(radioBtn2);
132
btnGrp.add(radioBtn3);
133
radioBtn1.setSelected(true);
134
135
box.add(radioBtn1);
136
box.add(radioBtn2);
137
box.add(btnMiddle);
138
box.add(radioBtn3);
139
140
radioBtnSingle = new JRadioButton("Not Grouped");
141
radioBtnSingle.setSelected(true);
142
143
mainFrame.getContentPane().add(btnStart);
144
mainFrame.getContentPane().add(box);
145
mainFrame.getContentPane().add(radioBtnSingle);
146
mainFrame.getContentPane().add(btnEnd);
147
148
mainFrame.getRootPane().setDefaultButton(btnStart);
149
btnStart.requestFocus();
150
151
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
152
mainFrame.setLayout(new BoxLayout(mainFrame.getContentPane(), BoxLayout.Y_AXIS));
153
154
mainFrame.setSize(300, 300);
155
mainFrame.setLocation(200, 200);
156
mainFrame.setVisible(true);
157
mainFrame.toFront();
158
}
159
160
// Radio button Group as a single component when traversing through tab key
161
private static void runTest1() throws Exception {
162
hitKey(robot, KeyEvent.VK_TAB);
163
hitKey(robot, KeyEvent.VK_TAB);
164
hitKey(robot, KeyEvent.VK_TAB);
165
166
SwingUtilities.invokeAndWait(() -> {
167
if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtnSingle) {
168
System.out.println("Radio Button Group Go To Next Component through Tab Key failed");
169
throw new RuntimeException("Focus is not on Radio Button Single as Expected");
170
}
171
});
172
}
173
174
// Non-Grouped Radio button as a single component when traversing through tab key
175
private static void runTest2() throws Exception {
176
hitKey(robot, KeyEvent.VK_TAB);
177
SwingUtilities.invokeAndWait(() -> {
178
if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != btnEnd) {
179
System.out.println("Non Grouped Radio Button Go To Next Component through Tab Key failed");
180
throw new RuntimeException("Focus is not on Button End as Expected");
181
}
182
});
183
}
184
185
// Non-Grouped Radio button and Group Radio button as a single component when traversing through shift-tab key
186
private static void runTest3() throws Exception {
187
hitKey(robot, KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);
188
hitKey(robot, KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);
189
hitKey(robot, KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);
190
SwingUtilities.invokeAndWait(() -> {
191
if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn1) {
192
System.out.println("Radio button Group/Non Grouped Radio Button SHIFT-Tab Key Test failed");
193
throw new RuntimeException("Focus is not on Radio Button A as Expected");
194
}
195
});
196
}
197
198
// Using arrow key to move focus in radio button group
199
private static void runTest4() throws Exception {
200
hitKey(robot, KeyEvent.VK_DOWN);
201
hitKey(robot, KeyEvent.VK_RIGHT);
202
SwingUtilities.invokeAndWait(() -> {
203
if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn3) {
204
System.out.println("Radio button Group UP/LEFT Arrow Key Move Focus Failed");
205
throw new RuntimeException("Focus is not on Radio Button C as Expected");
206
}
207
});
208
}
209
210
private static void runTest5() throws Exception {
211
hitKey(robot, KeyEvent.VK_UP);
212
hitKey(robot, KeyEvent.VK_LEFT);
213
SwingUtilities.invokeAndWait(() -> {
214
if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn1) {
215
System.out.println("Radio button Group Left/Up Arrow Key Move Focus Failed");
216
throw new RuntimeException("Focus is not on Radio Button A as Expected");
217
}
218
});
219
}
220
221
private static void runTest6() throws Exception {
222
hitKey(robot, KeyEvent.VK_UP);
223
hitKey(robot, KeyEvent.VK_UP);
224
SwingUtilities.invokeAndWait(() -> {
225
if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn2) {
226
System.out.println("Radio button Group Circle Back To First Button Test");
227
throw new RuntimeException("Focus is not on Radio Button B as Expected");
228
}
229
});
230
}
231
232
private static void runTest7() throws Exception {
233
hitKey(robot, KeyEvent.VK_TAB);
234
SwingUtilities.invokeAndWait(() -> {
235
if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != btnMiddle) {
236
System.out.println("Separate Component added in button group layout");
237
throw new RuntimeException("Focus is not on Middle Button as Expected");
238
}
239
});
240
}
241
242
private static void runTest8() throws Exception {
243
hitKey(robot, KeyEvent.VK_TAB);
244
SwingUtilities.invokeAndWait(() -> {
245
if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtnSingle) {
246
System.out.println("Separate Component added in button group layout");
247
throw new RuntimeException("Focus is not on Radio Button Single as Expected");
248
}
249
});
250
}
251
252
private static Boolean actRB1 = false;
253
private static Boolean actRB2 = false;
254
private static Boolean actRB3 = false;
255
256
// JDK-8226892: Verify that ActionListener is called when a RadioButton is selected using arrow key.
257
private static void runTest9() throws Exception {
258
SwingUtilities.invokeAndWait(() -> {
259
radioBtn1.setSelected(true);
260
radioBtn1.requestFocusInWindow();
261
});
262
263
ActionListener actLrRB1 = e -> actRB1 = true;
264
ActionListener actLrRB2 = e -> actRB2 = true;
265
ActionListener actLrRB3 = e -> actRB3 = true;
266
267
radioBtn1.addActionListener(actLrRB1);
268
radioBtn2.addActionListener(actLrRB2);
269
radioBtn3.addActionListener(actLrRB3);
270
271
hitKey(robot, KeyEvent.VK_DOWN);
272
hitKey(robot, KeyEvent.VK_DOWN);
273
hitKey(robot, KeyEvent.VK_DOWN);
274
275
String failMessage = "ActionListener not invoked when selected using arrow key.";
276
if (!actRB2) {
277
throw new RuntimeException("RadioButton 2: " + failMessage);
278
}
279
if (!actRB3) {
280
throw new RuntimeException("RadioButton 3: " + failMessage);
281
}
282
if (!actRB1) {
283
throw new RuntimeException("RadioButton 1: " + failMessage);
284
}
285
286
radioBtn1.removeActionListener(actLrRB1);
287
radioBtn2.removeActionListener(actLrRB2);
288
radioBtn3.removeActionListener(actLrRB3);
289
}
290
291
private static void hitKey(Robot robot, int keycode) {
292
robot.keyPress(keycode);
293
robot.keyRelease(keycode);
294
robot.waitForIdle();
295
}
296
297
private static void hitKey(Robot robot, int mode, int keycode) {
298
robot.keyPress(mode);
299
robot.keyPress(keycode);
300
robot.keyRelease(mode);
301
robot.keyRelease(keycode);
302
robot.waitForIdle();
303
}
304
}
305
306