test/jdk/java/awt/Mixing/OpaqueTest.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.
    54     static String testSeq = new String("");
    54     static String testSeq = new String("");
    55     final static String checkSeq = new String("010000101");
    55     final static String checkSeq = new String("010000101");
    56 
    56 
    57     private static void init()
    57     private static void init()
    58     {
    58     {
    59         //*** Create instructions for the user here ***
       
    60 
       
    61         String[] instructions =
       
    62         {
       
    63             "This is an AUTOMATIC test, simply wait until it is done.",
       
    64             "The result (passed or failed) will be shown in the",
       
    65             "message window below."
       
    66         };
       
    67         Sysout.createDialog( );
       
    68         Sysout.printInstructions( instructions );
       
    69 
       
    70 
       
    71         // Create components
    59         // Create components
    72         final Frame f = new Frame("Button-JButton mix test");
    60         final Frame f = new Frame("Button-JButton mix test");
    73         final Panel p = new Panel();
    61         final Panel p = new Panel();
    74         final Button heavy = new Button("  Heavyweight Button  ");
    62         final Button heavy = new Button("  Heavyweight Button  ");
    75         final JButton light = new JButton("  LW Button  ");
    63         final JButton light = new JButton("  LW Button  ");
   218         sleepTime = seconds * 1000;
   206         sleepTime = seconds * 1000;
   219     }
   207     }
   220 
   208 
   221     public static synchronized void pass()
   209     public static synchronized void pass()
   222     {
   210     {
   223         Sysout.println( "The test passed." );
   211         System.out.println( "The test passed." );
   224         Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
   212         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
   225         //first check if this is executing in main thread
   213         //first check if this is executing in main thread
   226         if ( mainThread == Thread.currentThread() )
   214         if ( mainThread == Thread.currentThread() )
   227         {
   215         {
   228             //Still in the main thread, so set the flag just for kicks,
   216             //Still in the main thread, so set the flag just for kicks,
   229             // and throw a test passed exception which will be caught
   217             // and throw a test passed exception which will be caught
   242         fail( "it just plain failed! :-)" );
   230         fail( "it just plain failed! :-)" );
   243     }
   231     }
   244 
   232 
   245     public static synchronized void fail( String whyFailed )
   233     public static synchronized void fail( String whyFailed )
   246     {
   234     {
   247         Sysout.println( "The test failed: " + whyFailed );
   235         System.out.println( "The test failed: " + whyFailed );
   248         Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
   236         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
   249         //check if this called from main thread
   237         //check if this called from main thread
   250         if ( mainThread == Thread.currentThread() )
   238         if ( mainThread == Thread.currentThread() )
   251         {
   239         {
   252             //If main thread, fail now 'cause not sleeping
   240             //If main thread, fail now 'cause not sleeping
   253             throw new RuntimeException( whyFailed );
   241             throw new RuntimeException( whyFailed );
   264 // when it's determined that the test has passed, and immediately
   252 // when it's determined that the test has passed, and immediately
   265 // end the test.
   253 // end the test.
   266 class TestPassedException extends RuntimeException
   254 class TestPassedException extends RuntimeException
   267 {
   255 {
   268 }
   256 }
   269 
       
   270 //*********** End Standard Test Machinery Section **********
       
   271 
       
   272 
       
   273 //************ Begin classes defined for the test ****************
       
   274 
       
   275 // if want to make listeners, here is the recommended place for them, then instantiate
       
   276 //  them in init()
       
   277 
       
   278 /* Example of a class which may be written as part of a test
       
   279 class NewClass implements anInterface
       
   280  {
       
   281    static int newVar = 0;
       
   282 
       
   283    public void eventDispatched(AWTEvent e)
       
   284     {
       
   285       //Counting events to see if we get enough
       
   286       eventCount++;
       
   287 
       
   288       if( eventCount == 20 )
       
   289        {
       
   290          //got enough events, so pass
       
   291 
       
   292          OpaqueTest.pass();
       
   293        }
       
   294       else if( tries == 20 )
       
   295        {
       
   296          //tried too many times without getting enough events so fail
       
   297 
       
   298          OpaqueTest.fail();
       
   299        }
       
   300 
       
   301     }// eventDispatched()
       
   302 
       
   303  }// NewClass class
       
   304 
       
   305 */
       
   306 
       
   307 
       
   308 //************** End classes defined for the test *******************
       
   309 
       
   310 
       
   311 
       
   312 
       
   313 /****************************************************
       
   314  Standard Test Machinery
       
   315  DO NOT modify anything below -- it's a standard
       
   316   chunk of code whose purpose is to make user
       
   317   interaction uniform, and thereby make it simpler
       
   318   to read and understand someone else's test.
       
   319  ****************************************************/
       
   320 
       
   321 /**
       
   322  This is part of the standard test machinery.
       
   323  It creates a dialog (with the instructions), and is the interface
       
   324   for sending text messages to the user.
       
   325  To print the instructions, send an array of strings to Sysout.createDialog
       
   326   WithInstructions method.  Put one line of instructions per array entry.
       
   327  To display a message for the tester to see, simply call Sysout.println
       
   328   with the string to be displayed.
       
   329  This mimics System.out.println but works within the test harness as well
       
   330   as standalone.
       
   331  */
       
   332 
       
   333 class Sysout
       
   334 {
       
   335     private static TestDialog dialog;
       
   336 
       
   337     public static void createDialogWithInstructions( String[] instructions )
       
   338     {
       
   339         dialog = new TestDialog( new Frame(), "Instructions" );
       
   340         dialog.printInstructions( instructions );
       
   341         dialog.setVisible(true);
       
   342         println( "Any messages for the tester will display here." );
       
   343     }
       
   344 
       
   345     public static void createDialog( )
       
   346     {
       
   347         dialog = new TestDialog( new Frame(), "Instructions" );
       
   348         String[] defInstr = { "Instructions will appear here. ", "" } ;
       
   349         dialog.printInstructions( defInstr );
       
   350         dialog.setVisible(true);
       
   351         println( "Any messages for the tester will display here." );
       
   352     }
       
   353 
       
   354 
       
   355     public static void printInstructions( String[] instructions )
       
   356     {
       
   357         dialog.printInstructions( instructions );
       
   358     }
       
   359 
       
   360 
       
   361     public static void println( String messageIn )
       
   362     {
       
   363         dialog.displayMessage( messageIn );
       
   364         System.out.println(messageIn);
       
   365     }
       
   366 
       
   367 }// Sysout  class
       
   368 
       
   369 /**
       
   370   This is part of the standard test machinery.  It provides a place for the
       
   371    test instructions to be displayed, and a place for interactive messages
       
   372    to the user to be displayed.
       
   373   To have the test instructions displayed, see Sysout.
       
   374   To have a message to the user be displayed, see Sysout.
       
   375   Do not call anything in this dialog directly.
       
   376   */
       
   377 class TestDialog extends Dialog
       
   378 {
       
   379 
       
   380     TextArea instructionsText;
       
   381     TextArea messageText;
       
   382     int maxStringLength = 80;
       
   383 
       
   384     //DO NOT call this directly, go through Sysout
       
   385     public TestDialog( Frame frame, String name )
       
   386     {
       
   387         super( frame, name );
       
   388         int scrollBoth = TextArea.SCROLLBARS_BOTH;
       
   389         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
       
   390         add( "North", instructionsText );
       
   391 
       
   392         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
       
   393         add("Center", messageText);
       
   394 
       
   395         pack();
       
   396 
       
   397         setVisible(true);
       
   398     }// TestDialog()
       
   399 
       
   400     //DO NOT call this directly, go through Sysout
       
   401     public void printInstructions( String[] instructions )
       
   402     {
       
   403         //Clear out any current instructions
       
   404         instructionsText.setText( "" );
       
   405 
       
   406         //Go down array of instruction strings
       
   407 
       
   408         String printStr, remainingStr;
       
   409         for( int i=0; i < instructions.length; i++ )
       
   410         {
       
   411             //chop up each into pieces maxSringLength long
       
   412             remainingStr = instructions[ i ];
       
   413             while( remainingStr.length() > 0 )
       
   414             {
       
   415                 //if longer than max then chop off first max chars to print
       
   416                 if( remainingStr.length() >= maxStringLength )
       
   417                 {
       
   418                     //Try to chop on a word boundary
       
   419                     int posOfSpace = remainingStr.
       
   420                         lastIndexOf( ' ', maxStringLength - 1 );
       
   421 
       
   422                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
       
   423 
       
   424                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
       
   425                     remainingStr = remainingStr.substring( posOfSpace + 1 );
       
   426                 }
       
   427                 //else just print
       
   428                 else
       
   429                 {
       
   430                     printStr = remainingStr;
       
   431                     remainingStr = "";
       
   432                 }
       
   433 
       
   434                 instructionsText.append( printStr + "\n" );
       
   435 
       
   436             }// while
       
   437 
       
   438         }// for
       
   439 
       
   440     }//printInstructions()
       
   441 
       
   442     //DO NOT call this directly, go through Sysout
       
   443     public void displayMessage( String messageIn )
       
   444     {
       
   445         messageText.append( messageIn + "\n" );
       
   446         System.out.println(messageIn);
       
   447     }
       
   448 
       
   449 }// TestDialog  class