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