Path: blob/master/test/jdk/java/awt/Focus/InputVerifierTest3/InputVerifierTest3.java
41152 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 643266527@summary Inputverifier is not executed when focus owner is removed28@author oleg.sukhodolsky: area=awt.focus29@library ../../regtesthelpers30@build Util31@run main InputVerifierTest332*/3334/**35* InputVerifierTest3.java36*37* summary: Inputverifier is not executed when focus owner is removed38*/3940import java.awt.AWTException;41import java.awt.BorderLayout;42import java.awt.Component;43import java.awt.Dialog;44import java.awt.FlowLayout;45import java.awt.Frame;46import java.awt.KeyboardFocusManager;47import java.awt.Point;48import java.awt.Robot;49import java.awt.TextArea;50import java.awt.Toolkit;5152import java.awt.event.InputEvent;5354import javax.swing.InputVerifier;55import javax.swing.JComponent;56import javax.swing.JFrame;57import javax.swing.JTextField;5859import test.java.awt.regtesthelpers.Util;6061public class InputVerifierTest362{63static volatile boolean verifier_called = false;6465private static void init()66{67//*** Create instructions for the user here ***6869JFrame frame = new JFrame();70frame.getContentPane().setLayout(new FlowLayout());71JTextField tf1 = new JTextField(10);72tf1.setInputVerifier(new InputVerifier() {73public boolean verify(JComponent input) {74System.err.println("verify on " + input);75verifier_called = true;76return true;77}78});79frame.getContentPane().add(tf1);80JTextField tf2 = new JTextField(10);81frame.getContentPane().add(tf2);8283frame.setSize(200, 200);84frame.setVisible(true);8586Robot r = null;87try {88r = new Robot();89} catch (AWTException e) {90InputVerifierTest3.fail(e);91}929394try {95Util.waitForIdle(r);96Util.clickOnComp(tf1, r);97Util.waitForIdle(r);9899100if (!tf1.isFocusOwner()) {101System.out.println("focus owner = " + KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());102throw new RuntimeException("tf1 is not a focus owner");103}104105frame.getContentPane().remove(tf1);106Util.waitForIdle(r);107108if (!tf2.isFocusOwner()) {109System.out.println("focus owner = " + KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());110throw new RuntimeException("tf2 is not a focus owner");111}112113if (!verifier_called) {114throw new RuntimeException("verifier was not called");115}116117} catch (Exception e) {118InputVerifierTest3.fail(e);119}120121InputVerifierTest3.pass();122123}//End init()124125/*****************************************************126* Standard Test Machinery Section127* DO NOT modify anything in this section -- it's a128* standard chunk of code which has all of the129* synchronisation necessary for the test harness.130* By keeping it the same in all tests, it is easier131* to read and understand someone else's test, as132* well as insuring that all tests behave correctly133* with the test harness.134* There is a section following this for test-135* classes136******************************************************/137private static boolean theTestPassed = false;138private static boolean testGeneratedInterrupt = false;139private static String failureMessage = "";140141private static Thread mainThread = null;142143private static int sleepTime = 300000;144145// Not sure about what happens if multiple of this test are146// instantiated in the same VM. Being static (and using147// static vars), it aint gonna work. Not worrying about148// it for now.149public static void main( String args[] ) throws InterruptedException150{151mainThread = Thread.currentThread();152try153{154init();155}156catch( TestPassedException e )157{158//The test passed, so just return from main and harness will159// interepret this return as a pass160return;161}162//At this point, neither test pass nor test fail has been163// called -- either would have thrown an exception and ended the164// test, so we know we have multiple threads.165166//Test involves other threads, so sleep and wait for them to167// called pass() or fail()168try169{170Thread.sleep( sleepTime );171//Timed out, so fail the test172throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );173}174catch (InterruptedException e)175{176//The test harness may have interrupted the test. If so, rethrow the exception177// so that the harness gets it and deals with it.178if( ! testGeneratedInterrupt ) throw e;179180//reset flag in case hit this code more than once for some reason (just safety)181testGeneratedInterrupt = false;182183if ( theTestPassed == false )184{185throw new RuntimeException( failureMessage );186}187}188189}//main190191public static synchronized void setTimeoutTo( int seconds )192{193sleepTime = seconds * 1000;194}195196public static synchronized void pass()197{198System.out.println( "The test passed." );199System.out.println( "The test is over, hit Ctl-C to stop Java VM" );200//first check if this is executing in main thread201if ( mainThread == Thread.currentThread() )202{203//Still in the main thread, so set the flag just for kicks,204// and throw a test passed exception which will be caught205// and end the test.206theTestPassed = true;207throw new TestPassedException();208}209theTestPassed = true;210testGeneratedInterrupt = true;211mainThread.interrupt();212}//pass()213214public static synchronized void fail( Exception whyFailed )215{216System.out.println( "The test failed: " + whyFailed );217System.out.println( "The test is over, hit Ctl-C to stop Java VM" );218//check if this called from main thread219if ( mainThread == Thread.currentThread() )220{221//If main thread, fail now 'cause not sleeping222throw new RuntimeException( whyFailed );223}224theTestPassed = false;225testGeneratedInterrupt = true;226failureMessage = whyFailed.toString();227mainThread.interrupt();228}//fail()229230}// class InputVerifierTest3231232//This exception is used to exit from any level of call nesting233// when it's determined that the test has passed, and immediately234// end the test.235class TestPassedException extends RuntimeException236{237}238239240