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