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 @key headful 27 @bug 4823903 28 @summary Tests actual focused window retaining. 29 @author Anton.Tarasov: area=awt.focus 30 @library ../../regtesthelpers 31 @build Util 32 @run main ActualFocusedWindowRetaining 33 */ 34 35 import java.awt.*; 36 import java.awt.event.*; 37 import java.lang.reflect.*; 38 import java.applet.*; 39 import test.java.awt.regtesthelpers.Util; 40 41 public class ActualFocusedWindowRetaining extends Applet { 42 public static Frame frame = new Frame("Other Frame"); 43 public static Frame owner = new Frame("Test Frame"); 44 public static Button otherButton1 = new Button("Other Button 1"); 45 public static Button otherButton2 = new Button("Other Button 2"); 46 public static Button otherButton3 = new Button("Other Button 3"); 47 public static Button testButton1 = new Button("Test Button 1"); 48 public static Button testButton2 = new Button("Test Button 2"); 49 public static Button testButton3 = new Button("Test Button 3"); 50 public static Window window1 = new TestWindow(owner, otherButton2, testButton2, 800, 200); 51 public static Window window2 = new TestWindow(owner, otherButton3, testButton3, 800, 300); 52 public static int step; 53 public static Robot robot = Util.createRobot(); 54 55 public static void main(String[] args) { 56 ActualFocusedWindowRetaining a = new ActualFocusedWindowRetaining(); 57 a.init(); 58 a.start(); 59 } 60 61 public void start () { 62 Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() { 63 public void eventDispatched(AWTEvent e) { 64 Object src = e.getSource(); 65 Class cls = src.getClass(); 66 67 if (cls == TestWindow.class) { 68 System.out.println(e.paramString() + " on <" + (src == window1 ? "Window 1" : "Window 2") + ">"); 69 } else if (cls == Frame.class) { 70 System.out.println(e.paramString() + " on <" + ((Frame)src).getTitle() + ">"); 71 } else if (cls == Button.class) { 72 System.out.println(e.paramString() + " on <" + ((Button)src).getLabel() + ">"); 73 } else { 74 System.out.println(e.paramString() + " on <Non-testing component>"); 75 } 76 } 77 }, AWTEvent.WINDOW_EVENT_MASK | AWTEvent.WINDOW_FOCUS_EVENT_MASK | AWTEvent.FOCUS_EVENT_MASK); 78 79 setSize (500, 200); 80 setVisible(true); 81 validate(); 82 83 frame.setSize(new Dimension(400, 100)); 84 frame.setLocation(800, 400); 85 frame.setVisible(true); 86 frame.toFront(); 87 88 owner.setLayout(new FlowLayout()); 89 owner.add(testButton1); 90 owner.add(otherButton1); 91 owner.pack(); 92 owner.setLocation(800, 100); 93 owner.setSize(new Dimension(400, 100)); 94 owner.setVisible(true); 95 owner.toFront(); 96 Util.waitTillShown(owner); 97 98 window1.setVisible(true); 99 window2.setVisible(true); 100 window1.toFront(); 101 window2.toFront(); 102 // Wait longer... 103 Util.waitTillShown(window1); 104 Util.waitTillShown(window2); 105 106 test(); 107 108 frame.dispose(); 109 owner.dispose(); 110 } 111 112 public void test() { 113 Button[] butArr = new Button[] {testButton3, testButton2, testButton1}; 114 Window[] winArr = new Window[] {window2, window1, owner}; 115 116 step = 1; 117 for (int i = 0; i < 3; i++) { 118 clickInSeriesCheckFocus(null, butArr[i], frame); 119 clickOwnerCheckFocus(winArr[i], butArr[i]); 120 step++; 121 } 122 123 step = 4; 124 clickInSeriesCheckFocus(testButton3, testButton1, frame); 125 clickOwnerCheckFocus(owner, testButton1); 126 127 step = 5; 128 clickInSeriesCheckFocus(testButton3, testButton2, frame); 129 clickOwnerCheckFocus(window1, testButton2); 130 131 step = 6; 132 clickInSeriesCheckFocus(testButton1, testButton2, frame); 133 clickOwnerCheckFocus(window1, testButton2); 134 135 step = 7; 136 clickInSeriesCheckFocus(testButton1, testButton2, frame); 137 window1.setVisible(false); 138 Util.waitForIdle(robot); 139 clickOwnerCheckFocus(owner, testButton1); 140 141 step = 8; 142 window1.setVisible(true); 143 Util.waitTillShown(window1); 144 clickInSeriesCheckFocus(null, testButton2, frame); 145 clickOwnerCheckFocus(window1, testButton2); 146 } 147 148 boolean checkFocusOwner(Component comp) { 149 return (comp == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()); 150 } 151 152 boolean checkFocusedWindow(Window win) { 153 return (win == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow()); 154 } 155 156 void clickOwnerCheckFocus(Window focusedWindow, Component focusedComp) { 157 Util.clickOnTitle(owner, robot); 158 robot.delay(500); 159 160 if (!checkFocusedWindow(focusedWindow)) { 161 stopTest("Test failed: actual focused window didn't get a focus"); 162 } 163 if (!checkFocusOwner(focusedComp)) { 164 stopTest("Test failed: actual focus owner didn't get a focus"); 165 } 166 } 167 168 void clickInSeriesCheckFocus(Component comp1, Component comp2, Frame frame) { 169 if (comp1 != null) { 170 clickOnCheckFocusOwner(comp1); 171 } 172 if (comp2 != null) { 173 clickOnCheckFocusOwner(comp2); 174 } 175 clickOnCheckFocusedWindow(frame); 176 } 177 178 void clickOnCheckFocusOwner(Component c) { 179 Util.clickOnComp(c, robot); 180 robot.delay(500); 181 182 if (!checkFocusOwner(c)) { 183 stopTest("Error: can't bring a focus on Component by clicking on it"); 184 } 185 } 186 187 void clickOnCheckFocusedWindow(Frame f) { 188 Util.clickOnTitle(f, robot); 189 robot.delay(500); 190 191 if (!checkFocusedWindow(f)) { 192 stopTest("Error: can't bring a focus on Frame by clicking on it"); 193 } 194 } 195 196 void stopTest(String msg) { 197 throw new RuntimeException(new String("Step " + step + ": " + msg)); 198 } 199 } 200 201 class TestWindow extends Window { 202 TestWindow(Frame owner, Button otherButton, Button testButton, int x, int y) { 203 super(owner); 204 205 setLayout(new FlowLayout()); 206 setLocation(x, y); 207 add(testButton); 208 add(otherButton); 209 pack(); 210 setBackground(Color.green); 211 } 212 }