Path: blob/master/test/jdk/java/awt/Focus/6401036/InputVerifierTest2.java
41153 views
/*1* Copyright (c) 2006, 2018, 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 640103627@summary InputVerifier shouldn't be called when requestFocus() is called on comp from another toplevel28@author oleg.sukhodolsky: area=awt.focus29@run main InputVerifierTest230*/3132/**33* InputVerifierTest2.java34*35* summary: REGRESSION: InputVerifier and JOptionPane36*/3738import java.awt.AWTException;39import java.awt.BorderLayout;40import java.awt.Component;41import java.awt.Dialog;42import java.awt.Frame;43import java.awt.Point;44import java.awt.Robot;45import java.awt.TextArea;4647import java.awt.event.InputEvent;4849import javax.swing.InputVerifier;50import javax.swing.JButton;51import javax.swing.JComponent;52import javax.swing.JFrame;53import javax.swing.JTextField;54import javax.swing.JWindow;5556public class InputVerifierTest257{5859private static void init()60{61JFrame frame = new JFrame();62frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);63JTextField tf = new JTextField(10);64frame.getContentPane().add(tf);6566final JWindow w = new JWindow(frame);67JButton btn1 = new JButton("window");68btn1.setName("bnt1");69w.getContentPane().add(btn1);70w.pack();71w.setVisible(true);7273frame.setSize(200, 200);74frame.setVisible(true);757677Robot r = null;78try {79r = new Robot();80} catch (AWTException e) {81InputVerifierTest2.fail(e);82}838485try {86r.waitForIdle();87mouseClickOnComp(r, tf);88r.waitForIdle();8990if (!tf.isFocusOwner()) {91throw new RuntimeException("t1 is not a focus owner");92}9394tf.setInputVerifier(new InputVerifier() {95public boolean verify(JComponent input) {96System.err.println("verify on " + input);97throw new RuntimeException("InputVerifier should not be called");98}99});100btn1.requestFocus();101} catch (Exception e) {102InputVerifierTest2.fail(e);103}104105InputVerifierTest2.pass();106107}//End init()108109110static void mouseClickOnComp(Robot r, Component comp) {111Point loc = comp.getLocationOnScreen();112loc.x += comp.getWidth() / 2;113loc.y += comp.getHeight() / 2;114r.mouseMove(loc.x, loc.y);115r.delay(10);116r.mousePress(InputEvent.BUTTON1_MASK);117r.delay(10);118r.mouseRelease(InputEvent.BUTTON1_MASK);119}120121/*****************************************************122* Standard Test Machinery Section123* DO NOT modify anything in this section -- it's a124* standard chunk of code which has all of the125* synchronisation necessary for the test harness.126* By keeping it the same in all tests, it is easier127* to read and understand someone else's test, as128* well as insuring that all tests behave correctly129* with the test harness.130* There is a section following this for test-131* classes132******************************************************/133private static boolean theTestPassed = false;134private static boolean testGeneratedInterrupt = false;135private static String failureMessage = "";136137private static Thread mainThread = null;138139private static int sleepTime = 300000;140141// Not sure about what happens if multiple of this test are142// instantiated in the same VM. Being static (and using143// static vars), it aint gonna work. Not worrying about144// it for now.145public static void main( String args[] ) throws InterruptedException146{147mainThread = Thread.currentThread();148try149{150init();151}152catch( TestPassedException e )153{154//The test passed, so just return from main and harness will155// interepret this return as a pass156return;157}158//At this point, neither test pass nor test fail has been159// called -- either would have thrown an exception and ended the160// test, so we know we have multiple threads.161162//Test involves other threads, so sleep and wait for them to163// called pass() or fail()164try165{166Thread.sleep( sleepTime );167//Timed out, so fail the test168throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );169}170catch (InterruptedException e)171{172//The test harness may have interrupted the test. If so, rethrow the exception173// so that the harness gets it and deals with it.174if( ! testGeneratedInterrupt ) throw e;175176//reset flag in case hit this code more than once for some reason (just safety)177testGeneratedInterrupt = false;178179if ( theTestPassed == false )180{181throw new RuntimeException( failureMessage );182}183}184185}//main186187public static synchronized void setTimeoutTo( int seconds )188{189sleepTime = seconds * 1000;190}191192public static synchronized void pass()193{194System.out.println( "The test passed." );195System.out.println( "The test is over, hit Ctl-C to stop Java VM" );196//first check if this is executing in main thread197if ( mainThread == Thread.currentThread() )198{199//Still in the main thread, so set the flag just for kicks,200// and throw a test passed exception which will be caught201// and end the test.202theTestPassed = true;203throw new TestPassedException();204}205theTestPassed = true;206testGeneratedInterrupt = true;207mainThread.interrupt();208}//pass()209210public static synchronized void fail( Exception whyFailed )211{212System.out.println( "The test failed: " + whyFailed );213System.out.println( "The test is over, hit Ctl-C to stop Java VM" );214//check if this called from main thread215if ( mainThread == Thread.currentThread() )216{217//If main thread, fail now 'cause not sleeping218throw new RuntimeException( whyFailed );219}220theTestPassed = false;221testGeneratedInterrupt = true;222failureMessage = whyFailed.toString();223mainThread.interrupt();224}//fail()225226}// class InputVerifierTest2227228//This exception is used to exit from any level of call nesting229// when it's determined that the test has passed, and immediately230// end the test.231class TestPassedException extends RuntimeException232{233}234235236