jdk/test/javax/swing/JRadioButton/ButtonGroupFocus/ButtonGroupFocusTest.java
author vromero
Thu, 05 Jan 2017 11:16:39 -0800
changeset 43033 c37b643739a7
parent 42941 040a43b1c909
child 47147 857d25fae728
permissions -rw-r--r--
8148100: Convert lambda most specific positive tests to check runtime behavior Reviewed-by: mcimadamore Contributed-by: bsrbnd@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42941
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
     1
/*
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
     4
 *
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
     7
 * published by the Free Software Foundation.
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
     8
 *
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    13
 * accompanied this code).
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    14
 *
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    18
 *
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    21
 * questions.
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    22
 */
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    23
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    24
/* @test
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    25
 * @bug 8074883
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    26
 * @summary Tab key should move to focused button in a button group
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    27
 * @run main ButtonGroupFocusTest
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    28
 */
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    29
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    30
import javax.swing.*;
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    31
import java.awt.*;
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    32
import java.awt.event.KeyEvent;
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    33
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    34
public class ButtonGroupFocusTest {
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    35
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    36
    private static JRadioButton button1;
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    37
    private static JRadioButton button2;
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    38
    private static JRadioButton button3;
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    39
    private static JRadioButton button4;
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    40
    private static JRadioButton button5;
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    41
    private static Robot robot;
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    42
    private static JFrame frame;
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    43
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    44
    public static void main(String[] args) throws Exception {
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    45
        robot = new Robot();
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    46
        robot.setAutoDelay(100);
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    47
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    48
        SwingUtilities.invokeAndWait(() -> {
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    49
            frame = new JFrame();
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    50
            Container contentPane = frame.getContentPane();
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    51
            contentPane.setLayout(new FlowLayout());
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    52
            button1 = new JRadioButton("Button 1");
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    53
            contentPane.add(button1);
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    54
            button2 = new JRadioButton("Button 2");
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    55
            contentPane.add(button2);
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    56
            button3 = new JRadioButton("Button 3");
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    57
            contentPane.add(button3);
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    58
            button4 = new JRadioButton("Button 4");
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    59
            contentPane.add(button4);
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    60
            button5 = new JRadioButton("Button 5");
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    61
            contentPane.add(button5);
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    62
            ButtonGroup group = new ButtonGroup();
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    63
            group.add(button1);
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    64
            group.add(button2);
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    65
            group.add(button3);
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    66
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    67
            group = new ButtonGroup();
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    68
            group.add(button4);
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    69
            group.add(button5);
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    70
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    71
            button2.setSelected(true);
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    72
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    73
            frame.pack();
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    74
            frame.setVisible(true);
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    75
        });
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    76
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    77
        robot.waitForIdle();
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    78
        robot.delay(200);
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    79
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    80
        SwingUtilities.invokeAndWait(() -> {
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    81
            if( !button2.hasFocus() ) {
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    82
                frame.dispose();
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    83
                throw new RuntimeException(
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    84
                        "Button 2 should get focus after activation");
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    85
            }
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    86
        });
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    87
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    88
        robot.keyPress(KeyEvent.VK_TAB);
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    89
        robot.keyRelease(KeyEvent.VK_TAB);
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    90
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    91
        robot.waitForIdle();
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    92
        robot.delay(200);
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    93
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    94
        SwingUtilities.invokeAndWait(() -> {
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    95
            if( !button4.hasFocus() ) {
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    96
                frame.dispose();
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    97
                throw new RuntimeException(
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    98
                        "Button 4 should get focus");
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
    99
            }
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
   100
            button3.setSelected(true);
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
   101
        });
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
   102
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
   103
        robot.keyPress(KeyEvent.VK_TAB);
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
   104
        robot.keyRelease(KeyEvent.VK_TAB);
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
   105
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
   106
        robot.waitForIdle();
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
   107
        robot.delay(200);
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
   108
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
   109
        SwingUtilities.invokeAndWait(() -> {
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
   110
            if( !button3.hasFocus() ) {
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
   111
                frame.dispose();
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
   112
                throw new RuntimeException(
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
   113
                        "selected Button 3 should get focus");
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
   114
            }
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
   115
        });
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
   116
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
   117
        SwingUtilities.invokeLater(frame::dispose);
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
   118
    }
040a43b1c909 8074883: Tab key should move to focused button in a button group
ssadetsky
parents:
diff changeset
   119
}