Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Mixing/NonOpaqueInternalFrame.java
47661 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 676833226@summary Tests whether internal frames are always considered opaque27@author anthony.petrov@...: area=awt.mixing28@library ../regtesthelpers29@build Util30@run main NonOpaqueInternalFrame31*/323334/**35* NonOpaqueInternalFrame.java36*37* summary: Tests whether internal frames are always considered opaque38*/3940import java.awt.*;41import java.awt.event.*;42import java.beans.PropertyVetoException;43import javax.swing.*;44import java.util.Vector;45import test.java.awt.regtesthelpers.Util;46474849public class NonOpaqueInternalFrame50{51static volatile boolean failed = false;5253private static final class MyButton extends Button54implements ActionListener55{56public MyButton() {57setPreferredSize(new Dimension(100, 100));58addActionListener(this);59}6061public void actionPerformed(ActionEvent e) {62failed = true;63}64}6566private static void init()67{68String[] instructions =69{70"This is an AUTOMATIC test, simply wait until it is done.",71"The result (passed or failed) will be shown in the",72"message window below."73};74Sysout.createDialog( );75Sysout.printInstructions( instructions );767778// Create a frame with two non-opaque JInternalFrame's containing79// heavyweight buttons.80JFrame jframe = new JFrame("mixing test");81JDesktopPane desktop = new JDesktopPane();82jframe.setContentPane(desktop);83JInternalFrame iframe1 = new JInternalFrame("iframe 1");84iframe1.setIconifiable(true);85iframe1.add(new MyButton());86iframe1.setBounds(10, 10, 100, 100);87iframe1.setOpaque(false);88iframe1.setVisible(true);89desktop.add(iframe1);90JInternalFrame iframe2 = new JInternalFrame("iframe 2");91iframe2.setIconifiable(true);92iframe2.add(new MyButton());93iframe2.setBounds(50, 50, 100, 100);94iframe2.setOpaque(false);95iframe2.setVisible(true);96desktop.add(iframe2);97jframe.setSize(300, 300);98jframe.setVisible(true);99100Robot robot = Util.createRobot();101robot.setAutoDelay(20);102103Util.waitForIdle(robot);104105// Try selecting the bottommost frame106try {107iframe2.setSelected(true);108} catch (PropertyVetoException ex) {109ex.printStackTrace();110}111112// Click the title bar of the internal frame113Point lLoc = iframe2.getLocationOnScreen();114System.err.println("lLoc: " + lLoc);115robot.mouseMove(lLoc.x + 10, lLoc.y + 10);116Util.waitForIdle(robot);117118robot.mousePress(InputEvent.BUTTON1_MASK);119robot.mouseRelease(InputEvent.BUTTON1_MASK);120Util.waitForIdle(robot);121122123if (failed) {124fail("The JInternalFrame is considered non-opaque.");125} else {126pass();127}128}//End init()129130131132/*****************************************************133* Standard Test Machinery Section134* DO NOT modify anything in this section -- it's a135* standard chunk of code which has all of the136* synchronisation necessary for the test harness.137* By keeping it the same in all tests, it is easier138* to read and understand someone else's test, as139* well as insuring that all tests behave correctly140* with the test harness.141* There is a section following this for test-142* classes143******************************************************/144private static boolean theTestPassed = false;145private static boolean testGeneratedInterrupt = false;146private static String failureMessage = "";147148private static Thread mainThread = null;149150private static int sleepTime = 300000;151152// Not sure about what happens if multiple of this test are153// instantiated in the same VM. Being static (and using154// static vars), it aint gonna work. Not worrying about155// it for now.156public static void main( String args[] ) throws InterruptedException157{158mainThread = Thread.currentThread();159try160{161init();162}163catch( TestPassedException e )164{165//The test passed, so just return from main and harness will166// interepret this return as a pass167return;168}169//At this point, neither test pass nor test fail has been170// called -- either would have thrown an exception and ended the171// test, so we know we have multiple threads.172173//Test involves other threads, so sleep and wait for them to174// called pass() or fail()175try176{177Thread.sleep( sleepTime );178//Timed out, so fail the test179throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );180}181catch (InterruptedException e)182{183//The test harness may have interrupted the test. If so, rethrow the exception184// so that the harness gets it and deals with it.185if( ! testGeneratedInterrupt ) throw e;186187//reset flag in case hit this code more than once for some reason (just safety)188testGeneratedInterrupt = false;189190if ( theTestPassed == false )191{192throw new RuntimeException( failureMessage );193}194}195196}//main197198public static synchronized void setTimeoutTo( int seconds )199{200sleepTime = seconds * 1000;201}202203public static synchronized void pass()204{205Sysout.println( "The test passed." );206Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );207//first check if this is executing in main thread208if ( mainThread == Thread.currentThread() )209{210//Still in the main thread, so set the flag just for kicks,211// and throw a test passed exception which will be caught212// and end the test.213theTestPassed = true;214throw new TestPassedException();215}216theTestPassed = true;217testGeneratedInterrupt = true;218mainThread.interrupt();219}//pass()220221public static synchronized void fail()222{223//test writer didn't specify why test failed, so give generic224fail( "it just plain failed! :-)" );225}226227public static synchronized void fail( String whyFailed )228{229Sysout.println( "The test failed: " + whyFailed );230Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );231//check if this called from main thread232if ( mainThread == Thread.currentThread() )233{234//If main thread, fail now 'cause not sleeping235throw new RuntimeException( whyFailed );236}237theTestPassed = false;238testGeneratedInterrupt = true;239failureMessage = whyFailed;240mainThread.interrupt();241}//fail()242243}// class NonOpaqueInternalFrame244245//This exception is used to exit from any level of call nesting246// when it's determined that the test has passed, and immediately247// end the test.248class TestPassedException extends RuntimeException249{250}251252//*********** End Standard Test Machinery Section **********253254255//************ Begin classes defined for the test ****************256257// if want to make listeners, here is the recommended place for them, then instantiate258// them in init()259260/* Example of a class which may be written as part of a test261class NewClass implements anInterface262{263static int newVar = 0;264265public void eventDispatched(AWTEvent e)266{267//Counting events to see if we get enough268eventCount++;269270if( eventCount == 20 )271{272//got enough events, so pass273274NonOpaqueInternalFrame.pass();275}276else if( tries == 20 )277{278//tried too many times without getting enough events so fail279280NonOpaqueInternalFrame.fail();281}282283}// eventDispatched()284285}// NewClass class286287*/288289290//************** End classes defined for the test *******************291292293294295/****************************************************296Standard Test Machinery297DO NOT modify anything below -- it's a standard298chunk of code whose purpose is to make user299interaction uniform, and thereby make it simpler300to read and understand someone else's test.301****************************************************/302303/**304This is part of the standard test machinery.305It creates a dialog (with the instructions), and is the interface306for sending text messages to the user.307To print the instructions, send an array of strings to Sysout.createDialog308WithInstructions method. Put one line of instructions per array entry.309To display a message for the tester to see, simply call Sysout.println310with the string to be displayed.311This mimics System.out.println but works within the test harness as well312as standalone.313*/314315class Sysout316{317private static TestDialog dialog;318319public static void createDialogWithInstructions( String[] instructions )320{321dialog = new TestDialog( new Frame(), "Instructions" );322dialog.printInstructions( instructions );323dialog.setVisible(true);324println( "Any messages for the tester will display here." );325}326327public static void createDialog( )328{329dialog = new TestDialog( new Frame(), "Instructions" );330String[] defInstr = { "Instructions will appear here. ", "" } ;331dialog.printInstructions( defInstr );332dialog.setVisible(true);333println( "Any messages for the tester will display here." );334}335336337public static void printInstructions( String[] instructions )338{339dialog.printInstructions( instructions );340}341342343public static void println( String messageIn )344{345dialog.displayMessage( messageIn );346System.out.println(messageIn);347}348349}// Sysout class350351/**352This is part of the standard test machinery. It provides a place for the353test instructions to be displayed, and a place for interactive messages354to the user to be displayed.355To have the test instructions displayed, see Sysout.356To have a message to the user be displayed, see Sysout.357Do not call anything in this dialog directly.358*/359class TestDialog extends Dialog360{361362TextArea instructionsText;363TextArea messageText;364int maxStringLength = 80;365366//DO NOT call this directly, go through Sysout367public TestDialog( Frame frame, String name )368{369super( frame, name );370int scrollBoth = TextArea.SCROLLBARS_BOTH;371instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );372add( "North", instructionsText );373374messageText = new TextArea( "", 5, maxStringLength, scrollBoth );375add("Center", messageText);376377pack();378379setVisible(true);380}// TestDialog()381382//DO NOT call this directly, go through Sysout383public void printInstructions( String[] instructions )384{385//Clear out any current instructions386instructionsText.setText( "" );387388//Go down array of instruction strings389390String printStr, remainingStr;391for( int i=0; i < instructions.length; i++ )392{393//chop up each into pieces maxSringLength long394remainingStr = instructions[ i ];395while( remainingStr.length() > 0 )396{397//if longer than max then chop off first max chars to print398if( remainingStr.length() >= maxStringLength )399{400//Try to chop on a word boundary401int posOfSpace = remainingStr.402lastIndexOf( ' ', maxStringLength - 1 );403404if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;405406printStr = remainingStr.substring( 0, posOfSpace + 1 );407remainingStr = remainingStr.substring( posOfSpace + 1 );408}409//else just print410else411{412printStr = remainingStr;413remainingStr = "";414}415416instructionsText.append( printStr + "\n" );417418}// while419420}// for421422}//printInstructions()423424//DO NOT call this directly, go through Sysout425public void displayMessage( String messageIn )426{427messageText.append( messageIn + "\n" );428System.out.println(messageIn);429}430431}// TestDialog class432433434435436