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