Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/awt/Translucency/WindowOpacity.java
38855 views
/*1* Copyright (c) 2008, 2010, 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 659413126@summary Tests the AWTUtilities.get/setWindowOpacity() methods27@author anthony.petrov@...: area=awt.toplevel28@run main WindowOpacity29*/3031import java.awt.*;32import java.awt.event.*;3334import com.sun.awt.AWTUtilities;3536public class WindowOpacity37{38//*** test-writer defined static variables go here ***3940private static Robot robot;414243private static void init()44{45//*** Create instructions for the user here ***46String[] instructions =47{48"This is an AUTOMATIC test, simply wait until it is done.",49"The result (passed or failed) will be shown in the",50"message window below."51};52Sysout.createDialog( );53Sysout.printInstructions( instructions );5455if (!AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency.TRANSLUCENT)) {56System.out.println("Either the Toolkit or the native system does not support controlling the window opacity level.");57pass();58}59try {60robot = new Robot();61}catch(Exception ex) {62ex.printStackTrace();63throw new RuntimeException ("Unexpected failure");64}6566boolean passed;6768Frame f = new Frame("Opacity test");69f.setUndecorated(true);7071passed = false;72try {73AWTUtilities.getWindowOpacity(null);74} catch (NullPointerException e) {75passed = true;76}77if (!passed) {78fail("getWindowOpacity() allows passing null.");79}808182passed = false;83try {84AWTUtilities.setWindowOpacity(null, 0.5f);85} catch (NullPointerException e) {86passed = true;87}88if (!passed) {89fail("setWindowOpacity() allows passing null.");90}919293float curOpacity = AWTUtilities.getWindowOpacity(f);94if (curOpacity < 1.0f || curOpacity > 1.0f) {95fail("getWindowOpacity() reports the initial opacity level other than 1.0: " + curOpacity);96}979899100passed = false;101try {102AWTUtilities.setWindowOpacity(f, -0.5f);103} catch (IllegalArgumentException e) {104passed = true;105}106if (!passed) {107fail("setWindowOpacity() allows passing negative opacity level.");108}109110111112passed = false;113try {114AWTUtilities.setWindowOpacity(f, 1.5f);115} catch (IllegalArgumentException e) {116passed = true;117}118if (!passed) {119fail("setWindowOpacity() allows passing opacity level greater than 1.0.");120}121122123AWTUtilities.setWindowOpacity(f, 0.5f);124125curOpacity = AWTUtilities.getWindowOpacity(f);126if (curOpacity < 0.5f || curOpacity > 0.5f) {127fail("getWindowOpacity() reports the opacity level that differs from the value set with setWindowOpacity: " + curOpacity);128}129130131AWTUtilities.setWindowOpacity(f, 0.75f);132133curOpacity = AWTUtilities.getWindowOpacity(f);134if (curOpacity < 0.75f || curOpacity > 0.75f) {135fail("getWindowOpacity() reports the opacity level that differs from the value set with setWindowOpacity the second time: " + curOpacity);136}137138139f.setBounds(100, 100, 300, 200);140f.setVisible(true);141142robot.waitForIdle();143144curOpacity = AWTUtilities.getWindowOpacity(f);145if (curOpacity < 0.75f || curOpacity > 0.75f) {146fail("getWindowOpacity() reports the opacity level that differs from the value set with setWindowOpacity before showing the frame: " + curOpacity);147}148149150151AWTUtilities.setWindowOpacity(f, 0.5f);152robot.waitForIdle();153154curOpacity = AWTUtilities.getWindowOpacity(f);155if (curOpacity < 0.5f || curOpacity > 0.5f) {156fail("getWindowOpacity() reports the opacity level that differs from the value set with setWindowOpacity after showing the frame: " + curOpacity);157}158159WindowOpacity.pass();160161}//End init()162163164165/*****************************************************166* Standard Test Machinery Section167* DO NOT modify anything in this section -- it's a168* standard chunk of code which has all of the169* synchronisation necessary for the test harness.170* By keeping it the same in all tests, it is easier171* to read and understand someone else's test, as172* well as insuring that all tests behave correctly173* with the test harness.174* There is a section following this for test-175* classes176******************************************************/177private static boolean theTestPassed = false;178private static boolean testGeneratedInterrupt = false;179private static String failureMessage = "";180181private static Thread mainThread = null;182183private static int sleepTime = 300000;184185// Not sure about what happens if multiple of this test are186// instantiated in the same VM. Being static (and using187// static vars), it aint gonna work. Not worrying about188// it for now.189public static void main( String args[] ) throws InterruptedException190{191mainThread = Thread.currentThread();192try193{194init();195}196catch( TestPassedException e )197{198//The test passed, so just return from main and harness will199// interepret this return as a pass200return;201}202//At this point, neither test pass nor test fail has been203// called -- either would have thrown an exception and ended the204// test, so we know we have multiple threads.205206//Test involves other threads, so sleep and wait for them to207// called pass() or fail()208try209{210Thread.sleep( sleepTime );211//Timed out, so fail the test212throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );213}214catch (InterruptedException e)215{216//The test harness may have interrupted the test. If so, rethrow the exception217// so that the harness gets it and deals with it.218if( ! testGeneratedInterrupt ) throw e;219220//reset flag in case hit this code more than once for some reason (just safety)221testGeneratedInterrupt = false;222223if ( theTestPassed == false )224{225throw new RuntimeException( failureMessage );226}227}228229}//main230231public static synchronized void setTimeoutTo( int seconds )232{233sleepTime = seconds * 1000;234}235236public static synchronized void pass()237{238Sysout.println( "The test passed." );239Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );240//first check if this is executing in main thread241if ( mainThread == Thread.currentThread() )242{243//Still in the main thread, so set the flag just for kicks,244// and throw a test passed exception which will be caught245// and end the test.246theTestPassed = true;247throw new TestPassedException();248}249theTestPassed = true;250testGeneratedInterrupt = true;251mainThread.interrupt();252}//pass()253254public static synchronized void fail()255{256//test writer didn't specify why test failed, so give generic257fail( "it just plain failed! :-)" );258}259260public static synchronized void fail( String whyFailed )261{262Sysout.println( "The test failed: " + whyFailed );263Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );264//check if this called from main thread265if ( mainThread == Thread.currentThread() )266{267//If main thread, fail now 'cause not sleeping268throw new RuntimeException( whyFailed );269}270theTestPassed = false;271testGeneratedInterrupt = true;272failureMessage = whyFailed;273mainThread.interrupt();274}//fail()275276}// class WindowOpacity277278//This exception is used to exit from any level of call nesting279// when it's determined that the test has passed, and immediately280// end the test.281class TestPassedException extends RuntimeException282{283}284285//*********** End Standard Test Machinery Section **********286287288//************ Begin classes defined for the test ****************289290// if want to make listeners, here is the recommended place for them, then instantiate291// them in init()292293/* Example of a class which may be written as part of a test294class NewClass implements anInterface295{296static int newVar = 0;297298public void eventDispatched(AWTEvent e)299{300//Counting events to see if we get enough301eventCount++;302303if( eventCount == 20 )304{305//got enough events, so pass306307WindowOpacity.pass();308}309else if( tries == 20 )310{311//tried too many times without getting enough events so fail312313WindowOpacity.fail();314}315316}// eventDispatched()317318}// NewClass class319320*/321322323//************** End classes defined for the test *******************324325326327328/****************************************************329Standard Test Machinery330DO NOT modify anything below -- it's a standard331chunk of code whose purpose is to make user332interaction uniform, and thereby make it simpler333to read and understand someone else's test.334****************************************************/335336/**337This is part of the standard test machinery.338It creates a dialog (with the instructions), and is the interface339for sending text messages to the user.340To print the instructions, send an array of strings to Sysout.createDialog341WithInstructions method. Put one line of instructions per array entry.342To display a message for the tester to see, simply call Sysout.println343with the string to be displayed.344This mimics System.out.println but works within the test harness as well345as standalone.346*/347348class Sysout349{350private static TestDialog dialog;351352public static void createDialogWithInstructions( String[] instructions )353{354dialog = new TestDialog( new Frame(), "Instructions" );355dialog.printInstructions( instructions );356dialog.setVisible(true);357println( "Any messages for the tester will display here." );358}359360public static void createDialog( )361{362dialog = new TestDialog( new Frame(), "Instructions" );363String[] defInstr = { "Instructions will appear here. ", "" } ;364dialog.printInstructions( defInstr );365dialog.setVisible(true);366println( "Any messages for the tester will display here." );367}368369370public static void printInstructions( String[] instructions )371{372dialog.printInstructions( instructions );373}374375376public static void println( String messageIn )377{378dialog.displayMessage( messageIn );379System.out.println(messageIn);380}381382}// Sysout class383384/**385This is part of the standard test machinery. It provides a place for the386test instructions to be displayed, and a place for interactive messages387to the user to be displayed.388To have the test instructions displayed, see Sysout.389To have a message to the user be displayed, see Sysout.390Do not call anything in this dialog directly.391*/392class TestDialog extends Dialog393{394395TextArea instructionsText;396TextArea messageText;397int maxStringLength = 80;398399//DO NOT call this directly, go through Sysout400public TestDialog( Frame frame, String name )401{402super( frame, name );403int scrollBoth = TextArea.SCROLLBARS_BOTH;404instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );405add( "North", instructionsText );406407messageText = new TextArea( "", 5, maxStringLength, scrollBoth );408add("Center", messageText);409410pack();411412setVisible(true);413}// TestDialog()414415//DO NOT call this directly, go through Sysout416public void printInstructions( String[] instructions )417{418//Clear out any current instructions419instructionsText.setText( "" );420421//Go down array of instruction strings422423String printStr, remainingStr;424for( int i=0; i < instructions.length; i++ )425{426//chop up each into pieces maxSringLength long427remainingStr = instructions[ i ];428while( remainingStr.length() > 0 )429{430//if longer than max then chop off first max chars to print431if( remainingStr.length() >= maxStringLength )432{433//Try to chop on a word boundary434int posOfSpace = remainingStr.435lastIndexOf( ' ', maxStringLength - 1 );436437if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;438439printStr = remainingStr.substring( 0, posOfSpace + 1 );440remainingStr = remainingStr.substring( posOfSpace + 1 );441}442//else just print443else444{445printStr = remainingStr;446remainingStr = "";447}448449instructionsText.append( printStr + "\n" );450451}// while452453}// for454455}//printInstructions()456457//DO NOT call this directly, go through Sysout458public void displayMessage( String messageIn )459{460messageText.append( messageIn + "\n" );461System.out.println(messageIn);462}463464}// TestDialog class465466467