Path: blob/master/test/jdk/javax/swing/ButtonGroup/TestButtonGroupFocusTraversal.java
41152 views
/*1* Copyright (c) 2020, 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* @bug 8249548 825923727* @summary Test focus traversal in button group containing JToggleButton28* and JRadioButton29* @run main TestButtonGroupFocusTraversal30*/3132import javax.swing.AbstractAction;33import javax.swing.ButtonGroup;34import javax.swing.JCheckBox;35import javax.swing.JFrame;36import javax.swing.JRadioButton;37import javax.swing.JTextField;38import javax.swing.JToggleButton;39import javax.swing.SwingUtilities;40import javax.swing.UIManager;41import java.awt.Component;42import java.awt.Container;43import java.awt.FlowLayout;44import java.awt.KeyboardFocusManager;45import java.awt.Point;46import java.awt.Robot;47import java.awt.event.ActionEvent;48import java.awt.event.KeyEvent;4950public class TestButtonGroupFocusTraversal {51private static JFrame frame;52private static JTextField textFieldFirst, textFieldLast;53private static JToggleButton toggleButton1, toggleButton2;54private static JCheckBox checkBox1, checkBox2;55private static boolean toggleButtonActionPerformed;56private static boolean checkboxActionPerformed;57private static JRadioButton radioButton1, radioButton2;58private static Robot robot;5960private static void blockTillDisplayed(Component comp) {61Point p = null;62while (p == null) {63try {64p = comp.getLocationOnScreen();65} catch (IllegalStateException e) {66try {67Thread.sleep(500);68} catch (InterruptedException ie) {69}70}71}72}7374private static void createUI() throws Exception {75SwingUtilities.invokeAndWait(new Runnable() {76public void run() {77textFieldFirst = new JTextField("First");78textFieldLast = new JTextField("Last");79toggleButton1 = new JToggleButton("1");80toggleButton2 = new JToggleButton("2");81radioButton1 = new JRadioButton("1");82radioButton2 = new JRadioButton("2");83checkBox1 = new JCheckBox("1");84checkBox2 = new JCheckBox("2");8586toggleButton1.setAction(new AbstractAction() {87@Override88public void actionPerformed(ActionEvent e) {89toggleButtonActionPerformed = true;90}91});9293toggleButton2.setAction(new AbstractAction() {94@Override95public void actionPerformed(ActionEvent e) {96toggleButtonActionPerformed = true;97}98});99100checkBox1.setAction(new AbstractAction() {101@Override102public void actionPerformed(ActionEvent e) {103checkboxActionPerformed = true;104}105});106107checkBox2.setAction(new AbstractAction() {108@Override109public void actionPerformed(ActionEvent e) {110checkboxActionPerformed = true;111}112});113114ButtonGroup toggleGroup = new ButtonGroup();115toggleGroup.add(toggleButton1);116toggleGroup.add(toggleButton2);117118ButtonGroup radioGroup = new ButtonGroup();119radioGroup.add(radioButton1);120radioGroup.add(radioButton2);121122ButtonGroup checkboxButtonGroup = new ButtonGroup();123checkboxButtonGroup.add(checkBox1);124checkboxButtonGroup.add(checkBox2);125126toggleButton2.setSelected(true);127radioButton2.setSelected(true);128checkBox2.setSelected(true);129130frame = new JFrame("Test");131frame.setLayout(new FlowLayout());132133Container pane = frame.getContentPane();134pane.add(textFieldFirst);135pane.add(toggleButton1);136pane.add(toggleButton2);137pane.add(radioButton1);138pane.add(radioButton2);139pane.add(checkBox1);140pane.add(checkBox2);141pane.add(textFieldLast);142143frame.pack();144frame.setAlwaysOnTop(true);145frame.setLocationRelativeTo(null);146frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);147frame.setVisible(true);148}149});150}151152private static void pressKey(int ...keys) {153int num = keys.length;154for (int i=0; i<num; i++)155robot.keyPress(keys[i]);156for (int i=num; i>0; i--)157robot.keyRelease(keys[i-1]);158159robot.waitForIdle();160robot.delay(200);161}162163private static void checkFocusedComponent (Component component) {164Component focusedComponent = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();165if (!focusedComponent.equals(component)) {166System.out.println(component);167System.out.println(focusedComponent);168throw new RuntimeException("Wrong Component Selected");169}170}171172private static void checkToggleButtonActionPerformed() {173if (toggleButtonActionPerformed) {174throw new RuntimeException("Toggle Button Action should not be" +175"performed");176}177}178179private static void checkCheckboxActionPerformed() {180if (toggleButtonActionPerformed) {181throw new RuntimeException("Checkbox Action should not be" +182"performed");183}184}185186public static void main(String[] args) throws Exception {187robot = new Robot();188robot.setAutoDelay(100);189190UIManager.LookAndFeelInfo infos[] = UIManager.getInstalledLookAndFeels();191for (UIManager.LookAndFeelInfo info : infos) {192UIManager.setLookAndFeel(info.getClassName());193System.out.println(info.getClassName());194try {195createUI();196197robot.waitForIdle();198robot.delay(200);199200blockTillDisplayed(frame);201202SwingUtilities.invokeAndWait(textFieldFirst::requestFocus);203204if (!textFieldFirst.equals(KeyboardFocusManager.getCurrentKeyboardFocusManager()205.getFocusOwner())) {206try {207Thread.sleep(100);208} catch (InterruptedException e) {209e.printStackTrace();210}211SwingUtilities.invokeAndWait(textFieldFirst::requestFocus);212}213214robot.waitForIdle();215robot.delay(200);216217pressKey(KeyEvent.VK_TAB);218checkFocusedComponent(toggleButton2);219220pressKey(KeyEvent.VK_TAB);221checkFocusedComponent(radioButton2);222223pressKey(KeyEvent.VK_TAB);224checkFocusedComponent(checkBox2);225226pressKey(KeyEvent.VK_TAB);227checkFocusedComponent(textFieldLast);228229pressKey(KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);230checkFocusedComponent(checkBox2);231232pressKey(KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);233checkFocusedComponent(radioButton2);234235pressKey(KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);236checkFocusedComponent(toggleButton2);237238pressKey(KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);239checkFocusedComponent(textFieldFirst);240241pressKey(KeyEvent.VK_TAB);242checkFocusedComponent(toggleButton2);243244pressKey(KeyEvent.VK_LEFT);245checkFocusedComponent(toggleButton1);246checkToggleButtonActionPerformed();247248pressKey(KeyEvent.VK_RIGHT);249checkFocusedComponent(toggleButton2);250checkToggleButtonActionPerformed();251252pressKey(KeyEvent.VK_UP);253checkFocusedComponent(toggleButton1);254checkToggleButtonActionPerformed();255256pressKey(KeyEvent.VK_DOWN);257checkFocusedComponent(toggleButton2);258checkToggleButtonActionPerformed();259260pressKey(KeyEvent.VK_TAB);261checkFocusedComponent(radioButton2);262263pressKey(KeyEvent.VK_LEFT);264checkFocusedComponent(radioButton1);265266pressKey(KeyEvent.VK_RIGHT);267checkFocusedComponent(radioButton2);268269pressKey(KeyEvent.VK_UP);270checkFocusedComponent(radioButton1);271272pressKey(KeyEvent.VK_DOWN);273checkFocusedComponent(radioButton2);274275pressKey(KeyEvent.VK_TAB);276checkFocusedComponent(checkBox2);277278pressKey(KeyEvent.VK_LEFT);279checkCheckboxActionPerformed();280checkFocusedComponent(checkBox1);281282pressKey(KeyEvent.VK_RIGHT);283checkCheckboxActionPerformed();284checkFocusedComponent(checkBox2);285286pressKey(KeyEvent.VK_UP);287checkCheckboxActionPerformed();288checkFocusedComponent(checkBox1);289290pressKey(KeyEvent.VK_DOWN);291checkCheckboxActionPerformed();292checkFocusedComponent(checkBox2);293} finally {294if (frame != null) {295SwingUtilities.invokeAndWait(frame::dispose);296}297}298}299}300}301302303