1 /* 2 * Copyright (c) 2008, 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 @bug 6522725 27 @summary Tests for proper request-focus-back on FOCUS_LOST. 28 @author Anton Tarasov: area=awt-focus 29 @library ../../regtesthelpers 30 @build Util 31 @run main IconifiedFrameFocusChangeTest 32 */ 33 34 import java.awt.*; 35 import java.applet.Applet; 36 import java.awt.event.*; 37 import test.java.awt.regtesthelpers.Util; 38 39 public class IconifiedFrameFocusChangeTest extends Applet { 40 Frame testFrame = new Frame("Test Frame"); 41 Frame otherFrame = new Frame("Other Frame"); 42 Button testButton = new Button("test button"); 43 Button otherButton = new Button("other button"); 44 Robot robot; 45 46 public static void main(String[] args) { 47 IconifiedFrameFocusChangeTest app = new IconifiedFrameFocusChangeTest(); 48 app.init(); 49 app.start(); 50 } 51 52 public void init() { 53 robot = Util.createRobot(); 54 55 testFrame.add(testButton); 56 testFrame.pack(); 57 otherFrame.add(otherButton); 58 otherFrame.pack(); 59 otherFrame.setLocation(200, 0); 60 61 testButton.addFocusListener(new FocusAdapter() { 62 public void focusLost(FocusEvent e) { 63 testButton.requestFocus(); 64 } 65 }); 66 } 67 68 public void start() { 69 otherFrame.setVisible(true); 70 Util.waitForIdle(robot); 71 testFrame.setVisible(true); 72 Util.waitForIdle(robot); 73 74 robot.delay(1000); // additional delay is required 75 76 if (!testButton.hasFocus()) { 77 testButton.requestFocus(); 78 Util.waitForIdle(robot); 79 if (!testButton.hasFocus()) { 80 throw new TestErrorException("couldn't focus " + testButton); 81 } 82 } 83 84 /* 85 * Iconify the Frame. Test that focus switches properly. 86 */ 87 Runnable action = new Runnable() { 88 public void run() { 89 testFrame.setExtendedState(Frame.ICONIFIED); 90 } 91 }; 92 if (!Util.trackFocusGained(otherButton, action, 2000, true)) { 93 throw new TestFailedException("iconifying focused window didn't trigger focus change"); 94 } 95 96 /* 97 * Test that key events go into the focus owner. 98 */ 99 action = new Runnable() { 100 public void run() { 101 robot.keyPress(KeyEvent.VK_SPACE); 102 robot.delay(50); 103 robot.keyRelease(KeyEvent.VK_SPACE); 104 } 105 }; 106 if (!Util.trackActionPerformed(otherButton, action, 2000, true)) { 107 throw new TestFailedException("Java focus owner doesn't match to the native one"); 108 } 109 110 System.out.println("Test passed."); 111 } 112 } 113 114 /** 115 * Thrown when the behavior being verified is found wrong. 116 */ 117 class TestFailedException extends RuntimeException { 118 TestFailedException(String msg) { 119 super("Test failed: " + msg); 120 } 121 } 122 123 /** 124 * Thrown when an error not related to the behavior being verified is encountered. 125 */ 126 class TestErrorException extends RuntimeException { 127 TestErrorException(String msg) { 128 super("Unexpected error: " + msg); 129 } 130 }