1 /* 2 * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 @test 26 @key headful 27 @bug 6522725 28 @summary Tests for proper request-focus-back on FOCUS_LOST. 29 @author Anton Tarasov: area=awt-focus 30 @library ../../regtesthelpers 31 @build Util 32 @run main IconifiedFrameFocusChangeTest 33 */ 34 35 import java.awt.*; 36 import java.applet.Applet; 37 import java.awt.event.*; 38 import test.java.awt.regtesthelpers.Util; 39 40 public class IconifiedFrameFocusChangeTest extends Applet { 41 Frame testFrame = new Frame("Test Frame"); 42 Frame otherFrame = new Frame("Other Frame"); 43 Button testButton = new Button("test button"); 44 Button otherButton = new Button("other button"); 45 Robot robot; 46 47 public static void main(String[] args) { 48 IconifiedFrameFocusChangeTest app = new IconifiedFrameFocusChangeTest(); 49 app.init(); 50 app.start(); 51 } 52 53 public void init() { 54 robot = Util.createRobot(); 55 56 testFrame.add(testButton); 57 testFrame.pack(); 58 otherFrame.add(otherButton); 59 otherFrame.pack(); 60 otherFrame.setLocation(200, 0); 61 62 testButton.addFocusListener(new FocusAdapter() { 63 public void focusLost(FocusEvent e) { 64 testButton.requestFocus(); 65 } 66 }); 67 } 68 69 public void start() { 70 otherFrame.setVisible(true); 71 Util.waitForIdle(robot); 72 testFrame.setVisible(true); 73 Util.waitForIdle(robot); 74 75 robot.delay(1000); // additional delay is required 76 77 if (!testButton.hasFocus()) { 78 testButton.requestFocus(); 79 Util.waitForIdle(robot); 80 if (!testButton.hasFocus()) { 81 throw new TestErrorException("couldn't focus " + testButton); 82 } 83 } 84 85 /* 86 * Iconify the Frame. Test that focus switches properly. 87 */ 88 Runnable action = new Runnable() { 89 public void run() { 90 testFrame.setExtendedState(Frame.ICONIFIED); 91 } 92 }; 93 if (!Util.trackFocusGained(otherButton, action, 2000, true)) { 94 throw new TestFailedException("iconifying focused window didn't trigger focus change"); 95 } 96 97 /* 98 * Test that key events go into the focus owner. 99 */ 100 action = new Runnable() { 101 public void run() { 102 robot.keyPress(KeyEvent.VK_SPACE); 103 robot.delay(50); 104 robot.keyRelease(KeyEvent.VK_SPACE); 105 } 106 }; 107 if (!Util.trackActionPerformed(otherButton, action, 2000, true)) { 108 throw new TestFailedException("Java focus owner doesn't match to the native one"); 109 } 110 111 System.out.println("Test passed."); 112 } 113 } 114 115 /** 116 * Thrown when the behavior being verified is found wrong. 117 */ 118 class TestFailedException extends RuntimeException { 119 TestFailedException(String msg) { 120 super("Test failed: " + msg); 121 } 122 } 123 124 /** 125 * Thrown when an error not related to the behavior being verified is encountered. 126 */ 127 class TestErrorException extends RuntimeException { 128 TestErrorException(String msg) { 129 super("Unexpected error: " + msg); 130 } 131 }