Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/6382144/EndlessLoopTest.java
48230 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 638214426@summary REGRESSION: InputVerifier and JOptionPane27@author oleg.sukhodolsky: area=awt.focus28@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;5859public class EndlessLoopTest60{6162//*** test-writer defined static variables go here ***63static volatile int n_iv_calls;646566private static void init()67{68//*** Create instructions for the user here ***6970String[] instructions =71{72"This is an AUTOMATIC test, simply wait until it is done.",73"The result (passed or failed) will be shown in the",74"message window below."75};76Sysout.createDialog( );77Sysout.printInstructions( instructions );7879JFrame frame = new JFrame();80final JDialog dialog = new JDialog(frame, true);81JButton button = new JButton("press me");82button.addActionListener(new ActionListener() {83public void actionPerformed(ActionEvent ae) {84dialog.dispose();85}86});87dialog.getContentPane().add(button);88dialog.pack();8990JTextField t1 = new JTextField();91t1.setInputVerifier(new InputVerifier() {92public boolean verify(JComponent input) {93n_iv_calls++;94if (n_iv_calls == 1) {95dialog.setVisible(true);96}97return true;98}99});100JTextField t2 = new JTextField();101102103frame.getContentPane().add(t1, BorderLayout.NORTH);104frame.getContentPane().add(t2, BorderLayout.SOUTH);105frame.setSize(200, 200);106frame.setVisible(true);107108Robot r = null;109try {110r = new Robot();111} catch (AWTException e) {112EndlessLoopTest.fail(e);113}114115try {116r.waitForIdle();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.delay(10);127r.keyRelease(KeyEvent.VK_TAB);128r.waitForIdle();129130mouseClickOnComp(r, button);131r.waitForIdle();132} catch (Exception e) {133EndlessLoopTest.fail(e);134}135136if (n_iv_calls != 1) {137EndlessLoopTest.fail(new RuntimeException("InputVerifier was called " + n_iv_calls + " times"));138}139140EndlessLoopTest.pass();141142}//End init()143144145static void mouseClickOnComp(Robot r, Component comp) {146Point loc = comp.getLocationOnScreen();147loc.x += comp.getWidth() / 2;148loc.y += comp.getHeight() / 2;149r.mouseMove(loc.x, loc.y);150r.delay(10);151r.mousePress(InputEvent.BUTTON1_MASK);152r.delay(10);153r.mouseRelease(InputEvent.BUTTON1_MASK);154}155156/*****************************************************157* Standard Test Machinery Section158* DO NOT modify anything in this section -- it's a159* standard chunk of code which has all of the160* synchronisation necessary for the test harness.161* By keeping it the same in all tests, it is easier162* to read and understand someone else's test, as163* well as insuring that all tests behave correctly164* with the test harness.165* There is a section following this for test-166* classes167******************************************************/168private static boolean theTestPassed = false;169private static boolean testGeneratedInterrupt = false;170private static String failureMessage = "";171172private static Thread mainThread = null;173174private static int sleepTime = 300000;175176// Not sure about what happens if multiple of this test are177// instantiated in the same VM. Being static (and using178// static vars), it aint gonna work. Not worrying about179// it for now.180public static void main( String args[] ) throws InterruptedException181{182mainThread = Thread.currentThread();183try184{185init();186}187catch( TestPassedException e )188{189//The test passed, so just return from main and harness will190// interepret this return as a pass191return;192}193//At this point, neither test pass nor test fail has been194// called -- either would have thrown an exception and ended the195// test, so we know we have multiple threads.196197//Test involves other threads, so sleep and wait for them to198// called pass() or fail()199try200{201Thread.sleep( sleepTime );202//Timed out, so fail the test203throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" );204}205catch (InterruptedException e)206{207//The test harness may have interrupted the test. If so, rethrow the exception208// so that the harness gets it and deals with it.209if( ! testGeneratedInterrupt ) throw e;210211//reset flag in case hit this code more than once for some reason (just safety)212testGeneratedInterrupt = false;213214if ( theTestPassed == false )215{216throw new RuntimeException( failureMessage );217}218}219220}//main221222public static synchronized void setTimeoutTo( int seconds )223{224sleepTime = seconds * 1000;225}226227public static synchronized void pass()228{229Sysout.println( "The test passed." );230Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );231//first check if this is executing in main thread232if ( mainThread == Thread.currentThread() )233{234//Still in the main thread, so set the flag just for kicks,235// and throw a test passed exception which will be caught236// and end the test.237theTestPassed = true;238throw new TestPassedException();239}240theTestPassed = true;241testGeneratedInterrupt = true;242mainThread.interrupt();243}//pass()244245public static synchronized void fail( Exception whyFailed )246{247Sysout.println( "The test failed: " + whyFailed );248Sysout.println( "The test is over, hit Ctl-C to stop Java VM" );249//check if this called from main thread250if ( mainThread == Thread.currentThread() )251{252//If main thread, fail now 'cause not sleeping253throw new RuntimeException( whyFailed );254}255theTestPassed = false;256testGeneratedInterrupt = true;257failureMessage = whyFailed.toString();258mainThread.interrupt();259}//fail()260261}// class EndlessLoopTest262263//This exception is used to exit from any level of call nesting264// when it's determined that the test has passed, and immediately265// end the test.266class TestPassedException extends RuntimeException267{268}269270//*********** End Standard Test Machinery Section **********271272/****************************************************273Standard Test Machinery274DO NOT modify anything below -- it's a standard275chunk of code whose purpose is to make user276interaction uniform, and thereby make it simpler277to read and understand someone else's test.278****************************************************/279280/**281This is part of the standard test machinery.282It creates a dialog (with the instructions), and is the interface283for sending text messages to the user.284To print the instructions, send an array of strings to Sysout.createDialog285WithInstructions method. Put one line of instructions per array entry.286To display a message for the tester to see, simply call Sysout.println287with the string to be displayed.288This mimics System.out.println but works within the test harness as well289as standalone.290*/291292class Sysout293{294private static TestDialog dialog;295296public static void createDialogWithInstructions( String[] instructions )297{298dialog = new TestDialog( new Frame(), "Instructions" );299dialog.printInstructions( instructions );300dialog.setVisible(true);301println( "Any messages for the tester will display here." );302}303304public static void createDialog( )305{306dialog = new TestDialog( new Frame(), "Instructions" );307String[] defInstr = { "Instructions will appear here. ", "" } ;308dialog.printInstructions( defInstr );309dialog.setVisible(true);310println( "Any messages for the tester will display here." );311}312313314public static void printInstructions( String[] instructions )315{316dialog.printInstructions( instructions );317}318319320public static void println( String messageIn )321{322dialog.displayMessage( messageIn );323System.out.println(messageIn);324}325326}// Sysout class327328/**329This is part of the standard test machinery. It provides a place for the330test instructions to be displayed, and a place for interactive messages331to the user to be displayed.332To have the test instructions displayed, see Sysout.333To have a message to the user be displayed, see Sysout.334Do not call anything in this dialog directly.335*/336class TestDialog extends Dialog337{338339TextArea instructionsText;340TextArea messageText;341int maxStringLength = 80;342343//DO NOT call this directly, go through Sysout344public TestDialog( Frame frame, String name )345{346super( frame, name );347int scrollBoth = TextArea.SCROLLBARS_BOTH;348instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );349add( "North", instructionsText );350351messageText = new TextArea( "", 5, maxStringLength, scrollBoth );352add("Center", messageText);353354pack();355356setVisible(true);357}// TestDialog()358359//DO NOT call this directly, go through Sysout360public void printInstructions( String[] instructions )361{362//Clear out any current instructions363instructionsText.setText( "" );364365//Go down array of instruction strings366367String printStr, remainingStr;368for( int i=0; i < instructions.length; i++ )369{370//chop up each into pieces maxSringLength long371remainingStr = instructions[ i ];372while( remainingStr.length() > 0 )373{374//if longer than max then chop off first max chars to print375if( remainingStr.length() >= maxStringLength )376{377//Try to chop on a word boundary378int posOfSpace = remainingStr.379lastIndexOf( ' ', maxStringLength - 1 );380381if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;382383printStr = remainingStr.substring( 0, posOfSpace + 1 );384remainingStr = remainingStr.substring( posOfSpace + 1 );385}386//else just print387else388{389printStr = remainingStr;390remainingStr = "";391}392393instructionsText.append( printStr + "\n" );394395}// while396397}// for398399}//printInstructions()400401//DO NOT call this directly, go through Sysout402public void displayMessage( String messageIn )403{404messageText.append( messageIn + "\n" );405System.out.println(messageIn);406}407408}// TestDialog class409410411