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