1 /*
   2  * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25   @test
  26   @bug 6589530
  27   @summary Mixing code should correctly handle insertion of components with setComponentZOrder.
  28   @author antohny.petrov@...: area=awt.mixing
  29   @run main setComponentZOrder
  30 */
  31 
  32 /**
  33  * setComponentZOrder.java
  34  *
  35  * summary:  Mixing code should correctly handle insertion of components with setComponentZOrder.
  36  */
  37 
  38 import java.awt.*;
  39 import java.awt.event.*;
  40 
  41 
  42 public class setComponentZOrder
  43 {
  44 
  45     //*** test-writer defined static variables go here ***
  46 
  47 
  48     private static void init()
  49     {
  50         //*** Create instructions for the user here ***
  51 
  52         String[] instructions =
  53         {
  54             "This is an AUTOMATIC test, simply wait until it is done.",
  55             "The result (passed or failed) will be shown in the",
  56             "message window below."
  57         };
  58         Sysout.createDialog( );
  59         Sysout.printInstructions( instructions );
  60 
  61         try {
  62             Container c = new Container();
  63             Button b = new Button("b");
  64             c.setComponentZOrder(b, 0);
  65         } catch (ArrayIndexOutOfBoundsException e) {
  66             e.printStackTrace();
  67             fail("The setComponentZOrder method used to insert a component caused the mixing code to throw the exception: " + e);
  68         }
  69 
  70         pass();
  71 
  72     }//End  init()
  73 
  74 
  75 
  76     /*****************************************************
  77      * Standard Test Machinery Section
  78      * DO NOT modify anything in this section -- it's a
  79      * standard chunk of code which has all of the
  80      * synchronisation necessary for the test harness.
  81      * By keeping it the same in all tests, it is easier
  82      * to read and understand someone else's test, as
  83      * well as insuring that all tests behave correctly
  84      * with the test harness.
  85      * There is a section following this for test-
  86      * classes
  87      ******************************************************/
  88     private static boolean theTestPassed = false;
  89     private static boolean testGeneratedInterrupt = false;
  90     private static String failureMessage = "";
  91 
  92     private static Thread mainThread = null;
  93 
  94     private static int sleepTime = 300000;
  95 
  96     // Not sure about what happens if multiple of this test are
  97     //  instantiated in the same VM.  Being static (and using
  98     //  static vars), it aint gonna work.  Not worrying about
  99     //  it for now.
 100     public static void main( String args[] ) throws InterruptedException
 101     {
 102         mainThread = Thread.currentThread();
 103         try
 104         {
 105             init();
 106         }
 107         catch( TestPassedException e )
 108         {
 109             //The test passed, so just return from main and harness will
 110             // interepret this return as a pass
 111             return;
 112         }
 113         //At this point, neither test pass nor test fail has been
 114         // called -- either would have thrown an exception and ended the
 115         // test, so we know we have multiple threads.
 116 
 117         //Test involves other threads, so sleep and wait for them to
 118         // called pass() or fail()
 119         try
 120         {
 121             Thread.sleep( sleepTime );
 122             //Timed out, so fail the test
 123             throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
 124         }
 125         catch (InterruptedException e)
 126         {
 127             //The test harness may have interrupted the test.  If so, rethrow the exception
 128             // so that the harness gets it and deals with it.
 129             if( ! testGeneratedInterrupt ) throw e;
 130 
 131             //reset flag in case hit this code more than once for some reason (just safety)
 132             testGeneratedInterrupt = false;
 133 
 134             if ( theTestPassed == false )
 135             {
 136                 throw new RuntimeException( failureMessage );
 137             }
 138         }
 139 
 140     }//main
 141 
 142     public static synchronized void setTimeoutTo( int seconds )
 143     {
 144         sleepTime = seconds * 1000;
 145     }
 146 
 147     public static synchronized void pass()
 148     {
 149         Sysout.println( "The test passed." );
 150         Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
 151         //first check if this is executing in main thread
 152         if ( mainThread == Thread.currentThread() )
 153         {
 154             //Still in the main thread, so set the flag just for kicks,
 155             // and throw a test passed exception which will be caught
 156             // and end the test.
 157             theTestPassed = true;
 158             throw new TestPassedException();
 159         }
 160         theTestPassed = true;
 161         testGeneratedInterrupt = true;
 162         mainThread.interrupt();
 163     }//pass()
 164 
 165     public static synchronized void fail()
 166     {
 167         //test writer didn't specify why test failed, so give generic
 168         fail( "it just plain failed! :-)" );
 169     }
 170 
 171     public static synchronized void fail( String whyFailed )
 172     {
 173         Sysout.println( "The test failed: " + whyFailed );
 174         Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
 175         //check if this called from main thread
 176         if ( mainThread == Thread.currentThread() )
 177         {
 178             //If main thread, fail now 'cause not sleeping
 179             throw new RuntimeException( whyFailed );
 180         }
 181         theTestPassed = false;
 182         testGeneratedInterrupt = true;
 183         failureMessage = whyFailed;
 184         mainThread.interrupt();
 185     }//fail()
 186 
 187 }// class setComponentZOrder
 188 
 189 //This exception is used to exit from any level of call nesting
 190 // when it's determined that the test has passed, and immediately
 191 // end the test.
 192 class TestPassedException extends RuntimeException
 193 {
 194 }
 195 
 196 //*********** End Standard Test Machinery Section **********
 197 
 198 
 199 //************ Begin classes defined for the test ****************
 200 
 201 // if want to make listeners, here is the recommended place for them, then instantiate
 202 //  them in init()
 203 
 204 /* Example of a class which may be written as part of a test
 205 class NewClass implements anInterface
 206  {
 207    static int newVar = 0;
 208 
 209    public void eventDispatched(AWTEvent e)
 210     {
 211       //Counting events to see if we get enough
 212       eventCount++;
 213 
 214       if( eventCount == 20 )
 215        {
 216          //got enough events, so pass
 217 
 218          setComponentZOrder.pass();
 219        }
 220       else if( tries == 20 )
 221        {
 222          //tried too many times without getting enough events so fail
 223 
 224          setComponentZOrder.fail();
 225        }
 226 
 227     }// eventDispatched()
 228 
 229  }// NewClass class
 230 
 231 */
 232 
 233 
 234 //************** End classes defined for the test *******************
 235 
 236 
 237 
 238 
 239 /****************************************************
 240  Standard Test Machinery
 241  DO NOT modify anything below -- it's a standard
 242   chunk of code whose purpose is to make user
 243   interaction uniform, and thereby make it simpler
 244   to read and understand someone else's test.
 245  ****************************************************/
 246 
 247 /**
 248  This is part of the standard test machinery.
 249  It creates a dialog (with the instructions), and is the interface
 250   for sending text messages to the user.
 251  To print the instructions, send an array of strings to Sysout.createDialog
 252   WithInstructions method.  Put one line of instructions per array entry.
 253  To display a message for the tester to see, simply call Sysout.println
 254   with the string to be displayed.
 255  This mimics System.out.println but works within the test harness as well
 256   as standalone.
 257  */
 258 
 259 class Sysout
 260 {
 261     private static TestDialog dialog;
 262 
 263     public static void createDialogWithInstructions( String[] instructions )
 264     {
 265         dialog = new TestDialog( new Frame(), "Instructions" );
 266         dialog.printInstructions( instructions );
 267         dialog.setVisible(true);
 268         println( "Any messages for the tester will display here." );
 269     }
 270 
 271     public static void createDialog( )
 272     {
 273         dialog = new TestDialog( new Frame(), "Instructions" );
 274         String[] defInstr = { "Instructions will appear here. ", "" } ;
 275         dialog.printInstructions( defInstr );
 276         dialog.setVisible(true);
 277         println( "Any messages for the tester will display here." );
 278     }
 279 
 280 
 281     public static void printInstructions( String[] instructions )
 282     {
 283         dialog.printInstructions( instructions );
 284     }
 285 
 286 
 287     public static void println( String messageIn )
 288     {
 289         dialog.displayMessage( messageIn );
 290         System.out.println(messageIn);
 291     }
 292 
 293 }// Sysout  class
 294 
 295 /**
 296   This is part of the standard test machinery.  It provides a place for the
 297    test instructions to be displayed, and a place for interactive messages
 298    to the user to be displayed.
 299   To have the test instructions displayed, see Sysout.
 300   To have a message to the user be displayed, see Sysout.
 301   Do not call anything in this dialog directly.
 302   */
 303 class TestDialog extends Dialog
 304 {
 305 
 306     TextArea instructionsText;
 307     TextArea messageText;
 308     int maxStringLength = 80;
 309 
 310     //DO NOT call this directly, go through Sysout
 311     public TestDialog( Frame frame, String name )
 312     {
 313         super( frame, name );
 314         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 315         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 316         add( "North", instructionsText );
 317 
 318         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 319         add("Center", messageText);
 320 
 321         pack();
 322 
 323         setVisible(true);
 324     }// TestDialog()
 325 
 326     //DO NOT call this directly, go through Sysout
 327     public void printInstructions( String[] instructions )
 328     {
 329         //Clear out any current instructions
 330         instructionsText.setText( "" );
 331 
 332         //Go down array of instruction strings
 333 
 334         String printStr, remainingStr;
 335         for( int i=0; i < instructions.length; i++ )
 336         {
 337             //chop up each into pieces maxSringLength long
 338             remainingStr = instructions[ i ];
 339             while( remainingStr.length() > 0 )
 340             {
 341                 //if longer than max then chop off first max chars to print
 342                 if( remainingStr.length() >= maxStringLength )
 343                 {
 344                     //Try to chop on a word boundary
 345                     int posOfSpace = remainingStr.
 346                         lastIndexOf( ' ', maxStringLength - 1 );
 347 
 348                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 349 
 350                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
 351                     remainingStr = remainingStr.substring( posOfSpace + 1 );
 352                 }
 353                 //else just print
 354                 else
 355                 {
 356                     printStr = remainingStr;
 357                     remainingStr = "";
 358                 }
 359 
 360                 instructionsText.append( printStr + "\n" );
 361 
 362             }// while
 363 
 364         }// for
 365 
 366     }//printInstructions()
 367 
 368     //DO NOT call this directly, go through Sysout
 369     public void displayMessage( String messageIn )
 370     {
 371         messageText.append( messageIn + "\n" );
 372         System.out.println(messageIn);
 373     }
 374 
 375 }// TestDialog  class