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