1 /* 2 @test 3 @bug 6382144 4 @summary REGRESSION: InputVerifier and JOptionPane 5 @author oleg.sukhodolsky: area=awt.focus 6 @run main EndlessLoopTest 7 */ 8 9 /** 10 * EndlessLoopTest.java 11 * 12 * summary: REGRESSION: InputVerifier and JOptionPane 13 */ 14 15 import java.awt.AWTException; 16 import java.awt.BorderLayout; 17 import java.awt.Component; 18 import java.awt.Dialog; 19 import java.awt.Frame; 20 import java.awt.Point; 21 import java.awt.Robot; 22 import java.awt.TextArea; 23 import java.awt.Toolkit; 24 25 import java.awt.event.ActionEvent; 26 import java.awt.event.ActionListener; 27 import java.awt.event.InputEvent; 28 import java.awt.event.KeyEvent; 29 30 import javax.swing.InputVerifier; 31 import javax.swing.JButton; 32 import javax.swing.JComponent; 33 import javax.swing.JDialog; 34 import javax.swing.JFrame; 35 import javax.swing.JTextField; 36 37 public class EndlessLoopTest 38 { 39 40 //*** test-writer defined static variables go here *** 41 static volatile int n_iv_calls; 42 43 44 private static void init() 45 { 46 //*** Create instructions for the user here *** 47 48 String[] instructions = 49 { 50 "This is an AUTOMATIC test, simply wait until it is done.", 51 "The result (passed or failed) will be shown in the", 52 "message window below." 53 }; 54 Sysout.createDialog( ); 55 Sysout.printInstructions( instructions ); 56 57 JFrame frame = new JFrame(); 58 final JDialog dialog = new JDialog(frame, true); 59 JButton button = new JButton("press me"); 60 button.addActionListener(new ActionListener() { 61 public void actionPerformed(ActionEvent ae) { 62 dialog.dispose(); 63 } 64 }); 65 dialog.getContentPane().add(button); 66 dialog.pack(); 67 68 JTextField t1 = new JTextField(); 69 t1.setInputVerifier(new InputVerifier() { 70 public boolean verify(JComponent input) { 71 n_iv_calls++; 72 if (n_iv_calls == 1) { 73 dialog.setVisible(true); 74 } 75 return true; 76 } 77 }); 78 JTextField t2 = new JTextField(); 79 80 81 frame.getContentPane().add(t1, BorderLayout.NORTH); 82 frame.getContentPane().add(t2, BorderLayout.SOUTH); 83 frame.setSize(200, 200); 84 frame.setVisible(true); 85 86 Robot r = null; 87 try { 88 r = new Robot(); 89 } catch (AWTException e) { 90 EndlessLoopTest.fail(e); 91 } 92 93 try { 94 r.waitForIdle(); 95 96 mouseClickOnComp(r, t1); 97 r.waitForIdle(); 98 99 if (!t1.isFocusOwner()) { 100 throw new RuntimeException("t1 is not a focus owner"); 101 } 102 n_iv_calls = 0; 103 r.keyPress(KeyEvent.VK_TAB); 104 r.delay(10); 105 r.keyRelease(KeyEvent.VK_TAB); 106 r.waitForIdle(); 107 108 mouseClickOnComp(r, button); 109 r.waitForIdle(); 110 } catch (Exception e) { 111 EndlessLoopTest.fail(e); 112 } 113 114 if (n_iv_calls != 1) { 115 EndlessLoopTest.fail(new RuntimeException("InputVerifier was called " + n_iv_calls + " times")); 116 } 117 118 EndlessLoopTest.pass(); 119 120 }//End init() 121 122 123 static void mouseClickOnComp(Robot r, Component comp) { 124 Point loc = comp.getLocationOnScreen(); 125 loc.x += comp.getWidth() / 2; 126 loc.y += comp.getHeight() / 2; 127 r.mouseMove(loc.x, loc.y); 128 r.delay(10); 129 r.mousePress(InputEvent.BUTTON1_MASK); 130 r.delay(10); 131 r.mouseRelease(InputEvent.BUTTON1_MASK); 132 } 133 134 /***************************************************** 135 * Standard Test Machinery Section 136 * DO NOT modify anything in this section -- it's a 137 * standard chunk of code which has all of the 138 * synchronisation necessary for the test harness. 139 * By keeping it the same in all tests, it is easier 140 * to read and understand someone else's test, as 141 * well as insuring that all tests behave correctly 142 * with the test harness. 143 * There is a section following this for test- 144 * classes 145 ******************************************************/ 146 private static boolean theTestPassed = false; 147 private static boolean testGeneratedInterrupt = false; 148 private static String failureMessage = ""; 149 150 private static Thread mainThread = null; 151 152 private static int sleepTime = 300000; 153 154 // Not sure about what happens if multiple of this test are 155 // instantiated in the same VM. Being static (and using 156 // static vars), it aint gonna work. Not worrying about 157 // it for now. 158 public static void main( String args[] ) throws InterruptedException 159 { 160 mainThread = Thread.currentThread(); 161 try 162 { 163 init(); 164 } 165 catch( TestPassedException e ) 166 { 167 //The test passed, so just return from main and harness will 168 // interepret this return as a pass 169 return; 170 } 171 //At this point, neither test pass nor test fail has been 172 // called -- either would have thrown an exception and ended the 173 // test, so we know we have multiple threads. 174 175 //Test involves other threads, so sleep and wait for them to 176 // called pass() or fail() 177 try 178 { 179 Thread.sleep( sleepTime ); 180 //Timed out, so fail the test 181 throw new RuntimeException( "Timed out after " + sleepTime/1000 + " seconds" ); 182 } 183 catch (InterruptedException e) 184 { 185 //The test harness may have interrupted the test. If so, rethrow the exception 186 // so that the harness gets it and deals with it. 187 if( ! testGeneratedInterrupt ) throw e; 188 189 //reset flag in case hit this code more than once for some reason (just safety) 190 testGeneratedInterrupt = false; 191 192 if ( theTestPassed == false ) 193 { 194 throw new RuntimeException( failureMessage ); 195 } 196 } 197 198 }//main 199 200 public static synchronized void setTimeoutTo( int seconds ) 201 { 202 sleepTime = seconds * 1000; 203 } 204 205 public static synchronized void pass() 206 { 207 Sysout.println( "The test passed." ); 208 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" ); 209 //first check if this is executing in main thread 210 if ( mainThread == Thread.currentThread() ) 211 { 212 //Still in the main thread, so set the flag just for kicks, 213 // and throw a test passed exception which will be caught 214 // and end the test. 215 theTestPassed = true; 216 throw new TestPassedException(); 217 } 218 theTestPassed = true; 219 testGeneratedInterrupt = true; 220 mainThread.interrupt(); 221 }//pass() 222 223 public static synchronized void fail( Exception whyFailed ) 224 { 225 Sysout.println( "The test failed: " + whyFailed ); 226 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" ); 227 //check if this called from main thread 228 if ( mainThread == Thread.currentThread() ) 229 { 230 //If main thread, fail now 'cause not sleeping 231 throw new RuntimeException( whyFailed ); 232 } 233 theTestPassed = false; 234 testGeneratedInterrupt = true; 235 failureMessage = whyFailed.toString(); 236 mainThread.interrupt(); 237 }//fail() 238 239 }// class EndlessLoopTest 240 241 //This exception is used to exit from any level of call nesting 242 // when it's determined that the test has passed, and immediately 243 // end the test. 244 class TestPassedException extends RuntimeException 245 { 246 } 247 248 //*********** End Standard Test Machinery Section ********** 249 250 /**************************************************** 251 Standard Test Machinery 252 DO NOT modify anything below -- it's a standard 253 chunk of code whose purpose is to make user 254 interaction uniform, and thereby make it simpler 255 to read and understand someone else's test. 256 ****************************************************/ 257 258 /** 259 This is part of the standard test machinery. 260 It creates a dialog (with the instructions), and is the interface 261 for sending text messages to the user. 262 To print the instructions, send an array of strings to Sysout.createDialog 263 WithInstructions method. Put one line of instructions per array entry. 264 To display a message for the tester to see, simply call Sysout.println 265 with the string to be displayed. 266 This mimics System.out.println but works within the test harness as well 267 as standalone. 268 */ 269 270 class Sysout 271 { 272 private static TestDialog dialog; 273 274 public static void createDialogWithInstructions( String[] instructions ) 275 { 276 dialog = new TestDialog( new Frame(), "Instructions" ); 277 dialog.printInstructions( instructions ); 278 dialog.setVisible(true); 279 println( "Any messages for the tester will display here." ); 280 } 281 282 public static void createDialog( ) 283 { 284 dialog = new TestDialog( new Frame(), "Instructions" ); 285 String[] defInstr = { "Instructions will appear here. ", "" } ; 286 dialog.printInstructions( defInstr ); 287 dialog.setVisible(true); 288 println( "Any messages for the tester will display here." ); 289 } 290 291 292 public static void printInstructions( String[] instructions ) 293 { 294 dialog.printInstructions( instructions ); 295 } 296 297 298 public static void println( String messageIn ) 299 { 300 dialog.displayMessage( messageIn ); 301 System.out.println(messageIn); 302 } 303 304 }// Sysout class 305 306 /** 307 This is part of the standard test machinery. It provides a place for the 308 test instructions to be displayed, and a place for interactive messages 309 to the user to be displayed. 310 To have the test instructions displayed, see Sysout. 311 To have a message to the user be displayed, see Sysout. 312 Do not call anything in this dialog directly. 313 */ 314 class TestDialog extends Dialog 315 { 316 317 TextArea instructionsText; 318 TextArea messageText; 319 int maxStringLength = 80; 320 321 //DO NOT call this directly, go through Sysout 322 public TestDialog( Frame frame, String name ) 323 { 324 super( frame, name ); 325 int scrollBoth = TextArea.SCROLLBARS_BOTH; 326 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); 327 add( "North", instructionsText ); 328 329 messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); 330 add("Center", messageText); 331 332 pack(); 333 334 setVisible(true); 335 }// TestDialog() 336 337 //DO NOT call this directly, go through Sysout 338 public void printInstructions( String[] instructions ) 339 { 340 //Clear out any current instructions 341 instructionsText.setText( "" ); 342 343 //Go down array of instruction strings 344 345 String printStr, remainingStr; 346 for( int i=0; i < instructions.length; i++ ) 347 { 348 //chop up each into pieces maxSringLength long 349 remainingStr = instructions[ i ]; 350 while( remainingStr.length() > 0 ) 351 { 352 //if longer than max then chop off first max chars to print 353 if( remainingStr.length() >= maxStringLength ) 354 { 355 //Try to chop on a word boundary 356 int posOfSpace = remainingStr. 357 lastIndexOf( ' ', maxStringLength - 1 ); 358 359 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; 360 361 printStr = remainingStr.substring( 0, posOfSpace + 1 ); 362 remainingStr = remainingStr.substring( posOfSpace + 1 ); 363 } 364 //else just print 365 else 366 { 367 printStr = remainingStr; 368 remainingStr = ""; 369 } 370 371 instructionsText.append( printStr + "\n" ); 372 373 }// while 374 375 }// for 376 377 }//printInstructions() 378 379 //DO NOT call this directly, go through Sysout 380 public void displayMessage( String messageIn ) 381 { 382 messageText.append( messageIn + "\n" ); 383 System.out.println(messageIn); 384 } 385 386 }// TestDialog class