Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/FocusSubRequestTest/FocusSubRequestTest.java
47525 views
/*1* Copyright (c) 2004, 2014, 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/*24test25@bug 508231926@summary Tests that focus request for already focused component doesn't block key events.27@author [email protected]28@run applet FocusSubRequestTest.html29*/3031import java.applet.Applet;32import java.awt.*;33import java.awt.event.*;3435public class FocusSubRequestTest extends Applet {36Frame frame = new Frame("Test Frame");37Button button = new Button("button");38boolean passed = false;39Robot robot;4041public void init() {42frame.add(button);43button.addFocusListener(new FocusAdapter() {44public void focusGained(FocusEvent e) {45System.out.println("FocusSubRequestTest: focusGained for: " + e.getSource());46((Component)e.getSource()).requestFocus();47}48});4950button.addKeyListener(new KeyAdapter() {51public void keyPressed(KeyEvent e) {52System.out.println("FocusSubRequestTest: keyPressed for: " + e.getSource());53passed = true;54}55});5657try {58robot = new Robot();59} catch(Exception e) {60throw new RuntimeException("Error: unable to create robot", e);61}62}6364public void start() {65frame.pack();66frame.setLocation(getLocation().x + getSize().width + 20, 0);67frame.setVisible(true);6869waitTillShown(button);70frame.toFront();7172robot.delay(100);73robot.keyPress(KeyEvent.VK_K);74robot.delay(100);75robot.keyRelease(KeyEvent.VK_K);7677robot.waitForIdle();7879if(passed) {80System.out.println("Test passed.");81} else {82throw new RuntimeException("Test failed.");83}84}8586private void waitTillShown(Component component) {87while (true) {88try {89Thread.sleep(100);90component.getLocationOnScreen();91break;92} catch(InterruptedException ie) {93throw new RuntimeException(ie);94} catch(IllegalComponentStateException icse) {}95}96}97}9899100