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