Path: blob/master/test/jdk/java/awt/Focus/RollbackFocusFromAnotherWindowTest/RollbackFocusFromAnotherWindowTest.java
41152 views
/*1* Copyright (c) 2016, 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 813921827@summary Dialog that opens and closes quickly changes focus in original28focusowner29@run main RollbackFocusFromAnotherWindowTest30*/3132import java.awt.*;33import java.awt.event.*;3435import javax.swing.*;3637public class RollbackFocusFromAnotherWindowTest extends JFrame38implements KeyListener39{40private static RollbackFocusFromAnotherWindowTest tfs;41private static Robot robot;4243public static void main(String[] args) throws Exception {44robot = new Robot();4546SwingUtilities.invokeAndWait(() -> {47tfs = new RollbackFocusFromAnotherWindowTest();48tfs.setVisible(true);49});5051robot.waitForIdle();52robot.delay(200);5354try {55for (int i = 0; i < 10; i++) {56robot.keyPress(KeyEvent.VK_A);57robot.delay(10);58robot.keyRelease(KeyEvent.VK_A);59robot.waitForIdle();60robot.delay(200);61SwingUtilities.invokeAndWait(() -> {62String name = tfs.getFocusOwner().getName();63System.out.println(name);64if (!"Comp0".equals(name)) {65throw new RuntimeException(66"Focus is not restored correctly");67}68});69}70System.out.println("ok");71} finally {72SwingUtilities.invokeLater(() -> tfs.dispose());73}74}7576public RollbackFocusFromAnotherWindowTest()77{78setUndecorated(true);79Container c = getContentPane();80c.setLayout(new FlowLayout());81for (int i = 0; i < 10; i++)82{83JTextField tf = new JTextField("" + i, 10);84tf.setName("Comp" + i);85c.add(tf);86tf.addKeyListener(this);87}88pack();89}9091@Override92public void keyTyped(KeyEvent e) {9394}9596@Override97public void keyPressed(KeyEvent e) {98Frame frame = new Frame();99frame.setVisible(true);100try {101Thread.sleep(2);102} catch (InterruptedException e1) {103e1.printStackTrace();104}105frame.dispose();106}107108@Override109public void keyReleased(KeyEvent e) {110111}112}113114115