Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/NonFocusableResizableTooSmall/NonFocusableResizableTooSmall.java
47490 views
/*1* Copyright (c) 2008, 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 658192726@summary Non-focusable frame should honor the size of the frame buttons/decorations when resizing27@library ../../regtesthelpers28@build Util29@author anthony.petrov@...: area=awt.toplevel30@run main NonFocusableResizableTooSmall31*/3233/**34* NonFocusableResizableTooSmall.java35*36* summary: Non-focusable frame should honor the size of the frame buttons/decorations when resizing37*/3839import java.awt.*;40import java.awt.event.*;41import test.java.awt.regtesthelpers.Util;4243public class NonFocusableResizableTooSmall44{4546//*** test-writer defined static variables go here ***474849private static void init()50{51//*** Create instructions for the user here ***5253String[] instructions =54{55"This is an AUTOMATIC test, simply wait until it is done.",56"The result (passed or failed) will be shown in the",57"message window below."58};59Sysout.createDialog( );60Sysout.printInstructions( instructions );6162final Frame frame = new Frame();63frame.setFocusableWindowState(false);64frame.setSize(200, 100);65frame.setVisible(true);6667final Robot robot = Util.createRobot();68robot.setAutoDelay(20);6970// To be sure the window is shown and packed71Util.waitForIdle(robot);7273final Insets insets = frame.getInsets();74System.out.println("The insets of the frame: " + insets);75if (insets.right == 0 || insets.bottom == 0) {76System.out.println("The test environment must have non-zero right & bottom insets!");77pass();78return;79}8081// Let's move the mouse pointer to the bottom-right coner of the frame (the "size-grip")82final Rectangle bounds1 = frame.getBounds();83System.out.println("The bounds before resizing: " + bounds1);8485robot.mouseMove(bounds1.x + bounds1.width - 1, bounds1.y + bounds1.height - 1);8687// ... and start resizing to some very small88robot.mousePress( InputEvent.BUTTON1_MASK );8990// Now resize the frame so that the width is smaller91// than the widths of the left and the right borders.92// The sum of widths of the icon of the frame + the control-buttons93// (close, minimize, etc.) should be definitely larger!94robot.mouseMove(bounds1.x + insets.left + insets.right - 5, bounds1.y + bounds1.height - 1);95Util.waitForIdle(robot);9697robot.mouseRelease( InputEvent.BUTTON1_MASK );9899Util.waitForIdle(robot);100101// Check the current bounds of the frame102final Rectangle bounds2 = frame.getBounds();103System.out.println("The bounds after resizing: " + bounds2);104105if (bounds2.width <= (insets.left + insets.right)) {106fail("The frame has been resized to very small.");107}108pass();109}//End init()110111112113/*****************************************************114* Standard Test Machinery Section115* DO NOT modify anything in this section -- it's a116* standard chunk of code which has all of the117* synchronisation necessary for the test harness.118* By keeping it the same in all tests, it is easier119* to read and understand someone else's test, as120* well as insuring that all tests behave correctly121* with the test harness.122* There is a section following this for test-123* classes124******************************************************/125private static boolean theTestPassed = false;126private static boolean testGeneratedInterrupt = false;127private static String failureMessage = "";128129private static Thread mainThread = null;130131private static int sleepTime = 300000;132133// Not sure about what happens if multiple of this test are134// instantiated in the same VM. Being static (and using135// static vars), it aint gonna work. Not worrying about136// it for now.137public static void main( String args[] ) throws InterruptedException138{139mainThread = Thread.currentThread();140try141{142init();143}144catch( TestPassedException e )145{146//The test passed, so just return from main and harness will147// interepret this return as a pass148return;149}150//At this point, neither test pass nor test fail has been151// called -- either would have thrown an exception and ended the152// test, so we know we have multiple threads.153154//Test involves other threads, so sleep and wait for them to155// called pass() or fail()156try157{158Thread.sleep( sleepTime );159//Timed out, so fail the test160throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );161}162catch (InterruptedException e)163{164//The test harness may have interrupted the test. If so, rethrow the exception165// so that the harness gets it and deals with it.166if( ! testGeneratedInterrupt ) throw e;167168//reset flag in case hit this code more than once for some reason (just safety)169testGeneratedInterrupt = false;170171if ( theTestPassed == false )172{173throw new RuntimeException( failureMessage );174}175}176177}//main178179public static synchronized void setTimeoutTo( int seconds )180{181sleepTime = seconds * 1000;182}183184public static synchronized void pass()185{186Sysout.println( "The test passed." );187Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );188//first check if this is executing in main thread189if ( 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 caught193// and end the test.194theTestPassed = true;195throw new TestPassedException();196}197theTestPassed = true;198testGeneratedInterrupt = true;199mainThread.interrupt();200}//pass()201202public static synchronized void fail()203{204//test writer didn't specify why test failed, so give generic205fail( "it just plain failed! :-)" );206}207208public static synchronized void fail( String whyFailed )209{210Sysout.println( "The test failed: " + whyFailed );211Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );212//check if this called from main thread213if ( mainThread == Thread.currentThread() )214{215//If main thread, fail now 'cause not sleeping216throw new RuntimeException( whyFailed );217}218theTestPassed = false;219testGeneratedInterrupt = true;220failureMessage = whyFailed;221mainThread.interrupt();222}//fail()223224}// class NonFocusableResizableTooSmall225226//This exception is used to exit from any level of call nesting227// when it's determined that the test has passed, and immediately228// end the test.229class TestPassedException extends RuntimeException230{231}232233//*********** End Standard Test Machinery Section **********234235236//************ Begin classes defined for the test ****************237238// if want to make listeners, here is the recommended place for them, then instantiate239// them in init()240241/* Example of a class which may be written as part of a test242class NewClass implements anInterface243{244static int newVar = 0;245246public void eventDispatched(AWTEvent e)247{248//Counting events to see if we get enough249eventCount++;250251if( eventCount == 20 )252{253//got enough events, so pass254255NonFocusableResizableTooSmall.pass();256}257else if( tries == 20 )258{259//tried too many times without getting enough events so fail260261NonFocusableResizableTooSmall.fail();262}263264}// eventDispatched()265266}// NewClass class267268*/269270271//************** End classes defined for the test *******************272273274275276/****************************************************277Standard Test Machinery278DO NOT modify anything below -- it's a standard279chunk of code whose purpose is to make user280interaction uniform, and thereby make it simpler281to read and understand someone else's test.282****************************************************/283284/**285This is part of the standard test machinery.286It creates a dialog (with the instructions), and is the interface287for sending text messages to the user.288To print the instructions, send an array of strings to Sysout.createDialog289WithInstructions method. Put one line of instructions per array entry.290To display a message for the tester to see, simply call Sysout.println291with the string to be displayed.292This mimics System.out.println but works within the test harness as well293as standalone.294*/295296class Sysout297{298private static TestDialog dialog;299300public static void createDialogWithInstructions( String[] instructions )301{302dialog = new TestDialog( new Frame(), "Instructions" );303dialog.printInstructions( instructions );304dialog.setVisible(true);305println( "Any messages for the tester will display here." );306}307308public static void createDialog( )309{310dialog = new TestDialog( new Frame(), "Instructions" );311String[] defInstr = { "Instructions will appear here. ", "" } ;312dialog.printInstructions( defInstr );313dialog.setVisible(true);314println( "Any messages for the tester will display here." );315}316317318public static void printInstructions( String[] instructions )319{320dialog.printInstructions( instructions );321}322323324public static void println( String messageIn )325{326dialog.displayMessage( messageIn );327System.out.println(messageIn);328}329330}// Sysout class331332/**333This is part of the standard test machinery. It provides a place for the334test instructions to be displayed, and a place for interactive messages335to the user to be displayed.336To have the test instructions displayed, see Sysout.337To have a message to the user be displayed, see Sysout.338Do not call anything in this dialog directly.339*/340class TestDialog extends Dialog341{342343TextArea instructionsText;344TextArea messageText;345int maxStringLength = 80;346347//DO NOT call this directly, go through Sysout348public TestDialog( Frame frame, String name )349{350super( frame, name );351int scrollBoth = TextArea.SCROLLBARS_BOTH;352instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );353add( "North", instructionsText );354355messageText = new TextArea( "", 5, maxStringLength, scrollBoth );356add("Center", messageText);357358pack();359360setVisible(true);361}// TestDialog()362363//DO NOT call this directly, go through Sysout364public void printInstructions( String[] instructions )365{366//Clear out any current instructions367instructionsText.setText( "" );368369//Go down array of instruction strings370371String printStr, remainingStr;372for( int i=0; i < instructions.length; i++ )373{374//chop up each into pieces maxSringLength long375remainingStr = instructions[ i ];376while( remainingStr.length() > 0 )377{378//if longer than max then chop off first max chars to print379if( remainingStr.length() >= maxStringLength )380{381//Try to chop on a word boundary382int posOfSpace = remainingStr.383lastIndexOf( ' ', maxStringLength - 1 );384385if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;386387printStr = remainingStr.substring( 0, posOfSpace + 1 );388remainingStr = remainingStr.substring( posOfSpace + 1 );389}390//else just print391else392{393printStr = remainingStr;394remainingStr = "";395}396397instructionsText.append( printStr + "\n" );398399}// while400401}// for402403}//printInstructions()404405//DO NOT call this directly, go through Sysout406public void displayMessage( String messageIn )407{408messageText.append( messageIn + "\n" );409System.out.println(messageIn);410}411412}// TestDialog class413414415