1 /* 2 * Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 @test 26 @key headful 27 @bug 6382144 28 @summary REGRESSION: InputVerifier and JOptionPane 29 @author oleg.sukhodolsky: area=awt.focus 30 @run main EndlessLoopTest 31 */ 32 33 /** 34 * EndlessLoopTest.java 35 * 36 * summary: REGRESSION: InputVerifier and JOptionPane 37 */ 38 39 import java.awt.AWTException; 40 import java.awt.BorderLayout; 41 import java.awt.Component; 42 import java.awt.Dialog; 43 import java.awt.Frame; 44 import java.awt.Point; 45 import java.awt.Robot; 46 import java.awt.TextArea; 47 import java.awt.Toolkit; 48 49 import java.awt.event.ActionEvent; 50 import java.awt.event.ActionListener; 51 import java.awt.event.InputEvent; 52 import java.awt.event.KeyEvent; 53 54 import javax.swing.InputVerifier; 55 import javax.swing.JButton; 56 import javax.swing.JComponent; 57 import javax.swing.JDialog; 58 import javax.swing.JFrame; 59 import javax.swing.JTextField; 60 61 public class EndlessLoopTest 62 { 63 64 //*** test-writer defined static variables go here *** 65 static volatile int n_iv_calls; 66 67 68 private static void init() 69 { 70 //*** Create instructions for the user here *** 71 72 JFrame frame = new JFrame(); 73 final JDialog dialog = new JDialog(frame, true); 74 JButton button = new JButton("press me"); 75 button.addActionListener(new ActionListener() { 76 public void actionPerformed(ActionEvent ae) { 77 dialog.dispose(); 78 } 79 }); 80 dialog.getContentPane().add(button); 81 dialog.pack(); 82 83 JTextField t1 = new JTextField(); 84 t1.setInputVerifier(new InputVerifier() { 85 public boolean verify(JComponent input) { 86 n_iv_calls++; 87 if (n_iv_calls == 1) { 88 dialog.setVisible(true); 89 } 90 return true; 91 } 92 }); 93 JTextField t2 = new JTextField(); 94 95 96 frame.getContentPane().add(t1, BorderLayout.NORTH); 97 frame.getContentPane().add(t2, BorderLayout.SOUTH); 98 frame.setSize(200, 200); 99 frame.setVisible(true); 100 101 Robot r = null; 102 try { 103 r = new Robot(); 104 } catch (AWTException e) { 105 EndlessLoopTest.fail(e); 106 } 107 108 try { 109 r.waitForIdle(); 110 111 mouseClickOnComp(r, t1); 112 r.waitForIdle(); 113 114 if (!t1.isFocusOwner()) { 115 throw new RuntimeException("t1 is not a focus owner"); 116 } 117 n_iv_calls = 0; 118 r.keyPress(KeyEvent.VK_TAB); 119 r.delay(10); 120 r.keyRelease(KeyEvent.VK_TAB); 121 r.waitForIdle(); 122 123 mouseClickOnComp(r, button); 124 r.waitForIdle(); 125 } catch (Exception e) { 126 EndlessLoopTest.fail(e); 127 } 128 129 if (n_iv_calls != 1) { 130 EndlessLoopTest.fail(new RuntimeException("InputVerifier was called " + n_iv_calls + " times")); 131 } 132 133 EndlessLoopTest.pass(); 134 135 }//End init() 136 137 138 static void mouseClickOnComp(Robot r, Component comp) { 139 Point loc = comp.getLocationOnScreen(); 140 loc.x += comp.getWidth() / 2; 141 loc.y += comp.getHeight() / 2; 142 r.mouseMove(loc.x, loc.y); 143 r.delay(10); 144 r.mousePress(InputEvent.BUTTON1_MASK); 145 r.delay(10); 146 r.mouseRelease(InputEvent.BUTTON1_MASK); 147 } 148 149 /***************************************************** 150 * Standard Test Machinery Section 151 * DO NOT modify anything in this section -- it's a 152 * standard chunk of code which has all of the 153 * synchronisation necessary for the test harness. 154 * By keeping it the same in all tests, it is easier 155 * to read and understand someone else's test, as 156 * well as insuring that all tests behave correctly 157 * with the test harness. 158 * There is a section following this for test- 159 * classes 160 ******************************************************/ 161 private static boolean theTestPassed = false; 162 private static boolean testGeneratedInterrupt = false; 163 private static String failureMessage = ""; 164 165 private static Thread mainThread = null; 166 167 private static int sleepTime = 300000; 168 169 // Not sure about what happens if multiple of this test are 170 // instantiated in the same VM. Being static (and using 171 // static vars), it aint gonna work. Not worrying about 172 // it for now. 173 public static void main( String args[] ) throws InterruptedException 174 { 175 mainThread = Thread.currentThread(); 176 try 177 { 178 init(); 179 } 180 catch( TestPassedException e ) 181 { 182 //The test passed, so just return from main and harness will 183 // interepret this return as a pass 184 return; 185 } 186 //At this point, neither test pass nor test fail has been 187 // called -- either would have thrown an exception and ended the 188 // test, so we know we have multiple threads. 189 190 //Test involves other threads, so sleep and wait for them to 191 // called pass() or fail() 192 try 193 { 194 Thread.sleep( sleepTime ); 195 //Timed out, so fail the test 196 throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" ); 197 } 198 catch (InterruptedException e) 199 { 200 //The test harness may have interrupted the test. If so, rethrow the exception 201 // so that the harness gets it and deals with it. 202 if( ! testGeneratedInterrupt ) throw e; 203 204 //reset flag in case hit this code more than once for some reason (just safety) 205 testGeneratedInterrupt = false; 206 207 if ( theTestPassed == false ) 208 { 209 throw new RuntimeException( failureMessage ); 210 } 211 } 212 213 }//main 214 215 public static synchronized void setTimeoutTo( int seconds ) 216 { 217 sleepTime = seconds * 1000; 218 } 219 220 public static synchronized void pass() 221 { 222 System.out.println( "The test passed." ); 223 System.out.println( "The test is over, hit Ctl-C to stop Java VM" ); 224 //first check if this is executing in main thread 225 if ( mainThread == Thread.currentThread() ) 226 { 227 //Still in the main thread, so set the flag just for kicks, 228 // and throw a test passed exception which will be caught 229 // and end the test. 230 theTestPassed = true; 231 throw new TestPassedException(); 232 } 233 theTestPassed = true; 234 testGeneratedInterrupt = true; 235 mainThread.interrupt(); 236 }//pass() 237 238 public static synchronized void fail( Exception whyFailed ) 239 { 240 System.out.println( "The test failed: " + whyFailed ); 241 System.out.println( "The test is over, hit Ctl-C to stop Java VM" ); 242 //check if this called from main thread 243 if ( mainThread == Thread.currentThread() ) 244 { 245 //If main thread, fail now 'cause not sleeping 246 throw new RuntimeException( whyFailed ); 247 } 248 theTestPassed = false; 249 testGeneratedInterrupt = true; 250 failureMessage = whyFailed.toString(); 251 mainThread.interrupt(); 252 }//fail() 253 254 }// class EndlessLoopTest 255 256 //This exception is used to exit from any level of call nesting 257 // when it's determined that the test has passed, and immediately 258 // end the test. 259 class TestPassedException extends RuntimeException 260 { 261 }