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