Path: blob/master/test/jdk/java/awt/Dialog/NonResizableDialogSysMenuResize/NonResizableDialogSysMenuResize.java
41153 views
/*1* Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24@test25@key headful26@bug 649401627@summary Nonresizable dialogs should not be resized using the Size SystemMenu command28@author anthony.petrov@...: area=awt.toplevel29@library ../../regtesthelpers30@build Util31@run main NonResizableDialogSysMenuResize32*/333435/**36* NonResizableDialogSysMenuResize.java37*38* summary: Nonresizable dialogs should not be resized using the Size SystemMenu command39*/4041import java.awt.*;42import java.awt.event.*;43import test.java.awt.regtesthelpers.Util;444546public class NonResizableDialogSysMenuResize47{4849//*** test-writer defined static variables go here ***505152private static void init()53{54// We must be sure that the Size system command has the S key as the shortcut one in the System menu.55System.out.println("NOTE: The test is known to work correctly with English MS Windows only.");5657String s = Toolkit.getDefaultToolkit().getClass().getName();5859// This is Windows-only test60if (!s.contains("WToolkit")) {61pass();62return;63}6465Dialog d = new Dialog((Frame)null, "dlg", false);66d.setResizable(false);67d.setSize(100, 100);68d.setLocation(200, 200);69d.setVisible(true);7071Robot robot = Util.createRobot();72robot.setAutoDelay(20);7374// To be sure both the frame and the dialog are shown and packed75Util.waitForIdle(robot);767778// The initial dialog position and size.79Point loc1 = d.getLocation();80Dimension dim1 = d.getSize();8182System.out.println("The initial position of the dialog is: " + loc1 + "; the size is: " + dim1);8384try { Thread.sleep(1000); } catch (Exception e) {};8586// Alt-Space opens System menu87robot.keyPress(KeyEvent.VK_ALT);88robot.keyPress(KeyEvent.VK_SPACE);89robot.keyRelease(KeyEvent.VK_SPACE);90robot.keyRelease(KeyEvent.VK_ALT);9192// Try to choose the Size command93robot.keyPress(KeyEvent.VK_S);94robot.keyRelease(KeyEvent.VK_S);9596// Try to change the size a little97for (int i = 0; i < 5; i++) {98robot.keyPress(KeyEvent.VK_DOWN);99robot.keyRelease(KeyEvent.VK_DOWN);100robot.keyPress(KeyEvent.VK_LEFT);101robot.keyRelease(KeyEvent.VK_LEFT);102}103104// End the Size loop105robot.keyPress(KeyEvent.VK_ENTER);106robot.keyRelease(KeyEvent.VK_ENTER);107108Util.waitForIdle(robot);109110// The dialog position and size after trying to change its size.111Point loc2 = d.getLocation();112Dimension dim2 = d.getSize();113114System.out.println("AFTER RESIZE: The position of the dialog is: " + loc2 + "; the size is: " + dim2);115116if (loc2.equals(loc1) && dim2.equals(dim1)) {117pass();118} else {119fail("The non-resizable dialog has changed its size and/or location.");120}121122}//End init()123124125126/*****************************************************127* Standard Test Machinery Section128* DO NOT modify anything in this section -- it's a129* standard chunk of code which has all of the130* synchronisation necessary for the test harness.131* By keeping it the same in all tests, it is easier132* to read and understand someone else's test, as133* well as insuring that all tests behave correctly134* with the test harness.135* There is a section following this for test-136* classes137******************************************************/138private static boolean theTestPassed = false;139private static boolean testGeneratedInterrupt = false;140private static String failureMessage = "";141142private static Thread mainThread = null;143144private static int sleepTime = 300000;145146// Not sure about what happens if multiple of this test are147// instantiated in the same VM. Being static (and using148// static vars), it aint gonna work. Not worrying about149// it for now.150public static void main( String args[] ) throws InterruptedException151{152mainThread = Thread.currentThread();153try154{155init();156}157catch( TestPassedException e )158{159//The test passed, so just return from main and harness will160// interepret this return as a pass161return;162}163//At this point, neither test pass nor test fail has been164// called -- either would have thrown an exception and ended the165// test, so we know we have multiple threads.166167//Test involves other threads, so sleep and wait for them to168// called pass() or fail()169try170{171Thread.sleep( sleepTime );172//Timed out, so fail the test173throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );174}175catch (InterruptedException e)176{177//The test harness may have interrupted the test. If so, rethrow the exception178// so that the harness gets it and deals with it.179if( ! testGeneratedInterrupt ) throw e;180181//reset flag in case hit this code more than once for some reason (just safety)182testGeneratedInterrupt = false;183184if ( theTestPassed == false )185{186throw new RuntimeException( failureMessage );187}188}189190}//main191192public static synchronized void setTimeoutTo( int seconds )193{194sleepTime = seconds * 1000;195}196197public static synchronized void pass()198{199System.out.println( "The test passed." );200System.out.println( "The test is over, hit Ctl-C to stop Java VM" );201//first check if this is executing in main thread202if ( mainThread == Thread.currentThread() )203{204//Still in the main thread, so set the flag just for kicks,205// and throw a test passed exception which will be caught206// and end the test.207theTestPassed = true;208throw new TestPassedException();209}210theTestPassed = true;211testGeneratedInterrupt = true;212mainThread.interrupt();213}//pass()214215public static synchronized void fail()216{217//test writer didn't specify why test failed, so give generic218fail( "it just plain failed! :-)" );219}220221public static synchronized void fail( String whyFailed )222{223System.out.println( "The test failed: " + whyFailed );224System.out.println( "The test is over, hit Ctl-C to stop Java VM" );225//check if this called from main thread226if ( mainThread == Thread.currentThread() )227{228//If main thread, fail now 'cause not sleeping229throw new RuntimeException( whyFailed );230}231theTestPassed = false;232testGeneratedInterrupt = true;233failureMessage = whyFailed;234mainThread.interrupt();235}//fail()236237}// class NonResizableDialogSysMenuResize238239//This exception is used to exit from any level of call nesting240// when it's determined that the test has passed, and immediately241// end the test.242class TestPassedException extends RuntimeException243{244}245246247