Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/InputVerifierTest3/InputVerifierTest3.java
47525 views
/*1* Copyright (c) 2006, 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 643266526@summary Inputverifier is not executed when focus owner is removed27@author oleg.sukhodolsky: area=awt.focus28@library ../../regtesthelpers29@build Util30@run main InputVerifierTest331*/3233/**34* InputVerifierTest3.java35*36* summary: Inputverifier is not executed when focus owner is removed37*/3839import java.awt.AWTException;40import java.awt.BorderLayout;41import java.awt.Component;42import java.awt.Dialog;43import java.awt.FlowLayout;44import java.awt.Frame;45import java.awt.KeyboardFocusManager;46import java.awt.Point;47import java.awt.Robot;48import java.awt.TextArea;49import java.awt.Toolkit;5051import java.awt.event.InputEvent;5253import javax.swing.InputVerifier;54import javax.swing.JComponent;55import javax.swing.JFrame;56import javax.swing.JTextField;5758import test.java.awt.regtesthelpers.Util;5960public class InputVerifierTest361{62static volatile boolean verifier_called = false;6364private static void init()65{66//*** Create instructions for the user here ***6768String[] instructions =69{70"This is an AUTOMATIC test, simply wait until it is done.",71"The result (passed or failed) will be shown in the",72"message window below."73};74Sysout.createDialog( );75Sysout.printInstructions( instructions );7677JFrame frame = new JFrame();78frame.getContentPane().setLayout(new FlowLayout());79JTextField tf1 = new JTextField(10);80tf1.setInputVerifier(new InputVerifier() {81public boolean verify(JComponent input) {82System.err.println("verify on " + input);83verifier_called = true;84return true;85}86});87frame.getContentPane().add(tf1);88JTextField tf2 = new JTextField(10);89frame.getContentPane().add(tf2);9091frame.setSize(200, 200);92frame.setVisible(true);9394Robot r = null;95try {96r = new Robot();97} catch (AWTException e) {98InputVerifierTest3.fail(e);99}100101102try {103Util.waitForIdle(r);104Util.clickOnComp(tf1, r);105Util.waitForIdle(r);106107108if (!tf1.isFocusOwner()) {109System.out.println("focus owner = " + KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());110throw new RuntimeException("tf1 is not a focus owner");111}112113frame.getContentPane().remove(tf1);114Util.waitForIdle(r);115116if (!tf2.isFocusOwner()) {117System.out.println("focus owner = " + KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());118throw new RuntimeException("tf2 is not a focus owner");119}120121if (!verifier_called) {122throw new RuntimeException("verifier was not called");123}124125} catch (Exception e) {126InputVerifierTest3.fail(e);127}128129InputVerifierTest3.pass();130131}//End init()132133/*****************************************************134* Standard Test Machinery Section135* DO NOT modify anything in this section -- it's a136* standard chunk of code which has all of the137* synchronisation necessary for the test harness.138* By keeping it the same in all tests, it is easier139* to read and understand someone else's test, as140* well as insuring that all tests behave correctly141* with the test harness.142* There is a section following this for test-143* classes144******************************************************/145private static boolean theTestPassed = false;146private static boolean testGeneratedInterrupt = false;147private static String failureMessage = "";148149private static Thread mainThread = null;150151private static int sleepTime = 300000;152153// Not sure about what happens if multiple of this test are154// instantiated in the same VM. Being static (and using155// static vars), it aint gonna work. Not worrying about156// it for now.157public static void main( String args[] ) throws InterruptedException158{159mainThread = Thread.currentThread();160try161{162init();163}164catch( TestPassedException e )165{166//The test passed, so just return from main and harness will167// interepret this return as a pass168return;169}170//At this point, neither test pass nor test fail has been171// called -- either would have thrown an exception and ended the172// test, so we know we have multiple threads.173174//Test involves other threads, so sleep and wait for them to175// called pass() or fail()176try177{178Thread.sleep( sleepTime );179//Timed out, so fail the test180throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );181}182catch (InterruptedException e)183{184//The test harness may have interrupted the test. If so, rethrow the exception185// so that the harness gets it and deals with it.186if( ! testGeneratedInterrupt ) throw e;187188//reset flag in case hit this code more than once for some reason (just safety)189testGeneratedInterrupt = false;190191if ( theTestPassed == false )192{193throw new RuntimeException( failureMessage );194}195}196197}//main198199public static synchronized void setTimeoutTo( int seconds )200{201sleepTime = seconds * 1000;202}203204public static synchronized void pass()205{206Sysout.println( "The test passed." );207Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );208//first check if this is executing in main thread209if ( mainThread == Thread.currentThread() )210{211//Still in the main thread, so set the flag just for kicks,212// and throw a test passed exception which will be caught213// and end the test.214theTestPassed = true;215throw new TestPassedException();216}217theTestPassed = true;218testGeneratedInterrupt = true;219mainThread.interrupt();220}//pass()221222public static synchronized void fail( Exception whyFailed )223{224Sysout.println( "The test failed: " + whyFailed );225Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );226//check if this called from main thread227if ( mainThread == Thread.currentThread() )228{229//If main thread, fail now 'cause not sleeping230throw new RuntimeException( whyFailed );231}232theTestPassed = false;233testGeneratedInterrupt = true;234failureMessage = whyFailed.toString();235mainThread.interrupt();236}//fail()237238}// class InputVerifierTest3239240//This exception is used to exit from any level of call nesting241// when it's determined that the test has passed, and immediately242// end the test.243class TestPassedException extends RuntimeException244{245}246247//*********** End Standard Test Machinery Section **********248249/****************************************************250Standard Test Machinery251DO NOT modify anything below -- it's a standard252chunk of code whose purpose is to make user253interaction uniform, and thereby make it simpler254to read and understand someone else's test.255****************************************************/256257/**258This is part of the standard test machinery.259It creates a dialog (with the instructions), and is the interface260for sending text messages to the user.261To print the instructions, send an array of strings to Sysout.createDialog262WithInstructions method. Put one line of instructions per array entry.263To display a message for the tester to see, simply call Sysout.println264with the string to be displayed.265This mimics System.out.println but works within the test harness as well266as standalone.267*/268269class Sysout270{271private static TestDialog dialog;272273public static void createDialogWithInstructions( String[] instructions )274{275dialog = new TestDialog( new Frame(), "Instructions" );276dialog.printInstructions( instructions );277dialog.setVisible(true);278println( "Any messages for the tester will display here." );279}280281public static void createDialog( )282{283dialog = new TestDialog( new Frame(), "Instructions" );284String[] defInstr = { "Instructions will appear here. ", "" } ;285dialog.printInstructions( defInstr );286dialog.setVisible(true);287println( "Any messages for the tester will display here." );288}289290291public static void printInstructions( String[] instructions )292{293dialog.printInstructions( instructions );294}295296297public static void println( String messageIn )298{299dialog.displayMessage( messageIn );300System.out.println(messageIn);301}302303}// Sysout class304305/**306This is part of the standard test machinery. It provides a place for the307test instructions to be displayed, and a place for interactive messages308to the user to be displayed.309To have the test instructions displayed, see Sysout.310To have a message to the user be displayed, see Sysout.311Do not call anything in this dialog directly.312*/313class TestDialog extends Dialog314{315316TextArea instructionsText;317TextArea messageText;318int maxStringLength = 80;319320//DO NOT call this directly, go through Sysout321public TestDialog( Frame frame, String name )322{323super( frame, name );324int scrollBoth = TextArea.SCROLLBARS_BOTH;325instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );326add( "North", instructionsText );327328messageText = new TextArea( "", 5, maxStringLength, scrollBoth );329add("Center", messageText);330331pack();332333setVisible(true);334}// TestDialog()335336//DO NOT call this directly, go through Sysout337public void printInstructions( String[] instructions )338{339//Clear out any current instructions340instructionsText.setText( "" );341342//Go down array of instruction strings343344String printStr, remainingStr;345for( int i=0; i < instructions.length; i++ )346{347//chop up each into pieces maxSringLength long348remainingStr = instructions[ i ];349while( remainingStr.length() > 0 )350{351//if longer than max then chop off first max chars to print352if( remainingStr.length() >= maxStringLength )353{354//Try to chop on a word boundary355int posOfSpace = remainingStr.356lastIndexOf( ' ', maxStringLength - 1 );357358if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;359360printStr = remainingStr.substring( 0, posOfSpace + 1 );361remainingStr = remainingStr.substring( posOfSpace + 1 );362}363//else just print364else365{366printStr = remainingStr;367remainingStr = "";368}369370instructionsText.append( printStr + "\n" );371372}// while373374}// for375376}//printInstructions()377378//DO NOT call this directly, go through Sysout379public void displayMessage( String messageIn )380{381messageText.append( messageIn + "\n" );382System.out.println(messageIn);383}384385}// TestDialog class386387388