Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Mixing/OpaqueTest.java
47661 views
/*1* Copyright (c) 2007, 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 481109626@summary Tests whether opaque and non-opaque components mix correctly27@author anthony.petrov@...: area=awt.mixing28@library ../regtesthelpers29@build Util30@run main OpaqueTest31*/323334/**35* OpaqueTest.java36*37* summary: OpaqueTest38*/3940import java.awt.*;41import java.awt.event.*;42import javax.swing.*;43import test.java.awt.regtesthelpers.Util;44import com.sun.awt.AWTUtilities;45464748public class OpaqueTest49{5051//*** test-writer defined static variables go here ***5253static String testSeq = new String("");54final static String checkSeq = new String("010000101");5556private static void init()57{58//*** Create instructions for the user here ***5960String[] instructions =61{62"This is an AUTOMATIC test, simply wait until it is done.",63"The result (passed or failed) will be shown in the",64"message window below."65};66Sysout.createDialog( );67Sysout.printInstructions( instructions );686970// Create components71final Frame f = new Frame("Button-JButton mix test");72final Panel p = new Panel();73final Button heavy = new Button(" Heavyweight Button ");74final JButton light = new JButton(" LW Button ");7576// Actions for the buttons add appropriate number to the test sequence77heavy.addActionListener(new java.awt.event.ActionListener()78{79public void actionPerformed(java.awt.event.ActionEvent e) {80p.setComponentZOrder(light, 0);81f.validate();82testSeq = testSeq + "0";83}84}85);8687light.addActionListener(new java.awt.event.ActionListener()88{89public void actionPerformed(java.awt.event.ActionEvent e) {90p.setComponentZOrder(heavy, 0);91f.validate();92testSeq = testSeq + "1";93}94}95);9697// Overlap the buttons98heavy.setBounds(30, 30, 200, 200);99light.setBounds(10, 10, 50, 50);100101// Put the components into the frame102p.setLayout(null);103p.add(heavy);104p.add(light);105f.add(p);106f.setBounds(50, 50, 400, 400);107f.show();108109110Robot robot = Util.createRobot();111robot.setAutoDelay(20);112113Util.waitForIdle(robot);114115// Move the mouse pointer to the position where both116// buttons overlap117Point heavyLoc = heavy.getLocationOnScreen();118robot.mouseMove(heavyLoc.x + 5, heavyLoc.y + 5);119120// Now perform the click at this point for 9 times121// In the middle of the process toggle the opaque122// flag value.123for (int i = 0; i < 9; ++i) {124if (i == 3) {125AWTUtilities.setComponentMixingCutoutShape(light,126new Rectangle());127}128if (i == 6) {129AWTUtilities.setComponentMixingCutoutShape(light,130null);131}132133robot.mousePress(InputEvent.BUTTON1_MASK);134robot.mouseRelease(InputEvent.BUTTON1_MASK);135Util.waitForIdle(robot);136}137138Util.waitForIdle(robot);139140// If the buttons are correctly mixed, the test sequence141// is equal to the check sequence.142if (testSeq.equals(checkSeq)) {143OpaqueTest.pass();144} else {145OpaqueTest.fail("The components changed their visible Z-order in a wrong sequence: '" + testSeq + "' instead of '" + checkSeq + "'");146}147}//End init()148149150151/*****************************************************152* Standard Test Machinery Section153* DO NOT modify anything in this section -- it's a154* standard chunk of code which has all of the155* synchronisation necessary for the test harness.156* By keeping it the same in all tests, it is easier157* to read and understand someone else's test, as158* well as insuring that all tests behave correctly159* with the test harness.160* There is a section following this for test-161* classes162******************************************************/163private static boolean theTestPassed = false;164private static boolean testGeneratedInterrupt = false;165private static String failureMessage = "";166167private static Thread mainThread = null;168169private static int sleepTime = 300000;170171// Not sure about what happens if multiple of this test are172// instantiated in the same VM. Being static (and using173// static vars), it aint gonna work. Not worrying about174// it for now.175public static void main( String args[] ) throws InterruptedException176{177mainThread = Thread.currentThread();178try179{180init();181}182catch( TestPassedException e )183{184//The test passed, so just return from main and harness will185// interepret this return as a pass186return;187}188//At this point, neither test pass nor test fail has been189// called -- either would have thrown an exception and ended the190// test, so we know we have multiple threads.191192//Test involves other threads, so sleep and wait for them to193// called pass() or fail()194try195{196Thread.sleep( sleepTime );197//Timed out, so fail the test198throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );199}200catch (InterruptedException e)201{202//The test harness may have interrupted the test. If so, rethrow the exception203// so that the harness gets it and deals with it.204if( ! testGeneratedInterrupt ) throw e;205206//reset flag in case hit this code more than once for some reason (just safety)207testGeneratedInterrupt = false;208209if ( theTestPassed == false )210{211throw new RuntimeException( failureMessage );212}213}214215}//main216217public static synchronized void setTimeoutTo( int seconds )218{219sleepTime = seconds * 1000;220}221222public static synchronized void pass()223{224Sysout.println( "The test passed." );225Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );226//first check if this is executing in main thread227if ( mainThread == Thread.currentThread() )228{229//Still in the main thread, so set the flag just for kicks,230// and throw a test passed exception which will be caught231// and end the test.232theTestPassed = true;233throw new TestPassedException();234}235theTestPassed = true;236testGeneratedInterrupt = true;237mainThread.interrupt();238}//pass()239240public static synchronized void fail()241{242//test writer didn't specify why test failed, so give generic243fail( "it just plain failed! :-)" );244}245246public static synchronized void fail( String whyFailed )247{248Sysout.println( "The test failed: " + whyFailed );249Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );250//check if this called from main thread251if ( mainThread == Thread.currentThread() )252{253//If main thread, fail now 'cause not sleeping254throw new RuntimeException( whyFailed );255}256theTestPassed = false;257testGeneratedInterrupt = true;258failureMessage = whyFailed;259mainThread.interrupt();260}//fail()261262}// class OpaqueTest263264//This exception is used to exit from any level of call nesting265// when it's determined that the test has passed, and immediately266// end the test.267class TestPassedException extends RuntimeException268{269}270271//*********** End Standard Test Machinery Section **********272273274//************ Begin classes defined for the test ****************275276// if want to make listeners, here is the recommended place for them, then instantiate277// them in init()278279/* Example of a class which may be written as part of a test280class NewClass implements anInterface281{282static int newVar = 0;283284public void eventDispatched(AWTEvent e)285{286//Counting events to see if we get enough287eventCount++;288289if( eventCount == 20 )290{291//got enough events, so pass292293OpaqueTest.pass();294}295else if( tries == 20 )296{297//tried too many times without getting enough events so fail298299OpaqueTest.fail();300}301302}// eventDispatched()303304}// NewClass class305306*/307308309//************** End classes defined for the test *******************310311312313314/****************************************************315Standard Test Machinery316DO NOT modify anything below -- it's a standard317chunk of code whose purpose is to make user318interaction uniform, and thereby make it simpler319to read and understand someone else's test.320****************************************************/321322/**323This is part of the standard test machinery.324It creates a dialog (with the instructions), and is the interface325for sending text messages to the user.326To print the instructions, send an array of strings to Sysout.createDialog327WithInstructions method. Put one line of instructions per array entry.328To display a message for the tester to see, simply call Sysout.println329with the string to be displayed.330This mimics System.out.println but works within the test harness as well331as standalone.332*/333334class Sysout335{336private static TestDialog dialog;337338public static void createDialogWithInstructions( String[] instructions )339{340dialog = new TestDialog( new Frame(), "Instructions" );341dialog.printInstructions( instructions );342dialog.setVisible(true);343println( "Any messages for the tester will display here." );344}345346public static void createDialog( )347{348dialog = new TestDialog( new Frame(), "Instructions" );349String[] defInstr = { "Instructions will appear here. ", "" } ;350dialog.printInstructions( defInstr );351dialog.setVisible(true);352println( "Any messages for the tester will display here." );353}354355356public static void printInstructions( String[] instructions )357{358dialog.printInstructions( instructions );359}360361362public static void println( String messageIn )363{364dialog.displayMessage( messageIn );365System.out.println(messageIn);366}367368}// Sysout class369370/**371This is part of the standard test machinery. It provides a place for the372test instructions to be displayed, and a place for interactive messages373to the user to be displayed.374To have the test instructions displayed, see Sysout.375To have a message to the user be displayed, see Sysout.376Do not call anything in this dialog directly.377*/378class TestDialog extends Dialog379{380381TextArea instructionsText;382TextArea messageText;383int maxStringLength = 80;384385//DO NOT call this directly, go through Sysout386public TestDialog( Frame frame, String name )387{388super( frame, name );389int scrollBoth = TextArea.SCROLLBARS_BOTH;390instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );391add( "North", instructionsText );392393messageText = new TextArea( "", 5, maxStringLength, scrollBoth );394add("Center", messageText);395396pack();397398setVisible(true);399}// TestDialog()400401//DO NOT call this directly, go through Sysout402public void printInstructions( String[] instructions )403{404//Clear out any current instructions405instructionsText.setText( "" );406407//Go down array of instruction strings408409String printStr, remainingStr;410for( int i=0; i < instructions.length; i++ )411{412//chop up each into pieces maxSringLength long413remainingStr = instructions[ i ];414while( remainingStr.length() > 0 )415{416//if longer than max then chop off first max chars to print417if( remainingStr.length() >= maxStringLength )418{419//Try to chop on a word boundary420int posOfSpace = remainingStr.421lastIndexOf( ' ', maxStringLength - 1 );422423if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;424425printStr = remainingStr.substring( 0, posOfSpace + 1 );426remainingStr = remainingStr.substring( posOfSpace + 1 );427}428//else just print429else430{431printStr = remainingStr;432remainingStr = "";433}434435instructionsText.append( printStr + "\n" );436437}// while438439}// for440441}//printInstructions()442443//DO NOT call this directly, go through Sysout444public void displayMessage( String messageIn )445{446messageText.append( messageIn + "\n" );447System.out.println(messageIn);448}449450}// TestDialog class451452453