Path: blob/master/test/jdk/java/awt/Focus/FocusSubRequestTest/FocusSubRequestTest.java
41152 views
/*1* Copyright (c) 2004, 2021, 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 508231927@summary Tests that focus request for already focused component doesn't block key events.28@run main FocusSubRequestTest29*/3031import java.awt.*;32import java.awt.event.*;3334public class FocusSubRequestTest {35Frame frame = new Frame("Test Frame");36Button button = new Button("button");37boolean passed = false;38Robot robot;3940public static void main(final String[] args) {41FocusSubRequestTest app = new FocusSubRequestTest();42app.init();43app.start();44}4546public void init() {47frame.add(button);48button.addFocusListener(new FocusAdapter() {49public void focusGained(FocusEvent e) {50System.out.println("FocusSubRequestTest: focusGained for: " + e.getSource());51((Component)e.getSource()).requestFocus();52}53});5455button.addKeyListener(new KeyAdapter() {56public void keyPressed(KeyEvent e) {57System.out.println("FocusSubRequestTest: keyPressed for: " + e.getSource());58passed = true;59}60});6162try {63robot = new Robot();64robot.setAutoDelay(100);65} catch(Exception e) {66throw new RuntimeException("Error: unable to create robot", e);67}68}6970public void start() {71frame.pack();72frame.setLocationRelativeTo(null);73frame.setVisible(true);7475waitTillShown(button);76frame.toFront();7778robot.delay(100);79robot.keyPress(KeyEvent.VK_K);80robot.keyRelease(KeyEvent.VK_K);8182robot.waitForIdle();8384if(passed) {85System.out.println("Test passed.");86} else {87throw new RuntimeException("Test failed.");88}89}9091private void waitTillShown(Component component) {92Point p = null;93while (p == null) {94try {95p = component.getLocationOnScreen();96} catch (IllegalStateException e) {97try {98Thread.sleep(500);99} catch (InterruptedException ie) {100}101}102}103}104}105106107