1 /*
   2  * Copyright (c) 2006, 2018, 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 6494016
  28   @summary Nonresizable dialogs should not be resized using the Size SystemMenu command
  29   @author anthony.petrov@...: area=awt.toplevel
  30   @library ../../regtesthelpers
  31   @build Util
  32   @run main NonResizableDialogSysMenuResize
  33 */
  34 
  35 
  36 /**
  37  * NonResizableDialogSysMenuResize.java
  38  *
  39  * summary: Nonresizable dialogs should not be resized using the Size SystemMenu command
  40  */
  41 
  42 import java.awt.*;
  43 import java.awt.event.*;
  44 import test.java.awt.regtesthelpers.Util;
  45 
  46 
  47 public class NonResizableDialogSysMenuResize
  48 {
  49 
  50     //*** test-writer defined static variables go here ***
  51 
  52 
  53     private static void init()
  54     {
  55         // We must be sure that the Size system command has the S key as the shortcut one in the System menu.
  56         System.out.println("NOTE: The test is known to work correctly with English MS Windows only.");
  57 
  58         String s = Toolkit.getDefaultToolkit().getClass().getName();
  59 
  60         // This is Windows-only test
  61         if (!s.contains("WToolkit")) {
  62             pass();
  63             return;
  64         }
  65 
  66         Dialog d = new Dialog((Frame)null, "dlg", false);
  67         d.setResizable(false);
  68         d.setSize(100, 100);
  69         d.setLocation(200, 200);
  70         d.setVisible(true);
  71 
  72         Robot robot = Util.createRobot();
  73         robot.setAutoDelay(20);
  74 
  75         // To be sure both the frame and the dialog are shown and packed
  76         Util.waitForIdle(robot);
  77 
  78 
  79         // The initial dialog position and size.
  80         Point loc1 = d.getLocation();
  81         Dimension dim1 = d.getSize();
  82 
  83         System.out.println("The initial position of the dialog is: " + loc1 + "; the size is: " + dim1);
  84 
  85         try { Thread.sleep(1000); } catch (Exception e) {};
  86 
  87         // Alt-Space opens System menu
  88         robot.keyPress(KeyEvent.VK_ALT);
  89         robot.keyPress(KeyEvent.VK_SPACE);
  90         robot.keyRelease(KeyEvent.VK_SPACE);
  91         robot.keyRelease(KeyEvent.VK_ALT);
  92 
  93         // Try to choose the Size command
  94         robot.keyPress(KeyEvent.VK_S);
  95         robot.keyRelease(KeyEvent.VK_S);
  96 
  97         // Try to change the size a little
  98         for (int i = 0; i < 5; i++) {
  99             robot.keyPress(KeyEvent.VK_DOWN);
 100             robot.keyRelease(KeyEvent.VK_DOWN);
 101             robot.keyPress(KeyEvent.VK_LEFT);
 102             robot.keyRelease(KeyEvent.VK_LEFT);
 103         }
 104 
 105         // End the Size loop
 106         robot.keyPress(KeyEvent.VK_ENTER);
 107         robot.keyRelease(KeyEvent.VK_ENTER);
 108 
 109         Util.waitForIdle(robot);
 110 
 111         // The dialog position and size after trying to change its size.
 112         Point loc2 = d.getLocation();
 113         Dimension dim2 = d.getSize();
 114 
 115         System.out.println("AFTER RESIZE: The position of the dialog is: " + loc2 + "; the size is: " + dim2);
 116 
 117         if (loc2.equals(loc1) && dim2.equals(dim1)) {
 118             pass();
 119         } else {
 120             fail("The non-resizable dialog has changed its size and/or location.");
 121         }
 122 
 123     }//End  init()
 124 
 125 
 126 
 127     /*****************************************************
 128      * Standard Test Machinery Section
 129      * DO NOT modify anything in this section -- it's a
 130      * standard chunk of code which has all of the
 131      * synchronisation necessary for the test harness.
 132      * By keeping it the same in all tests, it is easier
 133      * to read and understand someone else's test, as
 134      * well as insuring that all tests behave correctly
 135      * with the test harness.
 136      * There is a section following this for test-
 137      * classes
 138      ******************************************************/
 139     private static boolean theTestPassed = false;
 140     private static boolean testGeneratedInterrupt = false;
 141     private static String failureMessage = "";
 142 
 143     private static Thread mainThread = null;
 144 
 145     private static int sleepTime = 300000;
 146 
 147     // Not sure about what happens if multiple of this test are
 148     //  instantiated in the same VM.  Being static (and using
 149     //  static vars), it aint gonna work.  Not worrying about
 150     //  it for now.
 151     public static void main( String args[] ) throws InterruptedException
 152     {
 153         mainThread = Thread.currentThread();
 154         try
 155         {
 156             init();
 157         }
 158         catch( TestPassedException e )
 159         {
 160             //The test passed, so just return from main and harness will
 161             // interepret this return as a pass
 162             return;
 163         }
 164         //At this point, neither test pass nor test fail has been
 165         // called -- either would have thrown an exception and ended the
 166         // test, so we know we have multiple threads.
 167 
 168         //Test involves other threads, so sleep and wait for them to
 169         // called pass() or fail()
 170         try
 171         {
 172             Thread.sleep( sleepTime );
 173             //Timed out, so fail the test
 174             throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
 175         }
 176         catch (InterruptedException e)
 177         {
 178             //The test harness may have interrupted the test.  If so, rethrow the exception
 179             // so that the harness gets it and deals with it.
 180             if( ! testGeneratedInterrupt ) throw e;
 181 
 182             //reset flag in case hit this code more than once for some reason (just safety)
 183             testGeneratedInterrupt = false;
 184 
 185             if ( theTestPassed == false )
 186             {
 187                 throw new RuntimeException( failureMessage );
 188             }
 189         }
 190 
 191     }//main
 192 
 193     public static synchronized void setTimeoutTo( int seconds )
 194     {
 195         sleepTime = seconds * 1000;
 196     }
 197 
 198     public static synchronized void pass()
 199     {
 200         System.out.println( "The test passed." );
 201         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
 202         //first check if this is executing in main thread
 203         if ( mainThread == Thread.currentThread() )
 204         {
 205             //Still in the main thread, so set the flag just for kicks,
 206             // and throw a test passed exception which will be caught
 207             // and end the test.
 208             theTestPassed = true;
 209             throw new TestPassedException();
 210         }
 211         theTestPassed = true;
 212         testGeneratedInterrupt = true;
 213         mainThread.interrupt();
 214     }//pass()
 215 
 216     public static synchronized void fail()
 217     {
 218         //test writer didn't specify why test failed, so give generic
 219         fail( "it just plain failed! :-)" );
 220     }
 221 
 222     public static synchronized void fail( String whyFailed )
 223     {
 224         System.out.println( "The test failed: " + whyFailed );
 225         System.out.println( "The test is over, hit  Ctl-C to stop Java VM" );
 226         //check if this called from main thread
 227         if ( mainThread == Thread.currentThread() )
 228         {
 229             //If main thread, fail now 'cause not sleeping
 230             throw new RuntimeException( whyFailed );
 231         }
 232         theTestPassed = false;
 233         testGeneratedInterrupt = true;
 234         failureMessage = whyFailed;
 235         mainThread.interrupt();
 236     }//fail()
 237 
 238 }// class NonResizableDialogSysMenuResize
 239 
 240 //This exception is used to exit from any level of call nesting
 241 // when it's determined that the test has passed, and immediately
 242 // end the test.
 243 class TestPassedException extends RuntimeException
 244 {
 245 }