Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Mixing/Validating.java
47867 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 668204626@summary Mixing code does not always recalculate shapes correctly when resizing components27@author anthony.petrov@...: area=awt.mixing28@library ../regtesthelpers29@build Util30@run main Validating31*/3233/**34* Validating.java35*36* summary: Mixing code does not always recalculate shapes correctly when resizing components37*/3839import java.awt.*;40import java.awt.event.*;41import test.java.awt.regtesthelpers.Util;4243public class Validating44{45static volatile boolean clickPassed = false;4647private static void init()48{49//*** Create instructions for the user here ***5051String[] instructions =52{53"This is an AUTOMATIC test, simply wait until it is done.",54"The result (passed or failed) will be shown in the",55"message window below."56};57Sysout.createDialog( );58Sysout.printInstructions( instructions );5960if (!Toolkit.getDefaultToolkit().isFrameStateSupported(Frame.MAXIMIZED_BOTH)) {61System.out.println("The test environment does not support maximization. The test cannot be performed.");62pass();63return;64}6566// Create the frame with a button.67Frame f = new Frame();68Button b = new Button("ok");69b.addActionListener(new java.awt.event.ActionListener() {70public void actionPerformed(java.awt.event.ActionEvent e) {71clickPassed = true;72}73});74f.add(b);75// Make the frame maximized76f.setExtendedState(Frame.MAXIMIZED_BOTH);77f.pack();78f.setVisible(true);7980Robot robot = Util.createRobot();81robot.setAutoDelay(20);8283Util.waitForIdle(robot);8485// Now let's attempt to click in the middle of the button86// (i.e. in the middle of the window).87// If the button doesn't receive the click, it means that the test88// failed: the shape of the button was not enlarged.89Point heavyLoc = b.getLocationOnScreen();90robot.mouseMove(heavyLoc.x + b.getWidth() / 2, heavyLoc.y + b.getHeight() / 2);9192robot.mousePress(InputEvent.BUTTON1_MASK);93robot.mouseRelease(InputEvent.BUTTON1_MASK);94Util.waitForIdle(robot);9596if (clickPassed) {97pass();98} else {99fail("The button cannot be clicked.");100}101}//End init()102103104105/*****************************************************106* Standard Test Machinery Section107* DO NOT modify anything in this section -- it's a108* standard chunk of code which has all of the109* synchronisation necessary for the test harness.110* By keeping it the same in all tests, it is easier111* to read and understand someone else's test, as112* well as insuring that all tests behave correctly113* with the test harness.114* There is a section following this for test-115* classes116******************************************************/117private static boolean theTestPassed = false;118private static boolean testGeneratedInterrupt = false;119private static String failureMessage = "";120121private static Thread mainThread = null;122123private static int sleepTime = 300000;124125// Not sure about what happens if multiple of this test are126// instantiated in the same VM. Being static (and using127// static vars), it aint gonna work. Not worrying about128// it for now.129public static void main( String args[] ) throws InterruptedException130{131mainThread = Thread.currentThread();132try133{134init();135}136catch( TestPassedException e )137{138//The test passed, so just return from main and harness will139// interepret this return as a pass140return;141}142//At this point, neither test pass nor test fail has been143// called -- either would have thrown an exception and ended the144// test, so we know we have multiple threads.145146//Test involves other threads, so sleep and wait for them to147// called pass() or fail()148try149{150Thread.sleep( sleepTime );151//Timed out, so fail the test152throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );153}154catch (InterruptedException e)155{156//The test harness may have interrupted the test. If so, rethrow the exception157// so that the harness gets it and deals with it.158if( ! testGeneratedInterrupt ) throw e;159160//reset flag in case hit this code more than once for some reason (just safety)161testGeneratedInterrupt = false;162163if ( theTestPassed == false )164{165throw new RuntimeException( failureMessage );166}167}168169}//main170171public static synchronized void setTimeoutTo( int seconds )172{173sleepTime = seconds * 1000;174}175176public static synchronized void pass()177{178Sysout.println( "The test passed." );179Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );180//first check if this is executing in main thread181if ( mainThread == Thread.currentThread() )182{183//Still in the main thread, so set the flag just for kicks,184// and throw a test passed exception which will be caught185// and end the test.186theTestPassed = true;187throw new TestPassedException();188}189theTestPassed = true;190testGeneratedInterrupt = true;191mainThread.interrupt();192}//pass()193194public static synchronized void fail()195{196//test writer didn't specify why test failed, so give generic197fail( "it just plain failed! :-)" );198}199200public static synchronized void fail( String whyFailed )201{202Sysout.println( "The test failed: " + whyFailed );203Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );204//check if this called from main thread205if ( mainThread == Thread.currentThread() )206{207//If main thread, fail now 'cause not sleeping208throw new RuntimeException( whyFailed );209}210theTestPassed = false;211testGeneratedInterrupt = true;212failureMessage = whyFailed;213mainThread.interrupt();214}//fail()215216}// class Validating217218//This exception is used to exit from any level of call nesting219// when it's determined that the test has passed, and immediately220// end the test.221class TestPassedException extends RuntimeException222{223}224225//*********** End Standard Test Machinery Section **********226227228//************ Begin classes defined for the test ****************229230// if want to make listeners, here is the recommended place for them, then instantiate231// them in init()232233/* Example of a class which may be written as part of a test234class NewClass implements anInterface235{236static int newVar = 0;237238public void eventDispatched(AWTEvent e)239{240//Counting events to see if we get enough241eventCount++;242243if( eventCount == 20 )244{245//got enough events, so pass246247Validating.pass();248}249else if( tries == 20 )250{251//tried too many times without getting enough events so fail252253Validating.fail();254}255256}// eventDispatched()257258}// NewClass class259260*/261262263//************** End classes defined for the test *******************264265266267268/****************************************************269Standard Test Machinery270DO NOT modify anything below -- it's a standard271chunk of code whose purpose is to make user272interaction uniform, and thereby make it simpler273to read and understand someone else's test.274****************************************************/275276/**277This is part of the standard test machinery.278It creates a dialog (with the instructions), and is the interface279for sending text messages to the user.280To print the instructions, send an array of strings to Sysout.createDialog281WithInstructions method. Put one line of instructions per array entry.282To display a message for the tester to see, simply call Sysout.println283with the string to be displayed.284This mimics System.out.println but works within the test harness as well285as standalone.286*/287288class Sysout289{290private static TestDialog dialog;291292public static void createDialogWithInstructions( String[] instructions )293{294dialog = new TestDialog( new Frame(), "Instructions" );295dialog.printInstructions( instructions );296dialog.setVisible(true);297println( "Any messages for the tester will display here." );298}299300public static void createDialog( )301{302dialog = new TestDialog( new Frame(), "Instructions" );303String[] defInstr = { "Instructions will appear here. ", "" } ;304dialog.printInstructions( defInstr );305dialog.setVisible(true);306println( "Any messages for the tester will display here." );307}308309310public static void printInstructions( String[] instructions )311{312dialog.printInstructions( instructions );313}314315316public static void println( String messageIn )317{318dialog.displayMessage( messageIn );319System.out.println(messageIn);320}321322}// Sysout class323324/**325This is part of the standard test machinery. It provides a place for the326test instructions to be displayed, and a place for interactive messages327to the user to be displayed.328To have the test instructions displayed, see Sysout.329To have a message to the user be displayed, see Sysout.330Do not call anything in this dialog directly.331*/332class TestDialog extends Dialog333{334335TextArea instructionsText;336TextArea messageText;337int maxStringLength = 80;338339//DO NOT call this directly, go through Sysout340public TestDialog( Frame frame, String name )341{342super( frame, name );343int scrollBoth = TextArea.SCROLLBARS_BOTH;344instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );345add( "North", instructionsText );346347messageText = new TextArea( "", 5, maxStringLength, scrollBoth );348add("Center", messageText);349350pack();351352setVisible(true);353}// TestDialog()354355//DO NOT call this directly, go through Sysout356public void printInstructions( String[] instructions )357{358//Clear out any current instructions359instructionsText.setText( "" );360361//Go down array of instruction strings362363String printStr, remainingStr;364for( int i=0; i < instructions.length; i++ )365{366//chop up each into pieces maxSringLength long367remainingStr = instructions[ i ];368while( remainingStr.length() > 0 )369{370//if longer than max then chop off first max chars to print371if( remainingStr.length() >= maxStringLength )372{373//Try to chop on a word boundary374int posOfSpace = remainingStr.375lastIndexOf( ' ', maxStringLength - 1 );376377if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;378379printStr = remainingStr.substring( 0, posOfSpace + 1 );380remainingStr = remainingStr.substring( posOfSpace + 1 );381}382//else just print383else384{385printStr = remainingStr;386remainingStr = "";387}388389instructionsText.append( printStr + "\n" );390391}// while392393}// for394395}//printInstructions()396397//DO NOT call this directly, go through Sysout398public void displayMessage( String messageIn )399{400messageText.append( messageIn + "\n" );401System.out.println(messageIn);402}403404}// TestDialog class405406407