Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Dialog/NonResizableDialogSysMenuResize/NonResizableDialogSysMenuResize.java
38828 views
/*1* Copyright (c) 2006, 2007, 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@bug 649401626@summary Nonresizable dialogs should not be resized using the Size SystemMenu command27@author anthony.petrov@...: area=awt.toplevel28@library ../../regtesthelpers29@build Util30@run main NonResizableDialogSysMenuResize31*/323334/**35* NonResizableDialogSysMenuResize.java36*37* summary: Nonresizable dialogs should not be resized using the Size SystemMenu command38*/3940import java.awt.*;41import java.awt.event.*;42import test.java.awt.regtesthelpers.Util;434445public class NonResizableDialogSysMenuResize46{4748//*** test-writer defined static variables go here ***495051private static void init()52{53//*** Create instructions for the user here ***5455String[] 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};61Sysout.createDialog( );62Sysout.printInstructions( instructions );6364// We must be sure that the Size system command has the S key as the shortcut one in the System menu.65Sysout.println("NOTE: The test is known to work correctly with English MS Windows only.");6667String s = Toolkit.getDefaultToolkit().getClass().getName();6869// This is Windows-only test70if (!s.contains("WToolkit")) {71pass();72return;73}7475Dialog d = new Dialog((Frame)null, "dlg", false);76d.setResizable(false);77d.setSize(100, 100);78d.setLocation(200, 200);79d.setVisible(true);8081Robot robot = Util.createRobot();82robot.setAutoDelay(20);8384// To be sure both the frame and the dialog are shown and packed85Util.waitForIdle(robot);868788// The initial dialog position and size.89Point loc1 = d.getLocation();90Dimension dim1 = d.getSize();9192System.out.println("The initial position of the dialog is: " + loc1 + "; the size is: " + dim1);9394try { Thread.sleep(1000); } catch (Exception e) {};9596// Alt-Space opens System menu97robot.keyPress(KeyEvent.VK_ALT);98robot.keyPress(KeyEvent.VK_SPACE);99robot.keyRelease(KeyEvent.VK_SPACE);100robot.keyRelease(KeyEvent.VK_ALT);101102// Try to choose the Size command103robot.keyPress(KeyEvent.VK_S);104robot.keyRelease(KeyEvent.VK_S);105106// Try to change the size a little107for (int i = 0; i < 5; i++) {108robot.keyPress(KeyEvent.VK_DOWN);109robot.keyRelease(KeyEvent.VK_DOWN);110robot.keyPress(KeyEvent.VK_LEFT);111robot.keyRelease(KeyEvent.VK_LEFT);112}113114// End the Size loop115robot.keyPress(KeyEvent.VK_ENTER);116robot.keyRelease(KeyEvent.VK_ENTER);117118Util.waitForIdle(robot);119120// The dialog position and size after trying to change its size.121Point loc2 = d.getLocation();122Dimension dim2 = d.getSize();123124System.out.println("AFTER RESIZE: The position of the dialog is: " + loc2 + "; the size is: " + dim2);125126if (loc2.equals(loc1) && dim2.equals(dim1)) {127pass();128} else {129fail("The non-resizable dialog has changed its size and/or location.");130}131132}//End init()133134135136/*****************************************************137* Standard Test Machinery Section138* DO NOT modify anything in this section -- it's a139* standard chunk of code which has all of the140* synchronisation necessary for the test harness.141* By keeping it the same in all tests, it is easier142* to read and understand someone else's test, as143* well as insuring that all tests behave correctly144* with the test harness.145* There is a section following this for test-146* classes147******************************************************/148private static boolean theTestPassed = false;149private static boolean testGeneratedInterrupt = false;150private static String failureMessage = "";151152private static Thread mainThread = null;153154private static int sleepTime = 300000;155156// Not sure about what happens if multiple of this test are157// instantiated in the same VM. Being static (and using158// static vars), it aint gonna work. Not worrying about159// it for now.160public static void main( String args[] ) throws InterruptedException161{162mainThread = Thread.currentThread();163try164{165init();166}167catch( TestPassedException e )168{169//The test passed, so just return from main and harness will170// interepret this return as a pass171return;172}173//At this point, neither test pass nor test fail has been174// called -- either would have thrown an exception and ended the175// test, so we know we have multiple threads.176177//Test involves other threads, so sleep and wait for them to178// called pass() or fail()179try180{181Thread.sleep( sleepTime );182//Timed out, so fail the test183throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );184}185catch (InterruptedException e)186{187//The test harness may have interrupted the test. If so, rethrow the exception188// so that the harness gets it and deals with it.189if( ! testGeneratedInterrupt ) throw e;190191//reset flag in case hit this code more than once for some reason (just safety)192testGeneratedInterrupt = false;193194if ( theTestPassed == false )195{196throw new RuntimeException( failureMessage );197}198}199200}//main201202public static synchronized void setTimeoutTo( int seconds )203{204sleepTime = seconds * 1000;205}206207public static synchronized void pass()208{209Sysout.println( "The test passed." );210Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );211//first check if this is executing in main thread212if ( mainThread == Thread.currentThread() )213{214//Still in the main thread, so set the flag just for kicks,215// and throw a test passed exception which will be caught216// and end the test.217theTestPassed = true;218throw new TestPassedException();219}220theTestPassed = true;221testGeneratedInterrupt = true;222mainThread.interrupt();223}//pass()224225public static synchronized void fail()226{227//test writer didn't specify why test failed, so give generic228fail( "it just plain failed! :-)" );229}230231public static synchronized void fail( String whyFailed )232{233Sysout.println( "The test failed: " + whyFailed );234Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );235//check if this called from main thread236if ( mainThread == Thread.currentThread() )237{238//If main thread, fail now 'cause not sleeping239throw new RuntimeException( whyFailed );240}241theTestPassed = false;242testGeneratedInterrupt = true;243failureMessage = whyFailed;244mainThread.interrupt();245}//fail()246247}// class NonResizableDialogSysMenuResize248249//This exception is used to exit from any level of call nesting250// when it's determined that the test has passed, and immediately251// end the test.252class TestPassedException extends RuntimeException253{254}255256//*********** End Standard Test Machinery Section **********257258259//************ Begin classes defined for the test ****************260261// if want to make listeners, here is the recommended place for them, then instantiate262// them in init()263264/* Example of a class which may be written as part of a test265class NewClass implements anInterface266{267static int newVar = 0;268269public void eventDispatched(AWTEvent e)270{271//Counting events to see if we get enough272eventCount++;273274if( eventCount == 20 )275{276//got enough events, so pass277278NonResizableDialogSysMenuResize.pass();279}280else if( tries == 20 )281{282//tried too many times without getting enough events so fail283284NonResizableDialogSysMenuResize.fail();285}286287}// eventDispatched()288289}// NewClass class290291*/292293294//************** End classes defined for the test *******************295296297298299/****************************************************300Standard Test Machinery301DO NOT modify anything below -- it's a standard302chunk of code whose purpose is to make user303interaction uniform, and thereby make it simpler304to read and understand someone else's test.305****************************************************/306307/**308This is part of the standard test machinery.309It creates a dialog (with the instructions), and is the interface310for sending text messages to the user.311To print the instructions, send an array of strings to Sysout.createDialog312WithInstructions method. Put one line of instructions per array entry.313To display a message for the tester to see, simply call Sysout.println314with the string to be displayed.315This mimics System.out.println but works within the test harness as well316as standalone.317*/318319class Sysout320{321private static TestDialog dialog;322323public static void createDialogWithInstructions( String[] instructions )324{325dialog = new TestDialog( new Frame(), "Instructions" );326dialog.printInstructions( instructions );327dialog.setVisible(true);328println( "Any messages for the tester will display here." );329}330331public static void createDialog( )332{333dialog = new TestDialog( new Frame(), "Instructions" );334String[] defInstr = { "Instructions will appear here. ", "" } ;335dialog.printInstructions( defInstr );336dialog.setVisible(true);337println( "Any messages for the tester will display here." );338}339340341public static void printInstructions( String[] instructions )342{343dialog.printInstructions( instructions );344}345346347public static void println( String messageIn )348{349dialog.displayMessage( messageIn );350System.out.println(messageIn);351}352353}// Sysout class354355/**356This is part of the standard test machinery. It provides a place for the357test instructions to be displayed, and a place for interactive messages358to the user to be displayed.359To have the test instructions displayed, see Sysout.360To have a message to the user be displayed, see Sysout.361Do not call anything in this dialog directly.362*/363class TestDialog extends Dialog364{365366TextArea instructionsText;367TextArea messageText;368int maxStringLength = 80;369370//DO NOT call this directly, go through Sysout371public TestDialog( Frame frame, String name )372{373super( frame, name );374int scrollBoth = TextArea.SCROLLBARS_BOTH;375instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );376add( "North", instructionsText );377378messageText = new TextArea( "", 5, maxStringLength, scrollBoth );379add("Center", messageText);380381pack();382383setVisible(true);384}// TestDialog()385386//DO NOT call this directly, go through Sysout387public void printInstructions( String[] instructions )388{389//Clear out any current instructions390instructionsText.setText( "" );391392//Go down array of instruction strings393394String printStr, remainingStr;395for( int i=0; i < instructions.length; i++ )396{397//chop up each into pieces maxSringLength long398remainingStr = instructions[ i ];399while( remainingStr.length() > 0 )400{401//if longer than max then chop off first max chars to print402if( remainingStr.length() >= maxStringLength )403{404//Try to chop on a word boundary405int posOfSpace = remainingStr.406lastIndexOf( ' ', maxStringLength - 1 );407408if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;409410printStr = remainingStr.substring( 0, posOfSpace + 1 );411remainingStr = remainingStr.substring( posOfSpace + 1 );412}413//else just print414else415{416printStr = remainingStr;417remainingStr = "";418}419420instructionsText.append( printStr + "\n" );421422}// while423424}// for425426}//printInstructions()427428//DO NOT call this directly, go through Sysout429public void displayMessage( String messageIn )430{431messageText.append( messageIn + "\n" );432System.out.println(messageIn);433}434435}// TestDialog class436437438