Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JRadioButton/8033699/bug8033699.java
38854 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* @library ../../regtesthelpers26* @build Util27* @bug 8033699 822689228* @summary Incorrect radio button behavior when pressing tab key29* @author Vivi An30* @run main bug803369931*/3233import javax.swing.*;34import javax.swing.event.*;35import java.awt.event.*;36import java.awt.*;3738public class bug8033699 {39private static Robot robot;4041private static JButton btnStart;42private static ButtonGroup btnGrp;43private static JButton btnEnd;44private static JButton btnMiddle;45private static JRadioButton radioBtn1;46private static JRadioButton radioBtn2;47private static JRadioButton radioBtn3;48private static JRadioButton radioBtnSingle;4950public static void main(String args[]) throws Throwable {51SwingUtilities.invokeAndWait(new Runnable() {52public void run() {53createAndShowGUI();54}55});5657robot = new Robot();58Thread.sleep(100);5960robot.setAutoDelay(100);6162// tab key test grouped radio button63runTest1();6465// tab key test non-grouped radio button66runTest2();6768// shift tab key test grouped and non grouped radio button69runTest3();7071// left/up key test in grouped radio button72runTest4();7374// down/right key test in grouped radio button75runTest5();7677// tab from radio button in group to next component in the middle of button group layout78runTest6();7980// tab to radio button in group from component in the middle of button group layout81runTest7();8283// down key circle back to first button in grouped radio button84runTest8();8586// Verify that ActionListener is called when a RadioButton is selected using arrow key.87runTest9();88}8990private static void createAndShowGUI() {91JFrame mainFrame = new JFrame("Bug 8033699 - 8 Tests for Grouped/Non Group Radio Buttons");9293btnStart = new JButton("Start");94btnEnd = new JButton("End");95btnMiddle = new JButton("Middle");9697JPanel box = new JPanel();98box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));99box.setBorder(BorderFactory.createTitledBorder("Grouped Radio Buttons"));100radioBtn1 = new JRadioButton("A");101radioBtn2 = new JRadioButton("B");102radioBtn3 = new JRadioButton("C");103104ButtonGroup btnGrp = new ButtonGroup();105btnGrp.add(radioBtn1);106btnGrp.add(radioBtn2);107btnGrp.add(radioBtn3);108radioBtn1.setSelected(true);109110box.add(radioBtn1);111box.add(radioBtn2);112box.add(btnMiddle);113box.add(radioBtn3);114115radioBtnSingle = new JRadioButton("Not Grouped");116radioBtnSingle.setSelected(true);117118mainFrame.getContentPane().add(btnStart);119mainFrame.getContentPane().add(box);120mainFrame.getContentPane().add(radioBtnSingle);121mainFrame.getContentPane().add(btnEnd);122123mainFrame.getRootPane().setDefaultButton(btnStart);124btnStart.requestFocus();125126mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);127mainFrame.setLayout(new BoxLayout(mainFrame.getContentPane(), BoxLayout.Y_AXIS));128129mainFrame.setSize(300, 300);130mainFrame.setLocation(200, 200);131mainFrame.setVisible(true);132mainFrame.toFront();133}134135// Radio button Group as a single component when traversing through tab key136private static void runTest1() throws Exception{137hitKey(robot, KeyEvent.VK_TAB);138hitKey(robot, KeyEvent.VK_TAB);139140SwingUtilities.invokeAndWait(new Runnable() {141public void run() {142if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtnSingle) {143System.out.println("Radio Button Group Go To Next Component through Tab Key failed");144throw new RuntimeException("Focus is not on Radio Button Single as Expected");145}146}147});148}149150// Non-Grouped Radio button as a single component when traversing through tab key151private static void runTest2() throws Exception{152hitKey(robot, KeyEvent.VK_TAB);153SwingUtilities.invokeAndWait(new Runnable() {154public void run() {155if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != btnEnd) {156System.out.println("Non Grouped Radio Button Go To Next Component through Tab Key failed");157throw new RuntimeException("Focus is not on Button End as Expected");158}159}160});161}162163// Non-Grouped Radio button and Group Radio button as a single component when traversing through shift-tab key164private static void runTest3() throws Exception{165hitKey(robot, KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);166hitKey(robot, KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);167SwingUtilities.invokeAndWait(new Runnable() {168public void run() {169if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn3) {170System.out.println("Radio button Group/Non Grouped Radio Button SHIFT-Tab Key Test failed");171throw new RuntimeException("Focus is not on Radio Button C as Expected");172}173}174});175}176177// Using arrow key to move focus in radio button group178private static void runTest4() throws Exception{179hitKey(robot, KeyEvent.VK_UP);180hitKey(robot, KeyEvent.VK_LEFT);181SwingUtilities.invokeAndWait(new Runnable() {182public void run() {183if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn1) {184System.out.println("Radio button Group UP/LEFT Arrow Key Move Focus Failed");185throw new RuntimeException("Focus is not on Radio Button A as Expected");186}187}188});189}190191private static void runTest5() throws Exception{192hitKey(robot, KeyEvent.VK_DOWN);193hitKey(robot, KeyEvent.VK_RIGHT);194SwingUtilities.invokeAndWait(new Runnable() {195public void run() {196if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn3) {197System.out.println("Radio button Group Left/Up Arrow Key Move Focus Failed");198throw new RuntimeException("Focus is not on Radio Button C as Expected");199}200}201});202}203204private static void runTest6() throws Exception{205hitKey(robot, KeyEvent.VK_DOWN);206hitKey(robot, KeyEvent.VK_DOWN);207SwingUtilities.invokeAndWait(new Runnable() {208public void run() {209if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn2) {210System.out.println("Radio button Group Circle Back To First Button Test");211throw new RuntimeException("Focus is not on Radio Button A as Expected");212}213}214});215}216217private static void runTest7() throws Exception{218hitKey(robot, KeyEvent.VK_TAB);219SwingUtilities.invokeAndWait(new Runnable() {220public void run() {221if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != btnMiddle) {222System.out.println("Separate Component added in button group layout");223throw new RuntimeException("Focus is not on Middle Button as Expected");224}225}226});227}228229private static void runTest8() throws Exception{230hitKey(robot, KeyEvent.VK_TAB);231SwingUtilities.invokeAndWait(new Runnable() {232public void run() {233if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn3) {234System.out.println("Separate Component added in button group layout");235throw new RuntimeException("Focus is not on Radio Button C as Expected");236}237}238});239}240241private static Boolean actRB1 = false;242private static Boolean actRB2 = false;243private static Boolean actRB3 = false;244245// JDK-8226892: Verify that ActionListener is called when a RadioButton is selected using arrow key.246private static void runTest9() throws Exception {247SwingUtilities.invokeAndWait(() -> {248radioBtn1.setSelected(true);249radioBtn1.requestFocusInWindow();250});251252ActionListener actLrRB1 = e -> actRB1 = true;253ActionListener actLrRB2 = e -> actRB2 = true;254ActionListener actLrRB3 = e -> actRB3 = true;255256radioBtn1.addActionListener(actLrRB1);257radioBtn2.addActionListener(actLrRB2);258radioBtn3.addActionListener(actLrRB3);259260hitKey(robot, KeyEvent.VK_DOWN);261hitKey(robot, KeyEvent.VK_DOWN);262hitKey(robot, KeyEvent.VK_DOWN);263264String failMessage = "ActionListener not invoked when selected using arrow key.";265if (!actRB2) {266throw new RuntimeException("RadioButton 2: " + failMessage);267}268if (!actRB3) {269throw new RuntimeException("RadioButton 3: " + failMessage);270}271if (!actRB1) {272throw new RuntimeException("RadioButton 1: " + failMessage);273}274275radioBtn1.removeActionListener(actLrRB1);276radioBtn2.removeActionListener(actLrRB2);277radioBtn3.removeActionListener(actLrRB3);278}279280private static void hitKey(Robot robot, int keycode) {281robot.keyPress(keycode);282robot.keyRelease(keycode);283robot.waitForIdle();284}285286private static void hitKey(Robot robot, int mode, int keycode) {287robot.keyPress(mode);288robot.keyPress(keycode);289robot.keyRelease(mode);290robot.keyRelease(keycode);291robot.waitForIdle();292}293}294295296