Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Frame/DynamicLayout/DynamicLayout.java
38828 views
/*1* Copyright (c) 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 650047726@summary Tests whether DynamicLayout is really off27@author anthony.petrov@...: area=awt.toplevel28@library ../../regtesthelpers29@build Util30@run main DynamicLayout31*/323334/**35* DynamicLayout.java36*37* summary: tests whether DynamicLayout is really off38*/3940import java.awt.*;41import java.awt.event.*;42import javax.swing.*;43import java.util.*;44import test.java.awt.regtesthelpers.Util;454647public class DynamicLayout48{4950//*** test-writer defined static variables go here ***515253private static void init() {5455//*** Create instructions for the user here ***5657String[] instructions =58{59"This is an AUTOMATIC test, simply wait until it is done.",60"The result (passed or failed) will be shown in the",61"message window below."62};63Sysout.createDialog( );64Sysout.printInstructions( instructions );6566// Turn off the dynamic layouting67Toolkit.getDefaultToolkit().setDynamicLayout(false);68System.out.println("isDynamicLayoutActive(): " + Toolkit.getDefaultToolkit().isDynamicLayoutActive());697071final Frame frame = new Frame("Test Frame");7273// Add some components to check their position later74JPanel panel = new JPanel();75panel.setBackground(Color.RED);7677JTextField jf = new JTextField (10);78JTextField jf1 = new JTextField (10);79JButton jb = new JButton("Test");8081panel.add(jf);82panel.add(jf1);83panel.add(jb);84frame.add(panel);8586frame.setSize(400, 400);87frame.setVisible(true);8889Robot robot = Util.createRobot();90robot.setAutoDelay(20);9192// To be sure the window is shown and packed93Util.waitForIdle(robot);9495// The initial JTextField position. While resizing the position supposed to stay the same.96Point loc1 = jf1.getLocation();9798System.out.println("The initial position of the JTextField is: " + loc1);99100Insets insets = frame.getInsets();101if (insets.right == 0 || insets.bottom == 0) {102System.out.println("The test environment must have non-zero right & bottom insets! The current insets are: " + insets);103pass();104return;105}106107// Let's move the mouse pointer to the bottom-right coner of the frame (the "size-grip")108Rectangle bounds = frame.getBounds();109110robot.mouseMove(bounds.x + bounds.width - 1, bounds.y + bounds.height - 1);111112// ... and start resizing113robot.mousePress( InputEvent.BUTTON1_MASK );114robot.mouseMove(bounds.x + bounds.width + 20, bounds.y + bounds.height + 15);115Util.waitForIdle(robot);116117// And check whether the location of the JTextField has changed.118Point loc2 = jf1.getLocation();119System.out.println("Position of the JTextField while resizing is: " + loc2);120121robot.mouseRelease( InputEvent.BUTTON1_MASK );122123// Location of the component changed if relayouting has happened.124if (!loc2.equals(loc1)) {125fail("Location of a component has been changed.");126return;127}128129DynamicLayout.pass();130131}//End init()132133134135/*****************************************************136* Standard Test Machinery Section137* DO NOT modify anything in this section -- it's a138* standard chunk of code which has all of the139* synchronisation necessary for the test harness.140* By keeping it the same in all tests, it is easier141* to read and understand someone else's test, as142* well as insuring that all tests behave correctly143* with the test harness.144* There is a section following this for test-145* classes146******************************************************/147private static boolean theTestPassed = false;148private static boolean testGeneratedInterrupt = false;149private static String failureMessage = "";150151private static Thread mainThread = null;152153private static int sleepTime = 300000;154155// Not sure about what happens if multiple of this test are156// instantiated in the same VM. Being static (and using157// static vars), it aint gonna work. Not worrying about158// it for now.159public static void main( String args[] ) throws InterruptedException160{161mainThread = Thread.currentThread();162try163{164init();165}166catch( TestPassedException e )167{168//The test passed, so just return from main and harness will169// interepret this return as a pass170return;171}172//At this point, neither test pass nor test fail has been173// called -- either would have thrown an exception and ended the174// test, so we know we have multiple threads.175176//Test involves other threads, so sleep and wait for them to177// called pass() or fail()178try179{180Thread.sleep( sleepTime );181//Timed out, so fail the test182throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );183}184catch (InterruptedException e)185{186//The test harness may have interrupted the test. If so, rethrow the exception187// so that the harness gets it and deals with it.188if( ! testGeneratedInterrupt ) throw e;189190//reset flag in case hit this code more than once for some reason (just safety)191testGeneratedInterrupt = false;192193if ( theTestPassed == false )194{195throw new RuntimeException( failureMessage );196}197}198199}//main200201public static synchronized void setTimeoutTo( int seconds )202{203sleepTime = seconds * 1000;204}205206public static synchronized void pass()207{208Sysout.println( "The test passed." );209Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );210//first check if this is executing in main thread211if ( mainThread == Thread.currentThread() )212{213//Still in the main thread, so set the flag just for kicks,214// and throw a test passed exception which will be caught215// and end the test.216theTestPassed = true;217throw new TestPassedException();218}219theTestPassed = true;220testGeneratedInterrupt = true;221mainThread.interrupt();222}//pass()223224public static synchronized void fail()225{226//test writer didn't specify why test failed, so give generic227fail( "it just plain failed! :-)" );228}229230public static synchronized void fail( String whyFailed )231{232Sysout.println( "The test failed: " + whyFailed );233Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );234//check if this called from main thread235if ( mainThread == Thread.currentThread() )236{237//If main thread, fail now 'cause not sleeping238throw new RuntimeException( whyFailed );239}240theTestPassed = false;241testGeneratedInterrupt = true;242failureMessage = whyFailed;243mainThread.interrupt();244}//fail()245246}// class DynamicLayout247248//This exception is used to exit from any level of call nesting249// when it's determined that the test has passed, and immediately250// end the test.251class TestPassedException extends RuntimeException252{253}254255//*********** End Standard Test Machinery Section **********256257258//************ Begin classes defined for the test ****************259260// if want to make listeners, here is the recommended place for them, then instantiate261// them in init()262263/* Example of a class which may be written as part of a test264class NewClass implements anInterface265{266static int newVar = 0;267268public void eventDispatched(AWTEvent e)269{270//Counting events to see if we get enough271eventCount++;272273if( eventCount == 20 )274{275//got enough events, so pass276277DynamicLayout.pass();278}279else if( tries == 20 )280{281//tried too many times without getting enough events so fail282283DynamicLayout.fail();284}285286}// eventDispatched()287288}// NewClass class289290*/291292293//************** End classes defined for the test *******************294295296297298/****************************************************299Standard Test Machinery300DO NOT modify anything below -- it's a standard301chunk of code whose purpose is to make user302interaction uniform, and thereby make it simpler303to read and understand someone else's test.304****************************************************/305306/**307This is part of the standard test machinery.308It creates a dialog (with the instructions), and is the interface309for sending text messages to the user.310To print the instructions, send an array of strings to Sysout.createDialog311WithInstructions method. Put one line of instructions per array entry.312To display a message for the tester to see, simply call Sysout.println313with the string to be displayed.314This mimics System.out.println but works within the test harness as well315as standalone.316*/317318class Sysout319{320private static TestDialog dialog;321322public static void createDialogWithInstructions( String[] instructions )323{324dialog = new TestDialog( new Frame(), "Instructions" );325dialog.printInstructions( instructions );326dialog.setVisible(true);327println( "Any messages for the tester will display here." );328}329330public static void createDialog( )331{332dialog = new TestDialog( new Frame(), "Instructions" );333String[] defInstr = { "Instructions will appear here. ", "" } ;334dialog.printInstructions( defInstr );335dialog.setVisible(true);336println( "Any messages for the tester will display here." );337}338339340public static void printInstructions( String[] instructions )341{342dialog.printInstructions( instructions );343}344345346public static void println( String messageIn )347{348dialog.displayMessage( messageIn );349System.out.println(messageIn);350}351352}// Sysout class353354/**355This is part of the standard test machinery. It provides a place for the356test instructions to be displayed, and a place for interactive messages357to the user to be displayed.358To have the test instructions displayed, see Sysout.359To have a message to the user be displayed, see Sysout.360Do not call anything in this dialog directly.361*/362class TestDialog extends Dialog363{364365TextArea instructionsText;366TextArea messageText;367int maxStringLength = 80;368369//DO NOT call this directly, go through Sysout370public TestDialog( Frame frame, String name )371{372super( frame, name );373int scrollBoth = TextArea.SCROLLBARS_BOTH;374instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );375add( "North", instructionsText );376377messageText = new TextArea( "", 5, maxStringLength, scrollBoth );378add("Center", messageText);379380pack();381382setVisible(true);383}// TestDialog()384385//DO NOT call this directly, go through Sysout386public void printInstructions( String[] instructions )387{388//Clear out any current instructions389instructionsText.setText( "" );390391//Go down array of instruction strings392393String printStr, remainingStr;394for( int i=0; i < instructions.length; i++ )395{396//chop up each into pieces maxSringLength long397remainingStr = instructions[ i ];398while( remainingStr.length() > 0 )399{400//if longer than max then chop off first max chars to print401if( remainingStr.length() >= maxStringLength )402{403//Try to chop on a word boundary404int posOfSpace = remainingStr.405lastIndexOf( ' ', maxStringLength - 1 );406407if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;408409printStr = remainingStr.substring( 0, posOfSpace + 1 );410remainingStr = remainingStr.substring( posOfSpace + 1 );411}412//else just print413else414{415printStr = remainingStr;416remainingStr = "";417}418419instructionsText.append( printStr + "\n" );420421}// while422423}// for424425}//printInstructions()426427//DO NOT call this directly, go through Sysout428public void displayMessage( String messageIn )429{430messageText.append( messageIn + "\n" );431System.out.println(messageIn);432}433434}// TestDialog class435436437