Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/8073453/SwingFocusTransitionTest.java
48429 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/* @test24* @bug 807345325* @summary Focus doesn't move when pressing Shift + Tab keys26* @author Dmitry Markov27* @compile SwingFocusTransitionTest.java28* @run main/othervm SwingFocusTransitionTest29*/30import sun.awt.SunToolkit;3132import javax.swing.*;33import java.awt.*;34import java.awt.event.KeyEvent;3536public class SwingFocusTransitionTest {37private static SunToolkit toolkit;38private static Robot robot;3940private static JFrame frame;41private static JTextField textField;42private static JButton button;4344public static void main(String[] args) throws Exception {45toolkit = (SunToolkit)Toolkit.getDefaultToolkit();46robot = new Robot();47robot.setAutoDelay(50);4849try {50SwingUtilities.invokeAndWait(new Runnable() {51@Override52public void run() {53createAndShowGUI();54}55});5657toolkit.realSync();5859checkFocusOwner(textField);6061robot.keyPress(KeyEvent.VK_TAB);62robot.keyRelease(KeyEvent.VK_TAB);63toolkit.realSync();6465checkFocusOwner(button);6667robot.keyPress(KeyEvent.VK_SHIFT);68robot.keyPress(KeyEvent.VK_TAB);69robot.keyRelease(KeyEvent.VK_TAB);70robot.keyRelease(KeyEvent.VK_SHIFT);71toolkit.realSync();7273checkFocusOwner(textField);7475robot.keyPress(KeyEvent.VK_SHIFT);76robot.keyPress(KeyEvent.VK_TAB);77robot.keyRelease(KeyEvent.VK_TAB);78robot.keyRelease(KeyEvent.VK_SHIFT);79toolkit.realSync();8081checkFocusOwner(button);82} finally {83SwingUtilities.invokeLater(new Runnable() {84@Override85public void run() {86if (frame != null) {87frame.dispose();88}89}90});91}92System.out.println("Test Passed!");93}9495private static void createAndShowGUI() {96frame = new JFrame("SwingFocusTransitionTest");97frame.setSize(300, 300);98frame.setFocusTraversalPolicyProvider(true);99frame.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy());100101textField = new JTextField();102button = new JButton();103104JPanel panel = new JPanel();105panel.setFocusTraversalPolicyProvider(true);106panel.setFocusTraversalPolicy(new DefaultFocusTraversalPolicy());107108JPanel p = new JPanel();109p.setLayout(new GridLayout(3, 1));110p.add(textField);111p.add(button);112p.add(panel);113114frame.add(p);115frame.setVisible(true);116}117118private static void checkFocusOwner(final Component component) throws Exception {119SwingUtilities.invokeAndWait(new Runnable() {120@Override121public void run() {122if (component != frame.getFocusOwner()) {123throw new RuntimeException("Test Failed! Incorrect focus owner: " + frame.getFocusOwner() +124", but expected: " + component);125}126}127});128}129}130131132133