test/jdk/javax/swing/JRadioButton/8033699/bug8033699.java
changeset 51454 06417e487a28
parent 47216 71c04702a3d5
child 59173 0fa6b6461451
equal deleted inserted replaced
51453:59614cd6d8dc 51454:06417e487a28
     1 /*
     1 /*
     2  * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    24  /*
    24  /*
    25  * @test
    25  * @test
    26  * @key headful
    26  * @key headful
    27  * @library ../../regtesthelpers
    27  * @library ../../regtesthelpers
    28  * @build Util
    28  * @build Util
    29  * @bug 8033699 8154043 8167160
    29  * @bug 8033699 8154043 8167160 8208640
    30  * @summary  Incorrect radio button behavior when pressing tab key
    30  * @summary  Incorrect radio button behavior when pressing tab key
    31  * @run main bug8033699
    31  * @run main bug8033699
    32  */
    32  */
    33 import java.awt.KeyboardFocusManager;
    33 import java.awt.KeyboardFocusManager;
    34 import java.awt.Robot;
    34 import java.awt.Robot;
    57     private static JRadioButton radioBtn2;
    57     private static JRadioButton radioBtn2;
    58     private static JRadioButton radioBtn3;
    58     private static JRadioButton radioBtn3;
    59     private static JRadioButton radioBtnSingle;
    59     private static JRadioButton radioBtnSingle;
    60 
    60 
    61     public static void main(String args[]) throws Throwable {
    61     public static void main(String args[]) throws Throwable {
    62         SwingUtilities.invokeAndWait(new Runnable() {
    62         SwingUtilities.invokeAndWait(() -> {
    63             @Override
       
    64             public void run() {
       
    65                 changeLAF();
    63                 changeLAF();
    66                 createAndShowGUI();
    64                 createAndShowGUI();
    67             }
       
    68         });
    65         });
    69 
    66 
    70         robot = new Robot();
    67         robot = new Robot();
    71         Thread.sleep(100);
    68         Thread.sleep(100);
    72 
    69 
    94         runTest7();
    91         runTest7();
    95 
    92 
    96         // down key circle back to first button in grouped radio button
    93         // down key circle back to first button in grouped radio button
    97         runTest8();
    94         runTest8();
    98 
    95 
    99         SwingUtilities.invokeAndWait(new Runnable() {
    96         SwingUtilities.invokeAndWait(() -> mainFrame.dispose());
   100             @Override
       
   101             public void run() {
       
   102                 mainFrame.dispose();
       
   103             }
       
   104         });
       
   105     }
    97     }
   106 
    98 
   107     private static void changeLAF() {
    99     private static void changeLAF() {
   108         String currentLAF = UIManager.getLookAndFeel().toString();
   100         String currentLAF = UIManager.getLookAndFeel().toString();
   109         System.out.println(currentLAF);
   101         System.out.println(currentLAF);
   110         currentLAF = currentLAF.toLowerCase();
   102         currentLAF = currentLAF.toLowerCase();
   111         if (currentLAF.contains("aqua") || currentLAF.contains("nimbus")) {
   103         if (currentLAF.contains("nimbus")) {
   112             try {
   104             try {
   113                 UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
   105                 UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
   114             } catch (Exception ex) {
   106             } catch (Exception ex) {
   115                 ex.printStackTrace();
   107                 ex.printStackTrace();
   116             }
   108             }
   165     private static void runTest1() throws Exception {
   157     private static void runTest1() throws Exception {
   166         hitKey(robot, KeyEvent.VK_TAB);
   158         hitKey(robot, KeyEvent.VK_TAB);
   167         hitKey(robot, KeyEvent.VK_TAB);
   159         hitKey(robot, KeyEvent.VK_TAB);
   168         hitKey(robot, KeyEvent.VK_TAB);
   160         hitKey(robot, KeyEvent.VK_TAB);
   169 
   161 
   170         SwingUtilities.invokeAndWait(new Runnable() {
   162         SwingUtilities.invokeAndWait(() -> {
   171             @Override
   163             if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtnSingle) {
   172             public void run() {
   164                 System.out.println("Radio Button Group Go To Next Component through Tab Key failed");
   173                 if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtnSingle) {
   165                 throw new RuntimeException("Focus is not on Radio Button Single as Expected");
   174                     System.out.println("Radio Button Group Go To Next Component through Tab Key failed");
       
   175                     throw new RuntimeException("Focus is not on Radio Button Single as Expected");
       
   176                 }
       
   177             }
   166             }
   178         });
   167         });
   179     }
   168     }
   180 
   169 
   181     // Non-Grouped Radio button as a single component when traversing through tab key
   170     // Non-Grouped Radio button as a single component when traversing through tab key
   182     private static void runTest2() throws Exception {
   171     private static void runTest2() throws Exception {
   183         hitKey(robot, KeyEvent.VK_TAB);
   172         hitKey(robot, KeyEvent.VK_TAB);
   184         SwingUtilities.invokeAndWait(new Runnable() {
   173         SwingUtilities.invokeAndWait(() -> {
   185             @Override
   174             if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != btnEnd) {
   186             public void run() {
   175                 System.out.println("Non Grouped Radio Button Go To Next Component through Tab Key failed");
   187                 if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != btnEnd) {
   176                 throw new RuntimeException("Focus is not on Button End as Expected");
   188                     System.out.println("Non Grouped Radio Button Go To Next Component through Tab Key failed");
       
   189                     throw new RuntimeException("Focus is not on Button End as Expected");
       
   190                 }
       
   191             }
   177             }
   192         });
   178         });
   193     }
   179     }
   194 
   180 
   195     // Non-Grouped Radio button and Group Radio button as a single component when traversing through shift-tab key
   181     // Non-Grouped Radio button and Group Radio button as a single component when traversing through shift-tab key
   196     private static void runTest3() throws Exception {
   182     private static void runTest3() throws Exception {
   197         hitKey(robot, KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);
   183         hitKey(robot, KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);
   198         hitKey(robot, KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);
   184         hitKey(robot, KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);
   199         hitKey(robot, KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);
   185         hitKey(robot, KeyEvent.VK_SHIFT, KeyEvent.VK_TAB);
   200         SwingUtilities.invokeAndWait(new Runnable() {
   186         SwingUtilities.invokeAndWait(() -> {
   201             @Override
   187             if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn1) {
   202             public void run() {
   188                 System.out.println("Radio button Group/Non Grouped Radio Button SHIFT-Tab Key Test failed");
   203                 if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn1) {
   189                 throw new RuntimeException("Focus is not on Radio Button A as Expected");
   204                     System.out.println("Radio button Group/Non Grouped Radio Button SHIFT-Tab Key Test failed");
       
   205                     throw new RuntimeException("Focus is not on Radio Button A as Expected");
       
   206                 }
       
   207             }
   190             }
   208         });
   191         });
   209     }
   192     }
   210 
   193 
   211     // Using arrow key to move focus in radio button group
   194     // Using arrow key to move focus in radio button group
   212     private static void runTest4() throws Exception {
   195     private static void runTest4() throws Exception {
   213         hitKey(robot, KeyEvent.VK_DOWN);
   196         hitKey(robot, KeyEvent.VK_DOWN);
   214         hitKey(robot, KeyEvent.VK_RIGHT);
   197         hitKey(robot, KeyEvent.VK_RIGHT);
   215         SwingUtilities.invokeAndWait(new Runnable() {
   198         SwingUtilities.invokeAndWait(() -> {
   216             @Override
   199             if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn3) {
   217             public void run() {
   200                 System.out.println("Radio button Group UP/LEFT Arrow Key Move Focus Failed");
   218                 if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn3) {
   201                 throw new RuntimeException("Focus is not on Radio Button C as Expected");
   219                     System.out.println("Radio button Group UP/LEFT Arrow Key Move Focus Failed");
       
   220                     throw new RuntimeException("Focus is not on Radio Button C as Expected");
       
   221                 }
       
   222             }
   202             }
   223         });
   203         });
   224     }
   204     }
   225 
   205 
   226     private static void runTest5() throws Exception {
   206     private static void runTest5() throws Exception {
   227         hitKey(robot, KeyEvent.VK_UP);
   207         hitKey(robot, KeyEvent.VK_UP);
   228         hitKey(robot, KeyEvent.VK_LEFT);
   208         hitKey(robot, KeyEvent.VK_LEFT);
   229         SwingUtilities.invokeAndWait(new Runnable() {
   209         SwingUtilities.invokeAndWait(() -> {
   230             @Override
   210             if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn1) {
   231             public void run() {
   211                 System.out.println("Radio button Group Left/Up Arrow Key Move Focus Failed");
   232                 if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn1) {
   212                 throw new RuntimeException("Focus is not on Radio Button A as Expected");
   233                     System.out.println("Radio button Group Left/Up Arrow Key Move Focus Failed");
       
   234                     throw new RuntimeException("Focus is not on Radio Button A as Expected");
       
   235                 }
       
   236             }
   213             }
   237         });
   214         });
   238     }
   215     }
   239 
   216 
   240     private static void runTest6() throws Exception {
   217     private static void runTest6() throws Exception {
   241         hitKey(robot, KeyEvent.VK_UP);
   218         hitKey(robot, KeyEvent.VK_UP);
   242         hitKey(robot, KeyEvent.VK_UP);
   219         hitKey(robot, KeyEvent.VK_UP);
   243         SwingUtilities.invokeAndWait(new Runnable() {
   220         SwingUtilities.invokeAndWait(() -> {
   244             @Override
   221             if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn2) {
   245             public void run() {
   222                 System.out.println("Radio button Group Circle Back To First Button Test");
   246                 if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtn2) {
   223                 throw new RuntimeException("Focus is not on Radio Button B as Expected");
   247                     System.out.println("Radio button Group Circle Back To First Button Test");
       
   248                     throw new RuntimeException("Focus is not on Radio Button B as Expected");
       
   249                 }
       
   250             }
   224             }
   251         });
   225         });
   252     }
   226     }
   253 
   227 
   254     private static void runTest7() throws Exception {
   228     private static void runTest7() throws Exception {
   255         hitKey(robot, KeyEvent.VK_TAB);
   229         hitKey(robot, KeyEvent.VK_TAB);
   256         SwingUtilities.invokeAndWait(new Runnable() {
   230         SwingUtilities.invokeAndWait(() -> {
   257             @Override
   231             if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != btnMiddle) {
   258             public void run() {
   232                 System.out.println("Separate Component added in button group layout");
   259                 if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != btnMiddle) {
   233                 throw new RuntimeException("Focus is not on Middle Button as Expected");
   260                     System.out.println("Separate Component added in button group layout");
       
   261                     throw new RuntimeException("Focus is not on Middle Button as Expected");
       
   262                 }
       
   263             }
   234             }
   264         });
   235         });
   265     }
   236     }
   266 
   237 
   267     private static void runTest8() throws Exception {
   238     private static void runTest8() throws Exception {
   268         hitKey(robot, KeyEvent.VK_TAB);
   239         hitKey(robot, KeyEvent.VK_TAB);
   269         SwingUtilities.invokeAndWait(new Runnable() {
   240         SwingUtilities.invokeAndWait(() -> {
   270             @Override
   241             if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtnSingle) {
   271             public void run() {
   242                 System.out.println("Separate Component added in button group layout");
   272                 if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() != radioBtnSingle) {
   243                 throw new RuntimeException("Focus is not on Radio Button Single as Expected");
   273                     System.out.println("Separate Component added in button group layout");
       
   274                     throw new RuntimeException("Focus is not on Radio Button Single as Expected");
       
   275                 }
       
   276             }
   244             }
   277         });
   245         });
   278     }
   246     }
   279 
   247 
   280     private static void hitKey(Robot robot, int keycode) {
   248     private static void hitKey(Robot robot, int keycode) {