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