1 /* 2 * Copyright (c) 2007, 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 25 import java.awt.*; 26 import java.awt.event.*; 27 28 import static jdk.test.lib.Asserts.*; 29 30 31 32 public class TestDialog extends Dialog implements ActionListener, 33 FocusListener, WindowFocusListener, WindowListener { 34 35 public Button closeButton, openButton, dummyButton; 36 37 public Flag closeClicked, openClicked, dummyClicked; 38 public Flag closeGained, openGained, dummyGained; 39 public Flag closeLost, openLost, dummyLost; 40 public Flag focusGained, focusLost; 41 public Flag activated; 42 43 public Panel topPanel; 44 45 public static int delay = 500; 46 public static int keyDelay = 100; 47 48 public TestDialog(Frame frame) { 49 super(frame); 50 initializeGUI(); 51 } 52 53 public TestDialog(Dialog dialog) { 54 super(dialog); 55 initializeGUI(); 56 } 57 58 public void resetStatus() { 59 activated.reset(); 60 focusGained.reset(); 61 closeGained.reset(); 62 openGained.reset(); 63 closeClicked.reset(); 64 openClicked.reset(); 65 } 66 67 private void initFlags() { 68 closeClicked = new Flag(); 69 openClicked = new Flag(); 70 dummyClicked = new Flag(); 71 closeGained = new Flag(); 72 openGained = new Flag(); 73 dummyGained = new Flag(); 74 closeLost = new Flag(); 75 openLost = new Flag(); 76 dummyLost = new Flag(); 77 focusGained = new Flag(); 78 focusLost = new Flag(); 79 activated = new Flag(); 80 } 81 82 private void initializeGUI() { 83 84 initFlags(); 85 this.setTitle("Dialog"); 86 87 this.addWindowFocusListener(this); 88 this.addWindowListener(this); 89 90 this.setLayout(new GridLayout(3, 1)); 91 92 topPanel = new Panel(); 93 topPanel.setFocusable(false); 94 this.add(topPanel); 95 96 Panel p = new Panel(); 97 p.setLayout(new GridLayout(1, 3)); 98 99 closeButton = new Button("Close"); 100 closeButton.addActionListener(this); 101 closeButton.addFocusListener(this); 102 103 openButton = new Button("Open"); 104 openButton.addActionListener(this); 105 openButton.addFocusListener(this); 106 107 dummyButton = new Button("Dummy"); 108 dummyButton.addActionListener(this); 109 dummyButton.addFocusListener(this); 110 111 p.add(closeButton); 112 p.add(openButton); 113 p.add(dummyButton); 114 115 this.add(p); 116 117 Panel bottomPanel = new Panel(); 118 bottomPanel.setFocusable(false); 119 this.add(bottomPanel); 120 121 setSize(150, 150); 122 } 123 124 public void doOpenAction() {} 125 public void doCloseAction() {} 126 public void doDummyAction() {} 127 128 @Override 129 public void actionPerformed(ActionEvent event) { 130 if (closeButton.equals(event.getSource())) { 131 closeClicked.flagTriggered(); 132 doCloseAction(); 133 } else if (openButton.equals(event.getSource())) { 134 openClicked.flagTriggered(); 135 doOpenAction(); 136 } else if (dummyButton.equals(event.getSource())) { 137 dummyClicked.flagTriggered(); 138 doDummyAction(); 139 } 140 } 141 142 @Override 143 public void focusGained(FocusEvent event) { 144 if (closeButton.equals(event.getSource())) { 145 closeGained.flagTriggered(); 146 } else if (openButton.equals(event.getSource())) { 147 openGained.flagTriggered(); 148 } else if (dummyButton.equals(event.getSource())) { 149 dummyGained.flagTriggered(); 150 } 151 } 152 153 @Override 154 public void focusLost(FocusEvent event) { 155 if (closeButton.equals(event.getSource())) { 156 closeLost.flagTriggered(); 157 } else if (openButton.equals(event.getSource())) { 158 openLost.flagTriggered(); 159 } else if (dummyButton.equals(event.getSource())) { 160 dummyLost.flagTriggered(); 161 } 162 } 163 164 @Override 165 public void windowGainedFocus(WindowEvent event) { 166 focusGained.flagTriggered(); 167 } 168 169 @Override 170 public void windowLostFocus(WindowEvent event) { 171 focusLost.flagTriggered(); 172 } 173 174 @Override 175 public void windowActivated(WindowEvent e) { 176 activated.flagTriggered(); 177 } 178 179 @Override 180 public void windowClosed(WindowEvent e) { 181 } 182 183 @Override 184 public void windowClosing(WindowEvent e) { 185 assertTrue(false, "user closed window"); 186 } 187 188 @Override 189 public void windowDeactivated(WindowEvent e) {} 190 @Override 191 public void windowDeiconified(WindowEvent e) {} 192 @Override 193 public void windowIconified(WindowEvent e) {} 194 @Override 195 public void windowOpened(WindowEvent e) {} 196 197 public void clickButton(Button b, ExtendedRobot robot) { 198 199 try { 200 Flag.waitTillShown(b); 201 } catch (InterruptedException e) {} 202 203 if ((closeButton.equals(b) || openButton.equals(b) || 204 dummyButton.equals(b)) && robot != null) { 205 robot.mouseMove((int) b.getLocationOnScreen().x + b.getSize().width / 2, 206 (int) b.getLocationOnScreen().y + b.getSize().height / 2); 207 robot.delay(delay); 208 robot.click(); 209 robot.delay(delay); 210 } 211 } 212 213 public void clickOpenButton(ExtendedRobot robot) throws Exception { 214 clickOpenButton(robot, true, ""); 215 } 216 217 public void clickOpenButton(ExtendedRobot robot, 218 boolean refState, 219 String message) throws Exception { 220 openClicked.reset(); 221 clickButton(openButton, robot); 222 openClicked.waitForFlagTriggered(); 223 224 String msg = "Clicking the dialog Open button " + (refState ? 225 "did not trigger an action." : 226 "triggered an action when it should not."); 227 assertEQ(openClicked.flag(), refState, msg + " " + message); 228 } 229 230 public void clickCloseButton(ExtendedRobot robot) throws Exception { 231 clickCloseButton(robot, true, ""); 232 } 233 234 public void clickCloseButton(ExtendedRobot robot, 235 boolean refState, 236 String message) throws Exception { 237 closeClicked.reset(); 238 clickButton(closeButton, robot); 239 closeClicked.waitForFlagTriggered(); 240 241 String msg = "Clicking the dialog Close button " + (refState ? 242 "did not trigger an action." : 243 "triggered an action when it should not."); 244 assertEQ(closeClicked.flag(), refState, msg + " " + message); 245 } 246 247 public void clickDummyButton(ExtendedRobot robot) throws Exception { 248 clickDummyButton(robot, Flag.ATTEMPTS); 249 } 250 251 public void clickDummyButton(ExtendedRobot robot, 252 int attempts) throws Exception { 253 clickDummyButton(robot, attempts, true, ""); 254 } 255 256 public void clickDummyButton(ExtendedRobot robot, 257 int attempts, 258 boolean refState, 259 String message) throws Exception { 260 dummyClicked.reset(); 261 clickButton(dummyButton, robot); 262 dummyClicked.waitForFlagTriggered(attempts); 263 264 String msg = "Clicking the dialog Dummy button " + (refState ? 265 "did not trigger an action." : 266 "triggered an action when it should not."); 267 assertEQ(dummyClicked.flag(), refState, msg + " " + message); 268 } 269 270 271 private void clickInside(ExtendedRobot robot) throws Exception { 272 273 try { 274 Flag.waitTillShown(topPanel); 275 } catch (InterruptedException e) {} 276 277 if (robot != null) { 278 robot.mouseMove((int) topPanel.getLocationOnScreen().x + topPanel.getSize().width / 2, 279 (int) topPanel.getLocationOnScreen().y + topPanel.getSize().height / 2); 280 robot.delay(delay); 281 robot.click(); 282 robot.delay(delay); 283 } 284 } 285 286 public void transferFocusToDialog(ExtendedRobot robot, 287 String message, 288 Button b) throws Exception { 289 focusGained.reset(); 290 clickInside(robot); 291 focusGained.waitForFlagTriggered(); 292 assertTrue(focusGained.flag(), 293 "clicking inside the Dialog did not make it focused. " + message); 294 295 robot.waitForIdle(delay); 296 if (b != null) { 297 assertTrue(b.hasFocus(), "button " + b.getLabel() + 298 " did not gain focus when Dialog brought to top"); 299 } 300 } 301 302 public void transferFocusToBlockedDialog(ExtendedRobot robot, 303 String message, 304 Button b) throws Exception { 305 focusGained.reset(); 306 clickInside(robot); 307 robot.waitForIdle(delay); 308 309 assertFalse(focusGained.flag(), 310 "clicking inside a blocked Dialog made it focused. " + message); 311 312 robot.waitForIdle(delay); 313 if (b != null) { 314 assertFalse(b.hasFocus(), 315 "button " + b.getLabel() + " present in a blocked dialog gained focus"); 316 } 317 } 318 319 public void checkBlockedDialog(ExtendedRobot robot, 320 String message) throws Exception { 321 dummyGained.reset(); 322 dummyClicked.reset(); 323 focusGained.reset(); 324 325 clickButton(dummyButton, robot); 326 327 robot.waitForIdle(delay); 328 329 assertFalse(dummyClicked.flag(), 330 "DummyButton on blocked Dialog triggered action when clicked. " + message); 331 332 assertFalse(dummyGained.flag(), 333 "DummyButton on blocked Dialog gained focus when clicked. " + message); 334 335 assertFalse(focusGained.flag(), 336 "A blocked Dialog gained focus when component clicked. " + message); 337 } 338 339 public void checkUnblockedDialog(ExtendedRobot robot, 340 String message) throws Exception { 341 dummyGained.reset(); 342 dummyClicked.reset(); 343 clickButton(dummyButton, robot); 344 345 dummyGained.waitForFlagTriggered(); 346 assertTrue(dummyGained.flag(), 347 "DummyButton on Dialog did not gain focus when clicked. " + message); 348 349 dummyClicked.waitForFlagTriggered(); 350 assertTrue(dummyClicked.flag(), 351 "DummyButton on Dialog did not trigger action when clicked. " + message); 352 353 closeGained.reset(); 354 robot.type(KeyEvent.VK_TAB); 355 356 closeGained.waitForFlagTriggered(); 357 assertTrue(closeGained.flag(), 358 "Tab navigation on Dialog did not happen properly. First " + 359 "button did not gain focus. " + message); 360 } 361 362 public void checkCloseButtonFocusGained(boolean refState) { 363 checkCloseButtonFocusGained(refState, Flag.ATTEMPTS); 364 } 365 366 public void checkCloseButtonFocusGained(boolean refState, int attempts) { 367 try { 368 closeGained.waitForFlagTriggered(attempts); 369 } catch (InterruptedException e) {} 370 371 String msg = "dialog Close button "; 372 msg += (refState ? "did not gain focus" : 373 "gained focus when it should not"); 374 375 assertTrue(closeGained.flag() == refState, msg); 376 } 377 378 public void checkOpenButtonFocusGained(boolean refState) { 379 try { 380 openGained.waitForFlagTriggered(); 381 } catch (InterruptedException e) {} 382 383 String msg = "dialog Open button "; 384 msg += (refState ? "did not gain focus" : 385 "gained focus when it should not"); 386 387 assertTrue(openGained.flag() == refState, msg); 388 } 389 390 public void checkOpenButtonFocusLost(boolean refState) { 391 checkOpenButtonFocusLost(refState, Flag.ATTEMPTS); 392 } 393 394 public void checkOpenButtonFocusLost(boolean refState, int attempts) { 395 try { 396 openLost.waitForFlagTriggered(attempts); 397 } catch (InterruptedException e) {} 398 399 String msg = "dialog Open button "; 400 msg += (refState ? "did not lose focus" : 401 "lost focus when it should not"); 402 403 assertTrue(openLost.flag() == refState, msg); 404 } 405 }