Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Component/CompEventOnHiddenComponent/CompEventOnHiddenComponent.java
47791 views
/*1* Copyright (c) 2006, 2014, 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@bug 638390328@summary REGRESSION: componentMoved is now getting called for some hidden components29@author andrei.dmitriev: area=awt.component30@run main CompEventOnHiddenComponent31*/3233import java.awt.*;34import java.awt.event.*;35import javax.swing.*;3637public class CompEventOnHiddenComponent38{39transient static boolean moved = false;40transient static boolean resized = false;4142transient static boolean ancestor_moved = false;43transient static boolean ancestor_resized = false;44static String passed = "";4546private static void init()47{48String[] instructions =49{50"This is an AUTOMATIC test, simply wait until it is done.",51"The result (passed or failed) will be shown in the",52"message window below."53};54Sysout.createDialog( );55Sysout.printInstructions( instructions );5657Robot robot;58try {59robot = new Robot();60}catch(Exception ex) {61ex.printStackTrace();62throw new RuntimeException("Unexpected failure");63}6465EventQueue.invokeLater(new Runnable(){66public void run(){67JFrame f = new JFrame("JFrame");68JButton b = new JButton("JButton");69f.add(b);70new JOptionPane().71createInternalFrame(b, "Test").72addComponentListener(new ComponentAdapter() {73public void componentMoved(ComponentEvent e) {74moved = true;75System.out.println(e);76}77public void componentResized(ComponentEvent e) {78resized = true;79System.out.println(e);80}81});82}83});8485robot.waitForIdle();8687if (moved || resized){88passed = "Hidden component got COMPONENT_MOVED or COMPONENT_RESIZED event";89} else {90System.out.println("Stage 1 passed.");91}9293EventQueue.invokeLater(new Runnable() {94public void run() {95JFrame parentWindow = new JFrame("JFrame 1");96JButton component = new JButton("JButton 1");;97JButton smallButton = new JButton("Small Button");9899100smallButton.addHierarchyBoundsListener(new HierarchyBoundsAdapter() {101public void ancestorMoved(HierarchyEvent e) {102ancestor_moved = true;103System.out.println("SMALL COMPONENT >>>>>"+e);104}105public void ancestorResized(HierarchyEvent e) {106ancestor_resized = true;107System.out.println("SMALL COMPONENT >>>>>"+e);108}109});110111112parentWindow.add(component);113component.add(smallButton);114115component.setSize(100, 100);116component.setLocation(100, 100);117118}119});120121robot.waitForIdle();122123if (!ancestor_resized || !ancestor_moved){124passed = "Hidden component didn't get ANCESTOR event";125} else {126System.out.println("Stage 2 passed.");127}128129robot.waitForIdle();130131if (passed.equals("")){132CompEventOnHiddenComponent.pass();133} else {134CompEventOnHiddenComponent.fail(passed);135}136137}//End init()138139140141/*****************************************************142* Standard Test Machinery Section143* DO NOT modify anything in this section -- it's a144* standard chunk of code which has all of the145* synchronisation necessary for the test harness.146* By keeping it the same in all tests, it is easier147* to read and understand someone else's test, as148* well as insuring that all tests behave correctly149* with the test harness.150* There is a section following this for test-151* classes152******************************************************/153private static boolean theTestPassed = false;154private static boolean testGeneratedInterrupt = false;155private static String failureMessage = "";156157private static Thread mainThread = null;158159private static int sleepTime = 300000;160161// Not sure about what happens if multiple of this test are162// instantiated in the same VM. Being static (and using163// static vars), it aint gonna work. Not worrying about164// it for now.165public static void main( String args[] ) throws InterruptedException166{167mainThread = Thread.currentThread();168try169{170init();171}172catch( TestPassedException e )173{174//The test passed, so just return from main and harness will175// interepret this return as a pass176return;177}178//At this point, neither test pass nor test fail has been179// called -- either would have thrown an exception and ended the180// test, so we know we have multiple threads.181182//Test involves other threads, so sleep and wait for them to183// called pass() or fail()184try185{186Thread.sleep( sleepTime );187//Timed out, so fail the test188throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );189}190catch (InterruptedException e)191{192//The test harness may have interrupted the test. If so, rethrow the exception193// so that the harness gets it and deals with it.194if( ! testGeneratedInterrupt ) throw e;195196//reset flag in case hit this code more than once for some reason (just safety)197testGeneratedInterrupt = false;198199if ( theTestPassed == false )200{201throw new RuntimeException( failureMessage );202}203}204205}//main206207public static synchronized void setTimeoutTo( int seconds )208{209sleepTime = seconds * 1000;210}211212public static synchronized void pass()213{214Sysout.println( "The test passed." );215Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );216//first check if this is executing in main thread217if ( mainThread == Thread.currentThread() )218{219//Still in the main thread, so set the flag just for kicks,220// and throw a test passed exception which will be caught221// and end the test.222theTestPassed = true;223throw new TestPassedException();224}225theTestPassed = true;226testGeneratedInterrupt = true;227mainThread.interrupt();228}//pass()229230public static synchronized void fail()231{232//test writer didn't specify why test failed, so give generic233fail( "it just plain failed! :-)" );234}235236public static synchronized void fail( String whyFailed )237{238Sysout.println( "The test failed: " + whyFailed );239Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );240//check if this called from main thread241if ( mainThread == Thread.currentThread() )242{243//If main thread, fail now 'cause not sleeping244throw new RuntimeException( whyFailed );245}246theTestPassed = false;247testGeneratedInterrupt = true;248failureMessage = whyFailed;249mainThread.interrupt();250}//fail()251252}// class CompEventOnHiddenComponent253254//This exception is used to exit from any level of call nesting255// when it's determined that the test has passed, and immediately256// end the test.257class TestPassedException extends RuntimeException258{259}260261//*********** End Standard Test Machinery Section **********262263264//************ Begin classes defined for the test ****************265266// if want to make listeners, here is the recommended place for them, then instantiate267// them in init()268269/* Example of a class which may be written as part of a test270class NewClass implements anInterface271{272static int newVar = 0;273274public void eventDispatched(AWTEvent e)275{276//Counting events to see if we get enough277eventCount++;278279if( eventCount == 20 )280{281//got enough events, so pass282283CompEventOnHiddenComponent.pass();284}285else if( tries == 20 )286{287//tried too many times without getting enough events so fail288289CompEventOnHiddenComponent.fail();290}291292}// eventDispatched()293294}// NewClass class295296*/297298299//************** End classes defined for the test *******************300301302303304/****************************************************305Standard Test Machinery306DO NOT modify anything below -- it's a standard307chunk of code whose purpose is to make user308interaction uniform, and thereby make it simpler309to read and understand someone else's test.310****************************************************/311312/**313This is part of the standard test machinery.314It creates a dialog (with the instructions), and is the interface315for sending text messages to the user.316To print the instructions, send an array of strings to Sysout.createDialog317WithInstructions method. Put one line of instructions per array entry.318To display a message for the tester to see, simply call Sysout.println319with the string to be displayed.320This mimics System.out.println but works within the test harness as well321as standalone.322*/323324class Sysout325{326private static TestDialog dialog;327328public static void createDialogWithInstructions( String[] instructions )329{330dialog = new TestDialog( new Frame(), "Instructions" );331dialog.printInstructions( instructions );332dialog.setVisible(true);333println( "Any messages for the tester will display here." );334}335336public static void createDialog( )337{338dialog = new TestDialog( new Frame(), "Instructions" );339String[] defInstr = { "Instructions will appear here. ", "" } ;340dialog.printInstructions( defInstr );341dialog.setVisible(true);342println( "Any messages for the tester will display here." );343}344345346public static void printInstructions( String[] instructions )347{348dialog.printInstructions( instructions );349}350351352public static void println( String messageIn )353{354dialog.displayMessage( messageIn );355System.out.println(messageIn);356}357358}// Sysout class359360/**361This is part of the standard test machinery. It provides a place for the362test instructions to be displayed, and a place for interactive messages363to the user to be displayed.364To have the test instructions displayed, see Sysout.365To have a message to the user be displayed, see Sysout.366Do not call anything in this dialog directly.367*/368class TestDialog extends Dialog369{370371TextArea instructionsText;372TextArea messageText;373int maxStringLength = 80;374375//DO NOT call this directly, go through Sysout376public TestDialog( Frame frame, String name )377{378super( frame, name );379int scrollBoth = TextArea.SCROLLBARS_BOTH;380instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );381add( "North", instructionsText );382383messageText = new TextArea( "", 5, maxStringLength, scrollBoth );384add("Center", messageText);385386pack();387388setVisible(true);389}// TestDialog()390391//DO NOT call this directly, go through Sysout392public void printInstructions( String[] instructions )393{394//Clear out any current instructions395instructionsText.setText( "" );396397//Go down array of instruction strings398399String printStr, remainingStr;400for( int i=0; i < instructions.length; i++ )401{402//chop up each into pieces maxSringLength long403remainingStr = instructions[ i ];404while( remainingStr.length() > 0 )405{406//if longer than max then chop off first max chars to print407if( remainingStr.length() >= maxStringLength )408{409//Try to chop on a word boundary410int posOfSpace = remainingStr.411lastIndexOf( ' ', maxStringLength - 1 );412413if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;414415printStr = remainingStr.substring( 0, posOfSpace + 1 );416remainingStr = remainingStr.substring( posOfSpace + 1 );417}418//else just print419else420{421printStr = remainingStr;422remainingStr = "";423}424425instructionsText.append( printStr + "\n" );426427}// while428429}// for430431}//printInstructions()432433//DO NOT call this directly, go through Sysout434public void displayMessage( String messageIn )435{436messageText.append( messageIn + "\n" );437System.out.println(messageIn);438}439440}// TestDialog class441442443