Path: blob/master/test/jdk/java/awt/Modal/WsDisabledStyle/OverBlocker/OverBlocker.java
41159 views
/*1* Copyright (c) 2007, 2008, 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 %I% %E%25@bug 408002926@summary Modal Dialog block input to all frame windows not just its parent.27@author dmitry.cherepanov: area=awt.modal28@run main/manual OverBlocker29*/3031/**32* OverBlocker.java33*34* summary: The test verifies that if user tries to activate the blocked dialog35* then the blocker dialog appears over the other windows36*/3738import java.awt.*;39import java.awt.event.*;4041public class OverBlocker42{4344private static void init()45{46//*** Create instructions for the user here ***4748String[] instructions =49{50" the test will be run 4 times, to start next test just close all ",51" windows of previous; the instructions are the same for all tests: ",52" 1) there is a frame with 'show modal' button, ",53" 2) press the button to show a dialog, ",54" 3) activate any non-Java application, move the app over the dialog, ",55" 4) click on the frame by mouse, ",56" 5) make sure that the dialog comes up from the application and ",57" now the dialog overlaps the app as well as the frame, ",58" if it's true, then the test passed, otherwise, it failed. ",59" Press 'pass' button only after all of the 4 tests are completed, ",60" the number of the currently executed test is displayed on the ",61" output window. "62};63Sysout.createDialog( );64Sysout.printInstructions( instructions );6566test(false, true);67test(true, true);68test(true, false);69test(false, false);7071}//End init()7273private static final Object obj = new Object();74private static int counter = 0;7576/*77* The ownerless parameter indicates whether the blocker dialog78* has owner. The usual parameter indicates whether the blocker79* dialog is a Java dialog (non-native dialog like file dialog).80*/81private static void test(final boolean ownerless, final boolean usual) {8283Sysout.print(" * test #" + (++counter) + " is running ... ");8485final Frame frame = new Frame();86Button button = new Button("show modal");87button.addActionListener(new ActionListener() {88public void actionPerformed(ActionEvent ae) {89Dialog dialog = null;90Frame parent = ownerless ? null : frame;91if (usual) {92dialog = new Dialog(parent, "Sample", true);93} else {94dialog = new FileDialog(parent, "Sample", FileDialog.LOAD);95}96dialog.addWindowListener(new WindowAdapter(){97public void windowClosing(WindowEvent e){98e.getWindow().dispose();99}100});101dialog.setBounds(200, 200, 200, 200);102dialog.setVisible(true);103}104});105frame.add(button);106frame.setBounds(400, 400, 200, 200);107frame.addWindowListener(new WindowAdapter(){108public void windowClosing(WindowEvent e){109e.getWindow().dispose();110synchronized(obj) {111obj.notify();112}113}114});115frame.setVisible(true);116117synchronized(obj) {118try{119obj.wait();120} catch(Exception e) {121throw new RuntimeException(e);122}123}124125Sysout.println(" completed. ");126127}128129/*****************************************************130* Standard Test Machinery Section131* DO NOT modify anything in this section -- it's a132* standard chunk of code which has all of the133* synchronisation necessary for the test harness.134* By keeping it the same in all tests, it is easier135* to read and understand someone else's test, as136* well as insuring that all tests behave correctly137* with the test harness.138* There is a section following this for test-defined139* classes140******************************************************/141private static boolean theTestPassed = false;142private static boolean testGeneratedInterrupt = false;143private static String failureMessage = "";144145private static Thread mainThread = null;146147private static int sleepTime = 300000;148149public static void main( String args[] ) throws InterruptedException150{151mainThread = Thread.currentThread();152try153{154init();155}156catch( TestPassedException e )157{158//The test passed, so just return from main and harness will159// interepret this return as a pass160return;161}162//At this point, neither test passed nor test failed has been163// called -- either would have thrown an exception and ended the164// test, so we know we have multiple threads.165166//Test involves other threads, so sleep and wait for them to167// called pass() or fail()168try169{170Thread.sleep( sleepTime );171//Timed out, so fail the test172throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );173}174catch (InterruptedException e)175{176if( ! testGeneratedInterrupt ) throw e;177178//reset flag in case hit this code more than once for some reason (just safety)179testGeneratedInterrupt = false;180if ( theTestPassed == false )181{182throw new RuntimeException( failureMessage );183}184}185186}//main187188public static synchronized void setTimeoutTo( int seconds )189{190sleepTime = seconds * 1000;191}192193public static synchronized void pass()194{195Sysout.println( "The test passed." );196Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );197//first check if this is executing in main thread198if ( mainThread == Thread.currentThread() )199{200//Still in the main thread, so set the flag just for kicks,201// and throw a test passed exception which will be caught202// and end the test.203theTestPassed = true;204throw new TestPassedException();205}206//pass was called from a different thread, so set the flag and interrupt207// the main thead.208theTestPassed = true;209testGeneratedInterrupt = true;210mainThread.interrupt();211}//pass()212213public static synchronized void fail()214{215//test writer didn't specify why test failed, so give generic216fail( "it just plain failed! :-)" );217}218219public static synchronized void fail( String whyFailed )220{221Sysout.println( "The test failed: " + whyFailed );222Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );223//check if this called from main thread224if ( mainThread == Thread.currentThread() )225{226//If main thread, fail now 'cause not sleeping227throw new RuntimeException( whyFailed );228}229theTestPassed = false;230testGeneratedInterrupt = true;231failureMessage = whyFailed;232mainThread.interrupt();233}//fail()234235}// class ManualMainTest236237//This exception is used to exit from any level of call nesting238// when it's determined that the test has passed, and immediately239// end the test.240class TestPassedException extends RuntimeException241{242}243244//*********** End Standard Test Machinery Section **********245246247//************ Begin classes defined for the test ****************248249// make listeners in a class defined here, and instantiate them in init()250251/* Example of a class which may be written as part of a test252class NewClass implements anInterface253{254static int newVar = 0;255256public void eventDispatched(AWTEvent e)257{258//Counting events to see if we get enough259eventCount++;260261if( eventCount == 20 )262{263//got enough events, so pass264265ManualMainTest.pass();266}267else if( tries == 20 )268{269//tried too many times without getting enough events so fail270271ManualMainTest.fail();272}273274}// eventDispatched()275276}// NewClass class277278*/279280281//************** End classes defined for the test *******************282283284285286/****************************************************287Standard Test Machinery288DO NOT modify anything below -- it's a standard289chunk of code whose purpose is to make user290interaction uniform, and thereby make it simpler291to read and understand someone else's test.292****************************************************/293294/**295This is part of the standard test machinery.296It creates a dialog (with the instructions), and is the interface297for sending text messages to the user.298To print the instructions, send an array of strings to Sysout.createDialog299WithInstructions method. Put one line of instructions per array entry.300To display a message for the tester to see, simply call Sysout.println301with the string to be displayed.302This mimics System.out.println but works within the test harness as well303as standalone.304*/305306class Sysout307{308private static TestDialog dialog;309310public static void createDialogWithInstructions( String[] instructions )311{312dialog = new TestDialog( new Frame(), "Instructions" );313dialog.printInstructions( instructions );314dialog.setVisible(true);315println( "Any messages for the tester will display here." );316}317318public static void createDialog( )319{320dialog = new TestDialog( new Frame(), "Instructions" );321String[] defInstr = { "Instructions will appear here. ", "" } ;322dialog.printInstructions( defInstr );323dialog.setVisible(true);324println( "Any messages for the tester will display here." );325}326327328public static void printInstructions( String[] instructions )329{330dialog.printInstructions( instructions );331}332333334public static void println( String messageIn )335{336dialog.displayMessage( messageIn, true );337}338339public static void print( String messageIn )340{341dialog.displayMessage( messageIn, false );342}343344}// Sysout class345346/**347This is part of the standard test machinery. It provides a place for the348test instructions to be displayed, and a place for interactive messages349to the user to be displayed.350To have the test instructions displayed, see Sysout.351To have a message to the user be displayed, see Sysout.352Do not call anything in this dialog directly.353*/354class TestDialog extends Dialog implements ActionListener355{356357TextArea instructionsText;358TextArea messageText;359int maxStringLength = 80;360Panel buttonP = new Panel();361Button passB = new Button( "pass" );362Button failB = new Button( "fail" );363364//DO NOT call this directly, go through Sysout365public TestDialog( Frame frame, String name )366{367super( frame, name );368int scrollBoth = TextArea.SCROLLBARS_BOTH;369instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );370add( "North", instructionsText );371372messageText = new TextArea( "", 5, maxStringLength, scrollBoth );373add("Center", messageText);374375passB = new Button( "pass" );376passB.setActionCommand( "pass" );377passB.addActionListener( this );378buttonP.add( "East", passB );379380failB = new Button( "fail" );381failB.setActionCommand( "fail" );382failB.addActionListener( this );383buttonP.add( "West", failB );384385add( "South", buttonP );386pack();387388setVisible(true);389}// TestDialog()390391//DO NOT call this directly, go through Sysout392public void printInstructions( String[] instructions )393{394//Clear out any current instructions395instructionsText.setText( "" );396397//Go down array of instruction strings398399String printStr, remainingStr;400for( int i=0; i < instructions.length; i++ )401{402//chop up each into pieces maxSringLength long403remainingStr = instructions[ i ];404while( remainingStr.length() > 0 )405{406//if longer than max then chop off first max chars to print407if( remainingStr.length() >= maxStringLength )408{409//Try to chop on a word boundary410int posOfSpace = remainingStr.411lastIndexOf( ' ', maxStringLength - 1 );412413if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;414415printStr = remainingStr.substring( 0, posOfSpace + 1 );416remainingStr = remainingStr.substring( posOfSpace + 1 );417}418//else just print419else420{421printStr = remainingStr;422remainingStr = "";423}424425instructionsText.append( printStr + "\n" );426427}// while428429}// for430431}//printInstructions()432433//DO NOT call this directly, go through Sysout434public void displayMessage( String messageIn, boolean nextLine )435{436messageText.append( messageIn + (nextLine? "\n" : "") );437System.out.println(messageIn);438}439440//catch presses of the passed and failed buttons.441//simply call the standard pass() or fail() static methods of442//ManualMainTest443public void actionPerformed( ActionEvent e )444{445if( e.getActionCommand() == "pass" )446{447OverBlocker.pass();448}449else450{451OverBlocker.fail();452}453}454455}// TestDialog class456457458