Path: blob/master/test/jdk/java/awt/Modal/WsDisabledStyle/Winkey/Winkey.java
41159 views
/*1* Copyright (c) 2007, 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 6572263 6571808 800592026@summary PIT:FileDialog minimized to taskbar(through 'Show Desktop')selecting the fileDialog using windowList27@author dmitry.cherepanov: area=awt.modal28@run main/manual Winkey29*/3031/**32* Winkey.java33*34* summary: the test verifies that pressing combination of Windows key35* and M key to minimize all windows doesn't break AWT modality36*/3738import java.awt.*;39import java.awt.event.*;4041public class Winkey42{4344private static void init()45{46//*** Create instructions for the user here ***4748String[] instructions =49{50" 0. This test is for MS Windows only, if you use other OS, press \"pass\" button.",51" 1. there is a frame with a 'show modal' button, ",52" 2. press the button to show a modal dialog, ",53" 3. the modal dialog will be shown over the frame, ",54" 4. please verify that all (5.1, 5.2.1, 5.2.2) the following tests pass: ",55" ",56" 5.1. press combination Windows Key and M key to minimize all windows, ",57" note that the modal dialog and modal blocked windows are NOT minimized",58" 5.2. press combination Windows Key and D key to show desktop, ",59" 5.2.1. restore the dialog by choosing this one in the ALT-TAB list, ",60" 5.2.2. restore the dialog by mouse click on taskbar (on java or any other item)",61" ",62" 6. make sure that the dialog and the frame are visible, ",63" the bounds of the windows should be the same as before, ",64" if it's true, then the test passed; otherwise, it failed. "65};66Sysout.createDialog( );67Sysout.printInstructions( instructions );6869final Frame frame = new Frame();70Button button = new Button("show modal");71button.addActionListener(new ActionListener() {72public void actionPerformed(ActionEvent ae) {73FileDialog dialog = new FileDialog((Frame)null, "Sample", FileDialog.LOAD);74dialog.setVisible(true);75}76});77frame.add(button);78frame.setBounds(400, 400, 200, 200);79frame.setVisible(true);8081}//End init()8283/*****************************************************84* Standard Test Machinery Section85* DO NOT modify anything in this section -- it's a86* standard chunk of code which has all of the87* synchronisation necessary for the test harness.88* By keeping it the same in all tests, it is easier89* to read and understand someone else's test, as90* well as insuring that all tests behave correctly91* with the test harness.92* There is a section following this for test-defined93* classes94******************************************************/95private static boolean theTestPassed = false;96private static boolean testGeneratedInterrupt = false;97private static String failureMessage = "";9899private static Thread mainThread = null;100101private static int sleepTime = 300000;102103public static void main( String args[] ) throws InterruptedException104{105mainThread = Thread.currentThread();106try107{108init();109}110catch( TestPassedException e )111{112//The test passed, so just return from main and harness will113// interepret this return as a pass114return;115}116//At this point, neither test passed nor test failed has been117// called -- either would have thrown an exception and ended the118// test, so we know we have multiple threads.119120//Test involves other threads, so sleep and wait for them to121// called pass() or fail()122try123{124Thread.sleep( sleepTime );125//Timed out, so fail the test126throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );127}128catch (InterruptedException e)129{130if( ! testGeneratedInterrupt ) throw e;131132//reset flag in case hit this code more than once for some reason (just safety)133testGeneratedInterrupt = false;134if ( theTestPassed == false )135{136throw new RuntimeException( failureMessage );137}138}139140}//main141142public static synchronized void setTimeoutTo( int seconds )143{144sleepTime = seconds * 1000;145}146147public static synchronized void pass()148{149Sysout.println( "The test passed." );150Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );151//first check if this is executing in main thread152if ( mainThread == Thread.currentThread() )153{154//Still in the main thread, so set the flag just for kicks,155// and throw a test passed exception which will be caught156// and end the test.157theTestPassed = true;158throw new TestPassedException();159}160//pass was called from a different thread, so set the flag and interrupt161// the main thead.162theTestPassed = true;163testGeneratedInterrupt = true;164mainThread.interrupt();165}//pass()166167public static synchronized void fail()168{169//test writer didn't specify why test failed, so give generic170fail( "it just plain failed! :-)" );171}172173public static synchronized void fail( String whyFailed )174{175Sysout.println( "The test failed: " + whyFailed );176Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );177//check if this called from main thread178if ( mainThread == Thread.currentThread() )179{180//If main thread, fail now 'cause not sleeping181throw new RuntimeException( whyFailed );182}183theTestPassed = false;184testGeneratedInterrupt = true;185failureMessage = whyFailed;186mainThread.interrupt();187}//fail()188189}// class ManualMainTest190191//This exception is used to exit from any level of call nesting192// when it's determined that the test has passed, and immediately193// end the test.194class TestPassedException extends RuntimeException195{196}197198//*********** End Standard Test Machinery Section **********199200201//************ Begin classes defined for the test ****************202203// make listeners in a class defined here, and instantiate them in init()204205/* Example of a class which may be written as part of a test206class NewClass implements anInterface207{208static int newVar = 0;209210public void eventDispatched(AWTEvent e)211{212//Counting events to see if we get enough213eventCount++;214215if( eventCount == 20 )216{217//got enough events, so pass218219ManualMainTest.pass();220}221else if( tries == 20 )222{223//tried too many times without getting enough events so fail224225ManualMainTest.fail();226}227228}// eventDispatched()229230}// NewClass class231232*/233234235//************** End classes defined for the test *******************236237238239240/****************************************************241Standard Test Machinery242DO NOT modify anything below -- it's a standard243chunk of code whose purpose is to make user244interaction uniform, and thereby make it simpler245to read and understand someone else's test.246****************************************************/247248/**249This is part of the standard test machinery.250It creates a dialog (with the instructions), and is the interface251for sending text messages to the user.252To print the instructions, send an array of strings to Sysout.createDialog253WithInstructions method. Put one line of instructions per array entry.254To display a message for the tester to see, simply call Sysout.println255with the string to be displayed.256This mimics System.out.println but works within the test harness as well257as standalone.258*/259260class Sysout261{262private static TestDialog dialog;263264public static void createDialogWithInstructions( String[] instructions )265{266dialog = new TestDialog( new Frame(), "Instructions" );267dialog.printInstructions( instructions );268dialog.setVisible(true);269println( "Any messages for the tester will display here." );270}271272public static void createDialog( )273{274dialog = new TestDialog( new Frame(), "Instructions" );275String[] defInstr = { "Instructions will appear here. ", "" } ;276dialog.printInstructions( defInstr );277dialog.setVisible(true);278println( "Any messages for the tester will display here." );279}280281282public static void printInstructions( String[] instructions )283{284dialog.printInstructions( instructions );285}286287288public static void println( String messageIn )289{290dialog.displayMessage( messageIn );291}292293}// Sysout class294295/**296This is part of the standard test machinery. It provides a place for the297test instructions to be displayed, and a place for interactive messages298to the user to be displayed.299To have the test instructions displayed, see Sysout.300To have a message to the user be displayed, see Sysout.301Do not call anything in this dialog directly.302*/303class TestDialog extends Dialog implements ActionListener304{305306TextArea instructionsText;307TextArea messageText;308int maxStringLength = 80;309Panel buttonP = new Panel();310Button passB = new Button( "pass" );311Button failB = new Button( "fail" );312313//DO NOT call this directly, go through Sysout314public TestDialog( Frame frame, String name )315{316super( frame, name );317int scrollBoth = TextArea.SCROLLBARS_BOTH;318instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );319add( "North", instructionsText );320321messageText = new TextArea( "", 5, maxStringLength, scrollBoth );322add("Center", messageText);323324passB = new Button( "pass" );325passB.setActionCommand( "pass" );326passB.addActionListener( this );327buttonP.add( "East", passB );328329failB = new Button( "fail" );330failB.setActionCommand( "fail" );331failB.addActionListener( this );332buttonP.add( "West", failB );333334add( "South", buttonP );335pack();336337setVisible(true);338}// TestDialog()339340//DO NOT call this directly, go through Sysout341public void printInstructions( String[] instructions )342{343//Clear out any current instructions344instructionsText.setText( "" );345346//Go down array of instruction strings347348String printStr, remainingStr;349for( int i=0; i < instructions.length; i++ )350{351//chop up each into pieces maxSringLength long352remainingStr = instructions[ i ];353while( remainingStr.length() > 0 )354{355//if longer than max then chop off first max chars to print356if( remainingStr.length() >= maxStringLength )357{358//Try to chop on a word boundary359int posOfSpace = remainingStr.360lastIndexOf( ' ', maxStringLength - 1 );361362if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;363364printStr = remainingStr.substring( 0, posOfSpace + 1 );365remainingStr = remainingStr.substring( posOfSpace + 1 );366}367//else just print368else369{370printStr = remainingStr;371remainingStr = "";372}373374instructionsText.append( printStr + "\n" );375376}// while377378}// for379380}//printInstructions()381382//DO NOT call this directly, go through Sysout383public void displayMessage( String messageIn )384{385messageText.append( messageIn + "\n" );386System.out.println(messageIn);387}388389//catch presses of the passed and failed buttons.390//simply call the standard pass() or fail() static methods of391//ManualMainTest392public void actionPerformed( ActionEvent e )393{394if( e.getActionCommand() == "pass" )395{396Winkey.pass();397}398else399{400Winkey.fail();401}402}403404}// TestDialog class405406407