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