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         System.setSecurityManager( new SecurityManager() {
  95         // deny AWTPermission("showWindowWithoutWarningBanner")
  96             public boolean checkTopLevelWindow(Object window) {
  97                 return false;
  98             }
  99          });
 100         JFrame frame = new JFrame("Window Test");
 101         frame.setBounds(50, 50, 200, 200);
 102         frame.show();
 103 
 104         JWindow window = new JWindow( frame );
 105         JButton jbutton1 = new JButton( "First" );
 106         jbutton1.addMouseListener( new MouseAdapter() {
 107             public void mousePressed( MouseEvent me ) {
 108                 buttonClicked = true;
 109             }
 110          });
 111         JButton jbutton2 = new JButton( "Second" );
 112         window.setLocation( 300, 300 );
 113 
 114         window.add("North", jbutton1);
 115         window.add("South", jbutton2);
 116 
 117         window.pack();
 118         window.show();
 119         //wait for frame to show:
 120         getLocation( frame );
 121         window.toFront();
 122 
 123         Dimension size0 = window.getSize();
 124         Dimension size1 = null;
 125         try {
 126             Robot robot = new Robot();
 127 
 128             robot.delay(500);
 129             window.pack();
 130             robot.delay(500);
 131             window.pack();
 132             // size1 must be the same as size0
 133             size1 = window.getSize();
 134             robot.delay(500);
 135             Point pt = jbutton1.getLocationOnScreen();
 136             robot.mouseMove((int) jbutton1.getLocationOnScreen().x + jbutton1.getWidth() / 2,
 137                             (int) jbutton1.getLocationOnScreen().y + jbutton1.getHeight() / 2);
 138             robot.delay(500);
 139             robot.mousePress(MouseEvent.BUTTON1_MASK);
 140             robot.delay(100);
 141             robot.mouseRelease(MouseEvent.BUTTON1_MASK);
 142             robot.delay(2000);
 143          }catch(Exception e) {
 144             throw new RuntimeException( "Exception "+e );
 145          }
 146          if( !size0.equals(size1) ) {
 147             throw new RuntimeException( "Wrong Window size after multiple pack()s");
 148          }
 149          if( !buttonClicked ) {
 150             throw new RuntimeException( "Button was not clicked");
 151          }
 152          window.dispose();
 153          frame.dispose();
 154 
 155          System.out.println("Test Passed.");
 156     }// start()
 157     public static Point getLocation( Component co ) throws RuntimeException {
 158        Point pt = null;
 159        boolean bFound = false;
 160        int count = 0;
 161        while( !bFound ) {
 162           try {
 163              pt = co.getLocationOnScreen();
 164              bFound = true;
 165           }catch( Exception ex ) {
 166              bFound = false;
 167              count++;
 168           }
 169           if( !bFound && count > MAX_COUNT ) {
 170              throw new RuntimeException("don't see a component to get location");
 171           }
 172        }
 173        return pt;
 174     }
 175 
 176 
 177 }// class AutomaticAppletTest
 178 
 179 
 180 /****************************************************
 181  Standard Test Machinery
 182  DO NOT modify anything below -- it's a standard
 183   chunk of code whose purpose is to make user
 184   interaction uniform, and thereby make it simpler
 185   to read and understand someone else's test.
 186  ****************************************************/
 187 
 188 /**
 189  This is part of the standard test machinery.
 190  It creates a dialog (with the instructions), and is the interface
 191   for sending text messages to the user.
 192  To print the instructions, send an array of strings to Sysout.createDialog
 193   WithInstructions method.  Put one line of instructions per array entry.
 194  To display a message for the tester to see, simply call Sysout.println
 195   with the string to be displayed.
 196  This mimics System.out.println but works within the test harness as well
 197   as standalone.
 198  */
 199 
 200 class Sysout
 201 {
 202     private static TestDialog dialog;
 203 
 204     public static void createDialogWithInstructions( String[] instructions )
 205     {
 206         dialog = new TestDialog( new Frame(), "Instructions" );
 207         dialog.printInstructions( instructions );
 208         dialog.setVisible(true);
 209         println( "Any messages for the tester will display here." );
 210     }
 211 
 212     public static void createDialog( )
 213     {
 214         dialog = new TestDialog( new Frame(), "Instructions" );
 215         String[] defInstr = { "Instructions will appear here. ", "" } ;
 216         dialog.printInstructions( defInstr );
 217         dialog.setVisible(true);
 218         println( "Any messages for the tester will display here." );
 219     }
 220 
 221 
 222     public static void printInstructions( String[] instructions )
 223     {
 224         dialog.printInstructions( instructions );
 225     }
 226 
 227 
 228     public static void println( String messageIn )
 229     {
 230         dialog.displayMessage( messageIn );
 231     }
 232 
 233 }// Sysout  class
 234 
 235 /**
 236   This is part of the standard test machinery.  It provides a place for the
 237    test instructions to be displayed, and a place for interactive messages
 238    to the user to be displayed.
 239   To have the test instructions displayed, see Sysout.
 240   To have a message to the user be displayed, see Sysout.
 241   Do not call anything in this dialog directly.
 242   */
 243 class TestDialog extends Dialog
 244 {
 245 
 246     TextArea instructionsText;
 247     TextArea messageText;
 248     int maxStringLength = 80;
 249 
 250     //DO NOT call this directly, go through Sysout
 251     public TestDialog( Frame frame, String name )
 252     {
 253         super( frame, name );
 254         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 255         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 256         add( "North", instructionsText );
 257 
 258         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 259         add("Center", messageText);
 260 
 261         pack();
 262 
 263         show();
 264     }// TestDialog()
 265 
 266     //DO NOT call this directly, go through Sysout
 267     public void printInstructions( String[] instructions )
 268     {
 269         //Clear out any current instructions
 270         instructionsText.setText( "" );
 271 
 272         //Go down array of instruction strings
 273 
 274         String printStr, remainingStr;
 275         for( int i=0; i < instructions.length; i++ )
 276         {
 277             //chop up each into pieces maxSringLength long
 278             remainingStr = instructions[ i ];
 279             while( remainingStr.length() > 0 )
 280             {
 281                 //if longer than max then chop off first max chars to print
 282                 if( remainingStr.length() >= maxStringLength )
 283                 {
 284                     //Try to chop on a word boundary
 285                     int posOfSpace = remainingStr.
 286                         lastIndexOf( ' ', maxStringLength - 1 );
 287 
 288                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 289 
 290                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
 291                     remainingStr = remainingStr.substring( posOfSpace + 1 );
 292                 }
 293                 //else just print
 294                 else
 295                 {
 296                     printStr = remainingStr;
 297                     remainingStr = "";
 298                 }
 299 
 300                 instructionsText.append( printStr + "\n" );
 301 
 302             }// while
 303 
 304         }// for
 305 
 306     }//printInstructions()
 307 
 308     //DO NOT call this directly, go through Sysout
 309     public void displayMessage( String messageIn )
 310     {
 311         messageText.append( messageIn + "\n" );
 312         System.out.println(messageIn);
 313     }
 314 
 315 }// TestDialog  class