Path: blob/master/test/jdk/java/awt/Focus/6378278/InputVerifierTest.java
41153 views
/*1* Copyright (c) 2006, 2021, 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@key headful26@bug 637827827@summary Apparent missing key events causing Bugster to break28@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;53import javax.swing.SwingUtilities;5455public class InputVerifierTest56{5758//*** test-writer defined static variables go here ***59static volatile boolean ivWasCalled = false;60static JFrame frame;61static JTextField t1;62static JTextField t2;6364private static void init() throws Exception65{66try {67SwingUtilities.invokeAndWait(() -> {68frame = new JFrame();69t1 = new JTextField();70t1.setInputVerifier(new InputVerifier() {71public boolean verify(JComponent input) {72System.out.println("verify(" + input + ")");73ivWasCalled = true;74return true;75}76});77t2 = new JTextField();7879frame.getContentPane().add(t1, BorderLayout.NORTH);80frame.getContentPane().add(t2, BorderLayout.SOUTH);81frame.setLocationRelativeTo(null);82frame.setSize(200, 200);83frame.setVisible(true);84});8586Robot r = null;87try {88r = new Robot();89} catch (AWTException e) {90e.printStackTrace();91InputVerifierTest.fail(e.toString());92}9394try {95r.setAutoDelay(100);96r.waitForIdle();97r.delay(1000);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.keyRelease(KeyEvent.VK_TAB);108r.waitForIdle();109r.delay(500);110111if (!t2.isFocusOwner()) {112throw new RuntimeException("t2 is not a focus owner 1");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();128r.delay(500);129if (!t2.isFocusOwner()) {130throw new RuntimeException("t2 is not a focus owner 2");131}132if (!ivWasCalled) {133throw new RuntimeException("InputVErifier was not called after mouse press");134}135} catch (Exception e) {136e.printStackTrace();137InputVerifierTest.fail(e.toString());138}139140InputVerifierTest.pass();141} finally {142SwingUtilities.invokeAndWait(() -> {143if (frame != null) {144frame.dispose();145}146});147}148}//End init()149150static void mouseClickOnComp(Robot r, Component comp) {151Point loc = comp.getLocationOnScreen();152loc.x += comp.getWidth() / 2;153loc.y += comp.getHeight() / 2;154r.mouseMove(loc.x, loc.y);155r.waitForIdle();156r.mousePress(InputEvent.BUTTON1_DOWN_MASK);157r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);158}159160/*****************************************************161* Standard Test Machinery Section162* DO NOT modify anything in this section -- it's a163* standard chunk of code which has all of the164* synchronisation necessary for the test harness.165* By keeping it the same in all tests, it is easier166* to read and understand someone else's test, as167* well as insuring that all tests behave correctly168* with the test harness.169* There is a section following this for test-170* classes171******************************************************/172private static boolean theTestPassed = false;173private static boolean testGeneratedInterrupt = false;174private static String failureMessage = "";175176private static Thread mainThread = null;177178private static int sleepTime = 300000;179180// Not sure about what happens if multiple of this test are181// instantiated in the same VM. Being static (and using182// static vars), it aint gonna work. Not worrying about183// it for now.184public static void main( String args[] ) throws Exception185{186mainThread = Thread.currentThread();187try188{189init();190}191catch( TestPassedException e )192{193//The test passed, so just return from main and harness will194// interepret this return as a pass195return;196}197//At this point, neither test pass nor test fail has been198// called -- either would have thrown an exception and ended the199// test, so we know we have multiple threads.200201//Test involves other threads, so sleep and wait for them to202// called pass() or fail()203try204{205Thread.sleep( sleepTime );206//Timed out, so fail the test207throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );208}209catch (InterruptedException e)210{211//The test harness may have interrupted the test. If so, rethrow the exception212// so that the harness gets it and deals with it.213if( ! testGeneratedInterrupt ) throw e;214215//reset flag in case hit this code more than once for some reason (just safety)216testGeneratedInterrupt = false;217218if ( theTestPassed == false )219{220throw new RuntimeException( failureMessage );221}222}223224}//main225226public static synchronized void setTimeoutTo( int seconds )227{228sleepTime = seconds * 1000;229}230231public static synchronized void pass()232{233System.out.println( "The test passed." );234System.out.println( "The test is over, hit Ctl-C to stop Java VM" );235//first check if this is executing in main thread236if ( mainThread == Thread.currentThread() )237{238//Still in the main thread, so set the flag just for kicks,239// and throw a test passed exception which will be caught240// and end the test.241theTestPassed = true;242throw new TestPassedException();243}244theTestPassed = true;245testGeneratedInterrupt = true;246mainThread.interrupt();247}//pass()248249public static synchronized void fail()250{251//test writer didn't specify why test failed, so give generic252fail( "it just plain failed! :-)" );253}254255public static synchronized void fail( String whyFailed )256{257System.out.println( "The test failed: " + whyFailed );258System.out.println( "The test is over, hit Ctl-C to stop Java VM" );259//check if this called from main thread260if ( mainThread == Thread.currentThread() )261{262//If main thread, fail now 'cause not sleeping263throw new RuntimeException( whyFailed );264}265theTestPassed = false;266testGeneratedInterrupt = true;267failureMessage = whyFailed;268mainThread.interrupt();269}//fail()270271}// class InputVerifierTest272273//This exception is used to exit from any level of call nesting274// when it's determined that the test has passed, and immediately275// end the test.276class TestPassedException extends RuntimeException277{278}279280281