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 AWTFocusTransitionTest.java 31 * @run main/othervm AWTFocusTransitionTest 32 */ 33 import sun.awt.SunToolkit; 34 35 import java.awt.*; 36 import java.awt.event.KeyEvent; 37 38 public class AWTFocusTransitionTest { 39 private static SunToolkit toolkit; 40 private static Robot robot; 41 42 private static Frame frame; 43 private static TextField textField; 44 private static Button 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 createAndShowGUI(); 53 54 toolkit.realSync(); 55 56 checkFocusOwner(textField); 57 58 robot.keyPress(KeyEvent.VK_TAB); 59 robot.keyRelease(KeyEvent.VK_TAB); 60 toolkit.realSync(); 61 62 checkFocusOwner(button); 63 64 robot.keyPress(KeyEvent.VK_SHIFT); 65 robot.keyPress(KeyEvent.VK_TAB); 66 robot.keyRelease(KeyEvent.VK_TAB); 67 robot.keyRelease(KeyEvent.VK_SHIFT); 68 toolkit.realSync(); 69 70 checkFocusOwner(textField); 71 72 robot.keyPress(KeyEvent.VK_SHIFT); 73 robot.keyPress(KeyEvent.VK_TAB); 74 robot.keyRelease(KeyEvent.VK_TAB); 75 robot.keyRelease(KeyEvent.VK_SHIFT); 76 toolkit.realSync(); 77 78 checkFocusOwner(button); 79 } finally { 80 if (frame != null) { 81 frame.dispose(); 82 } 83 } 84 System.out.println("Test Passed!"); 85 } 86 87 private static void createAndShowGUI() { 88 frame = new Frame("AWTFocusTransitionTest"); 89 frame.setSize(300, 300); 90 frame.setFocusTraversalPolicyProvider(true); 91 frame.setFocusTraversalPolicy(new DefaultFocusTraversalPolicy()); 92 93 textField = new TextField(); 94 button = new Button(); 95 96 Panel panel = new Panel(); 97 panel.setFocusTraversalPolicyProvider(true); 98 panel.setFocusTraversalPolicy(new DefaultFocusTraversalPolicy()); 99 100 Panel p = new Panel(); 101 p.setLayout(new GridLayout(3, 1)); 102 p.add(textField); 103 p.add(button); 104 p.add(panel); 105 106 frame.add(p); 107 frame.setVisible(true); 108 } 109 110 private static void checkFocusOwner(Component component) { 111 if (component != frame.getFocusOwner()) { 112 throw new RuntimeException("Test Failed! Incorrect focus owner: " + frame.getFocusOwner() + 113 ", but expected: " + component); 114 } 115 } 116 } 117