Path: blob/master/test/jdk/java/awt/FileDialog/DefaultFocusOwner/DefaultFocusOwner.java
41153 views
/*1* Copyright (c) 2007, 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 657996226@summary FileDialog focus is incorrect (Linux)27@author dmitry.cherepanov area=awt.filedialog28@run main/manual DefaultFocusOwner29*/3031/*32* The test verifies that when the file dialog is open33* the focus is transfered on the correct component of the dialog34*/3536import java.awt.*;37import java.awt.event.*;3839public class DefaultFocusOwner40{4142private static void init()43{44//*** Create instructions for the user here ***4546String[] instructions =47{48" 1) there is a frame with a 'show dialog' button, ",49" 2) press the butoton, the dialog will be shown, ",50" 3) the focus will be transfered into the dialog, ",51" 4) check that initially focus owner is the specific text field, ",52" its label is 'File name' on Windows OS, 'Enter file name' on Linux OS, ",53" 5) if it's true then the test passed, otherwise it failed. "54};55Sysout.createDialog( );56Sysout.printInstructions( instructions );5758Frame frame = new Frame();59Button button = new Button("show dialog");60button.addActionListener(new ActionListener(){61public void actionPerformed(ActionEvent ae) {62FileDialog dialog = new FileDialog(new Frame(), "dialog");63dialog.setVisible(true);64}65});66frame.setBounds(200,200,200,200);67frame.add(button);68frame.setVisible(true);6970}//End init()71727374/*****************************************************75* Standard Test Machinery Section76* DO NOT modify anything in this section -- it's a77* standard chunk of code which has all of the78* synchronisation necessary for the test harness.79* By keeping it the same in all tests, it is easier80* to read and understand someone else's test, as81* well as insuring that all tests behave correctly82* with the test harness.83* There is a section following this for test-defined84* classes85******************************************************/86private static boolean theTestPassed = false;87private static boolean testGeneratedInterrupt = false;88private static String failureMessage = "";8990private static Thread mainThread = null;9192private static int sleepTime = 300000;9394public static void main( String args[] ) throws InterruptedException95{96mainThread = Thread.currentThread();97try98{99init();100}101catch( TestPassedException e )102{103//The test passed, so just return from main and harness will104// interepret this return as a pass105return;106}107//At this point, neither test passed nor test failed has been108// called -- either would have thrown an exception and ended the109// test, so we know we have multiple threads.110111//Test involves other threads, so sleep and wait for them to112// called pass() or fail()113try114{115Thread.sleep( sleepTime );116//Timed out, so fail the test117throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );118}119catch (InterruptedException e)120{121if( ! testGeneratedInterrupt ) throw e;122123//reset flag in case hit this code more than once for some reason (just safety)124testGeneratedInterrupt = false;125if ( theTestPassed == false )126{127throw new RuntimeException( failureMessage );128}129}130131}//main132133public static synchronized void setTimeoutTo( int seconds )134{135sleepTime = seconds * 1000;136}137138public static synchronized void pass()139{140Sysout.println( "The test passed." );141Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );142//first check if this is executing in main thread143if ( mainThread == Thread.currentThread() )144{145//Still in the main thread, so set the flag just for kicks,146// and throw a test passed exception which will be caught147// and end the test.148theTestPassed = true;149throw new TestPassedException();150}151//pass was called from a different thread, so set the flag and interrupt152// the main thead.153theTestPassed = true;154testGeneratedInterrupt = true;155mainThread.interrupt();156}//pass()157158public static synchronized void fail()159{160//test writer didn't specify why test failed, so give generic161fail( "it just plain failed! :-)" );162}163164public static synchronized void fail( String whyFailed )165{166Sysout.println( "The test failed: " + whyFailed );167Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );168//check if this called from main thread169if ( mainThread == Thread.currentThread() )170{171//If main thread, fail now 'cause not sleeping172throw new RuntimeException( whyFailed );173}174theTestPassed = false;175testGeneratedInterrupt = true;176failureMessage = whyFailed;177mainThread.interrupt();178}//fail()179180}// class ManualMainTest181182//This exception is used to exit from any level of call nesting183// when it's determined that the test has passed, and immediately184// end the test.185class TestPassedException extends RuntimeException186{187}188189//*********** End Standard Test Machinery Section **********190191192//************ Begin classes defined for the test ****************193194// make listeners in a class defined here, and instantiate them in init()195196/* Example of a class which may be written as part of a test197class NewClass implements anInterface198{199static int newVar = 0;200201public void eventDispatched(AWTEvent e)202{203//Counting events to see if we get enough204eventCount++;205206if( eventCount == 20 )207{208//got enough events, so pass209210ManualMainTest.pass();211}212else if( tries == 20 )213{214//tried too many times without getting enough events so fail215216ManualMainTest.fail();217}218219}// eventDispatched()220221}// NewClass class222223*/224225226//************** End classes defined for the test *******************227228229230231/****************************************************232Standard Test Machinery233DO NOT modify anything below -- it's a standard234chunk of code whose purpose is to make user235interaction uniform, and thereby make it simpler236to read and understand someone else's test.237****************************************************/238239/**240This is part of the standard test machinery.241It creates a dialog (with the instructions), and is the interface242for sending text messages to the user.243To print the instructions, send an array of strings to Sysout.createDialog244WithInstructions method. Put one line of instructions per array entry.245To display a message for the tester to see, simply call Sysout.println246with the string to be displayed.247This mimics System.out.println but works within the test harness as well248as standalone.249*/250251class Sysout252{253private static TestDialog dialog;254255public static void createDialogWithInstructions( String[] instructions )256{257dialog = new TestDialog( new Frame(), "Instructions" );258dialog.printInstructions( instructions );259dialog.setVisible(true);260println( "Any messages for the tester will display here." );261}262263public static void createDialog( )264{265dialog = new TestDialog( new Frame(), "Instructions" );266String[] defInstr = { "Instructions will appear here. ", "" } ;267dialog.printInstructions( defInstr );268dialog.setVisible(true);269println( "Any messages for the tester will display here." );270}271272273public static void printInstructions( String[] instructions )274{275dialog.printInstructions( instructions );276}277278279public static void println( String messageIn )280{281dialog.displayMessage( messageIn );282}283284}// Sysout class285286/**287This is part of the standard test machinery. It provides a place for the288test instructions to be displayed, and a place for interactive messages289to the user to be displayed.290To have the test instructions displayed, see Sysout.291To have a message to the user be displayed, see Sysout.292Do not call anything in this dialog directly.293*/294class TestDialog extends Dialog implements ActionListener295{296297TextArea instructionsText;298TextArea messageText;299int maxStringLength = 80;300Panel buttonP = new Panel();301Button passB = new Button( "pass" );302Button failB = new Button( "fail" );303304//DO NOT call this directly, go through Sysout305public TestDialog( Frame frame, String name )306{307super( frame, name );308int scrollBoth = TextArea.SCROLLBARS_BOTH;309instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );310add( "North", instructionsText );311312messageText = new TextArea( "", 5, maxStringLength, scrollBoth );313add("Center", messageText);314315passB = new Button( "pass" );316passB.setActionCommand( "pass" );317passB.addActionListener( this );318buttonP.add( "East", passB );319320failB = new Button( "fail" );321failB.setActionCommand( "fail" );322failB.addActionListener( this );323buttonP.add( "West", failB );324325add( "South", buttonP );326pack();327328setVisible(true);329}// TestDialog()330331//DO NOT call this directly, go through Sysout332public void printInstructions( String[] instructions )333{334//Clear out any current instructions335instructionsText.setText( "" );336337//Go down array of instruction strings338339String printStr, remainingStr;340for( int i=0; i < instructions.length; i++ )341{342//chop up each into pieces maxSringLength long343remainingStr = instructions[ i ];344while( remainingStr.length() > 0 )345{346//if longer than max then chop off first max chars to print347if( remainingStr.length() >= maxStringLength )348{349//Try to chop on a word boundary350int posOfSpace = remainingStr.351lastIndexOf( ' ', maxStringLength - 1 );352353if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;354355printStr = remainingStr.substring( 0, posOfSpace + 1 );356remainingStr = remainingStr.substring( posOfSpace + 1 );357}358//else just print359else360{361printStr = remainingStr;362remainingStr = "";363}364365instructionsText.append( printStr + "\n" );366367}// while368369}// for370371}//printInstructions()372373//DO NOT call this directly, go through Sysout374public void displayMessage( String messageIn )375{376messageText.append( messageIn + "\n" );377System.out.println(messageIn);378}379380//catch presses of the passed and failed buttons.381//simply call the standard pass() or fail() static methods of382//ManualMainTest383public void actionPerformed( ActionEvent e )384{385if( e.getActionCommand() == "pass" )386{387DefaultFocusOwner.pass();388}389else390{391DefaultFocusOwner.fail();392}393}394395}// TestDialog class396397398