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 /* 25 * @test 26 * @key headful 27 * @bug 8073453 28 * @summary Focus doesn't move when pressing Shift + Tab keys 29 * @author Dmitry Markov 30 * @compile SwingFocusTransitionTest.java 31 * @run main/othervm SwingFocusTransitionTest 32 */ 33 import sun.awt.SunToolkit; 34 35 import javax.swing.*; 36 import java.awt.*; 37 import java.awt.event.KeyEvent; 38 39 public class SwingFocusTransitionTest { 40 private static SunToolkit toolkit; 41 private static Robot robot; 42 43 private static JFrame frame; 44 private static JTextField textField; 45 private static JButton button; 46 47 public static void main(String[] args) throws Exception { 48 toolkit = (SunToolkit)Toolkit.getDefaultToolkit(); 49 robot = new Robot(); 50 robot.setAutoDelay(50); 51 52 try { 53 SwingUtilities.invokeAndWait(new Runnable() { 54 @Override 55 public void run() { 56 createAndShowGUI(); 57 } 58 }); 59 60 toolkit.realSync(); 61 62 checkFocusOwner(textField); 63 64 robot.keyPress(KeyEvent.VK_TAB); 65 robot.keyRelease(KeyEvent.VK_TAB); 66 toolkit.realSync(); 67 68 checkFocusOwner(button); 69 70 robot.keyPress(KeyEvent.VK_SHIFT); 71 robot.keyPress(KeyEvent.VK_TAB); 72 robot.keyRelease(KeyEvent.VK_TAB); 73 robot.keyRelease(KeyEvent.VK_SHIFT); 74 toolkit.realSync(); 75 76 checkFocusOwner(textField); 77 78 robot.keyPress(KeyEvent.VK_SHIFT); 79 robot.keyPress(KeyEvent.VK_TAB); 80 robot.keyRelease(KeyEvent.VK_TAB); 81 robot.keyRelease(KeyEvent.VK_SHIFT); 82 toolkit.realSync(); 83 84 checkFocusOwner(button); 85 } finally { 86 SwingUtilities.invokeLater(new Runnable() { 87 @Override 88 public void run() { 89 if (frame != null) { 90 frame.dispose(); 91 } 92 } 93 }); 94 } 95 System.out.println("Test Passed!"); 96 } 97 98 private static void createAndShowGUI() { 99 frame = new JFrame("SwingFocusTransitionTest"); 100 frame.setSize(300, 300); 101 frame.setFocusTraversalPolicyProvider(true); 102 frame.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy()); 103 104 textField = new JTextField(); 105 button = new JButton(); 106 107 JPanel panel = new JPanel(); 108 panel.setFocusTraversalPolicyProvider(true); 109 panel.setFocusTraversalPolicy(new DefaultFocusTraversalPolicy()); 110 111 JPanel p = new JPanel(); 112 p.setLayout(new GridLayout(3, 1)); 113 p.add(textField); 114 p.add(button); 115 p.add(panel); 116 117 frame.add(p); 118 frame.setVisible(true); 119 } 120 121 private static void checkFocusOwner(final Component component) throws Exception { 122 SwingUtilities.invokeAndWait(new Runnable() { 123 @Override 124 public void run() { 125 if (component != frame.getFocusOwner()) { 126 throw new RuntimeException("Test Failed! Incorrect focus owner: " + frame.getFocusOwner() + 127 ", but expected: " + component); 128 } 129 } 130 }); 131 } 132 } 133