1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 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 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 @bug 8129940 8132770 26 @summary JRadioButton should run custom FocusTraversalKeys for all LaFs 27 @run main FocusTraversal 28 */ 29 import javax.swing.*; 30 import java.awt.*; 31 import java.awt.Robot; 32 import java.awt.event.KeyEvent; 33 import java.util.HashSet; 34 import java.util.Set; 35 36 public class FocusTraversal { 37 38 private static JFrame frame; 39 private static JRadioButton a; 40 private static JRadioButton b; 41 private static JRadioButton c; 42 private static JRadioButton d; 43 private static JTextField next; 44 private static JTextField prev; 45 private static Robot robot; 46 47 public static void main(String[] args) throws Exception { 48 49 robot = new Robot(); 50 robot.setAutoDelay(5); 51 robot.delay(2000); 52 UIManager.LookAndFeelInfo[] lookAndFeelArray 53 = UIManager.getInstalledLookAndFeels(); 54 for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) { 55 executeCase(lookAndFeelItem.getClassName()); 56 } 57 } 58 59 private static void executeCase(String lookAndFeelString) throws Exception { 60 tryLookAndFeel(lookAndFeelString); 61 createUI(lookAndFeelString); 62 robot.delay(2000); 63 runTestCase(); 64 cleanUp(); 65 } 66 67 private static void createUI(String lookAndFeelString) throws Exception { 68 SwingUtilities.invokeAndWait(new Runnable() { 69 @Override 70 public void run() { 71 Set<KeyStroke> keystrokes = new HashSet<KeyStroke>(); 72 keystrokes.add(KeyStroke.getKeyStroke("TAB")); 73 keystrokes.add(KeyStroke.getKeyStroke("ENTER")); 74 frame = new JFrame("FocusTraversalTest " + lookAndFeelString); 75 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 76 frame.setUndecorated(true); 77 frame.setFocusTraversalKeys( 78 KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, 79 keystrokes); 80 81 a = new JRadioButton("a"); 82 b = new JRadioButton("b"); 83 c = new JRadioButton("c"); 84 d = new JRadioButton("d"); 85 86 ButtonGroup radioButtonGroup = new ButtonGroup(); 87 radioButtonGroup.add(a); 88 radioButtonGroup.add(b); 89 radioButtonGroup.add(c); 90 radioButtonGroup.add(d); 91 92 JPanel panel = new JPanel(); 93 prev = new JTextField("text"); 94 panel.add(prev); 95 panel.add(a); 96 panel.add(b); 97 panel.add(c); 98 panel.add(d); 99 next = new JTextField("text"); 100 panel.add(next); 101 102 JPanel root = new JPanel(); 103 root.setLayout(new BorderLayout()); 104 root.add(panel, BorderLayout.CENTER); 105 root.add(new JButton("OK"), BorderLayout.SOUTH); 106 107 frame.add(root); 108 frame.pack(); 109 frame.setLocationRelativeTo(null); 110 frame.setVisible(true); 111 frame.toFront(); 112 } 113 }); 114 } 115 116 private static void runTestCase() throws Exception { 117 LookAndFeel lookAndFeel = UIManager.getLookAndFeel(); 118 focusOn(a); 119 if (isExcludedLookAndFeel(lookAndFeel)) { 120 robot.keyPress(KeyEvent.VK_ENTER); 121 robot.keyRelease(KeyEvent.VK_ENTER); 122 robot.waitForIdle(); 123 isFocusOwner(b, "forward"); 124 robot.keyPress(KeyEvent.VK_SHIFT); 125 robot.keyPress(KeyEvent.VK_TAB); 126 robot.keyRelease(KeyEvent.VK_TAB); 127 robot.keyRelease(KeyEvent.VK_SHIFT); 128 robot.waitForIdle(); 129 isFocusOwner(a, "backward"); 130 131 } else { 132 133 robot.keyPress(KeyEvent.VK_ENTER); 134 robot.keyRelease(KeyEvent.VK_ENTER); 135 robot.waitForIdle(); 136 isFocusOwner(next, "forward"); 137 robot.keyPress(KeyEvent.VK_SHIFT); 138 robot.keyPress(KeyEvent.VK_TAB); 139 robot.keyRelease(KeyEvent.VK_TAB); 140 robot.keyRelease(KeyEvent.VK_SHIFT); 141 robot.waitForIdle(); 142 isFocusOwner(d, "backward"); 143 } 144 145 } 146 147 private static boolean isExcludedLookAndFeel(LookAndFeel lookAndFeel) { 148 149 return lookAndFeel.toString().toLowerCase().contains("aqua") 150 || lookAndFeel.toString().toLowerCase().contains("nimbus") 151 || lookAndFeel.toString().toLowerCase().contains("gtk"); 152 } 153 154 private static void focusOn(Component component) 155 throws Exception { 156 SwingUtilities.invokeAndWait(new Runnable() { 157 @Override 158 public void run() { 159 component.requestFocusInWindow(); 160 } 161 }); 162 } 163 164 private static void isFocusOwner(Component queriedFocusOwner, 165 String direction) 166 throws Exception { 167 SwingUtilities.invokeAndWait(new Runnable() { 168 @Override 169 public void run() { 170 Component actualFocusOwner 171 = FocusManager.getCurrentManager().getFocusOwner(); 172 if (actualFocusOwner != queriedFocusOwner) { 173 throw new RuntimeException( 174 "Focus component is wrong after " + direction 175 + " direction "); 176 177 } 178 } 179 }); 180 } 181 182 private static void tryLookAndFeel(String lookAndFeelString) 183 throws Exception { 184 try { 185 UIManager.setLookAndFeel( 186 lookAndFeelString); 187 } catch (UnsupportedLookAndFeelException 188 | ClassNotFoundException 189 | InstantiationException 190 | IllegalAccessException e) { 191 } 192 193 } 194 195 private static void cleanUp() throws Exception { 196 SwingUtilities.invokeAndWait(new Runnable() { 197 @Override 198 public void run() { 199 frame.dispose(); 200 } 201 }); 202 } 203 }