< prev index next >

test/jdk/java/awt/Toolkit/RealSync/RealSyncOnEDT.java

Print this page


   1 /*
   2  * Copyright (c) 2007, 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  */


  31   @run main/timeout=10 RealSyncOnEDT
  32 */
  33 
  34 
  35 /**
  36  * RealSyncOnEDT.java
  37  *
  38  * summary: Tests if the realSync() throws the IllegalThreadException while invoked on the EDT
  39  */
  40 
  41 import java.awt.*;
  42 import java.awt.event.*;
  43 
  44 
  45 public class RealSyncOnEDT
  46 {
  47     static Exception exceptionCaught = null;
  48 
  49     private static void init()
  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         // Try to invoke the realSync() on the EDT
  62         try {
  63             EventQueue.invokeAndWait(new Runnable() {
  64                 public void run() {
  65                     Frame frame = new Frame("Test frame");
  66 
  67                     frame.setSize(100, 100);
  68                     frame.setVisible(true);
  69 
  70                     try {
  71                         ((sun.awt.SunToolkit)java.awt.Toolkit.getDefaultToolkit()).realSync();
  72                     } catch (Exception e) {
  73                         exceptionCaught = e;
  74                     }
  75                 }
  76             });
  77         } catch (InterruptedException e) {
  78             e.printStackTrace();
  79             fail("Unexpected exception caught: " + e);
  80             return;


 155             if( ! testGeneratedInterrupt ) throw e;
 156 
 157             //reset flag in case hit this code more than once for some reason (just safety)
 158             testGeneratedInterrupt = false;
 159 
 160             if ( theTestPassed == false )
 161             {
 162                 throw new RuntimeException( failureMessage );
 163             }
 164         }
 165 
 166     }//main
 167 
 168     public static synchronized void setTimeoutTo( int seconds )
 169     {
 170         sleepTime = seconds * 1000;
 171     }
 172 
 173     public static synchronized void pass()
 174     {
 175         Sysout.println( "The test passed." );
 176         Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
 177         //first check if this is executing in main thread
 178         if ( mainThread == Thread.currentThread() )
 179         {
 180             //Still in the main thread, so set the flag just for kicks,
 181             // and throw a test passed exception which will be caught
 182             // and end the test.
 183             theTestPassed = true;
 184             throw new TestPassedException();
 185         }
 186         theTestPassed = true;
 187         testGeneratedInterrupt = true;
 188         mainThread.interrupt();
 189     }//pass()
 190 
 191     public static synchronized void fail()
 192     {
 193         //test writer didn't specify why test failed, so give generic
 194         fail( "it just plain failed! :-)" );
 195     }
 196 
 197     public static synchronized void fail( String whyFailed )
 198     {
 199         Sysout.println( "The test failed: " + whyFailed );
 200         Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
 201         //check if this called from main thread
 202         if ( mainThread == Thread.currentThread() )
 203         {
 204             //If main thread, fail now 'cause not sleeping
 205             throw new RuntimeException( whyFailed );
 206         }
 207         theTestPassed = false;
 208         testGeneratedInterrupt = true;
 209         failureMessage = whyFailed;
 210         mainThread.interrupt();
 211     }//fail()
 212 
 213 }// class RealSyncOnEDT
 214 
 215 //This exception is used to exit from any level of call nesting
 216 // when it's determined that the test has passed, and immediately
 217 // end the test.
 218 class TestPassedException extends RuntimeException
 219 {
 220 }
 221 
 222 //*********** End Standard Test Machinery Section **********
 223 
 224 
 225 //************ Begin classes defined for the test ****************
 226 
 227 // if want to make listeners, here is the recommended place for them, then instantiate
 228 //  them in init()
 229 
 230 /* Example of a class which may be written as part of a test
 231 class NewClass implements anInterface
 232  {
 233    static int newVar = 0;
 234 
 235    public void eventDispatched(AWTEvent e)
 236     {
 237       //Counting events to see if we get enough
 238       eventCount++;
 239 
 240       if( eventCount == 20 )
 241        {
 242          //got enough events, so pass
 243 
 244          RealSyncOnEDT.pass();
 245        }
 246       else if( tries == 20 )
 247        {
 248          //tried too many times without getting enough events so fail
 249 
 250          RealSyncOnEDT.fail();
 251        }
 252 
 253     }// eventDispatched()
 254 
 255  }// NewClass class
 256 
 257 */
 258 
 259 
 260 //************** End classes defined for the test *******************
 261 
 262 
 263 
 264 
 265 /****************************************************
 266  Standard Test Machinery
 267  DO NOT modify anything below -- it's a standard
 268   chunk of code whose purpose is to make user
 269   interaction uniform, and thereby make it simpler
 270   to read and understand someone else's test.
 271  ****************************************************/
 272 
 273 /**
 274  This is part of the standard test machinery.
 275  It creates a dialog (with the instructions), and is the interface
 276   for sending text messages to the user.
 277  To print the instructions, send an array of strings to Sysout.createDialog
 278   WithInstructions method.  Put one line of instructions per array entry.
 279  To display a message for the tester to see, simply call Sysout.println
 280   with the string to be displayed.
 281  This mimics System.out.println but works within the test harness as well
 282   as standalone.
 283  */
 284 
 285 class Sysout
 286 {
 287     private static TestDialog dialog;
 288 
 289     public static void createDialogWithInstructions( String[] instructions )
 290     {
 291         dialog = new TestDialog( new Frame(), "Instructions" );
 292         dialog.printInstructions( instructions );
 293         dialog.setVisible(true);
 294         println( "Any messages for the tester will display here." );
 295     }
 296 
 297     public static void createDialog( )
 298     {
 299         dialog = new TestDialog( new Frame(), "Instructions" );
 300         String[] defInstr = { "Instructions will appear here. ", "" } ;
 301         dialog.printInstructions( defInstr );
 302         dialog.setVisible(true);
 303         println( "Any messages for the tester will display here." );
 304     }
 305 
 306 
 307     public static void printInstructions( String[] instructions )
 308     {
 309         dialog.printInstructions( instructions );
 310     }
 311 
 312 
 313     public static void println( String messageIn )
 314     {
 315         dialog.displayMessage( messageIn );
 316         System.out.println(messageIn);
 317     }
 318 
 319 }// Sysout  class
 320 
 321 /**
 322   This is part of the standard test machinery.  It provides a place for the
 323    test instructions to be displayed, and a place for interactive messages
 324    to the user to be displayed.
 325   To have the test instructions displayed, see Sysout.
 326   To have a message to the user be displayed, see Sysout.
 327   Do not call anything in this dialog directly.
 328   */
 329 class TestDialog extends Dialog
 330 {
 331 
 332     TextArea instructionsText;
 333     TextArea messageText;
 334     int maxStringLength = 80;
 335 
 336     //DO NOT call this directly, go through Sysout
 337     public TestDialog( Frame frame, String name )
 338     {
 339         super( frame, name );
 340         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 341         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 342         add( "North", instructionsText );
 343 
 344         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 345         add("Center", messageText);
 346 
 347         pack();
 348 
 349         setVisible(true);
 350     }// TestDialog()
 351 
 352     //DO NOT call this directly, go through Sysout
 353     public void printInstructions( String[] instructions )
 354     {
 355         //Clear out any current instructions
 356         instructionsText.setText( "" );
 357 
 358         //Go down array of instruction strings
 359 
 360         String printStr, remainingStr;
 361         for( int i=0; i < instructions.length; i++ )
 362         {
 363             //chop up each into pieces maxSringLength long
 364             remainingStr = instructions[ i ];
 365             while( remainingStr.length() > 0 )
 366             {
 367                 //if longer than max then chop off first max chars to print
 368                 if( remainingStr.length() >= maxStringLength )
 369                 {
 370                     //Try to chop on a word boundary
 371                     int posOfSpace = remainingStr.
 372                         lastIndexOf( ' ', maxStringLength - 1 );
 373 
 374                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 375 
 376                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
 377                     remainingStr = remainingStr.substring( posOfSpace + 1 );
 378                 }
 379                 //else just print
 380                 else
 381                 {
 382                     printStr = remainingStr;
 383                     remainingStr = "";
 384                 }
 385 
 386                 instructionsText.append( printStr + "\n" );
 387 
 388             }// while
 389 
 390         }// for
 391 
 392     }//printInstructions()
 393 
 394     //DO NOT call this directly, go through Sysout
 395     public void displayMessage( String messageIn )
 396     {
 397         messageText.append( messageIn + "\n" );
 398         System.out.println(messageIn);
 399     }
 400 
 401 }// TestDialog  class
   1 /*
   2  * Copyright (c) 2007, 2018, 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  */


  31   @run main/timeout=10 RealSyncOnEDT
  32 */
  33 
  34 
  35 /**
  36  * RealSyncOnEDT.java
  37  *
  38  * summary: Tests if the realSync() throws the IllegalThreadException while invoked on the EDT
  39  */
  40 
  41 import java.awt.*;
  42 import java.awt.event.*;
  43 
  44 
  45 public class RealSyncOnEDT
  46 {
  47     static Exception exceptionCaught = null;
  48 
  49     private static void init()
  50     {










  51         // Try to invoke the realSync() on the EDT
  52         try {
  53             EventQueue.invokeAndWait(new Runnable() {
  54                 public void run() {
  55                     Frame frame = new Frame("Test frame");
  56 
  57                     frame.setSize(100, 100);
  58                     frame.setVisible(true);
  59 
  60                     try {
  61                         ((sun.awt.SunToolkit)java.awt.Toolkit.getDefaultToolkit()).realSync();
  62                     } catch (Exception e) {
  63                         exceptionCaught = e;
  64                     }
  65                 }
  66             });
  67         } catch (InterruptedException e) {
  68             e.printStackTrace();
  69             fail("Unexpected exception caught: " + e);
  70             return;


 145             if( ! testGeneratedInterrupt ) throw e;
 146 
 147             //reset flag in case hit this code more than once for some reason (just safety)
 148             testGeneratedInterrupt = false;
 149 
 150             if ( theTestPassed == false )
 151             {
 152                 throw new RuntimeException( failureMessage );
 153             }
 154         }
 155 
 156     }//main
 157 
 158     public static synchronized void setTimeoutTo( int seconds )
 159     {
 160         sleepTime = seconds * 1000;
 161     }
 162 
 163     public static synchronized void pass()
 164     {
 165         System.out.println( "The test passed." );
 166         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
 167         //first check if this is executing in main thread
 168         if ( mainThread == Thread.currentThread() )
 169         {
 170             //Still in the main thread, so set the flag just for kicks,
 171             // and throw a test passed exception which will be caught
 172             // and end the test.
 173             theTestPassed = true;
 174             throw new TestPassedException();
 175         }
 176         theTestPassed = true;
 177         testGeneratedInterrupt = true;
 178         mainThread.interrupt();
 179     }//pass()
 180 
 181     public static synchronized void fail()
 182     {
 183         //test writer didn't specify why test failed, so give generic
 184         fail( "it just plain failed! :-)" );
 185     }
 186 
 187     public static synchronized void fail( String whyFailed )
 188     {
 189         System.out.println( "The test failed: " + whyFailed );
 190         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
 191         //check if this called from main thread
 192         if ( mainThread == Thread.currentThread() )
 193         {
 194             //If main thread, fail now 'cause not sleeping
 195             throw new RuntimeException( whyFailed );
 196         }
 197         theTestPassed = false;
 198         testGeneratedInterrupt = true;
 199         failureMessage = whyFailed;
 200         mainThread.interrupt();
 201     }//fail()
 202 
 203 }// class RealSyncOnEDT
 204 
 205 //This exception is used to exit from any level of call nesting
 206 // when it's determined that the test has passed, and immediately
 207 // end the test.
 208 class TestPassedException extends RuntimeException
 209 {
 210 }





















































































































































































< prev index next >