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