Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/awt/Focus/6378278/InputVerifierTest.java
48795 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 637827826@summary Apparent missing key events causing Bugster to break27@author oleg.sukhodolsky: area=awt.focus28@run main InputVerifierTest29*/3031/**32* InputVerifierTest.java33*34* summary: Apparent missing key events causing Bugster to break35*/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;47import java.awt.event.KeyEvent;4849import javax.swing.InputVerifier;50import javax.swing.JComponent;51import javax.swing.JFrame;52import javax.swing.JTextField;5354public class InputVerifierTest55{5657//*** test-writer defined static variables go here ***58static volatile boolean ivWasCalled = false;5960private static void init()61{62//*** Create instructions for the user here ***63String[] instructions =64{65"This is an AUTOMATIC test, simply wait until it is done.",66"The result (passed or failed) will be shown in the",67"message window below."68};69Sysout.createDialog( );70Sysout.printInstructions( instructions );7172JFrame frame = new JFrame();73JTextField t1 = new JTextField();74t1.setInputVerifier(new InputVerifier() {75public boolean verify(JComponent input) {76Sysout.println("verify(" + input + ")");77ivWasCalled = true;78return true;79}80});81JTextField t2 = new JTextField();8283frame.getContentPane().add(t1, BorderLayout.NORTH);84frame.getContentPane().add(t2, BorderLayout.SOUTH);85frame.setSize(200, 200);86frame.setVisible(true);8788Robot r = null;89try {90r = new Robot();91} catch (AWTException e) {92e.printStackTrace();93InputVerifierTest.fail(e.toString());94}9596try {97r.waitForIdle();9899mouseClickOnComp(r, t1);100r.waitForIdle();101102if (!t1.isFocusOwner()) {103throw new RuntimeException("t1 is not a focus owner");104}105ivWasCalled = false;106r.keyPress(KeyEvent.VK_TAB);107r.delay(10);108r.keyRelease(KeyEvent.VK_TAB);109r.waitForIdle();110111if (!t2.isFocusOwner()) {112throw new RuntimeException("t2 is not a focus owner");113}114if (!ivWasCalled) {115throw new RuntimeException("InputVerifier was not called after tabbing");116}117118mouseClickOnComp(r, t1);119r.waitForIdle();120121if (!t1.isFocusOwner()) {122throw new RuntimeException("t1 is not a focus owner");123}124125ivWasCalled = false;126mouseClickOnComp(r, t2);127r.waitForIdle();128if (!t2.isFocusOwner()) {129throw new RuntimeException("t2 is not a focus owner");130}131if (!ivWasCalled) {132throw new RuntimeException("InputVErifier was not called after mouse press");133}134} catch (Exception e) {135e.printStackTrace();136InputVerifierTest.fail(e.toString());137}138139InputVerifierTest.pass();140141}//End init()142143static void mouseClickOnComp(Robot r, Component comp) {144Point loc = comp.getLocationOnScreen();145loc.x += comp.getWidth() / 2;146loc.y += comp.getHeight() / 2;147r.mouseMove(loc.x, loc.y);148r.delay(10);149r.mousePress(InputEvent.BUTTON1_MASK);150r.delay(10);151r.mouseRelease(InputEvent.BUTTON1_MASK);152}153154/*****************************************************155* Standard Test Machinery Section156* DO NOT modify anything in this section -- it's a157* standard chunk of code which has all of the158* synchronisation necessary for the test harness.159* By keeping it the same in all tests, it is easier160* to read and understand someone else's test, as161* well as insuring that all tests behave correctly162* with the test harness.163* There is a section following this for test-164* classes165******************************************************/166private static boolean theTestPassed = false;167private static boolean testGeneratedInterrupt = false;168private static String failureMessage = "";169170private static Thread mainThread = null;171172private static int sleepTime = 300000;173174// Not sure about what happens if multiple of this test are175// instantiated in the same VM. Being static (and using176// static vars), it aint gonna work. Not worrying about177// it for now.178public static void main( String args[] ) throws InterruptedException179{180mainThread = Thread.currentThread();181try182{183init();184}185catch( TestPassedException e )186{187//The test passed, so just return from main and harness will188// interepret this return as a pass189return;190}191//At this point, neither test pass nor test fail has been192// called -- either would have thrown an exception and ended the193// test, so we know we have multiple threads.194195//Test involves other threads, so sleep and wait for them to196// called pass() or fail()197try198{199Thread.sleep( sleepTime );200//Timed out, so fail the test201throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );202}203catch (InterruptedException e)204{205//The test harness may have interrupted the test. If so, rethrow the exception206// so that the harness gets it and deals with it.207if( ! testGeneratedInterrupt ) throw e;208209//reset flag in case hit this code more than once for some reason (just safety)210testGeneratedInterrupt = false;211212if ( theTestPassed == false )213{214throw new RuntimeException( failureMessage );215}216}217218}//main219220public static synchronized void setTimeoutTo( int seconds )221{222sleepTime = seconds * 1000;223}224225public static synchronized void pass()226{227Sysout.println( "The test passed." );228Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );229//first check if this is executing in main thread230if ( mainThread == Thread.currentThread() )231{232//Still in the main thread, so set the flag just for kicks,233// and throw a test passed exception which will be caught234// and end the test.235theTestPassed = true;236throw new TestPassedException();237}238theTestPassed = true;239testGeneratedInterrupt = true;240mainThread.interrupt();241}//pass()242243public static synchronized void fail()244{245//test writer didn't specify why test failed, so give generic246fail( "it just plain failed! :-)" );247}248249public static synchronized void fail( String whyFailed )250{251Sysout.println( "The test failed: " + whyFailed );252Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );253//check if this called from main thread254if ( mainThread == Thread.currentThread() )255{256//If main thread, fail now 'cause not sleeping257throw new RuntimeException( whyFailed );258}259theTestPassed = false;260testGeneratedInterrupt = true;261failureMessage = whyFailed;262mainThread.interrupt();263}//fail()264265}// class InputVerifierTest266267//This exception is used to exit from any level of call nesting268// when it's determined that the test has passed, and immediately269// end the test.270class TestPassedException extends RuntimeException271{272}273274//*********** End Standard Test Machinery Section **********275276/****************************************************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 );327System.out.println(messageIn);328}329330}// Sysout class331332/**333This is part of the standard test machinery. It provides a place for the334test instructions to be displayed, and a place for interactive messages335to the user to be displayed.336To have the test instructions displayed, see Sysout.337To have a message to the user be displayed, see Sysout.338Do not call anything in this dialog directly.339*/340class TestDialog extends Dialog341{342343TextArea instructionsText;344TextArea messageText;345int maxStringLength = 80;346347//DO NOT call this directly, go through Sysout348public TestDialog( Frame frame, String name )349{350super( frame, name );351int scrollBoth = TextArea.SCROLLBARS_BOTH;352instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );353add( "North", instructionsText );354355messageText = new TextArea( "", 5, maxStringLength, scrollBoth );356add("Center", messageText);357358pack();359360setVisible(true);361}// TestDialog()362363//DO NOT call this directly, go through Sysout364public void printInstructions( String[] instructions )365{366//Clear out any current instructions367instructionsText.setText( "" );368369//Go down array of instruction strings370371String printStr, remainingStr;372for( int i=0; i < instructions.length; i++ )373{374//chop up each into pieces maxSringLength long375remainingStr = instructions[ i ];376while( remainingStr.length() > 0 )377{378//if longer than max then chop off first max chars to print379if( remainingStr.length() >= maxStringLength )380{381//Try to chop on a word boundary382int posOfSpace = remainingStr.383lastIndexOf( ' ', maxStringLength - 1 );384385if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;386387printStr = remainingStr.substring( 0, posOfSpace + 1 );388remainingStr = remainingStr.substring( posOfSpace + 1 );389}390//else just print391else392{393printStr = remainingStr;394remainingStr = "";395}396397instructionsText.append( printStr + "\n" );398399}// while400401}// for402403}//printInstructions()404405//DO NOT call this directly, go through Sysout406public void displayMessage( String messageIn )407{408messageText.append( messageIn + "\n" );409System.out.println(messageIn);410}411412}// TestDialog class413414415