Path: blob/master/test/jdk/java/awt/Focus/IconifiedFrameFocusChangeTest/IconifiedFrameFocusChangeTest.java
41152 views
/*1* Copyright (c) 2008, 2018, 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 652272527@summary Tests for proper request-focus-back on FOCUS_LOST.28@library ../../regtesthelpers29@build Util30@run main IconifiedFrameFocusChangeTest31*/3233import java.awt.*;34import java.awt.event.*;35import test.java.awt.regtesthelpers.Util;3637public class IconifiedFrameFocusChangeTest {38Frame testFrame = new Frame("Test Frame");39Frame otherFrame = new Frame("Other Frame");40Button testButton = new Button("test button");41Button otherButton = new Button("other button");42Robot robot;4344public static void main(String[] args) {45IconifiedFrameFocusChangeTest app = new IconifiedFrameFocusChangeTest();46app.init();47app.start();48}4950public void init() {51robot = Util.createRobot();5253testFrame.add(testButton);54testFrame.pack();55otherFrame.add(otherButton);56otherFrame.pack();57otherFrame.setLocation(200, 0);5859testButton.addFocusListener(new FocusAdapter() {60public void focusLost(FocusEvent e) {61testButton.requestFocus();62}63});64}6566public void start() {67otherFrame.setVisible(true);68Util.waitForIdle(robot);69testFrame.setVisible(true);70Util.waitForIdle(robot);7172robot.delay(1000); // additional delay is required7374if (!testButton.hasFocus()) {75testButton.requestFocus();76Util.waitForIdle(robot);77if (!testButton.hasFocus()) {78throw new TestErrorException("couldn't focus " + testButton);79}80}8182/*83* Iconify the Frame. Test that focus switches properly.84*/85Runnable action = new Runnable() {86public void run() {87testFrame.setExtendedState(Frame.ICONIFIED);88}89};90if (!Util.trackFocusGained(otherButton, action, 2000, true)) {91throw new TestFailedException("iconifying focused window didn't trigger focus change");92}9394/*95* Test that key events go into the focus owner.96*/97action = new Runnable() {98public void run() {99robot.keyPress(KeyEvent.VK_SPACE);100robot.delay(50);101robot.keyRelease(KeyEvent.VK_SPACE);102}103};104if (!Util.trackActionPerformed(otherButton, action, 2000, true)) {105throw new TestFailedException("Java focus owner doesn't match to the native one");106}107108System.out.println("Test passed.");109}110}111112/**113* Thrown when the behavior being verified is found wrong.114*/115class TestFailedException extends RuntimeException {116TestFailedException(String msg) {117super("Test failed: " + msg);118}119}120121/**122* Thrown when an error not related to the behavior being verified is encountered.123*/124class TestErrorException extends RuntimeException {125TestErrorException(String msg) {126super("Unexpected error: " + msg);127}128}129130131