Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/IconifiedFrameFocusChangeTest/IconifiedFrameFocusChangeTest.java
47867 views
/*1* Copyright (c) 2008, 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 652272526@summary Tests for proper request-focus-back on FOCUS_LOST.27@author Anton Tarasov: area=awt-focus28@library ../../regtesthelpers29@build Util30@run main IconifiedFrameFocusChangeTest31*/3233import java.awt.*;34import java.applet.Applet;35import java.awt.event.*;36import test.java.awt.regtesthelpers.Util;3738public class IconifiedFrameFocusChangeTest extends Applet {39Frame testFrame = new Frame("Test Frame");40Frame otherFrame = new Frame("Other Frame");41Button testButton = new Button("test button");42Button otherButton = new Button("other button");43Robot robot;4445public static void main(String[] args) {46IconifiedFrameFocusChangeTest app = new IconifiedFrameFocusChangeTest();47app.init();48app.start();49}5051public void init() {52robot = Util.createRobot();5354testFrame.add(testButton);55testFrame.pack();56otherFrame.add(otherButton);57otherFrame.pack();58otherFrame.setLocation(200, 0);5960testButton.addFocusListener(new FocusAdapter() {61public void focusLost(FocusEvent e) {62testButton.requestFocus();63}64});65}6667public void start() {68otherFrame.setVisible(true);69Util.waitForIdle(robot);70testFrame.setVisible(true);71Util.waitForIdle(robot);7273robot.delay(1000); // additional delay is required7475if (!testButton.hasFocus()) {76testButton.requestFocus();77Util.waitForIdle(robot);78if (!testButton.hasFocus()) {79throw new TestErrorException("couldn't focus " + testButton);80}81}8283/*84* Iconify the Frame. Test that focus switches properly.85*/86Runnable action = new Runnable() {87public void run() {88testFrame.setExtendedState(Frame.ICONIFIED);89}90};91if (!Util.trackFocusGained(otherButton, action, 2000, true)) {92throw new TestFailedException("iconifying focused window didn't trigger focus change");93}9495/*96* Test that key events go into the focus owner.97*/98action = new Runnable() {99public void run() {100robot.keyPress(KeyEvent.VK_SPACE);101robot.delay(50);102robot.keyRelease(KeyEvent.VK_SPACE);103}104};105if (!Util.trackActionPerformed(otherButton, action, 2000, true)) {106throw new TestFailedException("Java focus owner doesn't match to the native one");107}108109System.out.println("Test passed.");110}111}112113/**114* Thrown when the behavior being verified is found wrong.115*/116class TestFailedException extends RuntimeException {117TestFailedException(String msg) {118super("Test failed: " + msg);119}120}121122/**123* Thrown when an error not related to the behavior being verified is encountered.124*/125class TestErrorException extends RuntimeException {126TestErrorException(String msg) {127super("Unexpected error: " + msg);128}129}130131132