Path: blob/master/test/jdk/java/awt/Frame/DynamicLayout/DynamicLayout.java
41153 views
/*1* Copyright (c) 2007, 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 650047727@summary Tests whether DynamicLayout is really off28@author anthony.petrov@...: area=awt.toplevel29@library ../../regtesthelpers30@build Util31@run main DynamicLayout32*/333435/**36* DynamicLayout.java37*38* summary: tests whether DynamicLayout is really off39*/4041import java.awt.*;42import java.awt.event.*;43import javax.swing.*;44import java.util.*;45import test.java.awt.regtesthelpers.Util;464748public class DynamicLayout49{5051//*** test-writer defined static variables go here ***525354private static void init() {5556//*** Create instructions for the user here ***5758// Turn off the dynamic layouting59Toolkit.getDefaultToolkit().setDynamicLayout(false);60System.out.println("isDynamicLayoutActive(): " + Toolkit.getDefaultToolkit().isDynamicLayoutActive());616263final Frame frame = new Frame("Test Frame");6465// Add some components to check their position later66JPanel panel = new JPanel();67panel.setBackground(Color.RED);6869JTextField jf = new JTextField (10);70JTextField jf1 = new JTextField (10);71JButton jb = new JButton("Test");7273panel.add(jf);74panel.add(jf1);75panel.add(jb);76frame.add(panel);7778frame.setSize(400, 400);79frame.setVisible(true);8081Robot robot = Util.createRobot();82robot.setAutoDelay(20);8384// To be sure the window is shown and packed85Util.waitForIdle(robot);8687// The initial JTextField position. While resizing the position supposed to stay the same.88Point loc1 = jf1.getLocation();8990System.out.println("The initial position of the JTextField is: " + loc1);9192Insets insets = frame.getInsets();93if (insets.right == 0 || insets.bottom == 0) {94System.out.println("The test environment must have non-zero right & bottom insets! The current insets are: " + insets);95pass();96return;97}9899// Let's move the mouse pointer to the bottom-right coner of the frame (the "size-grip")100Rectangle bounds = frame.getBounds();101102robot.mouseMove(bounds.x + bounds.width - 1, bounds.y + bounds.height - 1);103104// ... and start resizing105robot.mousePress( InputEvent.BUTTON1_MASK );106robot.mouseMove(bounds.x + bounds.width + 20, bounds.y + bounds.height + 15);107Util.waitForIdle(robot);108109// And check whether the location of the JTextField has changed.110Point loc2 = jf1.getLocation();111System.out.println("Position of the JTextField while resizing is: " + loc2);112113robot.mouseRelease( InputEvent.BUTTON1_MASK );114115// Location of the component changed if relayouting has happened.116if (!loc2.equals(loc1)) {117fail("Location of a component has been changed.");118return;119}120121DynamicLayout.pass();122123}//End init()124125126127/*****************************************************128* Standard Test Machinery Section129* DO NOT modify anything in this section -- it's a130* standard chunk of code which has all of the131* synchronisation necessary for the test harness.132* By keeping it the same in all tests, it is easier133* to read and understand someone else's test, as134* well as insuring that all tests behave correctly135* with the test harness.136* There is a section following this for test-137* classes138******************************************************/139private static boolean theTestPassed = false;140private static boolean testGeneratedInterrupt = false;141private static String failureMessage = "";142143private static Thread mainThread = null;144145private static int sleepTime = 300000;146147// Not sure about what happens if multiple of this test are148// instantiated in the same VM. Being static (and using149// static vars), it aint gonna work. Not worrying about150// it for now.151public static void main( String args[] ) throws InterruptedException152{153mainThread = Thread.currentThread();154try155{156init();157}158catch( TestPassedException e )159{160//The test passed, so just return from main and harness will161// interepret this return as a pass162return;163}164//At this point, neither test pass nor test fail has been165// called -- either would have thrown an exception and ended the166// test, so we know we have multiple threads.167168//Test involves other threads, so sleep and wait for them to169// called pass() or fail()170try171{172Thread.sleep( sleepTime );173//Timed out, so fail the test174throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );175}176catch (InterruptedException e)177{178//The test harness may have interrupted the test. If so, rethrow the exception179// so that the harness gets it and deals with it.180if( ! testGeneratedInterrupt ) throw e;181182//reset flag in case hit this code more than once for some reason (just safety)183testGeneratedInterrupt = false;184185if ( theTestPassed == false )186{187throw new RuntimeException( failureMessage );188}189}190191}//main192193public static synchronized void setTimeoutTo( int seconds )194{195sleepTime = seconds * 1000;196}197198public static synchronized void pass()199{200System.out.println( "The test passed." );201System.out.println( "The test is over, hit Ctl-C to stop Java VM" );202//first check if this is executing in main thread203if ( mainThread == Thread.currentThread() )204{205//Still in the main thread, so set the flag just for kicks,206// and throw a test passed exception which will be caught207// and end the test.208theTestPassed = true;209throw new TestPassedException();210}211theTestPassed = true;212testGeneratedInterrupt = true;213mainThread.interrupt();214}//pass()215216public static synchronized void fail()217{218//test writer didn't specify why test failed, so give generic219fail( "it just plain failed! :-)" );220}221222public static synchronized void fail( String whyFailed )223{224System.out.println( "The test failed: " + whyFailed );225System.out.println( "The test is over, hit Ctl-C to stop Java VM" );226//check if this called from main thread227if ( mainThread == Thread.currentThread() )228{229//If main thread, fail now 'cause not sleeping230throw new RuntimeException( whyFailed );231}232theTestPassed = false;233testGeneratedInterrupt = true;234failureMessage = whyFailed;235mainThread.interrupt();236}//fail()237238}// class DynamicLayout239240//This exception is used to exit from any level of call nesting241// when it's determined that the test has passed, and immediately242// end the test.243class TestPassedException extends RuntimeException244{245}246247248