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