Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/RollbackFocusFromAnotherWindowTest/RollbackFocusFromAnotherWindowTest.java
47525 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@bug 813921826@summary Dialog that opens and closes quickly changes focus in original27focusowner28@author Semyon Sadetsky: area=awt-focus29@run main RollbackFocusFromAnotherWindowTest30*/3132import java.awt.*;33import java.awt.event.*;3435import javax.swing.*;3637public class RollbackFocusFromAnotherWindowTest extends JFrame implements KeyListener38{39private static RollbackFocusFromAnotherWindowTest tfs;40private static Robot robot;4142public static void main(String[] args) throws Exception {43robot = new Robot();4445SwingUtilities.invokeAndWait(() -> {46tfs = new RollbackFocusFromAnotherWindowTest();47tfs.setVisible(true);48});4950robot.waitForIdle();51robot.delay(200);5253try {54for (int i = 0; i < 10; i++) {55robot.keyPress(KeyEvent.VK_A);56robot.delay(10);57robot.keyRelease(KeyEvent.VK_A);58robot.waitForIdle();59robot.delay(200);60SwingUtilities.invokeAndWait(() -> {61String name = tfs.getFocusOwner().getName();62System.out.println(name);63if (!"Comp0".equals(name)) {64throw new RuntimeException(65"Focus is not restored correctly");66}67});68}69System.out.println("ok");70} finally {71SwingUtilities.invokeLater(() -> tfs.dispose());72}73}7475public RollbackFocusFromAnotherWindowTest()76{77setUndecorated(true);78Container c = getContentPane();79c.setLayout(new FlowLayout());80for (int i = 0; i < 10; i++)81{82JTextField tf = new JTextField("" + i, 10);83tf.setName("Comp" + i);84c.add(tf);85tf.addKeyListener(this);86}87pack();88}8990@Override91public void keyTyped(KeyEvent e) {9293}9495@Override96public void keyPressed(KeyEvent e) {97Frame frame = new Frame();98frame.setVisible(true);99try {100Thread.sleep(2);101} catch (InterruptedException e1) {102e1.printStackTrace();103}104frame.dispose();105}106107@Override108public void keyReleased(KeyEvent e) {109110}111112113}114115116