Path: blob/master/test/jdk/javax/swing/JRadioButton/8033699/bug8033699.java
41153 views
/*1* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @library ../../regtesthelpers27* @build Util28* @bug 8033699 8154043 8167160 8208640 822689229* @summary Incorrect radio button behavior when pressing tab key30* @run main bug803369931*/32import java.awt.KeyboardFocusManager;33import java.awt.Robot;34import java.awt.event.ActionListener;35import java.awt.event.KeyEvent;36import java.util.logging.Level;37import java.util.logging.Logger;38import javax.swing.BorderFactory;39import javax.swing.BoxLayout;40import javax.swing.ButtonGroup;41import javax.swing.JButton;42import javax.swing.JFrame;43import javax.swing.JPanel;44import javax.swing.JRadioButton;45import javax.swing.SwingUtilities;46import javax.swing.UIManager;47import javax.swing.UnsupportedLookAndFeelException;4849public class bug8033699 {5051private static JFrame mainFrame;52private static Robot robot;53private static JButton btnStart;54private static JButton btnEnd;55private static JButton btnMiddle;56private static JRadioButton radioBtn1;57private static JRadioButton radioBtn2;58private static JRadioButton radioBtn3;59private static JRadioButton radioBtnSingle;6061public static void main(String args[]) throws Throwable {62SwingUtilities.invokeAndWait(() -> {63changeLAF();64createAndShowGUI();65});6667robot = new Robot();68Thread.sleep(100);6970robot.setAutoDelay(100);7172// tab key test grouped radio button73runTest1();7475// tab key test non-grouped radio button76runTest2();7778// shift tab key test grouped and non grouped radio button79runTest3();8081// left/up key test in grouped radio button82runTest4();8384// down/right key test in grouped radio button85runTest5();8687// tab from radio button in group to next component in the middle of button group layout88runTest6();8990// tab to radio button in group from component in the middle of button group layout91runTest7();9293// down key circle back to first button in grouped radio button94runTest8();9596// Verify that ActionListener is called when a RadioButton is selected using arrow key.97runTest9();9899SwingUtilities.invokeAndWait(() -> mainFrame.dispose());100}101102private static void changeLAF() {103String currentLAF = UIManager.getLookAndFeel().toString();104System.out.println(currentLAF);105currentLAF = currentLAF.toLowerCase();106if (currentLAF.contains("nimbus")) {107try {108UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");109} catch (Exception ex) {110ex.printStackTrace();111}112}113}114115private static void createAndShowGUI() {116mainFrame = new JFrame("Bug 8033699 - 8 Tests for Grouped/Non Group Radio Buttons");117btnStart = new JButton("Start");118btnEnd = new JButton("End");119btnMiddle = new JButton("Middle");120121JPanel box = new JPanel();122box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));123box.setBorder(BorderFactory.createTitledBorder("Grouped Radio Buttons"));124radioBtn1 = new JRadioButton("A");125radioBtn2 = new JRadioButton("B");126radioBtn3 = new JRadioButton("C");127128ButtonGroup btnGrp = new ButtonGroup();129btnGrp.add(radioBtn1);130btnGrp.add(radioBtn2);131btnGrp.add(radioBtn3);132radioBtn1.setSelected(true);133134box.add(radioBtn1);135box.add(radioBtn2);136box.add(btnMiddle);137box.add(radioBtn3);138139radioBtnSingle = new JRadioButton("Not Grouped");140radioBtnSingle.setSelected(true);141142mainFrame.getContentPane().add(btnStart);143mainFrame.getContentPane().add(box);144mainFrame.getContentPane().add(radioBtnSingle);145mainFrame.getContentPane().add(btnEnd);146147mainFrame.getRootPane().setDefaultButton(btnStart);148btnStart.requestFocus();149150mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);151mainFrame.setLayout(new BoxLayout(mainFrame.getContentPane(), BoxLayout.Y_AXIS));152153mainFrame.setSize(300, 300);154mainFrame.setLocation(200, 200);155mainFrame.setVisible(true);156mainFrame.toFront();157}158159// Radio button Group as a single component when traversing through tab key160private static void runTest1() throws Exception {161hitKey(robot, KeyEvent.VK_TAB);162hitKey(robot, KeyEvent.VK_TAB);163hitKey(robot, KeyEvent.VK_TAB);164165SwingUtilities.invokeAndWait(() -> {166if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtnSingle) {167System.out.println("Radio Button Group Go To Next Component through Tab Key failed");168throw new RuntimeException("Focus is not on Radio Button Single as Expected");169}170});171}172173// Non-Grouped Radio button as a single component when traversing through tab key174private static void runTest2() throws Exception {175hitKey(robot, KeyEvent.VK_TAB);176SwingUtilities.invokeAndWait(() -> {177if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != btnEnd) {178System.out.println("Non Grouped Radio Button Go To Next Component through Tab Key failed");179throw new RuntimeException("Focus is not on Button End as Expected");180}181});182}183184// Non-Grouped Radio button and Group Radio button as a single component when traversing through shift-tab key185private static void runTest3() throws Exception {186hitKey(robot, KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);187hitKey(robot, KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);188hitKey(robot, KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);189SwingUtilities.invokeAndWait(() -> {190if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn1) {191System.out.println("Radio button Group/Non Grouped Radio Button SHIFT-Tab Key Test failed");192throw new RuntimeException("Focus is not on Radio Button A as Expected");193}194});195}196197// Using arrow key to move focus in radio button group198private static void runTest4() throws Exception {199hitKey(robot, KeyEvent.VK_DOWN);200hitKey(robot, KeyEvent.VK_RIGHT);201SwingUtilities.invokeAndWait(() -> {202if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn3) {203System.out.println("Radio button Group UP/LEFT Arrow Key Move Focus Failed");204throw new RuntimeException("Focus is not on Radio Button C as Expected");205}206});207}208209private static void runTest5() throws Exception {210hitKey(robot, KeyEvent.VK_UP);211hitKey(robot, KeyEvent.VK_LEFT);212SwingUtilities.invokeAndWait(() -> {213if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn1) {214System.out.println("Radio button Group Left/Up Arrow Key Move Focus Failed");215throw new RuntimeException("Focus is not on Radio Button A as Expected");216}217});218}219220private static void runTest6() throws Exception {221hitKey(robot, KeyEvent.VK_UP);222hitKey(robot, KeyEvent.VK_UP);223SwingUtilities.invokeAndWait(() -> {224if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn2) {225System.out.println("Radio button Group Circle Back To First Button Test");226throw new RuntimeException("Focus is not on Radio Button B as Expected");227}228});229}230231private static void runTest7() throws Exception {232hitKey(robot, KeyEvent.VK_TAB);233SwingUtilities.invokeAndWait(() -> {234if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != btnMiddle) {235System.out.println("Separate Component added in button group layout");236throw new RuntimeException("Focus is not on Middle Button as Expected");237}238});239}240241private static void runTest8() throws Exception {242hitKey(robot, KeyEvent.VK_TAB);243SwingUtilities.invokeAndWait(() -> {244if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtnSingle) {245System.out.println("Separate Component added in button group layout");246throw new RuntimeException("Focus is not on Radio Button Single as Expected");247}248});249}250251private static Boolean actRB1 = false;252private static Boolean actRB2 = false;253private static Boolean actRB3 = false;254255// JDK-8226892: Verify that ActionListener is called when a RadioButton is selected using arrow key.256private static void runTest9() throws Exception {257SwingUtilities.invokeAndWait(() -> {258radioBtn1.setSelected(true);259radioBtn1.requestFocusInWindow();260});261262ActionListener actLrRB1 = e -> actRB1 = true;263ActionListener actLrRB2 = e -> actRB2 = true;264ActionListener actLrRB3 = e -> actRB3 = true;265266radioBtn1.addActionListener(actLrRB1);267radioBtn2.addActionListener(actLrRB2);268radioBtn3.addActionListener(actLrRB3);269270hitKey(robot, KeyEvent.VK_DOWN);271hitKey(robot, KeyEvent.VK_DOWN);272hitKey(robot, KeyEvent.VK_DOWN);273274String failMessage = "ActionListener not invoked when selected using arrow key.";275if (!actRB2) {276throw new RuntimeException("RadioButton 2: " + failMessage);277}278if (!actRB3) {279throw new RuntimeException("RadioButton 3: " + failMessage);280}281if (!actRB1) {282throw new RuntimeException("RadioButton 1: " + failMessage);283}284285radioBtn1.removeActionListener(actLrRB1);286radioBtn2.removeActionListener(actLrRB2);287radioBtn3.removeActionListener(actLrRB3);288}289290private static void hitKey(Robot robot, int keycode) {291robot.keyPress(keycode);292robot.keyRelease(keycode);293robot.waitForIdle();294}295296private static void hitKey(Robot robot, int mode, int keycode) {297robot.keyPress(mode);298robot.keyPress(keycode);299robot.keyRelease(mode);300robot.keyRelease(keycode);301robot.waitForIdle();302}303}304305306