1 /*
   2  * Copyright (c) 2006, 2008, 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 /*
  26   test
  27   @bug 6391770
  28   @summary Content of the Window should be laid out in the area left after WarningWindow was added.
  29   @author yuri nesterenko: area=
  30   @run applet WindowWithWarningTest.html
  31 */
  32 
  33 // Note there is no @ in front of test above.  This is so that the
  34 //  harness will not mistake this file as a test file.  It should
  35 //  only see the html file as a test file. (the harness runs all
  36 //  valid test files, so it would run this test twice if this file
  37 //  were valid as well as the html file.)
  38 // Also, note the area= after Your Name in the author tag.  Here, you
  39 //  should put which functional area the test falls in.  See the
  40 //  AWT-core home page -> test areas and/or -> AWT team  for a list of
  41 //  areas.
  42 // Note also the 'AutomaticAppletTest.html' in the run tag.  This should
  43 //  be changed to the name of the test.
  44 
  45 
  46 /**
  47  * WindowWithWarningTest.java
  48  *
  49  * summary:
  50  */
  51 
  52 import java.applet.Applet;
  53 import java.awt.*;
  54 import java.awt.event.*;
  55 import javax.swing.*;
  56 
  57 //Automated tests should run as applet tests if possible because they
  58 // get their environments cleaned up, including AWT threads, any
  59 // test created threads, and any system resources used by the test
  60 // such as file descriptors.  (This is normally not a problem as
  61 // main tests usually run in a separate VM, however on some platforms
  62 // such as the Mac, separate VMs are not possible and non-applet
  63 // tests will cause problems).  Also, you don't have to worry about
  64 // synchronisation stuff in Applet tests they way you do in main
  65 // tests...
  66 
  67 
  68 public class WindowWithWarningTest extends Applet
  69 {
  70     //Declare things used in the test, like buttons and labels here
  71     boolean buttonClicked = false;
  72     public static final int MAX_COUNT = 100;
  73 
  74     public void init()
  75     {
  76         //Create instructions for the user here, as well as set up
  77         // the environment -- set the layout manager, add buttons,
  78         // etc.
  79 
  80         this.setLayout (new BorderLayout ());
  81 
  82         String[] instructions =
  83         {
  84             "This is an AUTOMATIC test",
  85             "simply wait until it is done"
  86         };
  87         //Sysout.createDialog( );
  88         //Sysout.printInstructions( instructions );
  89 
  90     }//End  init()
  91     public void start ()
  92     {
  93         //Get things going.  Request focus, set size, et cetera
  94         JFrame frame = new JFrame("Window Test");
  95         frame.setBounds(50, 50, 200, 200);
  96         frame.show();
  97 
  98         JWindow window = new JWindow( frame );
  99         JButton jbutton1 = new JButton( "First" );
 100         jbutton1.addMouseListener( new MouseAdapter() {
 101             public void mousePressed( MouseEvent me ) {
 102                 buttonClicked = true;
 103             }
 104          });
 105         JButton jbutton2 = new JButton( "Second" );
 106         window.setLocation( 300, 300 );
 107 
 108         window.add("North", jbutton1);
 109         window.add("South", jbutton2);
 110 
 111         window.pack();
 112         window.show();
 113         //wait for frame to show:
 114         getLocation( frame );
 115         window.toFront();
 116 
 117         Dimension size0 = window.getSize();
 118         Dimension size1 = null;
 119         try {
 120             Robot robot = new Robot();
 121 
 122             robot.delay(500);
 123             window.pack();
 124             robot.delay(500);
 125             window.pack();
 126             // size1 must be the same as size0
 127             size1 = window.getSize();
 128             robot.delay(500);
 129             Point pt = jbutton1.getLocationOnScreen();
 130             robot.mouseMove((int) jbutton1.getLocationOnScreen().x + jbutton1.getWidth() / 2,
 131                             (int) jbutton1.getLocationOnScreen().y + jbutton1.getHeight() / 2);
 132             robot.delay(500);
 133             robot.mousePress(MouseEvent.BUTTON1_MASK);
 134             robot.delay(100);
 135             robot.mouseRelease(MouseEvent.BUTTON1_MASK);
 136             robot.delay(2000);
 137          }catch(Exception e) {
 138             throw new RuntimeException( "Exception "+e );
 139          }
 140          if( !size0.equals(size1) ) {
 141             throw new RuntimeException( "Wrong Window size after multiple pack()s");
 142          }
 143          if( !buttonClicked ) {
 144             throw new RuntimeException( "Button was not clicked");
 145          }
 146          window.dispose();
 147          frame.dispose();
 148 
 149          System.out.println("Test Passed.");
 150     }// start()
 151     public static Point getLocation( Component co ) throws RuntimeException {
 152        Point pt = null;
 153        boolean bFound = false;
 154        int count = 0;
 155        while( !bFound ) {
 156           try {
 157              pt = co.getLocationOnScreen();
 158              bFound = true;
 159           }catch( Exception ex ) {
 160              bFound = false;
 161              count++;
 162           }
 163           if( !bFound && count > MAX_COUNT ) {
 164              throw new RuntimeException("don't see a component to get location");
 165           }
 166        }
 167        return pt;
 168     }
 169 
 170 
 171 }// class AutomaticAppletTest
 172 
 173 
 174 /****************************************************
 175  Standard Test Machinery
 176  DO NOT modify anything below -- it's a standard
 177   chunk of code whose purpose is to make user
 178   interaction uniform, and thereby make it simpler
 179   to read and understand someone else's test.
 180  ****************************************************/
 181 
 182 /**
 183  This is part of the standard test machinery.
 184  It creates a dialog (with the instructions), and is the interface
 185   for sending text messages to the user.
 186  To print the instructions, send an array of strings to Sysout.createDialog
 187   WithInstructions method.  Put one line of instructions per array entry.
 188  To display a message for the tester to see, simply call Sysout.println
 189   with the string to be displayed.
 190  This mimics System.out.println but works within the test harness as well
 191   as standalone.
 192  */
 193 
 194 class Sysout
 195 {
 196     private static TestDialog dialog;
 197 
 198     public static void createDialogWithInstructions( String[] instructions )
 199     {
 200         dialog = new TestDialog( new Frame(), "Instructions" );
 201         dialog.printInstructions( instructions );
 202         dialog.setVisible(true);
 203         println( "Any messages for the tester will display here." );
 204     }
 205 
 206     public static void createDialog( )
 207     {
 208         dialog = new TestDialog( new Frame(), "Instructions" );
 209         String[] defInstr = { "Instructions will appear here. ", "" } ;
 210         dialog.printInstructions( defInstr );
 211         dialog.setVisible(true);
 212         println( "Any messages for the tester will display here." );
 213     }
 214 
 215 
 216     public static void printInstructions( String[] instructions )
 217     {
 218         dialog.printInstructions( instructions );
 219     }
 220 
 221 
 222     public static void println( String messageIn )
 223     {
 224         dialog.displayMessage( messageIn );
 225     }
 226 
 227 }// Sysout  class
 228 
 229 /**
 230   This is part of the standard test machinery.  It provides a place for the
 231    test instructions to be displayed, and a place for interactive messages
 232    to the user to be displayed.
 233   To have the test instructions displayed, see Sysout.
 234   To have a message to the user be displayed, see Sysout.
 235   Do not call anything in this dialog directly.
 236   */
 237 class TestDialog extends Dialog
 238 {
 239 
 240     TextArea instructionsText;
 241     TextArea messageText;
 242     int maxStringLength = 80;
 243 
 244     //DO NOT call this directly, go through Sysout
 245     public TestDialog( Frame frame, String name )
 246     {
 247         super( frame, name );
 248         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 249         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 250         add( "North", instructionsText );
 251 
 252         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 253         add("Center", messageText);
 254 
 255         pack();
 256 
 257         show();
 258     }// TestDialog()
 259 
 260     //DO NOT call this directly, go through Sysout
 261     public void printInstructions( String[] instructions )
 262     {
 263         //Clear out any current instructions
 264         instructionsText.setText( "" );
 265 
 266         //Go down array of instruction strings
 267 
 268         String printStr, remainingStr;
 269         for( int i=0; i < instructions.length; i++ )
 270         {
 271             //chop up each into pieces maxSringLength long
 272             remainingStr = instructions[ i ];
 273             while( remainingStr.length() > 0 )
 274             {
 275                 //if longer than max then chop off first max chars to print
 276                 if( remainingStr.length() >= maxStringLength )
 277                 {
 278                     //Try to chop on a word boundary
 279                     int posOfSpace = remainingStr.
 280                         lastIndexOf( ' ', maxStringLength - 1 );
 281 
 282                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 283 
 284                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
 285                     remainingStr = remainingStr.substring( posOfSpace + 1 );
 286                 }
 287                 //else just print
 288                 else
 289                 {
 290                     printStr = remainingStr;
 291                     remainingStr = "";
 292                 }
 293 
 294                 instructionsText.append( printStr + "\n" );
 295 
 296             }// while
 297 
 298         }// for
 299 
 300     }//printInstructions()
 301 
 302     //DO NOT call this directly, go through Sysout
 303     public void displayMessage( String messageIn )
 304     {
 305         messageText.append( messageIn + "\n" );
 306         System.out.println(messageIn);
 307     }
 308 
 309 }// TestDialog  class