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 6314575 27 @summary Tests that previosly focused owned window doesn't steal focus when an owner's component requests focus. 28 @author Anton.Tarasov: area=awt.focus 29 @library ../../regtesthelpers 30 @build Util 31 @run main ActualFocusedWindowBlockingTest 32 */ 33 34 import java.awt.*; 35 import java.awt.event.*; 36 import java.applet.Applet; 37 import java.util.concurrent.atomic.AtomicBoolean; 38 import java.lang.reflect.InvocationTargetException; 39 import test.java.awt.regtesthelpers.Util; 40 41 public class ActualFocusedWindowBlockingTest extends Applet { 42 Robot robot = Util.createRobot(); 43 Frame owner = new Frame("Owner Frame"); 44 Window win = new Window(owner); 45 Frame frame = new Frame("Auxiliary Frame"); 46 Button fButton = new Button("frame button") {public String toString() {return "Frame_Button";}}; 47 Button wButton = new Button("window button") {public String toString() {return "Window_Button";}}; 48 Button aButton = new Button("auxiliary button") {public String toString() {return "Auxiliary_Button";}}; 49 50 public static void main(String[] args) { 51 ActualFocusedWindowBlockingTest app = new ActualFocusedWindowBlockingTest(); 52 app.init(); 53 app.start(); 54 } 55 56 public void init() { 57 Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() { 58 public void eventDispatched(AWTEvent e) { 59 System.out.println("--> " + e); 60 } 61 }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK); 62 63 owner.add(fButton); 64 win.add(wButton); 65 frame.add(aButton); 66 67 owner.setName("OWNER_FRAME"); 68 win.setName("OWNED_WINDOW"); 69 frame.setName("AUX_FRAME"); 70 71 tuneAndShowWindows(new Window[] {owner, win, frame}); 72 } 73 74 public void start() { 75 if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) { 76 System.out.println("No testing on Motif. Test passed."); 77 return; 78 } 79 80 System.out.println("\nTest started:\n"); 81 82 // Test 1. 83 84 clickOnCheckFocus(wButton); 85 clickOnCheckFocus(aButton); 86 87 Util.clickOnComp(fButton, robot); 88 if (!testFocused(fButton)) { 89 throw new TestFailedException("The owner's component [" + fButton + "] couldn't be focused by click"); 90 } 91 92 // Test 2. 93 94 clickOnCheckFocus(wButton); 95 clickOnCheckFocus(aButton); 96 97 fButton.requestFocus(); 98 Util.waitForIdle(robot); 99 if (!testFocused(fButton)) { 100 throw new TestFailedException("The owner's component [" + fButton + "] couldn't be focused by request"); 101 } 102 103 // Test 3. 104 105 clickOnCheckFocus(wButton); 106 clickOnCheckFocus(aButton); 107 clickOnCheckFocus(fButton); 108 clickOnCheckFocus(aButton); 109 110 Util.clickOnTitle(owner, robot); 111 if (!testFocused(fButton)) { 112 throw new TestFailedException("The owner's component [" + fButton + "] couldn't be focused as the most recent focus owner"); 113 } 114 115 System.out.println("Test passed."); 116 } 117 118 void tuneAndShowWindows(Window[] arr) { 119 int y = 0; 120 for (Window w: arr) { 121 w.setLayout(new FlowLayout()); 122 w.setBounds(100, y, 400, 150); 123 w.setBackground(Color.blue); 124 w.setVisible(true); 125 y += 200; 126 Util.waitForIdle(robot); 127 } 128 } 129 130 void clickOnCheckFocus(Component c) { 131 if (c instanceof Frame) { 132 Util.clickOnTitle((Frame)c, robot); 133 } else { 134 Util.clickOnComp(c, robot); 135 } 136 if (!testFocused(c)) { 137 throw new TestErrorException(c + "couldn't get focus by click."); 138 } 139 } 140 141 boolean testFocused(Component c) { 142 for (int i=0; i<10; i++) { 143 if (KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner() == c) { 144 return true; 145 } 146 Util.waitForIdle(robot); 147 } 148 return false; 149 } 150 151 // Thrown when the behavior being verified is found wrong. 152 class TestFailedException extends RuntimeException { 153 TestFailedException(String msg) { 154 super("Test failed: " + msg); 155 } 156 } 157 158 // Thrown when an error not related to the behavior being verified is encountered. 159 class TestErrorException extends RuntimeException { 160 TestErrorException(String msg) { 161 super("Unexpected error: " + msg); 162 } 163 } 164 }