Path: blob/master/test/jdk/java/awt/Focus/6382144/EndlessLoopTest.java
41152 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 638214427@summary REGRESSION: InputVerifier and JOptionPane28@run main EndlessLoopTest29*/3031/**32* EndlessLoopTest.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;45import java.awt.Toolkit;4647import java.awt.event.ActionEvent;48import java.awt.event.ActionListener;49import java.awt.event.InputEvent;50import java.awt.event.KeyEvent;5152import javax.swing.InputVerifier;53import javax.swing.JButton;54import javax.swing.JComponent;55import javax.swing.JDialog;56import javax.swing.JFrame;57import javax.swing.JTextField;58import javax.swing.SwingUtilities;5960public class EndlessLoopTest61{6263//*** test-writer defined static variables go here ***64static volatile int n_iv_calls;65static JFrame frame;66static JTextField t1;67static JButton button;6869private static void init() throws Exception70{71//*** Create instructions for the user here ***7273try {74SwingUtilities.invokeAndWait(() -> {75frame = new JFrame();76final JDialog dialog = new JDialog(frame, true);77button = new JButton("press me");78button.addActionListener(new ActionListener() {79public void actionPerformed(ActionEvent ae) {80dialog.dispose();81}82});83dialog.getContentPane().add(button);84dialog.setLocationRelativeTo(null);85dialog.pack();8687t1 = new JTextField();88t1.setInputVerifier(new InputVerifier() {89public boolean verify(JComponent input) {90n_iv_calls++;91if (n_iv_calls == 1) {92dialog.setVisible(true);93}94return true;95}96});97JTextField t2 = new JTextField();9899frame.getContentPane().add(t1, BorderLayout.NORTH);100frame.getContentPane().add(t2, BorderLayout.SOUTH);101frame.setLocationRelativeTo(null);102frame.setSize(200, 200);103frame.setVisible(true);104});105106Robot r = null;107try {108r = new Robot();109} catch (AWTException e) {110EndlessLoopTest.fail(e);111}112113try {114r.setAutoDelay(100);115r.waitForIdle();116r.delay(1000);117118mouseClickOnComp(r, t1);119r.waitForIdle();120121if (!t1.isFocusOwner()) {122throw new RuntimeException("t1 is not a focus owner");123}124n_iv_calls = 0;125r.keyPress(KeyEvent.VK_TAB);126r.keyRelease(KeyEvent.VK_TAB);127r.waitForIdle();128129mouseClickOnComp(r, button);130r.waitForIdle();131} catch (Exception e) {132EndlessLoopTest.fail(e);133}134135if (n_iv_calls != 1) {136EndlessLoopTest.fail(new RuntimeException("InputVerifier was called " + n_iv_calls + " times"));137}138139EndlessLoopTest.pass();140} finally {141SwingUtilities.invokeAndWait(() -> {142if (frame != null) {143frame.dispose();144}145});146}147}//End init()148149150static 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( Exception whyFailed )250{251System.out.println( "The test failed: " + whyFailed );252System.out.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.toString();262mainThread.interrupt();263}//fail()264265}// class EndlessLoopTest266267//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}273274275