Path: blob/master/test/jdk/java/awt/Modal/ModalDialogMultiscreenTest/ModalDialogMultiscreenTest.java
41153 views
/*1* Copyright (c) 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 6430802 800837926@summary WM should not hang after show()/close()27@author [email protected]: area=awt.modal28@run main/manual ModalDialogMultiscreenTest29*/303132/**33* ModalDialogMultiscreenTest.java34*35* summary: Tests whether a WM will hang on show()/close() a modal dialog in multiscreen mode36*/3738import java.awt.*;39import java.awt.event.*;40import javax.swing.*;414243public class ModalDialogMultiscreenTest44{4546private static class ButtonActionListener implements ActionListener {47JFrame frame;48JDialog dialog;49public ButtonActionListener(JFrame frame, JDialog dialog) {50this.frame = frame;51this.dialog = dialog;52}53public void actionPerformed(ActionEvent e) {54dialog.setLocationRelativeTo(frame);55dialog.setVisible(true);56}57}58public static class TestDialog extends JDialog {59public TestDialog(Frame owner, String title, boolean modal, GraphicsConfiguration gc) {60super(owner, title, modal, gc);61setSize(200, 100);62JButton button = new JButton("Close");63button.addActionListener(new ActionListener() {64public void actionPerformed(ActionEvent e) {65dispose();66}67});68getContentPane().add(button);69}70}7172private static void init()73{74GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();75GraphicsDevice[] gs = ge.getScreenDevices();7677Sysout.createDialog( );7879if (gs.length < 2) {80System.out.println("Not multi-head environment, test not valid!");81ModalDialogMultiscreenTest.pass( );82}8384String[] instructions =85{86"The test should be run on a multi-head X (non-xinerama) systems.",87"Otherwise click the Pass button right now.",88"You will see an open Frame on each screen your system has.",89"The frame has an 'Open dialog' button.",90"Clicking the button opens a modal dialog with a Close button.",91"The test procedure:",92"1. Open a dialog and close it with appropriate buttons.",93"2. Switch to another screen ($ DISPLAY=X.Y xprop)",94"3. Repeat steps 1-2 several times (about 3*<number-of-screens>)",95"If the test doesn't cause the window manager to hang, it's passed."96};97Sysout.printInstructions( instructions );9899100for (int i = 0; i < gs.length; i++) {101JFrame frame = new JFrame("Frame "+i,gs[i].getDefaultConfiguration());102JButton button = new JButton("Open Dialog");103button.setMinimumSize(new Dimension(200, 100));104button.setPreferredSize(new Dimension(200, 100));105button.setSize(new Dimension(200, 100));106button.addActionListener(new ButtonActionListener(frame, new TestDialog(frame, "Dialog #"+i, true, gs[i].getDefaultConfiguration())));107frame.getContentPane().add(button);108frame.pack();109frame.setVisible(true);110}111112}//End init()113114115//ap203012: NO MORE CHANGES BELOW THIS LINE116117118119/*****************************************************120* Standard Test Machinery Section121* DO NOT modify anything in this section -- it's a122* standard chunk of code which has all of the123* synchronisation necessary for the test harness.124* By keeping it the same in all tests, it is easier125* to read and understand someone else's test, as126* well as insuring that all tests behave correctly127* with the test harness.128* There is a section following this for test-defined129* classes130******************************************************/131private static boolean theTestPassed = false;132private static boolean testGeneratedInterrupt = false;133private static String failureMessage = "";134135private static Thread mainThread = null;136137private static int sleepTime = 300000;138139public static void main( String args[] ) throws InterruptedException140{141mainThread = Thread.currentThread();142try143{144init();145}146catch( TestPassedException e )147{148//The test passed, so just return from main and harness will149// interepret this return as a pass150return;151}152//At this point, neither test passed nor test failed has been153// called -- either would have thrown an exception and ended the154// test, so we know we have multiple threads.155156//Test involves other threads, so sleep and wait for them to157// called pass() or fail()158try159{160Thread.sleep( sleepTime );161//Timed out, so fail the test162throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );163}164catch (InterruptedException e)165{166if( ! testGeneratedInterrupt ) throw e;167168//reset flag in case hit this code more than once for some reason (just safety)169testGeneratedInterrupt = false;170if ( theTestPassed == false )171{172throw new RuntimeException( failureMessage );173}174}175176}//main177178public static synchronized void setTimeoutTo( int seconds )179{180sleepTime = seconds * 1000;181}182183public static synchronized void pass()184{185Sysout.println( "The test passed." );186Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );187//first check if this is executing in main thread188if ( mainThread == Thread.currentThread() )189{190//Still in the main thread, so set the flag just for kicks,191// and throw a test passed exception which will be caught192// and end the test.193theTestPassed = true;194throw new TestPassedException();195}196//pass was called from a different thread, so set the flag and interrupt197// the main thead.198theTestPassed = true;199testGeneratedInterrupt = true;200mainThread.interrupt();201}//pass()202203public static synchronized void fail()204{205//test writer didn't specify why test failed, so give generic206fail( "it just plain failed! :-)" );207}208209public static synchronized void fail( String whyFailed )210{211Sysout.println( "The test failed: " + whyFailed );212Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );213//check if this called from main thread214if ( mainThread == Thread.currentThread() )215{216//If main thread, fail now 'cause not sleeping217throw new RuntimeException( whyFailed );218}219theTestPassed = false;220testGeneratedInterrupt = true;221failureMessage = whyFailed;222mainThread.interrupt();223}//fail()224225}// class ModalDialogMultiscreenTest226227//This exception is used to exit from any level of call nesting228// when it's determined that the test has passed, and immediately229// end the test.230class TestPassedException extends RuntimeException231{232}233234//*********** End Standard Test Machinery Section **********235236237//************ Begin classes defined for the test ****************238239// make listeners in a class defined here, and instantiate them in init()240241/* Example of a class which may be written as part of a test242class NewClass implements anInterface243{244static int newVar = 0;245246public void eventDispatched(AWTEvent e)247{248//Counting events to see if we get enough249eventCount++;250251if( eventCount == 20 )252{253//got enough events, so pass254255ModalDialogMultiscreenTest.pass();256}257else if( tries == 20 )258{259//tried too many times without getting enough events so fail260261ModalDialogMultiscreenTest.fail();262}263264}// eventDispatched()265266}// NewClass class267268*/269270271//************** End classes defined for the test *******************272273274275276/****************************************************277Standard Test Machinery278DO NOT modify anything below -- it's a standard279chunk of code whose purpose is to make user280interaction uniform, and thereby make it simpler281to read and understand someone else's test.282****************************************************/283284/**285This is part of the standard test machinery.286It creates a dialog (with the instructions), and is the interface287for sending text messages to the user.288To print the instructions, send an array of strings to Sysout.createDialog289WithInstructions method. Put one line of instructions per array entry.290To display a message for the tester to see, simply call Sysout.println291with the string to be displayed.292This mimics System.out.println but works within the test harness as well293as standalone.294*/295296class Sysout297{298private static TestDialog dialog;299300public static void createDialogWithInstructions( String[] instructions )301{302dialog = new TestDialog( new Frame(), "Instructions" );303dialog.printInstructions( instructions );304dialog.setVisible(true);305println( "Any messages for the tester will display here." );306}307308public static void createDialog( )309{310dialog = new TestDialog( new Frame(), "Instructions" );311String[] defInstr = { "Instructions will appear here. ", "" } ;312dialog.printInstructions( defInstr );313dialog.setVisible(true);314println( "Any messages for the tester will display here." );315}316317318public static void printInstructions( String[] instructions )319{320dialog.printInstructions( instructions );321}322323324public static void println( String messageIn )325{326dialog.displayMessage( messageIn );327}328329}// Sysout class330331/**332This is part of the standard test machinery. It provides a place for the333test instructions to be displayed, and a place for interactive messages334to the user to be displayed.335To have the test instructions displayed, see Sysout.336To have a message to the user be displayed, see Sysout.337Do not call anything in this dialog directly.338*/339class TestDialog extends Dialog implements ActionListener340{341342TextArea instructionsText;343TextArea messageText;344int maxStringLength = 80;345Panel buttonP = new Panel();346Button passB = new Button( "pass" );347Button failB = new Button( "fail" );348349//DO NOT call this directly, go through Sysout350public TestDialog( Frame frame, String name )351{352super( frame, name );353int scrollBoth = TextArea.SCROLLBARS_BOTH;354instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );355add( "North", instructionsText );356357messageText = new TextArea( "", 5, maxStringLength, scrollBoth );358add("Center", messageText);359360passB = new Button( "pass" );361passB.setActionCommand( "pass" );362passB.addActionListener( this );363buttonP.add( "East", passB );364365failB = new Button( "fail" );366failB.setActionCommand( "fail" );367failB.addActionListener( this );368buttonP.add( "West", failB );369370add( "South", buttonP );371pack();372373setVisible(true);374}// TestDialog()375376//DO NOT call this directly, go through Sysout377public void printInstructions( String[] instructions )378{379//Clear out any current instructions380instructionsText.setText( "" );381382//Go down array of instruction strings383384String printStr, remainingStr;385for( int i=0; i < instructions.length; i++ )386{387//chop up each into pieces maxSringLength long388remainingStr = instructions[ i ];389while( remainingStr.length() > 0 )390{391//if longer than max then chop off first max chars to print392if( remainingStr.length() >= maxStringLength )393{394//Try to chop on a word boundary395int posOfSpace = remainingStr.396lastIndexOf( ' ', maxStringLength - 1 );397398if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;399400printStr = remainingStr.substring( 0, posOfSpace + 1 );401remainingStr = remainingStr.substring( posOfSpace + 1 );402}403//else just print404else405{406printStr = remainingStr;407remainingStr = "";408}409410instructionsText.append( printStr + "\n" );411412}// while413414}// for415416}//printInstructions()417418//DO NOT call this directly, go through Sysout419public void displayMessage( String messageIn )420{421messageText.append( messageIn + "\n" );422System.out.println(messageIn);423}424425//catch presses of the passed and failed buttons.426//simply call the standard pass() or fail() static methods of427//ModalDialogMultiscreenTest428public void actionPerformed( ActionEvent e )429{430if( e.getActionCommand() == "pass" )431{432ModalDialogMultiscreenTest.pass();433}434else435{436ModalDialogMultiscreenTest.fail();437}438}439440}// TestDialog class441442443