Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Frame/LayoutOnMaximizeTest/LayoutOnMaximizeTest.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 635534026@summary Test correctness of laying out the contents of a frame on maximize27@author anthony.petrov@...: area=awt.toplevel28@library ../../regtesthelpers29@build Util30@run main LayoutOnMaximizeTest31*/323334/**35* LayoutOnMaximizeTest.java36*37* summary: tests the correctness of laying out the contents of a frame on maximize38*/3940import java.awt.*;41import java.awt.event.*;42import javax.swing.*;43import java.util.*;44import test.java.awt.regtesthelpers.Util;4546474849//*** global search and replace LayoutOnMaximizeTest with name of the test ***5051public class LayoutOnMaximizeTest52{5354//*** test-writer defined static variables go here ***555657private static void init() {5859//*** Create instructions for the user here ***6061String[] instructions =62{63"This is an AUTOMATIC test, simply wait until it is done.",64"The result (passed or failed) will be shown in the",65"message window below."66};67Sysout.createDialog( );68Sysout.printInstructions( instructions );6970// We must be sure that the Size system command is exactly the 3rd one in the System menu.71Sysout.println("NOTE: The test is known to work correctly with English MS Windows only.");727374String s = Toolkit.getDefaultToolkit().getClass().getName();7576// This is Windows-only test77if (!s.contains("WToolkit")) {78pass();79}8081// MAXIMIZED_BOTH is known to be supported on MS Windows.82if (!Toolkit.getDefaultToolkit().isFrameStateSupported(Frame.MAXIMIZED_BOTH)) {83fail("Toolkit doesn't support the Frame.MAXIMIZED_BOTH extended state.");84}8586final Frame frame = new Frame("Test Frame");8788// Add some components to check their position later89JPanel panel = new JPanel();90panel.setBackground(Color.RED);9192JTextField jf = new JTextField (10);93JTextField jf1 = new JTextField (10);94JButton jb = new JButton("Test");9596panel.add(jf);97panel.add(jf1);98panel.add(jb);99frame.add(panel);100101frame.setSize(400, 400);102frame.setVisible(true);103104Robot robot = Util.createRobot();105robot.setAutoDelay(20);106107// To be sure the window is shown and packed108Util.waitForIdle(robot);109110// The initial JTextField position. After maximization it's supposed to be changed.111Point loc1 = jf1.getLocation();112113System.out.println("The initial position of the JTextField is: " + loc1);114115// The point to move mouse pointer inside the frame116Point pt = frame.getLocation();117118// Alt-Space opens System menu119robot.keyPress(KeyEvent.VK_ALT);120robot.keyPress(KeyEvent.VK_SPACE);121robot.keyRelease(KeyEvent.VK_SPACE);122robot.keyRelease(KeyEvent.VK_ALT);123124125// Two "down arrow" presses move the menu selection to the Size menu item.126for (int i = 0; i < 2; i++) {127robot.keyPress(KeyEvent.VK_DOWN);128robot.keyRelease(KeyEvent.VK_DOWN);129}130131// And finally select the Size command132robot.keyPress(KeyEvent.VK_ENTER);133robot.keyRelease(KeyEvent.VK_ENTER);134135Util.waitForIdle(robot);136137// Now move the mouse pointer somewhere inside the frame.138robot.mouseMove(pt.x + 95, pt.y + 70);139140// And click once we are inside to imitate the canceling of the size operation.141robot.mousePress( InputEvent.BUTTON1_MASK );142robot.mouseRelease( InputEvent.BUTTON1_MASK );143Util.waitForIdle(robot);144145// Now we maximize the frame146frame.setExtendedState(Frame.MAXIMIZED_BOTH);147148149Util.waitForIdle(robot);150151// And check whether the location of the JTextField has changed.152Point loc2 = jf1.getLocation();153System.out.println("Position of the JTextField after maximization is: " + loc2);154155// Location of the component changed if relayouting has happened.156157if (loc2.equals(loc1)) {158fail("Location of a component has not been changed.");159return;160}161162LayoutOnMaximizeTest.pass();163164}//End init()165166167168/*****************************************************169* Standard Test Machinery Section170* DO NOT modify anything in this section -- it's a171* standard chunk of code which has all of the172* synchronisation necessary for the test harness.173* By keeping it the same in all tests, it is easier174* to read and understand someone else's test, as175* well as insuring that all tests behave correctly176* with the test harness.177* There is a section following this for test-178* classes179******************************************************/180private static boolean theTestPassed = false;181private static boolean testGeneratedInterrupt = false;182private static String failureMessage = "";183184private static Thread mainThread = null;185186private static int sleepTime = 300000;187188// Not sure about what happens if multiple of this test are189// instantiated in the same VM. Being static (and using190// static vars), it aint gonna work. Not worrying about191// it for now.192public static void main( String args[] ) throws InterruptedException193{194mainThread = Thread.currentThread();195try196{197init();198}199catch( TestPassedException e )200{201//The test passed, so just return from main and harness will202// interepret this return as a pass203return;204}205//At this point, neither test pass nor test fail has been206// called -- either would have thrown an exception and ended the207// test, so we know we have multiple threads.208209//Test involves other threads, so sleep and wait for them to210// called pass() or fail()211try212{213Thread.sleep( sleepTime );214//Timed out, so fail the test215throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );216}217catch (InterruptedException e)218{219//The test harness may have interrupted the test. If so, rethrow the exception220// so that the harness gets it and deals with it.221if( ! testGeneratedInterrupt ) throw e;222223//reset flag in case hit this code more than once for some reason (just safety)224testGeneratedInterrupt = false;225226if ( theTestPassed == false )227{228throw new RuntimeException( failureMessage );229}230}231232}//main233234public static synchronized void setTimeoutTo( int seconds )235{236sleepTime = seconds * 1000;237}238239public static synchronized void pass()240{241Sysout.println( "The test passed." );242Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );243//first check if this is executing in main thread244if ( mainThread == Thread.currentThread() )245{246//Still in the main thread, so set the flag just for kicks,247// and throw a test passed exception which will be caught248// and end the test.249theTestPassed = true;250throw new TestPassedException();251}252theTestPassed = true;253testGeneratedInterrupt = true;254mainThread.interrupt();255}//pass()256257public static synchronized void fail()258{259//test writer didn't specify why test failed, so give generic260fail( "it just plain failed! :-)" );261}262263public static synchronized void fail( String whyFailed )264{265Sysout.println( "The test failed: " + whyFailed );266Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );267//check if this called from main thread268if ( mainThread == Thread.currentThread() )269{270//If main thread, fail now 'cause not sleeping271throw new RuntimeException( whyFailed );272}273theTestPassed = false;274testGeneratedInterrupt = true;275failureMessage = whyFailed;276mainThread.interrupt();277}//fail()278279}// class LayoutOnMaximizeTest280281//This exception is used to exit from any level of call nesting282// when it's determined that the test has passed, and immediately283// end the test.284class TestPassedException extends RuntimeException285{286}287288//*********** End Standard Test Machinery Section **********289290291//************ Begin classes defined for the test ****************292293// if want to make listeners, here is the recommended place for them, then instantiate294// them in init()295296/* Example of a class which may be written as part of a test297class NewClass implements anInterface298{299static int newVar = 0;300301public void eventDispatched(AWTEvent e)302{303//Counting events to see if we get enough304eventCount++;305306if( eventCount == 20 )307{308//got enough events, so pass309310LayoutOnMaximizeTest.pass();311}312else if( tries == 20 )313{314//tried too many times without getting enough events so fail315316LayoutOnMaximizeTest.fail();317}318319}// eventDispatched()320321}// NewClass class322323*/324325326//************** End classes defined for the test *******************327328329330331/****************************************************332Standard Test Machinery333DO NOT modify anything below -- it's a standard334chunk of code whose purpose is to make user335interaction uniform, and thereby make it simpler336to read and understand someone else's test.337****************************************************/338339/**340This is part of the standard test machinery.341It creates a dialog (with the instructions), and is the interface342for sending text messages to the user.343To print the instructions, send an array of strings to Sysout.createDialog344WithInstructions method. Put one line of instructions per array entry.345To display a message for the tester to see, simply call Sysout.println346with the string to be displayed.347This mimics System.out.println but works within the test harness as well348as standalone.349*/350351class Sysout352{353private static TestDialog dialog;354355public static void createDialogWithInstructions( String[] instructions )356{357dialog = new TestDialog( new Frame(), "Instructions" );358dialog.printInstructions( instructions );359dialog.setVisible(true);360println( "Any messages for the tester will display here." );361}362363public static void createDialog( )364{365dialog = new TestDialog( new Frame(), "Instructions" );366String[] defInstr = { "Instructions will appear here. ", "" } ;367dialog.printInstructions( defInstr );368dialog.setVisible(true);369println( "Any messages for the tester will display here." );370}371372373public static void printInstructions( String[] instructions )374{375dialog.printInstructions( instructions );376}377378379public static void println( String messageIn )380{381dialog.displayMessage( messageIn );382System.out.println(messageIn);383}384385}// Sysout class386387/**388This is part of the standard test machinery. It provides a place for the389test instructions to be displayed, and a place for interactive messages390to the user to be displayed.391To have the test instructions displayed, see Sysout.392To have a message to the user be displayed, see Sysout.393Do not call anything in this dialog directly.394*/395class TestDialog extends Dialog396{397398TextArea instructionsText;399TextArea messageText;400int maxStringLength = 80;401402//DO NOT call this directly, go through Sysout403public TestDialog( Frame frame, String name )404{405super( frame, name );406int scrollBoth = TextArea.SCROLLBARS_BOTH;407instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );408add( "North", instructionsText );409410messageText = new TextArea( "", 5, maxStringLength, scrollBoth );411add("Center", messageText);412413pack();414415setVisible(true);416}// TestDialog()417418//DO NOT call this directly, go through Sysout419public void printInstructions( String[] instructions )420{421//Clear out any current instructions422instructionsText.setText( "" );423424//Go down array of instruction strings425426String printStr, remainingStr;427for( int i=0; i < instructions.length; i++ )428{429//chop up each into pieces maxSringLength long430remainingStr = instructions[ i ];431while( remainingStr.length() > 0 )432{433//if longer than max then chop off first max chars to print434if( remainingStr.length() >= maxStringLength )435{436//Try to chop on a word boundary437int posOfSpace = remainingStr.438lastIndexOf( ' ', maxStringLength - 1 );439440if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;441442printStr = remainingStr.substring( 0, posOfSpace + 1 );443remainingStr = remainingStr.substring( posOfSpace + 1 );444}445//else just print446else447{448printStr = remainingStr;449remainingStr = "";450}451452instructionsText.append( printStr + "\n" );453454}// while455456}// for457458}//printInstructions()459460//DO NOT call this directly, go through Sysout461public void displayMessage( String messageIn )462{463messageText.append( messageIn + "\n" );464System.out.println(messageIn);465}466467}// TestDialog class468469470