Path: blob/master/test/jdk/java/awt/Focus/8073453/AWTFocusTransitionTest.java
41153 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 807345327* @summary Focus doesn't move when pressing Shift + Tab keys28* @compile AWTFocusTransitionTest.java29* @run main/othervm AWTFocusTransitionTest30*/3132import java.awt.Button;33import java.awt.Component;34import java.awt.DefaultFocusTraversalPolicy;35import java.awt.Frame;36import java.awt.GridLayout;37import java.awt.Panel;38import java.awt.Robot;39import java.awt.TextField;40import java.awt.event.KeyEvent;4142public class AWTFocusTransitionTest {43private static Robot robot;4445private static Frame frame;46private static TextField textField;47private static Button button;4849public static void main(String[] args) throws Exception {50robot = new Robot();51robot.setAutoDelay(100);5253try {54createAndShowGUI();5556robot.waitForIdle();5758checkFocusOwner(textField);5960robot.keyPress(KeyEvent.VK_TAB);61robot.keyRelease(KeyEvent.VK_TAB);62robot.waitForIdle();6364checkFocusOwner(button);6566robot.keyPress(KeyEvent.VK_SHIFT);67robot.keyPress(KeyEvent.VK_TAB);68robot.keyRelease(KeyEvent.VK_TAB);69robot.keyRelease(KeyEvent.VK_SHIFT);70robot.waitForIdle();7172checkFocusOwner(textField);7374robot.keyPress(KeyEvent.VK_SHIFT);75robot.keyPress(KeyEvent.VK_TAB);76robot.keyRelease(KeyEvent.VK_TAB);77robot.keyRelease(KeyEvent.VK_SHIFT);78robot.waitForIdle();7980checkFocusOwner(button);81} finally {82if (frame != null) {83frame.dispose();84}85}86System.out.println("Test Passed!");87}8889private static void createAndShowGUI() {90frame = new Frame("AWTFocusTransitionTest");91frame.setSize(300, 300);92frame.setFocusTraversalPolicyProvider(true);93frame.setFocusTraversalPolicy(new DefaultFocusTraversalPolicy());9495textField = new TextField();96button = new Button();9798Panel panel = new Panel();99panel.setFocusTraversalPolicyProvider(true);100panel.setFocusTraversalPolicy(new DefaultFocusTraversalPolicy());101102Panel p = new Panel();103p.setLayout(new GridLayout(3, 1));104p.add(textField);105p.add(button);106p.add(panel);107108frame.add(p);109frame.setLocationRelativeTo(null);110frame.setVisible(true);111}112113private static void checkFocusOwner(Component component) {114if (component != frame.getFocusOwner()) {115throw new RuntimeException("Test Failed! Incorrect focus " +116"owner: " + frame.getFocusOwner() +117", but expected: " + component);118}119}120}121122123