Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Mixing/MixingInHwPanel.java
47661 views
/*1* Copyright (c) 2009, 2013, 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 682985826@summary Mixing should work inside heavyweight containers27@author [email protected]: area=awt.mixing28@library ../regtesthelpers29@build Util30@run main MixingInHwPanel31*/323334/**35* MixingInHwPanel.java36*37* summary: Mixing should work inside heavyweight containers38*/3940import java.awt.*;41import java.awt.event.*;42import javax.swing.*;43import test.java.awt.regtesthelpers.Util;44454647public class MixingInHwPanel48{49static volatile boolean failed = true;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 );6364// Create the components: frame -> hwPanel -> JDesktopPane ->65// -> JInternalFrame -> hwButton66Frame frame = new Frame("Mixing in a heavyweight Panel");67frame.setBounds(100, 100, 640, 480);6869Panel hwPanel = new Panel(new BorderLayout());70frame.add(hwPanel);7172JDesktopPane desktop = new JDesktopPane();73hwPanel.add(desktop);7475JInternalFrame iFrame = new JInternalFrame("one",76true, true, true, true);77iFrame.setPreferredSize(new Dimension(150, 55));78iFrame.setBounds(600, 100, 150, 55);79iFrame.setVisible(true);80desktop.add(iFrame);8182Button button = new Button("HW Button");83button.addActionListener(new ActionListener() {84public void actionPerformed(ActionEvent e) {85failed = false;86}87});88iFrame.add(button);8990// Show the frame with the hwButton slightly hidden initially91frame.setVisible(true);9293Robot robot = Util.createRobot();94robot.setAutoDelay(20);9596Util.waitForIdle(robot);9798// Now resize the frame so that the button is fully visible99frame.setBounds(100, 100, 800, 480);100frame.validate();101102Util.waitForIdle(robot);103104// And click the part of the button that has been previously hidden105Point bLoc = button.getLocationOnScreen();106robot.mouseMove(bLoc.x + button.getWidth() - 15, bLoc.y + button.getHeight() / 2);107108Util.waitForIdle(robot);109110robot.mousePress(InputEvent.BUTTON1_MASK);111robot.mouseRelease(InputEvent.BUTTON1_MASK);112113Util.waitForIdle(robot);114115// If the click happens (the shape is reapplied), the button's action116// listener will make failed == false.117if (failed) {118MixingInHwPanel.fail("The HW button did not receive the click.");119} else {120MixingInHwPanel.pass();121}122}//End init()123124125126/*****************************************************127* Standard Test Machinery Section128* DO NOT modify anything in this section -- it's a129* standard chunk of code which has all of the130* synchronisation necessary for the test harness.131* By keeping it the same in all tests, it is easier132* to read and understand someone else's test, as133* well as insuring that all tests behave correctly134* with the test harness.135* There is a section following this for test-136* classes137******************************************************/138private static boolean theTestPassed = false;139private static boolean testGeneratedInterrupt = false;140private static String failureMessage = "";141142private static Thread mainThread = null;143144private static int sleepTime = 300000;145146// Not sure about what happens if multiple of this test are147// instantiated in the same VM. Being static (and using148// static vars), it aint gonna work. Not worrying about149// it for now.150public static void main( String args[] ) throws InterruptedException151{152mainThread = Thread.currentThread();153try154{155init();156}157catch( TestPassedException e )158{159//The test passed, so just return from main and harness will160// interepret this return as a pass161return;162}163//At this point, neither test pass nor test fail has been164// called -- either would have thrown an exception and ended the165// test, so we know we have multiple threads.166167//Test involves other threads, so sleep and wait for them to168// called pass() or fail()169try170{171Thread.sleep( sleepTime );172//Timed out, so fail the test173throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );174}175catch (InterruptedException e)176{177//The test harness may have interrupted the test. If so, rethrow the exception178// so that the harness gets it and deals with it.179if( ! testGeneratedInterrupt ) throw e;180181//reset flag in case hit this code more than once for some reason (just safety)182testGeneratedInterrupt = false;183184if ( theTestPassed == false )185{186throw new RuntimeException( failureMessage );187}188}189190}//main191192public static synchronized void setTimeoutTo( int seconds )193{194sleepTime = seconds * 1000;195}196197public static synchronized void pass()198{199Sysout.println( "The test passed." );200Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );201//first check if this is executing in main thread202if ( mainThread == Thread.currentThread() )203{204//Still in the main thread, so set the flag just for kicks,205// and throw a test passed exception which will be caught206// and end the test.207theTestPassed = true;208throw new TestPassedException();209}210theTestPassed = true;211testGeneratedInterrupt = true;212mainThread.interrupt();213}//pass()214215public static synchronized void fail()216{217//test writer didn't specify why test failed, so give generic218fail( "it just plain failed! :-)" );219}220221public static synchronized void fail( String whyFailed )222{223Sysout.println( "The test failed: " + whyFailed );224Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );225//check if this called from main thread226if ( mainThread == Thread.currentThread() )227{228//If main thread, fail now 'cause not sleeping229throw new RuntimeException( whyFailed );230}231theTestPassed = false;232testGeneratedInterrupt = true;233failureMessage = whyFailed;234mainThread.interrupt();235}//fail()236237}// class MixingInHwPanel238239//This exception is used to exit from any level of call nesting240// when it's determined that the test has passed, and immediately241// end the test.242class TestPassedException extends RuntimeException243{244}245246//*********** End Standard Test Machinery Section **********247248249//************ Begin classes defined for the test ****************250251// if want to make listeners, here is the recommended place for them, then instantiate252// them in init()253254/* Example of a class which may be written as part of a test255class NewClass implements anInterface256{257static int newVar = 0;258259public void eventDispatched(AWTEvent e)260{261//Counting events to see if we get enough262eventCount++;263264if( eventCount == 20 )265{266//got enough events, so pass267268MixingInHwPanel.pass();269}270else if( tries == 20 )271{272//tried too many times without getting enough events so fail273274MixingInHwPanel.fail();275}276277}// eventDispatched()278279}// NewClass class280281*/282283284//************** End classes defined for the test *******************285286287288289/****************************************************290Standard Test Machinery291DO NOT modify anything below -- it's a standard292chunk of code whose purpose is to make user293interaction uniform, and thereby make it simpler294to read and understand someone else's test.295****************************************************/296297/**298This is part of the standard test machinery.299It creates a dialog (with the instructions), and is the interface300for sending text messages to the user.301To print the instructions, send an array of strings to Sysout.createDialog302WithInstructions method. Put one line of instructions per array entry.303To display a message for the tester to see, simply call Sysout.println304with the string to be displayed.305This mimics System.out.println but works within the test harness as well306as standalone.307*/308309class Sysout310{311private static TestDialog dialog;312313public static void createDialogWithInstructions( String[] instructions )314{315dialog = new TestDialog( new Frame(), "Instructions" );316dialog.printInstructions( instructions );317dialog.setVisible(true);318println( "Any messages for the tester will display here." );319}320321public static void createDialog( )322{323dialog = new TestDialog( new Frame(), "Instructions" );324String[] defInstr = { "Instructions will appear here. ", "" } ;325dialog.printInstructions( defInstr );326dialog.setVisible(true);327println( "Any messages for the tester will display here." );328}329330331public static void printInstructions( String[] instructions )332{333dialog.printInstructions( instructions );334}335336337public static void println( String messageIn )338{339dialog.displayMessage( messageIn );340System.out.println(messageIn);341}342343}// Sysout class344345/**346This is part of the standard test machinery. It provides a place for the347test instructions to be displayed, and a place for interactive messages348to the user to be displayed.349To have the test instructions displayed, see Sysout.350To have a message to the user be displayed, see Sysout.351Do not call anything in this dialog directly.352*/353class TestDialog extends Dialog354{355356TextArea instructionsText;357TextArea messageText;358int maxStringLength = 80;359360//DO NOT call this directly, go through Sysout361public TestDialog( Frame frame, String name )362{363super( frame, name );364int scrollBoth = TextArea.SCROLLBARS_BOTH;365instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );366add( "North", instructionsText );367368messageText = new TextArea( "", 5, maxStringLength, scrollBoth );369add("Center", messageText);370371pack();372373setVisible(true);374}// TestDialog()375376//DO NOT call this directly, go through Sysout377public void printInstructions( String[] instructions )378{379//Clear out any current instructions380instructionsText.setText( "" );381382//Go down array of instruction strings383384String printStr, remainingStr;385for( int i=0; i < instructions.length; i++ )386{387//chop up each into pieces maxSringLength long388remainingStr = instructions[ i ];389while( remainingStr.length() > 0 )390{391//if longer than max then chop off first max chars to print392if( remainingStr.length() >= maxStringLength )393{394//Try to chop on a word boundary395int posOfSpace = remainingStr.396lastIndexOf( ' ', maxStringLength - 1 );397398if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;399400printStr = remainingStr.substring( 0, posOfSpace + 1 );401remainingStr = remainingStr.substring( posOfSpace + 1 );402}403//else just print404else405{406printStr = remainingStr;407remainingStr = "";408}409410instructionsText.append( printStr + "\n" );411412}// while413414}// for415416}//printInstructions()417418//DO NOT call this directly, go through Sysout419public void displayMessage( String messageIn )420{421messageText.append( messageIn + "\n" );422System.out.println(messageIn);423}424425}// TestDialog class426427428429430