1 /*
   2  * Copyright (c) 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   @test
  26   @bug 6581927
  27   @summary Non-focusable frame should honor the size of the frame buttons/decorations when resizing
  28   @library ../../regtesthelpers
  29   @build Util
  30   @author anthony.petrov@...: area=awt.toplevel
  31   @run main NonFocusableResizableTooSmall
  32 */
  33 
  34 /**
  35  * NonFocusableResizableTooSmall.java
  36  *
  37  * summary:  Non-focusable frame should honor the size of the frame buttons/decorations when resizing
  38  */
  39 
  40 import java.awt.*;
  41 import java.awt.event.*;
  42 import test.java.awt.regtesthelpers.Util;
  43 
  44 public class NonFocusableResizableTooSmall
  45 {
  46 
  47     //*** test-writer defined static variables go here ***
  48 
  49 
  50     private static void init()
  51     {
  52         //*** Create instructions for the user here ***
  53 
  54         String[] instructions =
  55         {
  56             "This is an AUTOMATIC test, simply wait until it is done.",
  57             "The result (passed or failed) will be shown in the",
  58             "message window below."
  59         };
  60         Sysout.createDialog( );
  61         Sysout.printInstructions( instructions );
  62 
  63         final Frame frame = new Frame();
  64         frame.setFocusableWindowState(false);
  65         frame.setSize(200, 100);
  66         frame.setVisible(true);
  67 
  68         final Robot robot = Util.createRobot();
  69         robot.setAutoDelay(20);
  70 
  71         // To be sure the window is shown and packed
  72         Util.waitForIdle(robot);
  73 
  74         final Insets insets = frame.getInsets();
  75         System.out.println("The insets of the frame: " + insets);
  76         if (insets.right == 0 || insets.bottom == 0) {
  77             System.out.println("The test environment must have non-zero right & bottom insets!");
  78             pass();
  79             return;
  80         }
  81 
  82         // Let's move the mouse pointer to the bottom-right coner of the frame (the "size-grip")
  83         final Rectangle bounds1 = frame.getBounds();
  84         System.out.println("The bounds before resizing: " + bounds1);
  85 
  86         robot.mouseMove(bounds1.x + bounds1.width - 1, bounds1.y + bounds1.height - 1);
  87 
  88         // ... and start resizing to some very small
  89         robot.mousePress( InputEvent.BUTTON1_MASK );
  90 
  91         // Now resize the frame so that the width is smaller
  92         // than the widths of the left and the right borders.
  93         // The sum of widths of the icon of the frame + the control-buttons
  94         // (close, minimize, etc.) should be definitely larger!
  95         robot.mouseMove(bounds1.x + insets.left + insets.right - 5, bounds1.y + bounds1.height - 1);
  96         Util.waitForIdle(robot);
  97 
  98         robot.mouseRelease( InputEvent.BUTTON1_MASK );
  99 
 100         Util.waitForIdle(robot);
 101 
 102         // Check the current bounds of the frame
 103         final Rectangle bounds2 = frame.getBounds();
 104         System.out.println("The bounds after resizing: " + bounds2);
 105 
 106         if (bounds2.width <= (insets.left + insets.right)) {
 107             fail("The frame has been resized to very small.");
 108         }
 109         pass();
 110     }//End  init()
 111 
 112 
 113 
 114     /*****************************************************
 115      * Standard Test Machinery Section
 116      * DO NOT modify anything in this section -- it's a
 117      * standard chunk of code which has all of the
 118      * synchronisation necessary for the test harness.
 119      * By keeping it the same in all tests, it is easier
 120      * to read and understand someone else's test, as
 121      * well as insuring that all tests behave correctly
 122      * with the test harness.
 123      * There is a section following this for test-
 124      * classes
 125      ******************************************************/
 126     private static boolean theTestPassed = false;
 127     private static boolean testGeneratedInterrupt = false;
 128     private static String failureMessage = "";
 129 
 130     private static Thread mainThread = null;
 131 
 132     private static int sleepTime = 300000;
 133 
 134     // Not sure about what happens if multiple of this test are
 135     //  instantiated in the same VM.  Being static (and using
 136     //  static vars), it aint gonna work.  Not worrying about
 137     //  it for now.
 138     public static void main( String args[] ) throws InterruptedException
 139     {
 140         mainThread = Thread.currentThread();
 141         try
 142         {
 143             init();
 144         }
 145         catch( TestPassedException e )
 146         {
 147             //The test passed, so just return from main and harness will
 148             // interepret this return as a pass
 149             return;
 150         }
 151         //At this point, neither test pass nor test fail has been
 152         // called -- either would have thrown an exception and ended the
 153         // test, so we know we have multiple threads.
 154 
 155         //Test involves other threads, so sleep and wait for them to
 156         // called pass() or fail()
 157         try
 158         {
 159             Thread.sleep( sleepTime );
 160             //Timed out, so fail the test
 161             throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
 162         }
 163         catch (InterruptedException e)
 164         {
 165             //The test harness may have interrupted the test.  If so, rethrow the exception
 166             // so that the harness gets it and deals with it.
 167             if( ! testGeneratedInterrupt ) throw e;
 168 
 169             //reset flag in case hit this code more than once for some reason (just safety)
 170             testGeneratedInterrupt = false;
 171 
 172             if ( theTestPassed == false )
 173             {
 174                 throw new RuntimeException( failureMessage );
 175             }
 176         }
 177 
 178     }//main
 179 
 180     public static synchronized void setTimeoutTo( int seconds )
 181     {
 182         sleepTime = seconds * 1000;
 183     }
 184 
 185     public static synchronized void pass()
 186     {
 187         Sysout.println( "The test passed." );
 188         Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
 189         //first check if this is executing in main thread
 190         if ( mainThread == Thread.currentThread() )
 191         {
 192             //Still in the main thread, so set the flag just for kicks,
 193             // and throw a test passed exception which will be caught
 194             // and end the test.
 195             theTestPassed = true;
 196             throw new TestPassedException();
 197         }
 198         theTestPassed = true;
 199         testGeneratedInterrupt = true;
 200         mainThread.interrupt();
 201     }//pass()
 202 
 203     public static synchronized void fail()
 204     {
 205         //test writer didn't specify why test failed, so give generic
 206         fail( "it just plain failed! :-)" );
 207     }
 208 
 209     public static synchronized void fail( String whyFailed )
 210     {
 211         Sysout.println( "The test failed: " + whyFailed );
 212         Sysout.println( "The test is over, hit  Ctl-C to stop Java VM" );
 213         //check if this called from main thread
 214         if ( mainThread == Thread.currentThread() )
 215         {
 216             //If main thread, fail now 'cause not sleeping
 217             throw new RuntimeException( whyFailed );
 218         }
 219         theTestPassed = false;
 220         testGeneratedInterrupt = true;
 221         failureMessage = whyFailed;
 222         mainThread.interrupt();
 223     }//fail()
 224 
 225 }// class NonFocusableResizableTooSmall
 226 
 227 //This exception is used to exit from any level of call nesting
 228 // when it's determined that the test has passed, and immediately
 229 // end the test.
 230 class TestPassedException extends RuntimeException
 231 {
 232 }
 233 
 234 //*********** End Standard Test Machinery Section **********
 235 
 236 
 237 //************ Begin classes defined for the test ****************
 238 
 239 // if want to make listeners, here is the recommended place for them, then instantiate
 240 //  them in init()
 241 
 242 /* Example of a class which may be written as part of a test
 243 class NewClass implements anInterface
 244  {
 245    static int newVar = 0;
 246 
 247    public void eventDispatched(AWTEvent e)
 248     {
 249       //Counting events to see if we get enough
 250       eventCount++;
 251 
 252       if( eventCount == 20 )
 253        {
 254          //got enough events, so pass
 255 
 256          NonFocusableResizableTooSmall.pass();
 257        }
 258       else if( tries == 20 )
 259        {
 260          //tried too many times without getting enough events so fail
 261 
 262          NonFocusableResizableTooSmall.fail();
 263        }
 264 
 265     }// eventDispatched()
 266 
 267  }// NewClass class
 268 
 269 */
 270 
 271 
 272 //************** End classes defined for the test *******************
 273 
 274 
 275 
 276 
 277 /****************************************************
 278  Standard Test Machinery
 279  DO NOT modify anything below -- it's a standard
 280   chunk of code whose purpose is to make user
 281   interaction uniform, and thereby make it simpler
 282   to read and understand someone else's test.
 283  ****************************************************/
 284 
 285 /**
 286  This is part of the standard test machinery.
 287  It creates a dialog (with the instructions), and is the interface
 288   for sending text messages to the user.
 289  To print the instructions, send an array of strings to Sysout.createDialog
 290   WithInstructions method.  Put one line of instructions per array entry.
 291  To display a message for the tester to see, simply call Sysout.println
 292   with the string to be displayed.
 293  This mimics System.out.println but works within the test harness as well
 294   as standalone.
 295  */
 296 
 297 class Sysout
 298 {
 299     private static TestDialog dialog;
 300 
 301     public static void createDialogWithInstructions( String[] instructions )
 302     {
 303         dialog = new TestDialog( new Frame(), "Instructions" );
 304         dialog.printInstructions( instructions );
 305         dialog.setVisible(true);
 306         println( "Any messages for the tester will display here." );
 307     }
 308 
 309     public static void createDialog( )
 310     {
 311         dialog = new TestDialog( new Frame(), "Instructions" );
 312         String[] defInstr = { "Instructions will appear here. ", "" } ;
 313         dialog.printInstructions( defInstr );
 314         dialog.setVisible(true);
 315         println( "Any messages for the tester will display here." );
 316     }
 317 
 318 
 319     public static void printInstructions( String[] instructions )
 320     {
 321         dialog.printInstructions( instructions );
 322     }
 323 
 324 
 325     public static void println( String messageIn )
 326     {
 327         dialog.displayMessage( messageIn );
 328         System.out.println(messageIn);
 329     }
 330 
 331 }// Sysout  class
 332 
 333 /**
 334   This is part of the standard test machinery.  It provides a place for the
 335    test instructions to be displayed, and a place for interactive messages
 336    to the user to be displayed.
 337   To have the test instructions displayed, see Sysout.
 338   To have a message to the user be displayed, see Sysout.
 339   Do not call anything in this dialog directly.
 340   */
 341 class TestDialog extends Dialog
 342 {
 343 
 344     TextArea instructionsText;
 345     TextArea messageText;
 346     int maxStringLength = 80;
 347 
 348     //DO NOT call this directly, go through Sysout
 349     public TestDialog( Frame frame, String name )
 350     {
 351         super( frame, name );
 352         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 353         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 354         add( "North", instructionsText );
 355 
 356         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 357         add("Center", messageText);
 358 
 359         pack();
 360 
 361         setVisible(true);
 362     }// TestDialog()
 363 
 364     //DO NOT call this directly, go through Sysout
 365     public void printInstructions( String[] instructions )
 366     {
 367         //Clear out any current instructions
 368         instructionsText.setText( "" );
 369 
 370         //Go down array of instruction strings
 371 
 372         String printStr, remainingStr;
 373         for( int i=0; i < instructions.length; i++ )
 374         {
 375             //chop up each into pieces maxSringLength long
 376             remainingStr = instructions[ i ];
 377             while( remainingStr.length() > 0 )
 378             {
 379                 //if longer than max then chop off first max chars to print
 380                 if( remainingStr.length() >= maxStringLength )
 381                 {
 382                     //Try to chop on a word boundary
 383                     int posOfSpace = remainingStr.
 384                         lastIndexOf( ' ', maxStringLength - 1 );
 385 
 386                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 387 
 388                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
 389                     remainingStr = remainingStr.substring( posOfSpace + 1 );
 390                 }
 391                 //else just print
 392                 else
 393                 {
 394                     printStr = remainingStr;
 395                     remainingStr = "";
 396                 }
 397 
 398                 instructionsText.append( printStr + "\n" );
 399 
 400             }// while
 401 
 402         }// for
 403 
 404     }//printInstructions()
 405 
 406     //DO NOT call this directly, go through Sysout
 407     public void displayMessage( String messageIn )
 408     {
 409         messageText.append( messageIn + "\n" );
 410         System.out.println(messageIn);
 411     }
 412 
 413 }// TestDialog  class