test/jdk/java/awt/Toolkit/RealSync/RealSyncOnEDT.java
changeset 50834 9cf279436b9d
parent 47216 71c04702a3d5
equal deleted inserted replaced
50833:97852c0a7a91 50834:9cf279436b9d
     1 /*
     1 /*
     2  * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    46 {
    46 {
    47     static Exception exceptionCaught = null;
    47     static Exception exceptionCaught = null;
    48 
    48 
    49     private static void init()
    49     private static void init()
    50     {
    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
    51         // Try to invoke the realSync() on the EDT
    62         try {
    52         try {
    63             EventQueue.invokeAndWait(new Runnable() {
    53             EventQueue.invokeAndWait(new Runnable() {
    64                 public void run() {
    54                 public void run() {
    65                     Frame frame = new Frame("Test frame");
    55                     Frame frame = new Frame("Test frame");
   170         sleepTime = seconds * 1000;
   160         sleepTime = seconds * 1000;
   171     }
   161     }
   172 
   162 
   173     public static synchronized void pass()
   163     public static synchronized void pass()
   174     {
   164     {
   175         Sysout.println( "The test passed." );
   165         System.out.println( "The test passed." );
   176         Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
   166         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
   177         //first check if this is executing in main thread
   167         //first check if this is executing in main thread
   178         if ( mainThread == Thread.currentThread() )
   168         if ( mainThread == Thread.currentThread() )
   179         {
   169         {
   180             //Still in the main thread, so set the flag just for kicks,
   170             //Still in the main thread, so set the flag just for kicks,
   181             // and throw a test passed exception which will be caught
   171             // and throw a test passed exception which will be caught
   194         fail( "it just plain failed! :-)" );
   184         fail( "it just plain failed! :-)" );
   195     }
   185     }
   196 
   186 
   197     public static synchronized void fail( String whyFailed )
   187     public static synchronized void fail( String whyFailed )
   198     {
   188     {
   199         Sysout.println( "The test failed: " + whyFailed );
   189         System.out.println( "The test failed: " + whyFailed );
   200         Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
   190         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
   201         //check if this called from main thread
   191         //check if this called from main thread
   202         if ( mainThread == Thread.currentThread() )
   192         if ( mainThread == Thread.currentThread() )
   203         {
   193         {
   204             //If main thread, fail now 'cause not sleeping
   194             //If main thread, fail now 'cause not sleeping
   205             throw new RuntimeException( whyFailed );
   195             throw new RuntimeException( whyFailed );
   216 // when it's determined that the test has passed, and immediately
   206 // when it's determined that the test has passed, and immediately
   217 // end the test.
   207 // end the test.
   218 class TestPassedException extends RuntimeException
   208 class TestPassedException extends RuntimeException
   219 {
   209 {
   220 }
   210 }
   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