File: bug8033699.java

package info (click to toggle)
openjdk-25 25~32ea-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 825,280 kB
  • sloc: java: 5,584,902; cpp: 1,333,941; xml: 1,321,242; ansic: 487,993; asm: 404,003; objc: 21,088; sh: 15,102; javascript: 13,265; python: 8,319; makefile: 2,515; perl: 357; awk: 351; pascal: 103; exp: 83; sed: 72; jsp: 24
file content (359 lines) | stat: -rw-r--r-- 13,115 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
 * Copyright (c) 2014, 2025, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @key headful
 * @bug 8033699 8154043 8167160 8208640 8226892
 * @summary  Incorrect radio button behavior when pressing tab key
 * @run main bug8033699
 */

import java.awt.KeyboardFocusManager;
import java.awt.Robot;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class bug8033699 {
    private static JFrame mainFrame;
    private static Robot robot;
    private static JButton btnStart;
    private static JButton btnEnd;
    private static JButton btnMiddle;
    private static JRadioButton radioBtn1;
    private static JRadioButton radioBtn2;
    private static JRadioButton radioBtn3;
    private static JRadioButton radioBtnSingle;
    private static KeyboardFocusManager focusManager;

    public static void main(String[] args) throws Throwable {
        robot = new Robot();

        SwingUtilities.invokeAndWait(() ->
                focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager());

        UIManager.LookAndFeelInfo[] lafs = UIManager.getInstalledLookAndFeels();
        for (UIManager.LookAndFeelInfo laf : lafs) {
            testLaF(laf);
        }
    }

    private static void testLaF(UIManager.LookAndFeelInfo laf) throws Exception {
        try {
            System.out.println("Testing LaF: " + laf.getName());
            SwingUtilities.invokeAndWait(() -> {
                setLookAndFeel(laf);
                createAndShowGUI();
            });

            robot.waitForIdle();
            robot.delay(1000);

            // tab key test grouped radio button
            runTest1();
            robot.delay(100);

            // tab key test non-grouped radio button
            runTest2();
            robot.delay(100);

            // shift tab key test grouped and non-grouped radio button
            runTest3();
            robot.delay(100);

            // left/up key test in grouped radio button
            runTest4();
            robot.delay(100);

            // down/right key test in grouped radio button
            runTest5();
            robot.delay(100);

            // tab from radio button in group to next component in the middle of
            // button group layout
            runTest6();
            robot.delay(100);

            // tab to radio button in group from component in the middle of
            // button group layout
            runTest7();
            robot.delay(100);

            // down key circle back to first button in grouped radio button
            runTest8();
            robot.delay(100);

            // Verify that ActionListener is called when a RadioButton is
            // selected using arrow key
            runTest9();
            robot.delay(100);
        } catch (Exception e) {
            Throwable cause = e.getCause();
            throw new RuntimeException("Error testing LaF: " + laf.getName()
                    + (cause != null ? " - " + cause.getMessage() : ""),
                    e);
        } finally {
            SwingUtilities.invokeAndWait(() -> {
                if (mainFrame != null) {
                    mainFrame.dispose();
                    mainFrame = null;
                }
            });
        }
    }

    private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {
        try {
            UIManager.setLookAndFeel(laf.getClassName());
        } catch (ClassNotFoundException | InstantiationException |
                 IllegalAccessException | UnsupportedLookAndFeelException e) {
            System.err.println("Error setting LaF: " + laf.getName());
            throw new RuntimeException("Failed to set look and feel", e);
        }
    }

    private static void createAndShowGUI() {
        mainFrame = new JFrame("Radio Button Focus Tests");
        btnStart = new JButton("Start");
        btnEnd = new JButton("End");
        btnMiddle = new JButton("Middle");

        Box box = Box.createVerticalBox();
        box.setBorder(BorderFactory.createTitledBorder("Grouped Radio Buttons"));
        radioBtn1 = new JRadioButton("A");
        radioBtn2 = new JRadioButton("B");
        radioBtn3 = new JRadioButton("C");

        ButtonGroup btnGrp = new ButtonGroup();
        btnGrp.add(radioBtn1);
        btnGrp.add(radioBtn2);
        btnGrp.add(radioBtn3);
        radioBtn1.setSelected(true);

        box.add(radioBtn1);
        box.add(radioBtn2);
        box.add(btnMiddle);
        box.add(radioBtn3);

        radioBtnSingle = new JRadioButton("Not Grouped");
        radioBtnSingle.setSelected(true);

        Box mainBox = Box.createVerticalBox();
        mainBox.add(btnStart);
        mainBox.add(box);
        mainBox.add(radioBtnSingle);
        mainBox.add(btnEnd);

        mainFrame.add(mainBox);
        mainFrame.getRootPane().setDefaultButton(btnStart);
        btnStart.requestFocus();

        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        mainFrame.setSize(300, 300);
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setVisible(true);
        mainFrame.toFront();
    }

    // Radio button Group as a single component when traversing through tab key
    private static void runTest1() throws Exception {
        hitKey(KeyEvent.VK_TAB);
        hitKey(KeyEvent.VK_TAB);
        hitKey(KeyEvent.VK_TAB);

        SwingUtilities.invokeAndWait(() -> {
            if (focusManager.getFocusOwner() != radioBtnSingle) {
                System.out.println("Radio Button Group Go To "
                                   + "Next Component through Tab Key failed");
                throw new RuntimeException("Focus is not on "
                                           + "Radio Button Single as Expected");
            }
        });
    }

    // Non-Grouped Radio button as a single component when traversing through
    // tab key
    private static void runTest2() throws Exception {
        hitKey(KeyEvent.VK_TAB);
        SwingUtilities.invokeAndWait(() -> {
            if (focusManager.getFocusOwner() != btnEnd) {
                System.out.println("Non Grouped Radio Button Go To "
                                   + "Next Component through Tab Key failed");
                throw new RuntimeException("Focus is not on Button End "
                                            + "as Expected");
            }
        });
    }

    // Non-Grouped Radio button and Group Radio button as a single component
    // when traversing through shift-tab key
    private static void runTest3() throws Exception {
        hitKey(KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);
        hitKey(KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);
        hitKey(KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);
        SwingUtilities.invokeAndWait(() -> {
            if (focusManager.getFocusOwner() != radioBtn1) {
                System.out.println("Radio button Group/Non Grouped "
                                   + "Radio Button SHIFT-Tab Key Test failed");
                throw new RuntimeException("Focus is not on Radio Button A "
                                           + "as Expected");
            }
        });
    }

    // Using arrow key to move focus in radio button group
    private static void runTest4() throws Exception {
        hitKey(KeyEvent.VK_DOWN);
        hitKey(KeyEvent.VK_RIGHT);
        SwingUtilities.invokeAndWait(() -> {
            if (focusManager.getFocusOwner() != radioBtn3) {
                System.out.println("Radio button Group UP/LEFT Arrow Key "
                                   + "Move Focus Failed");
                throw new RuntimeException("Focus is not on Radio Button C "
                                           + "as Expected");
            }
        });
    }

    private static void runTest5() throws Exception {
        hitKey(KeyEvent.VK_UP);
        hitKey(KeyEvent.VK_LEFT);
        SwingUtilities.invokeAndWait(() -> {
            if (focusManager.getFocusOwner() != radioBtn1) {
                System.out.println("Radio button Group Left/Up Arrow Key "
                                   + "Move Focus Failed");
                throw new RuntimeException("Focus is not on Radio Button A "
                                           + "as Expected");
            }
        });
    }

    private static void runTest6() throws Exception {
        hitKey(KeyEvent.VK_UP);
        hitKey(KeyEvent.VK_UP);
        SwingUtilities.invokeAndWait(() -> {
            if (focusManager.getFocusOwner() != radioBtn2) {
                System.out.println("Radio button Group Circle Back To "
                                   + "First Button Test");
                throw new RuntimeException("Focus is not on Radio Button B "
                                           + "as Expected");
            }
        });
    }

    private static void runTest7() throws Exception {
        hitKey(KeyEvent.VK_TAB);
        SwingUtilities.invokeAndWait(() -> {
            if (focusManager.getFocusOwner() != btnMiddle) {
                System.out.println("Separate Component added in"
                                   + " button group layout");
                throw new RuntimeException("Focus is not on Middle Button"
                                           + " as Expected");
            }
        });
    }

    private static void runTest8() throws Exception {
        hitKey(KeyEvent.VK_TAB);
        SwingUtilities.invokeAndWait(() -> {
            if (focusManager.getFocusOwner() != radioBtnSingle) {
                System.out.println("Separate Component added in"
                                   + " button group layout");
                throw new RuntimeException("Focus is not on Radio Button Single"
                                           + " as Expected");
            }
        });
    }

    private static volatile boolean actRB1 = false;
    private static volatile boolean actRB2 = false;
    private static volatile boolean actRB3 = false;

    // JDK-8226892: Verify that ActionListener is called when a RadioButton
    // is selected using arrow key
    private static void runTest9() throws Exception {
        SwingUtilities.invokeAndWait(() -> {
            radioBtn1.setSelected(true);
            radioBtn1.requestFocusInWindow();
        });

        ActionListener actLrRB1 = e -> actRB1 = true;
        ActionListener actLrRB2 = e -> actRB2 = true;
        ActionListener actLrRB3 = e -> actRB3 = true;

        // Adding Action Listeners
        SwingUtilities.invokeAndWait(() -> {
            radioBtn1.addActionListener(actLrRB1);
            radioBtn2.addActionListener(actLrRB2);
            radioBtn3.addActionListener(actLrRB3);
        });

        hitKey(KeyEvent.VK_DOWN);
        hitKey(KeyEvent.VK_DOWN);
        hitKey(KeyEvent.VK_DOWN);

        String failMessage = "ActionListener not invoked when selected using "
                             + "arrow key.";
        if (!actRB2) {
            throw new RuntimeException("RadioButton 2: " + failMessage);
        }
        if (!actRB3) {
            throw new RuntimeException("RadioButton 3: " + failMessage);
        }
        if (!actRB1) {
            throw new RuntimeException("RadioButton 1: " + failMessage);
        }

        // Removing Action Listeners
        SwingUtilities.invokeAndWait(() -> {
            radioBtn1.removeActionListener(actLrRB1);
            radioBtn2.removeActionListener(actLrRB2);
            radioBtn3.removeActionListener(actLrRB3);
        });
    }

    private static void hitKey(int keycode) {
        robot.keyPress(keycode);
        robot.keyRelease(keycode);
        robot.waitForIdle();
    }

    private static void hitKey(int mode, int keycode) {
        robot.keyPress(mode);
        robot.keyPress(keycode);
        robot.keyRelease(keycode);
        robot.keyRelease(mode);
        robot.waitForIdle();
    }
}