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