99N/A/*
2362N/A * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
99N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
99N/A *
99N/A * This code is free software; you can redistribute it and/or modify it
99N/A * under the terms of the GNU General Public License version 2 only, as
99N/A * published by the Free Software Foundation.
99N/A *
99N/A * This code is distributed in the hope that it will be useful, but WITHOUT
99N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
99N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
99N/A * version 2 for more details (a copy is included in the LICENSE file that
99N/A * accompanied this code).
99N/A *
99N/A * You should have received a copy of the GNU General Public License version
99N/A * 2 along with this work; if not, write to the Free Software Foundation,
99N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
99N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
99N/A */
99N/A
99N/A/*
99N/A @test %I% %E%
99N/A @bug 4080029
99N/A @summary Modal Dialog block input to all frame windows not just its parent.
99N/A @author dmitry.cherepanov: area=awt.modal
99N/A @run main/manual OverBlocker
99N/A*/
99N/A
99N/A/**
99N/A * OverBlocker.java
99N/A *
99N/A * summary: The test verifies that if user tries to activate the blocked dialog
99N/A * then the blocker dialog appears over the other windows
99N/A */
99N/A
99N/Aimport java.awt.*;
99N/Aimport java.awt.event.*;
99N/A
99N/Apublic class OverBlocker
99N/A{
99N/A
99N/A private static void init()
99N/A {
99N/A //*** Create instructions for the user here ***
99N/A
99N/A String[] instructions =
99N/A {
99N/A " the test will be run 4 times, to start next test just close all ",
99N/A " windows of previous; the instructions are the same for all tests: ",
99N/A " 1) there is a frame with 'show modal' button, ",
99N/A " 2) press the button to show a dialog, ",
99N/A " 3) activate any non-Java application, move the app over the dialog, ",
99N/A " 4) click on the frame by mouse, ",
99N/A " 5) make sure that the dialog comes up from the application and ",
99N/A " now the dialog overlaps the app as well as the frame, ",
99N/A " if it's true, then the test passed, otherwise, it failed. ",
99N/A " Press 'pass' button only after all of the 4 tests are completed, ",
99N/A " the number of the currently executed test is displayed on the ",
99N/A " output window. "
99N/A };
99N/A Sysout.createDialog( );
99N/A Sysout.printInstructions( instructions );
99N/A
99N/A test(false, true);
99N/A test(true, true);
99N/A test(true, false);
99N/A test(false, false);
99N/A
99N/A }//End init()
99N/A
99N/A private static final Object obj = new Object();
99N/A private static int counter = 0;
99N/A
99N/A /*
99N/A * The ownerless parameter indicates whether the blocker dialog
99N/A * has owner. The usual parameter indicates whether the blocker
99N/A * dialog is a Java dialog (non-native dialog like file dialog).
99N/A */
99N/A private static void test(final boolean ownerless, final boolean usual) {
99N/A
99N/A Sysout.print(" * test #" + (++counter) + " is running ... ");
99N/A
99N/A final Frame frame = new Frame();
99N/A Button button = new Button("show modal");
99N/A button.addActionListener(new ActionListener() {
99N/A public void actionPerformed(ActionEvent ae) {
99N/A Dialog dialog = null;
99N/A Frame parent = ownerless ? null : frame;
99N/A if (usual) {
99N/A dialog = new Dialog(parent, "Sample", true);
99N/A } else {
99N/A dialog = new FileDialog(parent, "Sample", FileDialog.LOAD);
99N/A }
99N/A dialog.addWindowListener(new WindowAdapter(){
99N/A public void windowClosing(WindowEvent e){
99N/A e.getWindow().dispose();
99N/A }
99N/A });
99N/A dialog.setBounds(200, 200, 200, 200);
99N/A dialog.setVisible(true);
99N/A }
99N/A });
99N/A frame.add(button);
99N/A frame.setBounds(400, 400, 200, 200);
99N/A frame.addWindowListener(new WindowAdapter(){
99N/A public void windowClosing(WindowEvent e){
99N/A e.getWindow().dispose();
99N/A synchronized(obj) {
99N/A obj.notify();
99N/A }
99N/A }
99N/A });
99N/A frame.setVisible(true);
99N/A
99N/A synchronized(obj) {
99N/A try{
99N/A obj.wait();
99N/A } catch(Exception e) {
99N/A throw new RuntimeException(e);
99N/A }
99N/A }
99N/A
99N/A Sysout.println(" completed. ");
99N/A
99N/A }
99N/A
99N/A /*****************************************************
99N/A * Standard Test Machinery Section
99N/A * DO NOT modify anything in this section -- it's a
99N/A * standard chunk of code which has all of the
99N/A * synchronisation necessary for the test harness.
99N/A * By keeping it the same in all tests, it is easier
99N/A * to read and understand someone else's test, as
99N/A * well as insuring that all tests behave correctly
99N/A * with the test harness.
99N/A * There is a section following this for test-defined
99N/A * classes
99N/A ******************************************************/
99N/A private static boolean theTestPassed = false;
99N/A private static boolean testGeneratedInterrupt = false;
99N/A private static String failureMessage = "";
99N/A
99N/A private static Thread mainThread = null;
99N/A
99N/A private static int sleepTime = 300000;
99N/A
99N/A public static void main( String args[] ) throws InterruptedException
99N/A {
99N/A mainThread = Thread.currentThread();
99N/A try
99N/A {
99N/A init();
99N/A }
99N/A catch( TestPassedException e )
99N/A {
99N/A //The test passed, so just return from main and harness will
99N/A // interepret this return as a pass
99N/A return;
99N/A }
99N/A //At this point, neither test passed nor test failed has been
99N/A // called -- either would have thrown an exception and ended the
99N/A // test, so we know we have multiple threads.
99N/A
99N/A //Test involves other threads, so sleep and wait for them to
99N/A // called pass() or fail()
99N/A try
99N/A {
99N/A Thread.sleep( sleepTime );
99N/A //Timed out, so fail the test
99N/A throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );
99N/A }
99N/A catch (InterruptedException e)
99N/A {
99N/A if( ! testGeneratedInterrupt ) throw e;
99N/A
99N/A //reset flag in case hit this code more than once for some reason (just safety)
99N/A testGeneratedInterrupt = false;
99N/A if ( theTestPassed == false )
99N/A {
99N/A throw new RuntimeException( failureMessage );
99N/A }
99N/A }
99N/A
99N/A }//main
99N/A
99N/A public static synchronized void setTimeoutTo( int seconds )
99N/A {
99N/A sleepTime = seconds * 1000;
99N/A }
99N/A
99N/A public static synchronized void pass()
99N/A {
99N/A Sysout.println( "The test passed." );
99N/A Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );
99N/A //first check if this is executing in main thread
99N/A if ( mainThread == Thread.currentThread() )
99N/A {
99N/A //Still in the main thread, so set the flag just for kicks,
99N/A // and throw a test passed exception which will be caught
99N/A // and end the test.
99N/A theTestPassed = true;
99N/A throw new TestPassedException();
99N/A }
99N/A //pass was called from a different thread, so set the flag and interrupt
99N/A // the main thead.
99N/A theTestPassed = true;
99N/A testGeneratedInterrupt = true;
99N/A mainThread.interrupt();
99N/A }//pass()
99N/A
99N/A public static synchronized void fail()
99N/A {
99N/A //test writer didn't specify why test failed, so give generic
99N/A fail( "it just plain failed! :-)" );
99N/A }
99N/A
99N/A public static synchronized void fail( String whyFailed )
99N/A {
99N/A Sysout.println( "The test failed: " + whyFailed );
99N/A Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );
99N/A //check if this called from main thread
99N/A if ( mainThread == Thread.currentThread() )
99N/A {
99N/A //If main thread, fail now 'cause not sleeping
99N/A throw new RuntimeException( whyFailed );
99N/A }
99N/A theTestPassed = false;
99N/A testGeneratedInterrupt = true;
99N/A failureMessage = whyFailed;
99N/A mainThread.interrupt();
99N/A }//fail()
99N/A
99N/A}// class ManualMainTest
99N/A
99N/A//This exception is used to exit from any level of call nesting
99N/A// when it's determined that the test has passed, and immediately
99N/A// end the test.
99N/Aclass TestPassedException extends RuntimeException
99N/A{
99N/A}
99N/A
99N/A//*********** End Standard Test Machinery Section **********
99N/A
99N/A
99N/A//************ Begin classes defined for the test ****************
99N/A
99N/A// make listeners in a class defined here, and instantiate them in init()
99N/A
99N/A/* Example of a class which may be written as part of a test
99N/Aclass NewClass implements anInterface
99N/A {
99N/A static int newVar = 0;
99N/A
99N/A public void eventDispatched(AWTEvent e)
99N/A {
99N/A //Counting events to see if we get enough
99N/A eventCount++;
99N/A
99N/A if( eventCount == 20 )
99N/A {
99N/A //got enough events, so pass
99N/A
99N/A ManualMainTest.pass();
99N/A }
99N/A else if( tries == 20 )
99N/A {
99N/A //tried too many times without getting enough events so fail
99N/A
99N/A ManualMainTest.fail();
99N/A }
99N/A
99N/A }// eventDispatched()
99N/A
99N/A }// NewClass class
99N/A
99N/A*/
99N/A
99N/A
99N/A//************** End classes defined for the test *******************
99N/A
99N/A
99N/A
99N/A
99N/A/****************************************************
99N/A Standard Test Machinery
99N/A DO NOT modify anything below -- it's a standard
99N/A chunk of code whose purpose is to make user
99N/A interaction uniform, and thereby make it simpler
99N/A to read and understand someone else's test.
99N/A ****************************************************/
99N/A
99N/A/**
99N/A This is part of the standard test machinery.
99N/A It creates a dialog (with the instructions), and is the interface
99N/A for sending text messages to the user.
99N/A To print the instructions, send an array of strings to Sysout.createDialog
99N/A WithInstructions method. Put one line of instructions per array entry.
99N/A To display a message for the tester to see, simply call Sysout.println
99N/A with the string to be displayed.
99N/A This mimics System.out.println but works within the test harness as well
99N/A as standalone.
99N/A */
99N/A
99N/Aclass Sysout
99N/A{
99N/A private static TestDialog dialog;
99N/A
99N/A public static void createDialogWithInstructions( String[] instructions )
99N/A {
99N/A dialog = new TestDialog( new Frame(), "Instructions" );
99N/A dialog.printInstructions( instructions );
99N/A dialog.setVisible(true);
99N/A println( "Any messages for the tester will display here." );
99N/A }
99N/A
99N/A public static void createDialog( )
99N/A {
99N/A dialog = new TestDialog( new Frame(), "Instructions" );
99N/A String[] defInstr = { "Instructions will appear here. ", "" } ;
99N/A dialog.printInstructions( defInstr );
99N/A dialog.setVisible(true);
99N/A println( "Any messages for the tester will display here." );
99N/A }
99N/A
99N/A
99N/A public static void printInstructions( String[] instructions )
99N/A {
99N/A dialog.printInstructions( instructions );
99N/A }
99N/A
99N/A
99N/A public static void println( String messageIn )
99N/A {
99N/A dialog.displayMessage( messageIn, true );
99N/A }
99N/A
99N/A public static void print( String messageIn )
99N/A {
99N/A dialog.displayMessage( messageIn, false );
99N/A }
99N/A
99N/A}// Sysout class
99N/A
99N/A/**
99N/A This is part of the standard test machinery. It provides a place for the
99N/A test instructions to be displayed, and a place for interactive messages
99N/A to the user to be displayed.
99N/A To have the test instructions displayed, see Sysout.
99N/A To have a message to the user be displayed, see Sysout.
99N/A Do not call anything in this dialog directly.
99N/A */
99N/Aclass TestDialog extends Dialog implements ActionListener
99N/A{
99N/A
99N/A TextArea instructionsText;
99N/A TextArea messageText;
99N/A int maxStringLength = 80;
99N/A Panel buttonP = new Panel();
99N/A Button passB = new Button( "pass" );
99N/A Button failB = new Button( "fail" );
99N/A
99N/A //DO NOT call this directly, go through Sysout
99N/A public TestDialog( Frame frame, String name )
99N/A {
99N/A super( frame, name );
99N/A int scrollBoth = TextArea.SCROLLBARS_BOTH;
99N/A instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
99N/A add( "North", instructionsText );
99N/A
99N/A messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
99N/A add("Center", messageText);
99N/A
99N/A passB = new Button( "pass" );
99N/A passB.setActionCommand( "pass" );
99N/A passB.addActionListener( this );
99N/A buttonP.add( "East", passB );
99N/A
99N/A failB = new Button( "fail" );
99N/A failB.setActionCommand( "fail" );
99N/A failB.addActionListener( this );
99N/A buttonP.add( "West", failB );
99N/A
99N/A add( "South", buttonP );
99N/A pack();
99N/A
99N/A setVisible(true);
99N/A }// TestDialog()
99N/A
99N/A //DO NOT call this directly, go through Sysout
99N/A public void printInstructions( String[] instructions )
99N/A {
99N/A //Clear out any current instructions
99N/A instructionsText.setText( "" );
99N/A
99N/A //Go down array of instruction strings
99N/A
99N/A String printStr, remainingStr;
99N/A for( int i=0; i < instructions.length; i++ )
99N/A {
99N/A //chop up each into pieces maxSringLength long
99N/A remainingStr = instructions[ i ];
99N/A while( remainingStr.length() > 0 )
99N/A {
99N/A //if longer than max then chop off first max chars to print
99N/A if( remainingStr.length() >= maxStringLength )
99N/A {
99N/A //Try to chop on a word boundary
99N/A int posOfSpace = remainingStr.
99N/A lastIndexOf( ' ', maxStringLength - 1 );
99N/A
99N/A if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
99N/A
99N/A printStr = remainingStr.substring( 0, posOfSpace + 1 );
99N/A remainingStr = remainingStr.substring( posOfSpace + 1 );
99N/A }
99N/A //else just print
99N/A else
99N/A {
99N/A printStr = remainingStr;
99N/A remainingStr = "";
99N/A }
99N/A
99N/A instructionsText.append( printStr + "\n" );
99N/A
99N/A }// while
99N/A
99N/A }// for
99N/A
99N/A }//printInstructions()
99N/A
99N/A //DO NOT call this directly, go through Sysout
99N/A public void displayMessage( String messageIn, boolean nextLine )
99N/A {
99N/A messageText.append( messageIn + (nextLine? "\n" : "") );
99N/A System.out.println(messageIn);
99N/A }
99N/A
99N/A //catch presses of the passed and failed buttons.
99N/A //simply call the standard pass() or fail() static methods of
99N/A //ManualMainTest
99N/A public void actionPerformed( ActionEvent e )
99N/A {
99N/A if( e.getActionCommand() == "pass" )
99N/A {
99N/A OverBlocker.pass();
99N/A }
99N/A else
99N/A {
99N/A OverBlocker.fail();
99N/A }
99N/A }
99N/A
99N/A}// TestDialog class