Path: blob/master/test/jdk/java/awt/Component/CompEventOnHiddenComponent/CompEventOnHiddenComponent.java
41155 views
/*1* Copyright (c) 2006, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26@test27@key headful28@bug 6383903 814416629@summary REGRESSION: componentMoved is now getting called for some hidden components30@author andrei.dmitriev: area=awt.component31@run main CompEventOnHiddenComponent32*/3334import java.awt.*;35import java.awt.event.*;36import javax.swing.*;3738public class CompEventOnHiddenComponent39{40transient static boolean moved = false;41transient static boolean resized = false;4243transient static boolean ancestor_moved = false;44transient static boolean ancestor_resized = false;45static String passed = "";4647private static void init()48{49Robot robot;50try {51robot = new Robot();52}catch(Exception ex) {53ex.printStackTrace();54throw new RuntimeException("Unexpected failure");55}5657EventQueue.invokeLater(new Runnable(){58public void run(){59JFrame f = new JFrame("JFrame");60JButton b = new JButton("JButton");61f.add(b);62new JOptionPane().63createInternalFrame(b, "Test").64addComponentListener(new ComponentAdapter() {65public void componentMoved(ComponentEvent e) {66moved = true;67System.out.println(e);68}69public void componentResized(ComponentEvent e) {70resized = true;71System.out.println(e);72}73});74}75});7677robot.waitForIdle();7879if (moved || resized){80passed = "Hidden component got COMPONENT_MOVED or COMPONENT_RESIZED event";81} else {82System.out.println("Stage 1 passed.");83}8485EventQueue.invokeLater(new Runnable() {86public void run() {87JFrame parentWindow = new JFrame("JFrame 1");88JButton component = new JButton("JButton 1");;89JButton smallButton = new JButton("Small Button");909192smallButton.addHierarchyBoundsListener(new HierarchyBoundsAdapter() {93public void ancestorMoved(HierarchyEvent e) {94ancestor_moved = true;95System.out.println("SMALL COMPONENT >>>>>"+e);96}97public void ancestorResized(HierarchyEvent e) {98ancestor_resized = true;99System.out.println("SMALL COMPONENT >>>>>"+e);100}101});102103104parentWindow.add(component);105component.add(smallButton);106107component.setSize(100, 100);108component.setLocation(100, 100);109110}111});112113robot.waitForIdle();114115if (!ancestor_resized || !ancestor_moved){116passed = "Hidden component didn't get ANCESTOR event";117} else {118System.out.println("Stage 2 passed.");119}120121robot.waitForIdle();122123if (passed.equals("")){124CompEventOnHiddenComponent.pass();125} else {126CompEventOnHiddenComponent.fail(passed);127}128129}//End init()130131132133/*****************************************************134* Standard Test Machinery Section135* DO NOT modify anything in this section -- it's a136* standard chunk of code which has all of the137* synchronisation necessary for the test harness.138* By keeping it the same in all tests, it is easier139* to read and understand someone else's test, as140* well as insuring that all tests behave correctly141* with the test harness.142* There is a section following this for test-143* classes144******************************************************/145private static boolean theTestPassed = false;146private static boolean testGeneratedInterrupt = false;147private static String failureMessage = "";148149private static Thread mainThread = null;150151private static int sleepTime = 300000;152153// Not sure about what happens if multiple of this test are154// instantiated in the same VM. Being static (and using155// static vars), it aint gonna work. Not worrying about156// it for now.157public static void main( String args[] ) throws InterruptedException158{159mainThread = Thread.currentThread();160try161{162init();163}164catch( TestPassedException e )165{166//The test passed, so just return from main and harness will167// interepret this return as a pass168return;169}170//At this point, neither test pass nor test fail has been171// called -- either would have thrown an exception and ended the172// test, so we know we have multiple threads.173174//Test involves other threads, so sleep and wait for them to175// called pass() or fail()176try177{178Thread.sleep( sleepTime );179//Timed out, so fail the test180throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );181}182catch (InterruptedException e)183{184//The test harness may have interrupted the test. If so, rethrow the exception185// so that the harness gets it and deals with it.186if( ! testGeneratedInterrupt ) throw e;187188//reset flag in case hit this code more than once for some reason (just safety)189testGeneratedInterrupt = false;190191if ( theTestPassed == false )192{193throw new RuntimeException( failureMessage );194}195}196197}//main198199public static synchronized void setTimeoutTo( int seconds )200{201sleepTime = seconds * 1000;202}203204public static synchronized void pass()205{206System.out.println( "The test passed." );207System.out.println( "The test is over, hit Ctl-C to stop Java VM" );208//first check if this is executing in main thread209if ( mainThread == Thread.currentThread() )210{211//Still in the main thread, so set the flag just for kicks,212// and throw a test passed exception which will be caught213// and end the test.214theTestPassed = true;215throw new TestPassedException();216}217theTestPassed = true;218testGeneratedInterrupt = true;219mainThread.interrupt();220}//pass()221222public static synchronized void fail()223{224//test writer didn't specify why test failed, so give generic225fail( "it just plain failed! :-)" );226}227228public static synchronized void fail( String whyFailed )229{230System.out.println( "The test failed: " + whyFailed );231System.out.println( "The test is over, hit Ctl-C to stop Java VM" );232//check if this called from main thread233if ( mainThread == Thread.currentThread() )234{235//If main thread, fail now 'cause not sleeping236throw new RuntimeException( whyFailed );237}238theTestPassed = false;239testGeneratedInterrupt = true;240failureMessage = whyFailed;241mainThread.interrupt();242}//fail()243244}// class CompEventOnHiddenComponent245246//This exception is used to exit from any level of call nesting247// when it's determined that the test has passed, and immediately248// end the test.249class TestPassedException extends RuntimeException250{251}252253//*********** End Standard Test Machinery Section **********254255256