Path: blob/master/test/jdk/java/awt/Frame/LayoutOnMaximizeTest/LayoutOnMaximizeTest.java
41154 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 635534027@summary Test correctness of laying out the contents of a frame on maximize28@author anthony.petrov@...: area=awt.toplevel29@library ../../regtesthelpers30@build Util31@run main LayoutOnMaximizeTest32*/333435/**36* LayoutOnMaximizeTest.java37*38* summary: tests the correctness of laying out the contents of a frame on maximize39*/4041import java.awt.*;42import java.awt.event.*;43import javax.swing.*;44import java.util.*;45import test.java.awt.regtesthelpers.Util;4647484950//*** global search and replace LayoutOnMaximizeTest with name of the test ***5152public class LayoutOnMaximizeTest53{5455//*** test-writer defined static variables go here ***565758private static void init() {59// We must be sure that the Size system command is exactly the 3rd one in the System menu.60System.out.println("NOTE: The test is known to work correctly with English MS Windows only.");616263String s = Toolkit.getDefaultToolkit().getClass().getName();6465// This is Windows-only test66if (!s.contains("WToolkit")) {67pass();68}6970// MAXIMIZED_BOTH is known to be supported on MS Windows.71if (!Toolkit.getDefaultToolkit().isFrameStateSupported(Frame.MAXIMIZED_BOTH)) {72fail("Toolkit doesn't support the Frame.MAXIMIZED_BOTH extended state.");73}7475final Frame frame = new Frame("Test Frame");7677// Add some components to check their position later78JPanel panel = new JPanel();79panel.setBackground(Color.RED);8081JTextField jf = new JTextField (10);82JTextField jf1 = new JTextField (10);83JButton jb = new JButton("Test");8485panel.add(jf);86panel.add(jf1);87panel.add(jb);88frame.add(panel);8990frame.setSize(400, 400);91frame.setVisible(true);9293Robot robot = Util.createRobot();94robot.setAutoDelay(20);9596// To be sure the window is shown and packed97Util.waitForIdle(robot);9899// The initial JTextField position. After maximization it's supposed to be changed.100Point loc1 = jf1.getLocation();101102System.out.println("The initial position of the JTextField is: " + loc1);103104// The point to move mouse pointer inside the frame105Point pt = frame.getLocation();106107// Alt-Space opens System menu108robot.keyPress(KeyEvent.VK_ALT);109robot.keyPress(KeyEvent.VK_SPACE);110robot.keyRelease(KeyEvent.VK_SPACE);111robot.keyRelease(KeyEvent.VK_ALT);112113114// Two "down arrow" presses move the menu selection to the Size menu item.115for (int i = 0; i < 2; i++) {116robot.keyPress(KeyEvent.VK_DOWN);117robot.keyRelease(KeyEvent.VK_DOWN);118}119120// And finally select the Size command121robot.keyPress(KeyEvent.VK_ENTER);122robot.keyRelease(KeyEvent.VK_ENTER);123124Util.waitForIdle(robot);125126// Now move the mouse pointer somewhere inside the frame.127robot.mouseMove(pt.x + 95, pt.y + 70);128129// And click once we are inside to imitate the canceling of the size operation.130robot.mousePress( InputEvent.BUTTON1_MASK );131robot.mouseRelease( InputEvent.BUTTON1_MASK );132Util.waitForIdle(robot);133134// Now we maximize the frame135frame.setExtendedState(Frame.MAXIMIZED_BOTH);136137138Util.waitForIdle(robot);139140// And check whether the location of the JTextField has changed.141Point loc2 = jf1.getLocation();142System.out.println("Position of the JTextField after maximization is: " + loc2);143144// Location of the component changed if relayouting has happened.145146if (loc2.equals(loc1)) {147fail("Location of a component has not been changed.");148return;149}150151LayoutOnMaximizeTest.pass();152153}//End init()154155156157/*****************************************************158* Standard Test Machinery Section159* DO NOT modify anything in this section -- it's a160* standard chunk of code which has all of the161* synchronisation necessary for the test harness.162* By keeping it the same in all tests, it is easier163* to read and understand someone else's test, as164* well as insuring that all tests behave correctly165* with the test harness.166* There is a section following this for test-167* classes168******************************************************/169private static boolean theTestPassed = false;170private static boolean testGeneratedInterrupt = false;171private static String failureMessage = "";172173private static Thread mainThread = null;174175private static int sleepTime = 300000;176177// Not sure about what happens if multiple of this test are178// instantiated in the same VM. Being static (and using179// static vars), it aint gonna work. Not worrying about180// it for now.181public static void main( String args[] ) throws InterruptedException182{183mainThread = Thread.currentThread();184try185{186init();187}188catch( TestPassedException e )189{190//The test passed, so just return from main and harness will191// interepret this return as a pass192return;193}194//At this point, neither test pass nor test fail has been195// called -- either would have thrown an exception and ended the196// test, so we know we have multiple threads.197198//Test involves other threads, so sleep and wait for them to199// called pass() or fail()200try201{202Thread.sleep( sleepTime );203//Timed out, so fail the test204throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );205}206catch (InterruptedException e)207{208//The test harness may have interrupted the test. If so, rethrow the exception209// so that the harness gets it and deals with it.210if( ! testGeneratedInterrupt ) throw e;211212//reset flag in case hit this code more than once for some reason (just safety)213testGeneratedInterrupt = false;214215if ( theTestPassed == false )216{217throw new RuntimeException( failureMessage );218}219}220221}//main222223public static synchronized void setTimeoutTo( int seconds )224{225sleepTime = seconds * 1000;226}227228public static synchronized void pass()229{230System.out.println( "The test passed." );231System.out.println( "The test is over, hit Ctl-C to stop Java VM" );232//first check if this is executing in main thread233if ( mainThread == Thread.currentThread() )234{235//Still in the main thread, so set the flag just for kicks,236// and throw a test passed exception which will be caught237// and end the test.238theTestPassed = true;239throw new TestPassedException();240}241theTestPassed = true;242testGeneratedInterrupt = true;243mainThread.interrupt();244}//pass()245246public static synchronized void fail()247{248//test writer didn't specify why test failed, so give generic249fail( "it just plain failed! :-)" );250}251252public static synchronized void fail( String whyFailed )253{254System.out.println( "The test failed: " + whyFailed );255System.out.println( "The test is over, hit Ctl-C to stop Java VM" );256//check if this called from main thread257if ( mainThread == Thread.currentThread() )258{259//If main thread, fail now 'cause not sleeping260throw new RuntimeException( whyFailed );261}262theTestPassed = false;263testGeneratedInterrupt = true;264failureMessage = whyFailed;265mainThread.interrupt();266}//fail()267268}// class LayoutOnMaximizeTest269270//This exception is used to exit from any level of call nesting271// when it's determined that the test has passed, and immediately272// end the test.273class TestPassedException extends RuntimeException274{275}276277278