1 /*
   2   test 1.0 04/05/20
   3   @bug 4140484
   4   @summary Heavyweight components inside invisible lightweight containers still show
   5   @author Your Name: art@sparc.spb.su
   6   @run applet NativeInLightShow.html
   7 */
   8 
   9 // Note there is no @ in front of test above.  This is so that the
  10 //  harness will not mistake this file as a test file.  It should
  11 //  only see the html file as a test file. (the harness runs all
  12 //  valid test files, so it would run this test twice if this file
  13 //  were valid as well as the html file.)
  14 // Also, note the area= after Your Name in the author tag.  Here, you
  15 //  should put which functional area the test falls in.  See the
  16 //  AWT-core home page -> test areas and/or -> AWT team  for a list of
  17 //  areas.
  18 // Note also the 'AutomaticAppletTest.html' in the run tag.  This should
  19 //  be changed to the name of the test.
  20 
  21 
  22 import java.applet.Applet;
  23 import java.awt.*;
  24 import java.awt.event.*;
  25 
  26 
  27 //Automated tests should run as applet tests if possible because they
  28 // get their environments cleaned up, including AWT threads, any
  29 // test created threads, and any system resources used by the test
  30 // such as file descriptors.  (This is normally not a problem as
  31 // main tests usually run in a separate VM, however on some platforms
  32 // such as the Mac, separate VMs are not possible and non-applet
  33 // tests will cause problems).  Also, you don't have to worry about
  34 // synchronisation stuff in Applet tests they way you do in main
  35 // tests...
  36 
  37 // The test verifies that the mixing code correctly handles COMPONENT_SHOWN events
  38 // while the top-level container is invisible.
  39 
  40 public class NativeInLightShow extends Applet
  41 {
  42     //Declare things used in the test, like buttons and labels here
  43     boolean buttonPressed = false;
  44 
  45     public void init()
  46     {
  47         //Create instructions for the user here, as well as set up
  48         // the environment -- set the layout manager, add buttons,
  49         // etc.
  50 
  51         this.setLayout (new BorderLayout ());
  52 
  53         String[] instructions =
  54         {
  55             "This is an AUTOMATIC test",
  56             "simply wait until it is done"
  57         };
  58         Sysout.createDialog( );
  59         Sysout.printInstructions( instructions );
  60 
  61     }//End  init()
  62 
  63     public void start ()
  64     {
  65         //Get things going.  Request focus, set size, et cetera
  66         setSize (200,200);
  67         setLocation(0, 0);
  68         setVisible(true);
  69         validate();
  70 
  71         //What would normally go into main() will probably go here.
  72         //Use System.out.println for diagnostic messages that you want
  73         //to read after the test is done.
  74         //Use Sysout.println for messages you want the tester to read.
  75 
  76         Frame f = new Frame("Test");
  77 
  78         Robot robot = null;
  79         try {
  80             robot = new Robot();
  81         }
  82         catch (Exception e) {
  83             throw new RuntimeException("Couldn't create robot");
  84         }
  85 
  86         Container c = new Container();
  87         c.setLayout(new BorderLayout());
  88         Button b = new Button("I'm should be visible!");
  89         b.addActionListener(new ActionListener()
  90         {
  91             public void actionPerformed(ActionEvent e) {
  92                 Sysout.println("Test PASSED");
  93                 buttonPressed = true;
  94             }
  95         });
  96         c.add(b);
  97 
  98         f.add(c);
  99 
 100         f.pack();
 101 
 102         c.setVisible(false);
 103         c.setVisible(true);
 104 
 105         // Wait for a while for COMPONENT_SHOW event to be dispatched
 106         ((sun.awt.SunToolkit)Toolkit.getDefaultToolkit()).realSync();
 107 
 108         f.setVisible(true);
 109 
 110         ((sun.awt.SunToolkit)Toolkit.getDefaultToolkit()).realSync();
 111 
 112         Point buttonLocation = b.getLocationOnScreen();
 113 
 114         robot.mouseMove(buttonLocation.x + 5, buttonLocation.y + 5);
 115         robot.mousePress(InputEvent.BUTTON1_MASK);
 116         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 117 
 118         // Wait for a while for ACTION event to be dispatched
 119         ((sun.awt.SunToolkit)Toolkit.getDefaultToolkit()).realSync();
 120 
 121         if (!buttonPressed) {
 122             Sysout.println("Test FAILED");
 123             throw new RuntimeException();
 124         }
 125     }// start()
 126 
 127 }// class AutomaticAppletTest
 128 
 129 
 130 /****************************************************
 131  Standard Test Machinery
 132  DO NOT modify anything below -- it's a standard
 133   chunk of code whose purpose is to make user
 134   interaction uniform, and thereby make it simpler
 135   to read and understand someone else's test.
 136  ****************************************************/
 137 
 138 /**
 139  This is part of the standard test machinery.
 140  It creates a dialog (with the instructions), and is the interface
 141   for sending text messages to the user.
 142  To print the instructions, send an array of strings to Sysout.createDialog
 143   WithInstructions method.  Put one line of instructions per array entry.
 144  To display a message for the tester to see, simply call Sysout.println
 145   with the string to be displayed.
 146  This mimics System.out.println but works within the test harness as well
 147   as standalone.
 148  */
 149 
 150 class Sysout
 151 {
 152     private static TestDialog dialog;
 153 
 154     public static void createDialogWithInstructions( String[] instructions )
 155     {
 156         dialog = new TestDialog( new Frame(), "Instructions" );
 157         dialog.printInstructions( instructions );
 158         dialog.setVisible(true);
 159         println( "Any messages for the tester will display here." );
 160     }
 161 
 162     public static void createDialog( )
 163     {
 164         dialog = new TestDialog( new Frame(), "Instructions" );
 165         String[] defInstr = { "Instructions will appear here. ", "" } ;
 166         dialog.printInstructions( defInstr );
 167         dialog.setLocation(100, 100);
 168         dialog.setVisible(true);
 169         println( "Any messages for the tester will display here." );
 170     }
 171 
 172 
 173     public static void printInstructions( String[] instructions )
 174     {
 175         dialog.printInstructions( instructions );
 176     }
 177 
 178 
 179     public static void println( String messageIn )
 180     {
 181         dialog.displayMessage( messageIn );
 182     }
 183 
 184 }// Sysout  class
 185 
 186 /**
 187   This is part of the standard test machinery.  It provides a place for the
 188    test instructions to be displayed, and a place for interactive messages
 189    to the user to be displayed.
 190   To have the test instructions displayed, see Sysout.
 191   To have a message to the user be displayed, see Sysout.
 192   Do not call anything in this dialog directly.
 193   */
 194 class TestDialog extends Dialog
 195 {
 196 
 197     TextArea instructionsText;
 198     TextArea messageText;
 199     int maxStringLength = 80;
 200 
 201     //DO NOT call this directly, go through Sysout
 202     public TestDialog( Frame frame, String name )
 203     {
 204         super( frame, name );
 205         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 206         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 207         add( "North", instructionsText );
 208 
 209         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 210         add("Center", messageText);
 211 
 212         pack();
 213 
 214         show();
 215     }// TestDialog()
 216 
 217     //DO NOT call this directly, go through Sysout
 218     public void printInstructions( String[] instructions )
 219     {
 220         //Clear out any current instructions
 221         instructionsText.setText( "" );
 222 
 223         //Go down array of instruction strings
 224 
 225         String printStr, remainingStr;
 226         for( int i=0; i < instructions.length; i++ )
 227         {
 228             //chop up each into pieces maxSringLength long
 229             remainingStr = instructions[ i ];
 230             while( remainingStr.length() > 0 )
 231             {
 232                 //if longer than max then chop off first max chars to print
 233                 if( remainingStr.length() >= maxStringLength )
 234                 {
 235                     //Try to chop on a word boundary
 236                     int posOfSpace = remainingStr.
 237                         lastIndexOf( ' ', maxStringLength - 1 );
 238 
 239                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 240 
 241                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
 242                     remainingStr = remainingStr.substring( posOfSpace + 1 );
 243                 }
 244                 //else just print
 245                 else
 246                 {
 247                     printStr = remainingStr;
 248                     remainingStr = "";
 249                 }
 250 
 251                 instructionsText.append( printStr + "\n" );
 252 
 253             }// while
 254 
 255         }// for
 256 
 257     }//printInstructions()
 258 
 259     //DO NOT call this directly, go through Sysout
 260     public void displayMessage( String messageIn )
 261     {
 262         messageText.append( messageIn + "\n" );
 263         System.out.println(messageIn);
 264     }
 265 
 266 }// TestDialog  class