Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Mixing/setComponentZOrder.java
47867 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 658953026@summary Mixing code should correctly handle insertion of components with setComponentZOrder.27@author antohny.petrov@...: area=awt.mixing28@run main setComponentZOrder29*/3031/**32* setComponentZOrder.java33*34* summary: Mixing code should correctly handle insertion of components with setComponentZOrder.35*/3637import java.awt.*;38import java.awt.event.*;394041public class setComponentZOrder42{4344//*** test-writer defined static variables go here ***454647private 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 );5960try {61Container c = new Container();62Button b = new Button("b");63c.setComponentZOrder(b, 0);64} catch (ArrayIndexOutOfBoundsException e) {65e.printStackTrace();66fail("The setComponentZOrder method used to insert a component caused the mixing code to throw the exception: " + e);67}6869pass();7071}//End init()72737475/*****************************************************76* Standard Test Machinery Section77* DO NOT modify anything in this section -- it's a78* standard chunk of code which has all of the79* synchronisation necessary for the test harness.80* By keeping it the same in all tests, it is easier81* to read and understand someone else's test, as82* well as insuring that all tests behave correctly83* with the test harness.84* There is a section following this for test-85* classes86******************************************************/87private static boolean theTestPassed = false;88private static boolean testGeneratedInterrupt = false;89private static String failureMessage = "";9091private static Thread mainThread = null;9293private static int sleepTime = 300000;9495// Not sure about what happens if multiple of this test are96// instantiated in the same VM. Being static (and using97// static vars), it aint gonna work. Not worrying about98// it for now.99public static void main( String args[] ) throws InterruptedException100{101mainThread = Thread.currentThread();102try103{104init();105}106catch( TestPassedException e )107{108//The test passed, so just return from main and harness will109// interepret this return as a pass110return;111}112//At this point, neither test pass nor test fail has been113// called -- either would have thrown an exception and ended the114// test, so we know we have multiple threads.115116//Test involves other threads, so sleep and wait for them to117// called pass() or fail()118try119{120Thread.sleep( sleepTime );121//Timed out, so fail the test122throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );123}124catch (InterruptedException e)125{126//The test harness may have interrupted the test. If so, rethrow the exception127// so that the harness gets it and deals with it.128if( ! testGeneratedInterrupt ) throw e;129130//reset flag in case hit this code more than once for some reason (just safety)131testGeneratedInterrupt = false;132133if ( theTestPassed == false )134{135throw new RuntimeException( failureMessage );136}137}138139}//main140141public static synchronized void setTimeoutTo( int seconds )142{143sleepTime = seconds * 1000;144}145146public static synchronized void pass()147{148Sysout.println( "The test passed." );149Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );150//first check if this is executing in main thread151if ( mainThread == Thread.currentThread() )152{153//Still in the main thread, so set the flag just for kicks,154// and throw a test passed exception which will be caught155// and end the test.156theTestPassed = true;157throw new TestPassedException();158}159theTestPassed = true;160testGeneratedInterrupt = true;161mainThread.interrupt();162}//pass()163164public static synchronized void fail()165{166//test writer didn't specify why test failed, so give generic167fail( "it just plain failed! :-)" );168}169170public static synchronized void fail( String whyFailed )171{172Sysout.println( "The test failed: " + whyFailed );173Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );174//check if this called from main thread175if ( mainThread == Thread.currentThread() )176{177//If main thread, fail now 'cause not sleeping178throw new RuntimeException( whyFailed );179}180theTestPassed = false;181testGeneratedInterrupt = true;182failureMessage = whyFailed;183mainThread.interrupt();184}//fail()185186}// class setComponentZOrder187188//This exception is used to exit from any level of call nesting189// when it's determined that the test has passed, and immediately190// end the test.191class TestPassedException extends RuntimeException192{193}194195//*********** End Standard Test Machinery Section **********196197198//************ Begin classes defined for the test ****************199200// if want to make listeners, here is the recommended place for them, then instantiate201// them in init()202203/* Example of a class which may be written as part of a test204class NewClass implements anInterface205{206static int newVar = 0;207208public void eventDispatched(AWTEvent e)209{210//Counting events to see if we get enough211eventCount++;212213if( eventCount == 20 )214{215//got enough events, so pass216217setComponentZOrder.pass();218}219else if( tries == 20 )220{221//tried too many times without getting enough events so fail222223setComponentZOrder.fail();224}225226}// eventDispatched()227228}// NewClass class229230*/231232233//************** End classes defined for the test *******************234235236237238/****************************************************239Standard Test Machinery240DO NOT modify anything below -- it's a standard241chunk of code whose purpose is to make user242interaction uniform, and thereby make it simpler243to read and understand someone else's test.244****************************************************/245246/**247This is part of the standard test machinery.248It creates a dialog (with the instructions), and is the interface249for sending text messages to the user.250To print the instructions, send an array of strings to Sysout.createDialog251WithInstructions method. Put one line of instructions per array entry.252To display a message for the tester to see, simply call Sysout.println253with the string to be displayed.254This mimics System.out.println but works within the test harness as well255as standalone.256*/257258class Sysout259{260private static TestDialog dialog;261262public static void createDialogWithInstructions( String[] instructions )263{264dialog = new TestDialog( new Frame(), "Instructions" );265dialog.printInstructions( instructions );266dialog.setVisible(true);267println( "Any messages for the tester will display here." );268}269270public static void createDialog( )271{272dialog = new TestDialog( new Frame(), "Instructions" );273String[] defInstr = { "Instructions will appear here. ", "" } ;274dialog.printInstructions( defInstr );275dialog.setVisible(true);276println( "Any messages for the tester will display here." );277}278279280public static void printInstructions( String[] instructions )281{282dialog.printInstructions( instructions );283}284285286public static void println( String messageIn )287{288dialog.displayMessage( messageIn );289System.out.println(messageIn);290}291292}// Sysout class293294/**295This is part of the standard test machinery. It provides a place for the296test instructions to be displayed, and a place for interactive messages297to the user to be displayed.298To have the test instructions displayed, see Sysout.299To have a message to the user be displayed, see Sysout.300Do not call anything in this dialog directly.301*/302class TestDialog extends Dialog303{304305TextArea instructionsText;306TextArea messageText;307int maxStringLength = 80;308309//DO NOT call this directly, go through Sysout310public TestDialog( Frame frame, String name )311{312super( frame, name );313int scrollBoth = TextArea.SCROLLBARS_BOTH;314instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );315add( "North", instructionsText );316317messageText = new TextArea( "", 5, maxStringLength, scrollBoth );318add("Center", messageText);319320pack();321322setVisible(true);323}// TestDialog()324325//DO NOT call this directly, go through Sysout326public void printInstructions( String[] instructions )327{328//Clear out any current instructions329instructionsText.setText( "" );330331//Go down array of instruction strings332333String printStr, remainingStr;334for( int i=0; i < instructions.length; i++ )335{336//chop up each into pieces maxSringLength long337remainingStr = instructions[ i ];338while( remainingStr.length() > 0 )339{340//if longer than max then chop off first max chars to print341if( remainingStr.length() >= maxStringLength )342{343//Try to chop on a word boundary344int posOfSpace = remainingStr.345lastIndexOf( ' ', maxStringLength - 1 );346347if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;348349printStr = remainingStr.substring( 0, posOfSpace + 1 );350remainingStr = remainingStr.substring( posOfSpace + 1 );351}352//else just print353else354{355printStr = remainingStr;356remainingStr = "";357}358359instructionsText.append( printStr + "\n" );360361}// while362363}// for364365}//printInstructions()366367//DO NOT call this directly, go through Sysout368public void displayMessage( String messageIn )369{370messageText.append( messageIn + "\n" );371System.out.println(messageIn);372}373374}// TestDialog class375376377