Path: blob/master/test/jdk/java/awt/Modal/WsDisabledStyle/CloseBlocker/CloseBlocker.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 CloseBlocker29*/3031/**32* ManualMainTest.java33*34* summary: The test opens and closes blocker dialog, the test verifies35* that active window is correct when the dialog is closed.36*/3738import java.awt.*;39import java.awt.event.*;4041public class CloseBlocker42{4344private static void init()45{46//*** Create instructions for the user here ***4748String[] instructions =49{50" the test will be run 6 times, to start next test just close all ",51" windows of previous; the instructions are the same for all tests: ",52" 1) there are two frames (one the frames has 'show modal' button), ",53" 2) press the button to show a dialog, ",54" 3) close the dialog (an alternative scenario - activate another",55" native window before closing the dialog), ",56" 4) the frame with button should become next active window, ",57" if it's true, then the test passed, otherwise, it failed. ",58" Press 'pass' button only after all of the 6 tests are completed, ",59" the number of the currently executed test is displayed on the ",60" output window. "61};62Sysout.createDialog( );63Sysout.printInstructions( instructions );6465test(true, true, false);66test(true, true, true);67test(false, true, false); // 3rd parameter has no affect for ownerless6869test(true, false, false);70test(true, false, true);71test(false, false, false); // 3rd parameter has no affect for ownerless7273}//End init()7475private static final Object obj = new Object();76private static int counter = 0;7778/*79* The ownerless parameter indicates whether the blocker dialog80* has owner. The usual parameter indicates whether the blocker81* dialog is a Java dialog (non-native dialog like file dialog).82*/83private static void test(final boolean ownerless, final boolean usual, final boolean initiallyOwnerIsActive) {8485Sysout.print(" * test #" + (++counter) + " is running ... ");8687final Frame active = new Frame();88final Frame nonactive = new Frame();89Button button = new Button("show modal");90button.addActionListener(new ActionListener() {91public void actionPerformed(ActionEvent ae) {92Dialog dialog = null;93Frame parent = ownerless ? null : (initiallyOwnerIsActive? active : nonactive);94if (usual) {95dialog = new Dialog(parent, "Sample", true);96} else {97dialog = new FileDialog(parent, "Sample", FileDialog.LOAD);98}99dialog.addWindowListener(new WindowAdapter(){100public void windowClosing(WindowEvent e){101e.getWindow().dispose();102}103});104dialog.setBounds(200, 200, 200, 200);105dialog.setVisible(true);106}107});108109active.add(button);110active.setBounds(200, 400, 200, 200);111WindowAdapter adapter = new WindowAdapter(){112public void windowClosing(WindowEvent e){113active.dispose();114nonactive.dispose();115synchronized(obj) {116obj.notify();117}118}119};120active.addWindowListener(adapter);121active.setVisible(true);122123nonactive.setBounds(400, 400, 200, 200);124nonactive.addWindowListener(adapter);125nonactive.setVisible(true);126127synchronized(obj) {128try{129obj.wait();130} catch(Exception e) {131throw new RuntimeException(e);132}133}134135Sysout.println(" completed. ");136137}138139/*****************************************************140* Standard Test Machinery Section141* DO NOT modify anything in this section -- it's a142* standard chunk of code which has all of the143* synchronisation necessary for the test harness.144* By keeping it the same in all tests, it is easier145* to read and understand someone else's test, as146* well as insuring that all tests behave correctly147* with the test harness.148* There is a section following this for test-defined149* classes150******************************************************/151private static boolean theTestPassed = false;152private static boolean testGeneratedInterrupt = false;153private static String failureMessage = "";154155private static Thread mainThread = null;156157private static int sleepTime = 300000;158159public static void main( String args[] ) throws InterruptedException160{161mainThread = Thread.currentThread();162try163{164init();165}166catch( TestPassedException e )167{168//The test passed, so just return from main and harness will169// interepret this return as a pass170return;171}172//At this point, neither test passed nor test failed has been173// called -- either would have thrown an exception and ended the174// test, so we know we have multiple threads.175176//Test involves other threads, so sleep and wait for them to177// called pass() or fail()178try179{180Thread.sleep( sleepTime );181//Timed out, so fail the test182throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );183}184catch (InterruptedException e)185{186if( ! testGeneratedInterrupt ) throw e;187188//reset flag in case hit this code more than once for some reason (just safety)189testGeneratedInterrupt = false;190if ( 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}216//pass was called from a different thread, so set the flag and interrupt217// the main thead.218theTestPassed = true;219testGeneratedInterrupt = true;220mainThread.interrupt();221}//pass()222223public static synchronized void fail()224{225//test writer didn't specify why test failed, so give generic226fail( "it just plain failed! :-)" );227}228229public static synchronized void fail( String whyFailed )230{231Sysout.println( "The test failed: " + whyFailed );232Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );233//check if this called from main thread234if ( mainThread == Thread.currentThread() )235{236//If main thread, fail now 'cause not sleeping237throw new RuntimeException( whyFailed );238}239theTestPassed = false;240testGeneratedInterrupt = true;241failureMessage = whyFailed;242mainThread.interrupt();243}//fail()244245}// class ManualMainTest246247//This exception is used to exit from any level of call nesting248// when it's determined that the test has passed, and immediately249// end the test.250class TestPassedException extends RuntimeException251{252}253254//*********** End Standard Test Machinery Section **********255256257//************ Begin classes defined for the test ****************258259// make listeners in a class defined here, and instantiate them in init()260261/* Example of a class which may be written as part of a test262class NewClass implements anInterface263{264static int newVar = 0;265266public void eventDispatched(AWTEvent e)267{268//Counting events to see if we get enough269eventCount++;270271if( eventCount == 20 )272{273//got enough events, so pass274275ManualMainTest.pass();276}277else if( tries == 20 )278{279//tried too many times without getting enough events so fail280281ManualMainTest.fail();282}283284}// eventDispatched()285286}// NewClass class287288*/289290291//************** End classes defined for the test *******************292293294295296/****************************************************297Standard Test Machinery298DO NOT modify anything below -- it's a standard299chunk of code whose purpose is to make user300interaction uniform, and thereby make it simpler301to read and understand someone else's test.302****************************************************/303304/**305This is part of the standard test machinery.306It creates a dialog (with the instructions), and is the interface307for sending text messages to the user.308To print the instructions, send an array of strings to Sysout.createDialog309WithInstructions method. Put one line of instructions per array entry.310To display a message for the tester to see, simply call Sysout.println311with the string to be displayed.312This mimics System.out.println but works within the test harness as well313as standalone.314*/315316class Sysout317{318private static TestDialog dialog;319320public static void createDialogWithInstructions( String[] instructions )321{322dialog = new TestDialog( new Frame(), "Instructions" );323dialog.printInstructions( instructions );324dialog.setVisible(true);325println( "Any messages for the tester will display here." );326}327328public static void createDialog( )329{330dialog = new TestDialog( new Frame(), "Instructions" );331String[] defInstr = { "Instructions will appear here. ", "" } ;332dialog.printInstructions( defInstr );333dialog.setVisible(true);334println( "Any messages for the tester will display here." );335}336337338public static void printInstructions( String[] instructions )339{340dialog.printInstructions( instructions );341}342343344public static void println( String messageIn )345{346dialog.displayMessage( messageIn, true );347}348349public static void print( String messageIn )350{351dialog.displayMessage( messageIn, false );352}353354}// Sysout class355356/**357This is part of the standard test machinery. It provides a place for the358test instructions to be displayed, and a place for interactive messages359to the user to be displayed.360To have the test instructions displayed, see Sysout.361To have a message to the user be displayed, see Sysout.362Do not call anything in this dialog directly.363*/364class TestDialog extends Dialog implements ActionListener365{366367TextArea instructionsText;368TextArea messageText;369int maxStringLength = 80;370Panel buttonP = new Panel();371Button passB = new Button( "pass" );372Button failB = new Button( "fail" );373374//DO NOT call this directly, go through Sysout375public TestDialog( Frame frame, String name )376{377super( frame, name );378int scrollBoth = TextArea.SCROLLBARS_BOTH;379instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );380add( "North", instructionsText );381382messageText = new TextArea( "", 5, maxStringLength, scrollBoth );383add("Center", messageText);384385passB = new Button( "pass" );386passB.setActionCommand( "pass" );387passB.addActionListener( this );388buttonP.add( "East", passB );389390failB = new Button( "fail" );391failB.setActionCommand( "fail" );392failB.addActionListener( this );393buttonP.add( "West", failB );394395add( "South", buttonP );396pack();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, boolean nextLine )445{446messageText.append( messageIn + (nextLine? "\n" : "") );447System.out.println(messageIn);448}449450//catch presses of the passed and failed buttons.451//simply call the standard pass() or fail() static methods of452//ManualMainTest453public void actionPerformed( ActionEvent e )454{455if( e.getActionCommand() == "pass" )456{457CloseBlocker.pass();458}459else460{461CloseBlocker.fail();462}463}464465}// TestDialog class466467468