Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Container/isRemoveNotifyNeeded/JInternalFrameTest.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 655280326@summary moveToFront shouldn't remove peers of HW components27@author anthony.petrov@...: area=awt.container28@library ../../regtesthelpers29@build Util30@run main JInternalFrameTest31*/3233/**34* JInternalFrameTest.java35*36* summary: movtToFront invoked on the JInternalFrame shouldn't37* recreate peers of HW descendants of the JInternalFrame.38*/3940import java.awt.*;41import java.awt.event.*;42import java.beans.PropertyVetoException;43import javax.swing.*;44import test.java.awt.regtesthelpers.Util;4546public class JInternalFrameTest47{48// Indicates whether the removeNotify() was invoked on the HW Canvas49static volatile boolean isRemoveNotify = false;5051// The HW Canvas class.52private static final class MyCanvas extends Canvas {53private final Color background;54public MyCanvas(Color background) {55this.background = background;56setPreferredSize(new Dimension(100, 100));57}5859public void paint(Graphics g) {60g.setColor(background);61g.fillRect(0, 0, getWidth(), getHeight());62}6364public void addNotify() {65super.addNotify();66System.err.println("addNotify() on " + background);67}6869public void removeNotify() {70super.removeNotify();71isRemoveNotify = true;72System.err.println("removeNotify() on " + background);73Thread.dumpStack();74}75}767778private static void init()79{80//*** Create instructions for the user here ***8182String[] instructions =83{84"This is an AUTOMATIC test, simply wait until it is done.",85"The result (passed or failed) will be shown in the",86"message window below."87};88Sysout.createDialog( );89Sysout.printInstructions( instructions );9091// We create a JFrame with two JInternalFrame.92// Each JInternalFrame contains a HW Canvas component.93JFrame jframe = new JFrame("mixing test");94JDesktopPane desktop = new JDesktopPane();95jframe.setContentPane(desktop);96JInternalFrame iframe1 = new JInternalFrame("iframe 1");97iframe1.setIconifiable(true);98iframe1.add(new MyCanvas(Color.RED));99iframe1.setBounds(10, 10, 100, 100);100iframe1.setVisible(true);101desktop.add(iframe1);102JInternalFrame iframe2 = new JInternalFrame("iframe 2");103iframe2.setIconifiable(true);104iframe2.add(new MyCanvas(Color.BLUE));105iframe2.setBounds(50, 50, 100, 100);106iframe2.setVisible(true);107desktop.add(iframe2);108109jframe.setSize(300, 300);110jframe.setVisible(true);111112// Wait until everything gets shown113Util.waitForIdle(null);114115// Now cause a couple of z-order changing operations116iframe2.moveToFront();117Util.waitForIdle(null);118iframe1.moveToFront();119Util.waitForIdle(null);120iframe2.moveToFront();121122// Wait until all the operations complete123Util.waitForIdle(null);124125if (isRemoveNotify) {126fail("The removeNotify() was invoked on the HW Canvas");127}128JInternalFrameTest.pass();129130}//End init()131132133134/*****************************************************135* Standard Test Machinery Section136* DO NOT modify anything in this section -- it's a137* standard chunk of code which has all of the138* synchronisation necessary for the test harness.139* By keeping it the same in all tests, it is easier140* to read and understand someone else's test, as141* well as insuring that all tests behave correctly142* with the test harness.143* There is a section following this for test-144* classes145******************************************************/146private static boolean theTestPassed = false;147private static boolean testGeneratedInterrupt = false;148private static String failureMessage = "";149150private static Thread mainThread = null;151152private static int sleepTime = 300000;153154// Not sure about what happens if multiple of this test are155// instantiated in the same VM. Being static (and using156// static vars), it aint gonna work. Not worrying about157// it for now.158public static void main( String args[] ) throws InterruptedException159{160mainThread = Thread.currentThread();161try162{163init();164}165catch( TestPassedException e )166{167//The test passed, so just return from main and harness will168// interepret this return as a pass169return;170}171//At this point, neither test pass nor test fail has been172// called -- either would have thrown an exception and ended the173// test, so we know we have multiple threads.174175//Test involves other threads, so sleep and wait for them to176// called pass() or fail()177try178{179Thread.sleep( sleepTime );180//Timed out, so fail the test181throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );182}183catch (InterruptedException e)184{185//The test harness may have interrupted the test. If so, rethrow the exception186// so that the harness gets it and deals with it.187if( ! testGeneratedInterrupt ) throw e;188189//reset flag in case hit this code more than once for some reason (just safety)190testGeneratedInterrupt = false;191192if ( theTestPassed == false )193{194throw new RuntimeException( failureMessage );195}196}197198}//main199200public static synchronized void setTimeoutTo( int seconds )201{202sleepTime = seconds * 1000;203}204205public static synchronized void pass()206{207Sysout.println( "The test passed." );208Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );209//first check if this is executing in main thread210if ( mainThread == Thread.currentThread() )211{212//Still in the main thread, so set the flag just for kicks,213// and throw a test passed exception which will be caught214// and end the test.215theTestPassed = true;216throw new TestPassedException();217}218theTestPassed = true;219testGeneratedInterrupt = true;220mainThread.interrupt();221}//pass()222223public static synchronized void fail()224{225//test writer didn't specify why test failed, so give generic226fail( "it just plain failed! :-)" );227}228229public static synchronized void fail( String whyFailed )230{231Sysout.println( "The test failed: " + whyFailed );232Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );233//check if this called from main thread234if ( mainThread == Thread.currentThread() )235{236//If main thread, fail now 'cause not sleeping237throw new RuntimeException( whyFailed );238}239theTestPassed = false;240testGeneratedInterrupt = true;241failureMessage = whyFailed;242mainThread.interrupt();243}//fail()244245}// class JInternalFrameTest246247//This exception is used to exit from any level of call nesting248// when it's determined that the test has passed, and immediately249// end the test.250class TestPassedException extends RuntimeException251{252}253254//*********** End Standard Test Machinery Section **********255256257//************ Begin classes defined for the test ****************258259// if want to make listeners, here is the recommended place for them, then instantiate260// them in init()261262/* Example of a class which may be written as part of a test263class NewClass implements anInterface264{265static int newVar = 0;266267public void eventDispatched(AWTEvent e)268{269//Counting events to see if we get enough270eventCount++;271272if( eventCount == 20 )273{274//got enough events, so pass275276JInternalFrameTest.pass();277}278else if( tries == 20 )279{280//tried too many times without getting enough events so fail281282JInternalFrameTest.fail();283}284285}// eventDispatched()286287}// NewClass class288289*/290291292//************** End classes defined for the test *******************293294295296297/****************************************************298Standard Test Machinery299DO NOT modify anything below -- it's a standard300chunk of code whose purpose is to make user301interaction uniform, and thereby make it simpler302to read and understand someone else's test.303****************************************************/304305/**306This is part of the standard test machinery.307It creates a dialog (with the instructions), and is the interface308for sending text messages to the user.309To print the instructions, send an array of strings to Sysout.createDialog310WithInstructions method. Put one line of instructions per array entry.311To display a message for the tester to see, simply call Sysout.println312with the string to be displayed.313This mimics System.out.println but works within the test harness as well314as standalone.315*/316317class Sysout318{319private static TestDialog dialog;320321public static void createDialogWithInstructions( String[] instructions )322{323dialog = new TestDialog( new Frame(), "Instructions" );324dialog.printInstructions( instructions );325dialog.setVisible(true);326println( "Any messages for the tester will display here." );327}328329public static void createDialog( )330{331dialog = new TestDialog( new Frame(), "Instructions" );332String[] defInstr = { "Instructions will appear here. ", "" } ;333dialog.printInstructions( defInstr );334dialog.setVisible(true);335println( "Any messages for the tester will display here." );336}337338339public static void printInstructions( String[] instructions )340{341dialog.printInstructions( instructions );342}343344345public static void println( String messageIn )346{347dialog.displayMessage( messageIn );348System.out.println(messageIn);349}350351}// Sysout class352353/**354This is part of the standard test machinery. It provides a place for the355test instructions to be displayed, and a place for interactive messages356to the user to be displayed.357To have the test instructions displayed, see Sysout.358To have a message to the user be displayed, see Sysout.359Do not call anything in this dialog directly.360*/361class TestDialog extends Dialog362{363364TextArea instructionsText;365TextArea messageText;366int maxStringLength = 80;367368//DO NOT call this directly, go through Sysout369public TestDialog( Frame frame, String name )370{371super( frame, name );372int scrollBoth = TextArea.SCROLLBARS_BOTH;373instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );374add( "North", instructionsText );375376messageText = new TextArea( "", 5, maxStringLength, scrollBoth );377add("Center", messageText);378379pack();380381setVisible(true);382}// TestDialog()383384//DO NOT call this directly, go through Sysout385public void printInstructions( String[] instructions )386{387//Clear out any current instructions388instructionsText.setText( "" );389390//Go down array of instruction strings391392String printStr, remainingStr;393for( int i=0; i < instructions.length; i++ )394{395//chop up each into pieces maxSringLength long396remainingStr = instructions[ i ];397while( remainingStr.length() > 0 )398{399//if longer than max then chop off first max chars to print400if( remainingStr.length() >= maxStringLength )401{402//Try to chop on a word boundary403int posOfSpace = remainingStr.404lastIndexOf( ' ', maxStringLength - 1 );405406if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;407408printStr = remainingStr.substring( 0, posOfSpace + 1 );409remainingStr = remainingStr.substring( posOfSpace + 1 );410}411//else just print412else413{414printStr = remainingStr;415remainingStr = "";416}417418instructionsText.append( printStr + "\n" );419420}// while421422}// for423424}//printInstructions()425426//DO NOT call this directly, go through Sysout427public void displayMessage( String messageIn )428{429messageText.append( messageIn + "\n" );430System.out.println(messageIn);431}432433}// TestDialog class434435436