Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Mixing/JButtonInGlassPane.java
47867 views
/*1* Copyright (c) 2009, 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@test %W% %E%25@bug 677967026@summary Tests if a LW components in the glass pane affects HW in the content pane27@author anthony.petrov@...: area=awt.mixing28@library ../regtesthelpers29@build Util30@run main JButtonInGlassPane31*/323334/**35* JButtonInGlassPane.java36*37* summary: Tests whether a LW menu correctly overlaps a HW button38*/3940import java.awt.*;41import java.awt.event.*;42import javax.swing.*;43import test.java.awt.regtesthelpers.Util;44454647public class JButtonInGlassPane48{49static volatile boolean failed = false;5051private static void init()52{53//*** Create instructions for the user here ***5455String[] instructions =56{57"This is an AUTOMATIC test, simply wait until it is done.",58"The result (passed or failed) will be shown in the",59"message window below."60};61Sysout.createDialog( );62Sysout.printInstructions( instructions );6364JFrame frame = new JFrame("Glass Pane children test");65frame.setLayout(null);6667final Button button = new Button("AWT Button");68button.setBounds(100,100,100,100);69frame.add(button);7071button.addActionListener(new ActionListener() {72public void actionPerformed(ActionEvent e) {73failed = true;74}75});7677frame.getGlassPane().setVisible(true);78Container glassPane = (Container) frame.getGlassPane();79glassPane.setLayout(null);8081final JButton jbutton = new JButton("JButton");82jbutton.setBounds(50,50,100,100);83glassPane.add(jbutton);8485jbutton.setVisible(false);8687frame.setSize(400, 400);88frame.setLocationRelativeTo(null);89frame.setVisible(true);9091Robot robot = Util.createRobot();92robot.setAutoDelay(20);9394Util.waitForIdle(robot);9596jbutton.setVisible(true);97Util.waitForIdle(robot);9899// Click the LW button - in the area that intersects with100// the HW button.101Point lLoc = jbutton.getLocationOnScreen();102robot.mouseMove(lLoc.x + jbutton.getWidth() - 5, lLoc.y + jbutton.getHeight() - 5);103104robot.mousePress(InputEvent.BUTTON1_MASK);105robot.mouseRelease(InputEvent.BUTTON1_MASK);106Util.waitForIdle(robot);107108jbutton.setBounds(50,50,120,120);109Util.waitForIdle(robot);110111// Now click on the 'added' area of the LW button that again112// intersects with the HW.113robot.mouseMove(lLoc.x + jbutton.getWidth() - 5, lLoc.y + jbutton.getHeight() - 5);114115robot.mousePress(InputEvent.BUTTON1_MASK);116robot.mouseRelease(InputEvent.BUTTON1_MASK);117Util.waitForIdle(robot);118119if (failed) {120JButtonInGlassPane.fail("The LW button did not receive the click.");121} else {122JButtonInGlassPane.pass();123}124}//End init()125126127128/*****************************************************129* Standard Test Machinery Section130* DO NOT modify anything in this section -- it's a131* standard chunk of code which has all of the132* synchronisation necessary for the test harness.133* By keeping it the same in all tests, it is easier134* to read and understand someone else's test, as135* well as insuring that all tests behave correctly136* with the test harness.137* There is a section following this for test-138* classes139******************************************************/140private static boolean theTestPassed = false;141private static boolean testGeneratedInterrupt = false;142private static String failureMessage = "";143144private static Thread mainThread = null;145146private static int sleepTime = 300000;147148// Not sure about what happens if multiple of this test are149// instantiated in the same VM. Being static (and using150// static vars), it aint gonna work. Not worrying about151// it for now.152public static void main( String args[] ) throws InterruptedException153{154mainThread = Thread.currentThread();155try156{157init();158}159catch( TestPassedException e )160{161//The test passed, so just return from main and harness will162// interepret this return as a pass163return;164}165//At this point, neither test pass nor test fail has been166// called -- either would have thrown an exception and ended the167// test, so we know we have multiple threads.168169//Test involves other threads, so sleep and wait for them to170// called pass() or fail()171try172{173Thread.sleep( sleepTime );174//Timed out, so fail the test175throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );176}177catch (InterruptedException e)178{179//The test harness may have interrupted the test. If so, rethrow the exception180// so that the harness gets it and deals with it.181if( ! testGeneratedInterrupt ) throw e;182183//reset flag in case hit this code more than once for some reason (just safety)184testGeneratedInterrupt = false;185186if ( theTestPassed == false )187{188throw new RuntimeException( failureMessage );189}190}191192}//main193194public static synchronized void setTimeoutTo( int seconds )195{196sleepTime = seconds * 1000;197}198199public static synchronized void pass()200{201Sysout.println( "The test passed." );202Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );203//first check if this is executing in main thread204if ( mainThread == Thread.currentThread() )205{206//Still in the main thread, so set the flag just for kicks,207// and throw a test passed exception which will be caught208// and end the test.209theTestPassed = true;210throw new TestPassedException();211}212theTestPassed = true;213testGeneratedInterrupt = true;214mainThread.interrupt();215}//pass()216217public static synchronized void fail()218{219//test writer didn't specify why test failed, so give generic220fail( "it just plain failed! :-)" );221}222223public static synchronized void fail( String whyFailed )224{225Sysout.println( "The test failed: " + whyFailed );226Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );227//check if this called from main thread228if ( mainThread == Thread.currentThread() )229{230//If main thread, fail now 'cause not sleeping231throw new RuntimeException( whyFailed );232}233theTestPassed = false;234testGeneratedInterrupt = true;235failureMessage = whyFailed;236mainThread.interrupt();237}//fail()238239}// class JButtonInGlassPane240241//This exception is used to exit from any level of call nesting242// when it's determined that the test has passed, and immediately243// end the test.244class TestPassedException extends RuntimeException245{246}247248//*********** End Standard Test Machinery Section **********249250251//************ Begin classes defined for the test ****************252253// if want to make listeners, here is the recommended place for them, then instantiate254// them in init()255256/* Example of a class which may be written as part of a test257class NewClass implements anInterface258{259static int newVar = 0;260261public void eventDispatched(AWTEvent e)262{263//Counting events to see if we get enough264eventCount++;265266if( eventCount == 20 )267{268//got enough events, so pass269270JButtonInGlassPane.pass();271}272else if( tries == 20 )273{274//tried too many times without getting enough events so fail275276JButtonInGlassPane.fail();277}278279}// eventDispatched()280281}// NewClass class282283*/284285286//************** End classes defined for the test *******************287288289290291/****************************************************292Standard Test Machinery293DO NOT modify anything below -- it's a standard294chunk of code whose purpose is to make user295interaction uniform, and thereby make it simpler296to read and understand someone else's test.297****************************************************/298299/**300This is part of the standard test machinery.301It creates a dialog (with the instructions), and is the interface302for sending text messages to the user.303To print the instructions, send an array of strings to Sysout.createDialog304WithInstructions method. Put one line of instructions per array entry.305To display a message for the tester to see, simply call Sysout.println306with the string to be displayed.307This mimics System.out.println but works within the test harness as well308as standalone.309*/310311class Sysout312{313private static TestDialog dialog;314315public static void createDialogWithInstructions( String[] instructions )316{317dialog = new TestDialog( new Frame(), "Instructions" );318dialog.printInstructions( instructions );319dialog.setVisible(true);320println( "Any messages for the tester will display here." );321}322323public static void createDialog( )324{325dialog = new TestDialog( new Frame(), "Instructions" );326String[] defInstr = { "Instructions will appear here. ", "" } ;327dialog.printInstructions( defInstr );328dialog.setVisible(true);329println( "Any messages for the tester will display here." );330}331332333public static void printInstructions( String[] instructions )334{335dialog.printInstructions( instructions );336}337338339public static void println( String messageIn )340{341dialog.displayMessage( messageIn );342System.out.println(messageIn);343}344345}// Sysout class346347/**348This is part of the standard test machinery. It provides a place for the349test instructions to be displayed, and a place for interactive messages350to the user to be displayed.351To have the test instructions displayed, see Sysout.352To have a message to the user be displayed, see Sysout.353Do not call anything in this dialog directly.354*/355class TestDialog extends Dialog356{357358TextArea instructionsText;359TextArea messageText;360int maxStringLength = 80;361362//DO NOT call this directly, go through Sysout363public TestDialog( Frame frame, String name )364{365super( frame, name );366int scrollBoth = TextArea.SCROLLBARS_BOTH;367instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );368add( "North", instructionsText );369370messageText = new TextArea( "", 5, maxStringLength, scrollBoth );371add("Center", messageText);372373pack();374375setVisible(true);376}// TestDialog()377378//DO NOT call this directly, go through Sysout379public void printInstructions( String[] instructions )380{381//Clear out any current instructions382instructionsText.setText( "" );383384//Go down array of instruction strings385386String printStr, remainingStr;387for( int i=0; i < instructions.length; i++ )388{389//chop up each into pieces maxSringLength long390remainingStr = instructions[ i ];391while( remainingStr.length() > 0 )392{393//if longer than max then chop off first max chars to print394if( remainingStr.length() >= maxStringLength )395{396//Try to chop on a word boundary397int posOfSpace = remainingStr.398lastIndexOf( ' ', maxStringLength - 1 );399400if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;401402printStr = remainingStr.substring( 0, posOfSpace + 1 );403remainingStr = remainingStr.substring( posOfSpace + 1 );404}405//else just print406else407{408printStr = remainingStr;409remainingStr = "";410}411412instructionsText.append( printStr + "\n" );413414}// while415416}// for417418}//printInstructions()419420//DO NOT call this directly, go through Sysout421public void displayMessage( String messageIn )422{423messageText.append( messageIn + "\n" );424System.out.println(messageIn);425}426427}// TestDialog class428429430431432