1 /* 2 * Copyright (c) 2006, 2007, 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 6494016 27 @summary Nonresizable dialogs should not be resized using the Size SystemMenu command 28 @author anthony.petrov@...: area=awt.toplevel 29 @library ../../regtesthelpers 30 @build Util 31 @run main NonResizableDialogSysMenuResize 32 */ 33 34 35 /** 36 * NonResizableDialogSysMenuResize.java 37 * 38 * summary: Nonresizable dialogs should not be resized using the Size SystemMenu command 39 */ 40 41 import java.awt.*; 42 import java.awt.event.*; 43 import test.java.awt.regtesthelpers.Util; 44 45 46 public class NonResizableDialogSysMenuResize 47 { 48 49 //*** test-writer defined static variables go here *** 50 51 52 private static void init() 53 { 54 //*** Create instructions for the user here *** 55 56 String[] instructions = 57 { 58 "This is an AUTOMATIC test, simply wait until it is done.", 59 "The result (passed or failed) will be shown in the", 60 "message window below." 61 }; 62 Sysout.createDialog( ); 63 Sysout.printInstructions( instructions ); 64 65 // We must be sure that the Size system command has the S key as the shortcut one in the System menu. 66 Sysout.println("NOTE: The test is known to work correctly with English MS Windows only."); 67 68 String s = Toolkit.getDefaultToolkit().getClass().getName(); 69 70 // This is Windows-only test 71 if (!s.contains("WToolkit")) { 72 pass(); 73 return; 74 } 75 76 Dialog d = new Dialog((Frame)null, "dlg", false); 77 d.setResizable(false); 78 d.setSize(100, 100); 79 d.setLocation(200, 200); 80 d.setVisible(true); 81 82 Robot robot = Util.createRobot(); 83 robot.setAutoDelay(20); 84 85 // To be sure both the frame and the dialog are shown and packed 86 Util.waitForIdle(robot); 87 88 89 // The initial dialog position and size. 90 Point loc1 = d.getLocation(); 91 Dimension dim1 = d.getSize(); 92 93 System.out.println("The initial position of the dialog is: " + loc1 + "; the size is: " + dim1); 94 95 try { Thread.sleep(1000); } catch (Exception e) {}; 96 97 // Alt-Space opens System menu 98 robot.keyPress(KeyEvent.VK_ALT); 99 robot.keyPress(KeyEvent.VK_SPACE); 100 robot.keyRelease(KeyEvent.VK_SPACE); 101 robot.keyRelease(KeyEvent.VK_ALT); 102 103 // Try to choose the Size command 104 robot.keyPress(KeyEvent.VK_S); 105 robot.keyRelease(KeyEvent.VK_S); 106 107 // Try to change the size a little 108 for (int i = 0; i < 5; i++) { 109 robot.keyPress(KeyEvent.VK_DOWN); 110 robot.keyRelease(KeyEvent.VK_DOWN); 111 robot.keyPress(KeyEvent.VK_LEFT); 112 robot.keyRelease(KeyEvent.VK_LEFT); 113 } 114 115 // End the Size loop 116 robot.keyPress(KeyEvent.VK_ENTER); 117 robot.keyRelease(KeyEvent.VK_ENTER); 118 119 Util.waitForIdle(robot); 120 121 // The dialog position and size after trying to change its size. 122 Point loc2 = d.getLocation(); 123 Dimension dim2 = d.getSize(); 124 125 System.out.println("AFTER RESIZE: The position of the dialog is: " + loc2 + "; the size is: " + dim2); 126 127 if (loc2.equals(loc1) && dim2.equals(dim1)) { 128 pass(); 129 } else { 130 fail("The non-resizable dialog has changed its size and/or location."); 131 } 132 133 }//End init() 134 135 136 137 /***************************************************** 138 * Standard Test Machinery Section 139 * DO NOT modify anything in this section -- it's a 140 * standard chunk of code which has all of the 141 * synchronisation necessary for the test harness. 142 * By keeping it the same in all tests, it is easier 143 * to read and understand someone else's test, as 144 * well as insuring that all tests behave correctly 145 * with the test harness. 146 * There is a section following this for test- 147 * classes 148 ******************************************************/ 149 private static boolean theTestPassed = false; 150 private static boolean testGeneratedInterrupt = false; 151 private static String failureMessage = ""; 152 153 private static Thread mainThread = null; 154 155 private static int sleepTime = 300000; 156 157 // Not sure about what happens if multiple of this test are 158 // instantiated in the same VM. Being static (and using 159 // static vars), it aint gonna work. Not worrying about 160 // it for now. 161 public static void main( String args[] ) throws InterruptedException 162 { 163 mainThread = Thread.currentThread(); 164 try 165 { 166 init(); 167 } 168 catch( TestPassedException e ) 169 { 170 //The test passed, so just return from main and harness will 171 // interepret this return as a pass 172 return; 173 } 174 //At this point, neither test pass nor test fail has been 175 // called -- either would have thrown an exception and ended the 176 // test, so we know we have multiple threads. 177 178 //Test involves other threads, so sleep and wait for them to 179 // called pass() or fail() 180 try 181 { 182 Thread.sleep( sleepTime ); 183 //Timed out, so fail the test 184 throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" ); 185 } 186 catch (InterruptedException e) 187 { 188 //The test harness may have interrupted the test. If so, rethrow the exception 189 // so that the harness gets it and deals with it. 190 if( ! testGeneratedInterrupt ) throw e; 191 192 //reset flag in case hit this code more than once for some reason (just safety) 193 testGeneratedInterrupt = false; 194 195 if ( theTestPassed == false ) 196 { 197 throw new RuntimeException( failureMessage ); 198 } 199 } 200 201 }//main 202 203 public static synchronized void setTimeoutTo( int seconds ) 204 { 205 sleepTime = seconds * 1000; 206 } 207 208 public static synchronized void pass() 209 { 210 Sysout.println( "The test passed." ); 211 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" ); 212 //first check if this is executing in main thread 213 if ( mainThread == Thread.currentThread() ) 214 { 215 //Still in the main thread, so set the flag just for kicks, 216 // and throw a test passed exception which will be caught 217 // and end the test. 218 theTestPassed = true; 219 throw new TestPassedException(); 220 } 221 theTestPassed = true; 222 testGeneratedInterrupt = true; 223 mainThread.interrupt(); 224 }//pass() 225 226 public static synchronized void fail() 227 { 228 //test writer didn't specify why test failed, so give generic 229 fail( "it just plain failed! :-)" ); 230 } 231 232 public static synchronized void fail( String whyFailed ) 233 { 234 Sysout.println( "The test failed: " + whyFailed ); 235 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" ); 236 //check if this called from main thread 237 if ( mainThread == Thread.currentThread() ) 238 { 239 //If main thread, fail now 'cause not sleeping 240 throw new RuntimeException( whyFailed ); 241 } 242 theTestPassed = false; 243 testGeneratedInterrupt = true; 244 failureMessage = whyFailed; 245 mainThread.interrupt(); 246 }//fail() 247 248 }// class NonResizableDialogSysMenuResize 249 250 //This exception is used to exit from any level of call nesting 251 // when it's determined that the test has passed, and immediately 252 // end the test. 253 class TestPassedException extends RuntimeException 254 { 255 } 256 257 //*********** End Standard Test Machinery Section ********** 258 259 260 //************ Begin classes defined for the test **************** 261 262 // if want to make listeners, here is the recommended place for them, then instantiate 263 // them in init() 264 265 /* Example of a class which may be written as part of a test 266 class NewClass implements anInterface 267 { 268 static int newVar = 0; 269 270 public void eventDispatched(AWTEvent e) 271 { 272 //Counting events to see if we get enough 273 eventCount++; 274 275 if( eventCount == 20 ) 276 { 277 //got enough events, so pass 278 279 NonResizableDialogSysMenuResize.pass(); 280 } 281 else if( tries == 20 ) 282 { 283 //tried too many times without getting enough events so fail 284 285 NonResizableDialogSysMenuResize.fail(); 286 } 287 288 }// eventDispatched() 289 290 }// NewClass class 291 292 */ 293 294 295 //************** End classes defined for the test ******************* 296 297 298 299 300 /**************************************************** 301 Standard Test Machinery 302 DO NOT modify anything below -- it's a standard 303 chunk of code whose purpose is to make user 304 interaction uniform, and thereby make it simpler 305 to read and understand someone else's test. 306 ****************************************************/ 307 308 /** 309 This is part of the standard test machinery. 310 It creates a dialog (with the instructions), and is the interface 311 for sending text messages to the user. 312 To print the instructions, send an array of strings to Sysout.createDialog 313 WithInstructions method. Put one line of instructions per array entry. 314 To display a message for the tester to see, simply call Sysout.println 315 with the string to be displayed. 316 This mimics System.out.println but works within the test harness as well 317 as standalone. 318 */ 319 320 class Sysout 321 { 322 private static TestDialog dialog; 323 324 public static void createDialogWithInstructions( String[] instructions ) 325 { 326 dialog = new TestDialog( new Frame(), "Instructions" ); 327 dialog.printInstructions( instructions ); 328 dialog.setVisible(true); 329 println( "Any messages for the tester will display here." ); 330 } 331 332 public static void createDialog( ) 333 { 334 dialog = new TestDialog( new Frame(), "Instructions" ); 335 String[] defInstr = { "Instructions will appear here. ", "" } ; 336 dialog.printInstructions( defInstr ); 337 dialog.setVisible(true); 338 println( "Any messages for the tester will display here." ); 339 } 340 341 342 public static void printInstructions( String[] instructions ) 343 { 344 dialog.printInstructions( instructions ); 345 } 346 347 348 public static void println( String messageIn ) 349 { 350 dialog.displayMessage( messageIn ); 351 System.out.println(messageIn); 352 } 353 354 }// Sysout class 355 356 /** 357 This is part of the standard test machinery. It provides a place for the 358 test instructions to be displayed, and a place for interactive messages 359 to the user to be displayed. 360 To have the test instructions displayed, see Sysout. 361 To have a message to the user be displayed, see Sysout. 362 Do not call anything in this dialog directly. 363 */ 364 class TestDialog extends Dialog 365 { 366 367 TextArea instructionsText; 368 TextArea messageText; 369 int maxStringLength = 80; 370 371 //DO NOT call this directly, go through Sysout 372 public TestDialog( Frame frame, String name ) 373 { 374 super( frame, name ); 375 int scrollBoth = TextArea.SCROLLBARS_BOTH; 376 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); 377 add( "North", instructionsText ); 378 379 messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); 380 add("Center", messageText); 381 382 pack(); 383 384 setVisible(true); 385 }// TestDialog() 386 387 //DO NOT call this directly, go through Sysout 388 public void printInstructions( String[] instructions ) 389 { 390 //Clear out any current instructions 391 instructionsText.setText( "" ); 392 393 //Go down array of instruction strings 394 395 String printStr, remainingStr; 396 for( int i=0; i < instructions.length; i++ ) 397 { 398 //chop up each into pieces maxSringLength long 399 remainingStr = instructions[ i ]; 400 while( remainingStr.length() > 0 ) 401 { 402 //if longer than max then chop off first max chars to print 403 if( remainingStr.length() >= maxStringLength ) 404 { 405 //Try to chop on a word boundary 406 int posOfSpace = remainingStr. 407 lastIndexOf( ' ', maxStringLength - 1 ); 408 409 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; 410 411 printStr = remainingStr.substring( 0, posOfSpace + 1 ); 412 remainingStr = remainingStr.substring( posOfSpace + 1 ); 413 } 414 //else just print 415 else 416 { 417 printStr = remainingStr; 418 remainingStr = ""; 419 } 420 421 instructionsText.append( printStr + "\n" ); 422 423 }// while 424 425 }// for 426 427 }//printInstructions() 428 429 //DO NOT call this directly, go through Sysout 430 public void displayMessage( String messageIn ) 431 { 432 messageText.append( messageIn + "\n" ); 433 System.out.println(messageIn); 434 } 435 436 }// TestDialog class