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