Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/6401036/InputVerifierTest2.java
48230 views
/*1* Copyright (c) 2006, 2014, 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 640103626@summary InputVerifier shouldn't be called when requestFocus() is called on comp from another toplevel27@author oleg.sukhodolsky: area=awt.focus28@run main InputVerifierTest229*/3031/**32* InputVerifierTest2.java33*34* summary: REGRESSION: InputVerifier and JOptionPane35*/3637import java.awt.AWTException;38import java.awt.BorderLayout;39import java.awt.Component;40import java.awt.Dialog;41import java.awt.Frame;42import java.awt.Point;43import java.awt.Robot;44import java.awt.TextArea;4546import java.awt.event.InputEvent;4748import javax.swing.InputVerifier;49import javax.swing.JButton;50import javax.swing.JComponent;51import javax.swing.JFrame;52import javax.swing.JTextField;53import javax.swing.JWindow;5455public class InputVerifierTest256{5758private static void init()59{60//*** Create instructions for the user here ***6162String[] instructions =63{64"This is an AUTOMATIC test, simply wait until it is done.",65"The result (passed or failed) will be shown in the",66"message window below."67};68Sysout.createDialog( );69Sysout.printInstructions( instructions );7071JFrame frame = new JFrame();72frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);73JTextField tf = new JTextField(10);74frame.getContentPane().add(tf);7576final JWindow w = new JWindow(frame);77JButton btn1 = new JButton("window");78btn1.setName("bnt1");79w.getContentPane().add(btn1);80w.pack();81w.setVisible(true);8283frame.setSize(200, 200);84frame.setVisible(true);858687Robot r = null;88try {89r = new Robot();90} catch (AWTException e) {91InputVerifierTest2.fail(e);92}939495try {96r.waitForIdle();97mouseClickOnComp(r, tf);98r.waitForIdle();99100if (!tf.isFocusOwner()) {101throw new RuntimeException("t1 is not a focus owner");102}103104tf.setInputVerifier(new InputVerifier() {105public boolean verify(JComponent input) {106System.err.println("verify on " + input);107throw new RuntimeException("InputVerifier should not be called");108}109});110btn1.requestFocus();111} catch (Exception e) {112InputVerifierTest2.fail(e);113}114115InputVerifierTest2.pass();116117}//End init()118119120static void mouseClickOnComp(Robot r, Component comp) {121Point loc = comp.getLocationOnScreen();122loc.x += comp.getWidth() / 2;123loc.y += comp.getHeight() / 2;124r.mouseMove(loc.x, loc.y);125r.delay(10);126r.mousePress(InputEvent.BUTTON1_MASK);127r.delay(10);128r.mouseRelease(InputEvent.BUTTON1_MASK);129}130131/*****************************************************132* Standard Test Machinery Section133* DO NOT modify anything in this section -- it's a134* standard chunk of code which has all of the135* synchronisation necessary for the test harness.136* By keeping it the same in all tests, it is easier137* to read and understand someone else's test, as138* well as insuring that all tests behave correctly139* with the test harness.140* There is a section following this for test-141* classes142******************************************************/143private static boolean theTestPassed = false;144private static boolean testGeneratedInterrupt = false;145private static String failureMessage = "";146147private static Thread mainThread = null;148149private static int sleepTime = 300000;150151// Not sure about what happens if multiple of this test are152// instantiated in the same VM. Being static (and using153// static vars), it aint gonna work. Not worrying about154// it for now.155public static void main( String args[] ) throws InterruptedException156{157mainThread = Thread.currentThread();158try159{160init();161}162catch( TestPassedException e )163{164//The test passed, so just return from main and harness will165// interepret this return as a pass166return;167}168//At this point, neither test pass nor test fail has been169// called -- either would have thrown an exception and ended the170// test, so we know we have multiple threads.171172//Test involves other threads, so sleep and wait for them to173// called pass() or fail()174try175{176Thread.sleep( sleepTime );177//Timed out, so fail the test178throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );179}180catch (InterruptedException e)181{182//The test harness may have interrupted the test. If so, rethrow the exception183// so that the harness gets it and deals with it.184if( ! testGeneratedInterrupt ) throw e;185186//reset flag in case hit this code more than once for some reason (just safety)187testGeneratedInterrupt = false;188189if ( theTestPassed == false )190{191throw new RuntimeException( failureMessage );192}193}194195}//main196197public static synchronized void setTimeoutTo( int seconds )198{199sleepTime = seconds * 1000;200}201202public static synchronized void pass()203{204Sysout.println( "The test passed." );205Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );206//first check if this is executing in main thread207if ( mainThread == Thread.currentThread() )208{209//Still in the main thread, so set the flag just for kicks,210// and throw a test passed exception which will be caught211// and end the test.212theTestPassed = true;213throw new TestPassedException();214}215theTestPassed = true;216testGeneratedInterrupt = true;217mainThread.interrupt();218}//pass()219220public static synchronized void fail( Exception whyFailed )221{222Sysout.println( "The test failed: " + whyFailed );223Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );224//check if this called from main thread225if ( mainThread == Thread.currentThread() )226{227//If main thread, fail now 'cause not sleeping228throw new RuntimeException( whyFailed );229}230theTestPassed = false;231testGeneratedInterrupt = true;232failureMessage = whyFailed.toString();233mainThread.interrupt();234}//fail()235236}// class InputVerifierTest2237238//This exception is used to exit from any level of call nesting239// when it's determined that the test has passed, and immediately240// end the test.241class TestPassedException extends RuntimeException242{243}244245//*********** End Standard Test Machinery Section **********246247/****************************************************248Standard Test Machinery249DO NOT modify anything below -- it's a standard250chunk of code whose purpose is to make user251interaction uniform, and thereby make it simpler252to read and understand someone else's test.253****************************************************/254255/**256This is part of the standard test machinery.257It creates a dialog (with the instructions), and is the interface258for sending text messages to the user.259To print the instructions, send an array of strings to Sysout.createDialog260WithInstructions method. Put one line of instructions per array entry.261To display a message for the tester to see, simply call Sysout.println262with the string to be displayed.263This mimics System.out.println but works within the test harness as well264as standalone.265*/266267class Sysout268{269private static TestDialog dialog;270271public static void createDialogWithInstructions( String[] instructions )272{273dialog = new TestDialog( new Frame(), "Instructions" );274dialog.printInstructions( instructions );275dialog.setVisible(true);276println( "Any messages for the tester will display here." );277}278279public static void createDialog( )280{281dialog = new TestDialog( new Frame(), "Instructions" );282String[] defInstr = { "Instructions will appear here. ", "" } ;283dialog.printInstructions( defInstr );284dialog.setVisible(true);285println( "Any messages for the tester will display here." );286}287288289public static void printInstructions( String[] instructions )290{291dialog.printInstructions( instructions );292}293294295public static void println( String messageIn )296{297dialog.displayMessage( messageIn );298System.out.println(messageIn);299}300301}// Sysout class302303/**304This is part of the standard test machinery. It provides a place for the305test instructions to be displayed, and a place for interactive messages306to the user to be displayed.307To have the test instructions displayed, see Sysout.308To have a message to the user be displayed, see Sysout.309Do not call anything in this dialog directly.310*/311class TestDialog extends Dialog312{313314TextArea instructionsText;315TextArea messageText;316int maxStringLength = 80;317318//DO NOT call this directly, go through Sysout319public TestDialog( Frame frame, String name )320{321super( frame, name );322int scrollBoth = TextArea.SCROLLBARS_BOTH;323instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );324add( "North", instructionsText );325326messageText = new TextArea( "", 5, maxStringLength, scrollBoth );327add("Center", messageText);328329pack();330331setVisible(true);332}// TestDialog()333334//DO NOT call this directly, go through Sysout335public void printInstructions( String[] instructions )336{337//Clear out any current instructions338instructionsText.setText( "" );339340//Go down array of instruction strings341342String printStr, remainingStr;343for( int i=0; i < instructions.length; i++ )344{345//chop up each into pieces maxSringLength long346remainingStr = instructions[ i ];347while( remainingStr.length() > 0 )348{349//if longer than max then chop off first max chars to print350if( remainingStr.length() >= maxStringLength )351{352//Try to chop on a word boundary353int posOfSpace = remainingStr.354lastIndexOf( ' ', maxStringLength - 1 );355356if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;357358printStr = remainingStr.substring( 0, posOfSpace + 1 );359remainingStr = remainingStr.substring( posOfSpace + 1 );360}361//else just print362else363{364printStr = remainingStr;365remainingStr = "";366}367368instructionsText.append( printStr + "\n" );369370}// while371372}// for373374}//printInstructions()375376//DO NOT call this directly, go through Sysout377public void displayMessage( String messageIn )378{379messageText.append( messageIn + "\n" );380System.out.println(messageIn);381}382383}// TestDialog class384385386