Path: blob/master/test/jdk/java/awt/Container/isRemoveNotifyNeeded/JInternalFrameTest.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 655280327@summary moveToFront shouldn't remove peers of HW components28@author anthony.petrov@...: area=awt.container29@library ../../regtesthelpers30@build Util31@run main JInternalFrameTest32*/3334/**35* JInternalFrameTest.java36*37* summary: movtToFront invoked on the JInternalFrame shouldn't38* recreate peers of HW descendants of the JInternalFrame.39*/4041import java.awt.*;42import java.awt.event.*;43import java.beans.PropertyVetoException;44import javax.swing.*;45import test.java.awt.regtesthelpers.Util;4647public class JInternalFrameTest48{49// Indicates whether the removeNotify() was invoked on the HW Canvas50static volatile boolean isRemoveNotify = false;5152// The HW Canvas class.53private static final class MyCanvas extends Canvas {54private final Color background;55public MyCanvas(Color background) {56this.background = background;57setPreferredSize(new Dimension(100, 100));58}5960public void paint(Graphics g) {61g.setColor(background);62g.fillRect(0, 0, getWidth(), getHeight());63}6465public void addNotify() {66super.addNotify();67System.err.println("addNotify() on " + background);68}6970public void removeNotify() {71super.removeNotify();72isRemoveNotify = true;73System.err.println("removeNotify() on " + background);74Thread.dumpStack();75}76}777879private static void init()80{81// We create a JFrame with two JInternalFrame.82// Each JInternalFrame contains a HW Canvas component.83JFrame jframe = new JFrame("mixing test");84JDesktopPane desktop = new JDesktopPane();85jframe.setContentPane(desktop);86JInternalFrame iframe1 = new JInternalFrame("iframe 1");87iframe1.setIconifiable(true);88iframe1.add(new MyCanvas(Color.RED));89iframe1.setBounds(10, 10, 100, 100);90iframe1.setVisible(true);91desktop.add(iframe1);92JInternalFrame iframe2 = new JInternalFrame("iframe 2");93iframe2.setIconifiable(true);94iframe2.add(new MyCanvas(Color.BLUE));95iframe2.setBounds(50, 50, 100, 100);96iframe2.setVisible(true);97desktop.add(iframe2);9899jframe.setSize(300, 300);100jframe.setVisible(true);101102// Wait until everything gets shown103Util.waitForIdle(null);104105// Now cause a couple of z-order changing operations106iframe2.moveToFront();107Util.waitForIdle(null);108iframe1.moveToFront();109Util.waitForIdle(null);110iframe2.moveToFront();111112// Wait until all the operations complete113Util.waitForIdle(null);114115if (isRemoveNotify) {116fail("The removeNotify() was invoked on the HW Canvas");117}118JInternalFrameTest.pass();119120}//End init()121122123124/*****************************************************125* Standard Test Machinery Section126* DO NOT modify anything in this section -- it's a127* standard chunk of code which has all of the128* synchronisation necessary for the test harness.129* By keeping it the same in all tests, it is easier130* to read and understand someone else's test, as131* well as insuring that all tests behave correctly132* with the test harness.133* There is a section following this for test-134* classes135******************************************************/136private static boolean theTestPassed = false;137private static boolean testGeneratedInterrupt = false;138private static String failureMessage = "";139140private static Thread mainThread = null;141142private static int sleepTime = 300000;143144// Not sure about what happens if multiple of this test are145// instantiated in the same VM. Being static (and using146// static vars), it aint gonna work. Not worrying about147// it for now.148public static void main( String args[] ) throws InterruptedException149{150mainThread = Thread.currentThread();151try152{153init();154}155catch( TestPassedException e )156{157//The test passed, so just return from main and harness will158// interepret this return as a pass159return;160}161//At this point, neither test pass nor test fail has been162// called -- either would have thrown an exception and ended the163// test, so we know we have multiple threads.164165//Test involves other threads, so sleep and wait for them to166// called pass() or fail()167try168{169Thread.sleep( sleepTime );170//Timed out, so fail the test171throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );172}173catch (InterruptedException e)174{175//The test harness may have interrupted the test. If so, rethrow the exception176// so that the harness gets it and deals with it.177if( ! testGeneratedInterrupt ) throw e;178179//reset flag in case hit this code more than once for some reason (just safety)180testGeneratedInterrupt = false;181182if ( theTestPassed == false )183{184throw new RuntimeException( failureMessage );185}186}187188}//main189190public static synchronized void setTimeoutTo( int seconds )191{192sleepTime = seconds * 1000;193}194195public static synchronized void pass()196{197System.out.println( "The test passed." );198System.out.println( "The test is over, hit Ctl-C to stop Java VM" );199//first check if this is executing in main thread200if ( mainThread == Thread.currentThread() )201{202//Still in the main thread, so set the flag just for kicks,203// and throw a test passed exception which will be caught204// and end the test.205theTestPassed = true;206throw new TestPassedException();207}208theTestPassed = true;209testGeneratedInterrupt = true;210mainThread.interrupt();211}//pass()212213public static synchronized void fail()214{215//test writer didn't specify why test failed, so give generic216fail( "it just plain failed! :-)" );217}218219public static synchronized void fail( String whyFailed )220{221System.out.println( "The test failed: " + whyFailed );222System.out.println( "The test is over, hit Ctl-C to stop Java VM" );223//check if this called from main thread224if ( mainThread == Thread.currentThread() )225{226//If main thread, fail now 'cause not sleeping227throw new RuntimeException( whyFailed );228}229theTestPassed = false;230testGeneratedInterrupt = true;231failureMessage = whyFailed;232mainThread.interrupt();233}//fail()234235}// class JInternalFrameTest236237//This exception is used to exit from any level of call nesting238// when it's determined that the test has passed, and immediately239// end the test.240class TestPassedException extends RuntimeException241{242}243244245