diff --git a/test/jdk/javax/swing/JRadioButton/8033699/bug8033699.java b/test/jdk/javax/swing/JRadioButton/8033699/bug8033699.java index 471d30c5c19..e8cabb7e181 100644 --- a/test/jdk/javax/swing/JRadioButton/8033699/bug8033699.java +++ b/test/jdk/javax/swing/JRadioButton/8033699/bug8033699.java @@ -35,11 +35,10 @@ import java.awt.event.KeyEvent; import javax.swing.BorderFactory; -import javax.swing.BoxLayout; +import javax.swing.Box; import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JFrame; -import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.SwingUtilities; import javax.swing.UIManager; @@ -55,11 +54,14 @@ public class bug8033699 { 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(); - // Get all installed Look and Feels + SwingUtilities.invokeAndWait(() -> + focusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager()); + UIManager.LookAndFeelInfo[] lafs = UIManager.getInstalledLookAndFeels(); for (UIManager.LookAndFeelInfo laf : lafs) { testLaF(laf); @@ -97,11 +99,13 @@ private static void testLaF(UIManager.LookAndFeelInfo laf) throws Exception { runTest5(); robot.delay(100); - // tab from radio button in group to next component in the middle of button group layout + // 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 + // tab to radio button in group from component in the middle of + // button group layout runTest7(); robot.delay(100); @@ -109,11 +113,15 @@ private static void testLaF(UIManager.LookAndFeelInfo laf) throws Exception { runTest8(); robot.delay(100); - // Verify that ActionListener is called when a RadioButton is selected using arrow key. + // Verify that ActionListener is called when a RadioButton is + // selected using arrow key runTest9(); robot.delay(100); } catch (Exception e) { - throw new RuntimeException("Error testing LaF: " + laf.getName(), e); + Throwable cause = e.getCause(); + throw new RuntimeException("Error testing LaF: " + laf.getName() + + (cause != null ? " - " + cause.getMessage() : ""), + e); } finally { SwingUtilities.invokeAndWait(() -> { if (mainFrame != null) { @@ -135,13 +143,12 @@ private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) { } private static void createAndShowGUI() { - mainFrame = new JFrame("Bug 8033699 - 8 Tests for Grouped/Non Group Radio Buttons"); + mainFrame = new JFrame("Radio Button Focus Tests"); btnStart = new JButton("Start"); btnEnd = new JButton("End"); btnMiddle = new JButton("Middle"); - JPanel box = new JPanel(); - box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS)); + Box box = Box.createVerticalBox(); box.setBorder(BorderFactory.createTitledBorder("Grouped Radio Buttons")); radioBtn1 = new JRadioButton("A"); radioBtn2 = new JRadioButton("B"); @@ -161,16 +168,17 @@ private static void createAndShowGUI() { radioBtnSingle = new JRadioButton("Not Grouped"); radioBtnSingle.setSelected(true); - mainFrame.getContentPane().add(btnStart); - mainFrame.getContentPane().add(box); - mainFrame.getContentPane().add(radioBtnSingle); - mainFrame.getContentPane().add(btnEnd); + 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.setLayout(new BoxLayout(mainFrame.getContentPane(), BoxLayout.Y_AXIS)); mainFrame.setSize(300, 300); mainFrame.setLocationRelativeTo(null); @@ -185,33 +193,41 @@ private static void runTest1() throws Exception { hitKey(KeyEvent.VK_TAB); SwingUtilities.invokeAndWait(() -> { - if (KeyboardFocusManager.getCurrentKeyboardFocusManager().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"); + 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 + // 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 (KeyboardFocusManager.getCurrentKeyboardFocusManager().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"); + 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 + // 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 (KeyboardFocusManager.getCurrentKeyboardFocusManager().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"); + 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"); } }); } @@ -221,9 +237,11 @@ private static void runTest4() throws Exception { hitKey(KeyEvent.VK_DOWN); hitKey(KeyEvent.VK_RIGHT); SwingUtilities.invokeAndWait(() -> { - if (KeyboardFocusManager.getCurrentKeyboardFocusManager().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"); + 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"); } }); } @@ -232,9 +250,11 @@ private static void runTest5() throws Exception { hitKey(KeyEvent.VK_UP); hitKey(KeyEvent.VK_LEFT); SwingUtilities.invokeAndWait(() -> { - if (KeyboardFocusManager.getCurrentKeyboardFocusManager().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"); + 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"); } }); } @@ -243,9 +263,11 @@ private static void runTest6() throws Exception { hitKey(KeyEvent.VK_UP); hitKey(KeyEvent.VK_UP); SwingUtilities.invokeAndWait(() -> { - if (KeyboardFocusManager.getCurrentKeyboardFocusManager().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"); + 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"); } }); } @@ -253,9 +275,11 @@ private static void runTest6() throws Exception { private static void runTest7() throws Exception { hitKey(KeyEvent.VK_TAB); SwingUtilities.invokeAndWait(() -> { - if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != btnMiddle) { - System.out.println("Separate Component added in button group layout"); - throw new RuntimeException("Focus is not on Middle Button as Expected"); + 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"); } }); } @@ -263,18 +287,21 @@ private static void runTest7() throws Exception { private static void runTest8() throws Exception { hitKey(KeyEvent.VK_TAB); SwingUtilities.invokeAndWait(() -> { - if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtnSingle) { - System.out.println("Separate Component added in button group layout"); - throw new RuntimeException("Focus is not on Radio Button Single as Expected"); + 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 boolean actRB1 = false; - private static boolean actRB2 = false; - private static boolean actRB3 = false; + 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. + // 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); @@ -285,15 +312,19 @@ private static void runTest9() throws Exception { ActionListener actLrRB2 = e -> actRB2 = true; ActionListener actLrRB3 = e -> actRB3 = true; - radioBtn1.addActionListener(actLrRB1); - radioBtn2.addActionListener(actLrRB2); - radioBtn3.addActionListener(actLrRB3); + // 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."; + String failMessage = "ActionListener not invoked when selected using " + + "arrow key."; if (!actRB2) { throw new RuntimeException("RadioButton 2: " + failMessage); } @@ -304,9 +335,12 @@ private static void runTest9() throws Exception { throw new RuntimeException("RadioButton 1: " + failMessage); } - radioBtn1.removeActionListener(actLrRB1); - radioBtn2.removeActionListener(actLrRB2); - radioBtn3.removeActionListener(actLrRB3); + // Removing Action Listeners + SwingUtilities.invokeAndWait(() -> { + radioBtn1.removeActionListener(actLrRB1); + radioBtn2.removeActionListener(actLrRB2); + radioBtn3.removeActionListener(actLrRB3); + }); } private static void hitKey(int keycode) {