1 /* 2 * Copyright (c) 2004, 2014, 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 import java.awt.*; 25 import java.awt.event.*; 26 27 /* 28 * @test 29 * @summary Active and check whether the respective WindowEvents are triggered 30 * properly and traverse the focus through focusable & non focusable 31 * components in a window and check whether the FocusEvents are 32 * triggered properly. 33 * @author Aruna Samji 34 * @library ../../../lib/testlibrary 35 * @build ExtendedRobot 36 * @run main TaskWindowFocusEvents 37 */ 38 39 public class TaskWindowFocusEvents extends Task<GUIMerlinFocus> { 40 41 public static void main (String[] args) throws Exception { 42 new TaskWindowFocusEvents(GUIMerlinFocus.class, new ExtendedRobot()).runTask(); 43 } 44 45 TaskWindowFocusEvents(Class guiClass, ExtendedRobot robot) throws Exception { 46 super(guiClass, robot); 47 } 48 49 public void task() throws Exception { 50 EventQueue.invokeAndWait(() -> { 51 gui.setBooleans(); 52 gui.frame1.setVisible(true); 53 gui.frame2.setVisible(true); 54 }); 55 robot.waitForIdle(1000); 56 57 EventQueue.invokeAndWait(gui::framesSetLocationAndSize); 58 robot.waitForIdle(1000); 59 60 // WindowEvent.WINDOW_DEACTIVATED must be dispatched to 61 // the Frame 62 63 // WindowEvent.WINDOW_ACTIVATED must be dispatched to 64 // Frame when this Frame becomes the active window 65 66 // An Active Window must be either of the following 67 // Frame which is the Focused Window 68 // First Frame that is the owner of the focused Window 69 70 Point movePoint = gui.frame2.getLocationOnScreen(); 71 movePoint.translate(gui.frame2.getWidth()/2, gui.frame2.getHeight()/2); 72 robot.mouseMove(movePoint); 73 robot.waitForIdle(1000); 74 robot.click(); 75 76 gui.setBooleans(); 77 78 movePoint = gui.frame1.getLocationOnScreen(); 79 movePoint.translate(gui.frame1.getWidth()/2, gui.frame1.getHeight()/2); 80 robot.mouseMove(movePoint); 81 robot.waitForIdle(1000); 82 robot.click(); 83 84 if (!gui.frame1Activated) 85 throw new RuntimeException("Window activated event is not fired for " + 86 gui.frame1); 87 88 if (!gui.frame2Deactivated) 89 throw new RuntimeException("Window deactivated event is not fired for " + 90 gui.frame2); 91 92 if (!gui.frame1.isActive()) 93 throw new RuntimeException("Not an active window " + gui.frame1); 94 95 // WindowEvent.WINDOW_DEACTIVATED must be dispatched to 96 // the Dialog 97 98 // WindowEvent.WINDOW_ACTIVATED must be dispatched to 99 // Dialog when this Dialog becomes the active window 100 101 // An Active Window must be either of the following 102 // Dialog which is the Focused Window 103 // First Dialog that is the owner of the focused Window 104 105 EventQueue.invokeAndWait(() -> { 106 gui.dialog1.setVisible(true); 107 gui.dialog2.setVisible(true); 108 }); 109 robot.waitForIdle(1000); 110 111 EventQueue.invokeAndWait(gui::dialogsSetLocationAndSize); 112 robot.waitForIdle(1000); 113 114 movePoint = gui.dialog2.getLocationOnScreen(); 115 movePoint.translate(gui.dialog2.getWidth()/2, gui.dialog2.getHeight()/2); 116 robot.mouseMove(movePoint); 117 robot.waitForIdle(1000); 118 robot.click(); 119 120 gui.setBooleans(); 121 122 movePoint = gui.dialog1.getLocationOnScreen(); 123 movePoint.translate(gui.dialog1.getWidth()/2, gui.dialog1.getHeight()/2); 124 robot.mouseMove(movePoint); 125 robot.waitForIdle(1000); 126 robot.click(); 127 128 if (!gui.dialog1Activated) 129 throw new RuntimeException("Window activated event is not fired for " + 130 gui.dialog1); 131 132 if (!gui.dialog2Deactivated) 133 throw new RuntimeException("Window deactivated event is not fired for " + 134 gui.dialog2); 135 136 if (!gui.dialog1.isActive()) 137 throw new RuntimeException("Not an active window " + gui.dialog1); 138 139 gui.setBooleans(); 140 EventQueue.invokeAndWait(() -> { 141 gui.frame1.dispose(); 142 gui.frame2.dispose(); 143 gui.dialog1.dispose(); 144 gui.dialog2.dispose(); 145 146 gui.frame3.setVisible(true); 147 }); 148 robot.waitForIdle(1000); 149 150 // Only focus traversable component can receive 151 // FocusEvent.FOCUS_GAINED event. When client code or user 152 // try to focus a non focus traversable component, 153 // this component can not receive FocusEvent.FOCUS_GAINED event 154 155 // Bring mouse pointer on to button 156 Point buttonCenter = gui.button1.getLocationOnScreen(); 157 buttonCenter.translate(gui.button1.getWidth()/2, gui.button1.getHeight()/2); 158 robot.mouseMove(buttonCenter); 159 robot.waitForIdle(1000); 160 robot.click(); 161 162 if (gui.textfieldFocusLost) 163 throw new RuntimeException("Focus lost event has triggered for text " + 164 "field when clicked on disabled component.\n"); 165 166 if (gui.button1FocusGained) 167 throw new RuntimeException("Disabled component gained focus " + 168 gui.button1); 169 170 // FocusEvent.FOCUS_GAINED must be dispatched to a 171 // focus traversable component when this component 172 // becomes the Focus Owner 173 174 // FocusEvent.FOCUS_LOST must be dispatched to a component 175 // when this component is no longer a Focus Owner 176 177 gui.setBooleans(); 178 179 Point textfieldCenter = gui.textfield.getLocationOnScreen(); 180 textfieldCenter.translate(gui.textfield.getWidth() / 2, gui.textfield.getHeight() / 2); 181 robot.mouseMove(textfieldCenter); 182 robot.waitForIdle(); 183 robot.click(); 184 robot.type(KeyEvent.VK_TAB); 185 186 if( !gui.textfieldFocusLost ) 187 throw new RuntimeException("When tab is pressed, Focus lost event " + 188 "didn't fire for " + gui.textfield); 189 190 if(!gui.textareaFocusGained) 191 throw new RuntimeException("When tab is pressed, Focus gained event " + 192 "didn't fire for " + gui.textarea); 193 194 EventQueue.invokeAndWait(gui.frame3::dispose); 195 robot.waitForIdle(1000); 196 } 197 }