1 /* 2 @test 3 @bug 6378278 4 @summary Apparent missing key events causing Bugster to break 5 @author oleg.sukhodolsky: area=awt.focus 6 @run main InputVerifierTest 7 */ 8 9 /** 10 * InputVerifierTest.java 11 * 12 * summary: Apparent missing key events causing Bugster to break 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.InputEvent; 26 import java.awt.event.KeyEvent; 27 28 import javax.swing.InputVerifier; 29 import javax.swing.JComponent; 30 import javax.swing.JFrame; 31 import javax.swing.JTextField; 32 33 public class InputVerifierTest 34 { 35 36 //*** test-writer defined static variables go here *** 37 static volatile boolean ivWasCalled = false; 38 39 private static void init() 40 { 41 //*** Create instructions for the user here *** 42 String[] instructions = 43 { 44 "This is an AUTOMATIC test, simply wait until it is done.", 45 "The result (passed or failed) will be shown in the", 46 "message window below." 47 }; 48 Sysout.createDialog( ); 49 Sysout.printInstructions( instructions ); 50 51 JFrame frame = new JFrame(); 52 JTextField t1 = new JTextField(); 53 t1.setInputVerifier(new InputVerifier() { 54 public boolean verify(JComponent input) { 55 Sysout.println("verify(" + input + ")"); 56 ivWasCalled = true; 57 return true; 58 } 59 }); 60 JTextField t2 = new JTextField(); 61 62 frame.getContentPane().add(t1, BorderLayout.NORTH); 63 frame.getContentPane().add(t2, BorderLayout.SOUTH); 64 frame.setSize(200, 200); 65 frame.setVisible(true); 66 67 Robot r = null; 68 try { 69 r = new Robot(); 70 } catch (AWTException e) { 71 e.printStackTrace(); 72 InputVerifierTest.fail(e.toString()); 73 } 74 75 try { 76 sun.awt.SunToolkit tk = (sun.awt.SunToolkit) Toolkit.getDefaultToolkit(); 77 tk.realSync(); 78 79 mouseClickOnComp(r, t1); 80 tk.realSync(); 81 82 if (!t1.isFocusOwner()) { 83 throw new RuntimeException("t1 is not a focus owner"); 84 } 85 ivWasCalled = false; 86 r.keyPress(KeyEvent.VK_TAB); 87 r.delay(10); 88 r.keyRelease(KeyEvent.VK_TAB); 89 tk.realSync(); 90 91 if (!t2.isFocusOwner()) { 92 throw new RuntimeException("t2 is not a focus owner"); 93 } 94 if (!ivWasCalled) { 95 throw new RuntimeException("InputVerifier was not called after tabbing"); 96 } 97 98 mouseClickOnComp(r, t1); 99 tk.realSync(); 100 101 if (!t1.isFocusOwner()) { 102 throw new RuntimeException("t1 is not a focus owner"); 103 } 104 105 ivWasCalled = false; 106 mouseClickOnComp(r, t2); 107 tk.realSync(); 108 if (!t2.isFocusOwner()) { 109 throw new RuntimeException("t2 is not a focus owner"); 110 } 111 if (!ivWasCalled) { 112 throw new RuntimeException("InputVErifier was not called after mouse press"); 113 } 114 } catch (Exception e) { 115 e.printStackTrace(); 116 InputVerifierTest.fail(e.toString()); 117 } 118 119 InputVerifierTest.pass(); 120 121 }//End init() 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() 224 { 225 //test writer didn't specify why test failed, so give generic 226 fail( "it just plain failed! :-)" ); 227 } 228 229 public static synchronized void fail( String whyFailed ) 230 { 231 Sysout.println( "The test failed: " + whyFailed ); 232 Sysout.println( "The test is over, hit Ctl-C to stop Java VM" ); 233 //check if this called from main thread 234 if ( mainThread == Thread.currentThread() ) 235 { 236 //If main thread, fail now 'cause not sleeping 237 throw new RuntimeException( whyFailed ); 238 } 239 theTestPassed = false; 240 testGeneratedInterrupt = true; 241 failureMessage = whyFailed; 242 mainThread.interrupt(); 243 }//fail() 244 245 }// class InputVerifierTest 246 247 //This exception is used to exit from any level of call nesting 248 // when it's determined that the test has passed, and immediately 249 // end the test. 250 class TestPassedException extends RuntimeException 251 { 252 } 253 254 //*********** End Standard Test Machinery Section ********** 255 256 /**************************************************** 257 Standard Test Machinery 258 DO NOT modify anything below -- it's a standard 259 chunk of code whose purpose is to make user 260 interaction uniform, and thereby make it simpler 261 to read and understand someone else's test. 262 ****************************************************/ 263 264 /** 265 This is part of the standard test machinery. 266 It creates a dialog (with the instructions), and is the interface 267 for sending text messages to the user. 268 To print the instructions, send an array of strings to Sysout.createDialog 269 WithInstructions method. Put one line of instructions per array entry. 270 To display a message for the tester to see, simply call Sysout.println 271 with the string to be displayed. 272 This mimics System.out.println but works within the test harness as well 273 as standalone. 274 */ 275 276 class Sysout 277 { 278 private static TestDialog dialog; 279 280 public static void createDialogWithInstructions( String[] instructions ) 281 { 282 dialog = new TestDialog( new Frame(), "Instructions" ); 283 dialog.printInstructions( instructions ); 284 dialog.setVisible(true); 285 println( "Any messages for the tester will display here." ); 286 } 287 288 public static void createDialog( ) 289 { 290 dialog = new TestDialog( new Frame(), "Instructions" ); 291 String[] defInstr = { "Instructions will appear here. ", "" } ; 292 dialog.printInstructions( defInstr ); 293 dialog.setVisible(true); 294 println( "Any messages for the tester will display here." ); 295 } 296 297 298 public static void printInstructions( String[] instructions ) 299 { 300 dialog.printInstructions( instructions ); 301 } 302 303 304 public static void println( String messageIn ) 305 { 306 dialog.displayMessage( messageIn ); 307 System.out.println(messageIn); 308 } 309 310 }// Sysout class 311 312 /** 313 This is part of the standard test machinery. It provides a place for the 314 test instructions to be displayed, and a place for interactive messages 315 to the user to be displayed. 316 To have the test instructions displayed, see Sysout. 317 To have a message to the user be displayed, see Sysout. 318 Do not call anything in this dialog directly. 319 */ 320 class TestDialog extends Dialog 321 { 322 323 TextArea instructionsText; 324 TextArea messageText; 325 int maxStringLength = 80; 326 327 //DO NOT call this directly, go through Sysout 328 public TestDialog( Frame frame, String name ) 329 { 330 super( frame, name ); 331 int scrollBoth = TextArea.SCROLLBARS_BOTH; 332 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth ); 333 add( "North", instructionsText ); 334 335 messageText = new TextArea( "", 5, maxStringLength, scrollBoth ); 336 add("Center", messageText); 337 338 pack(); 339 340 setVisible(true); 341 }// TestDialog() 342 343 //DO NOT call this directly, go through Sysout 344 public void printInstructions( String[] instructions ) 345 { 346 //Clear out any current instructions 347 instructionsText.setText( "" ); 348 349 //Go down array of instruction strings 350 351 String printStr, remainingStr; 352 for( int i=0; i < instructions.length; i++ ) 353 { 354 //chop up each into pieces maxSringLength long 355 remainingStr = instructions[ i ]; 356 while( remainingStr.length() > 0 ) 357 { 358 //if longer than max then chop off first max chars to print 359 if( remainingStr.length() >= maxStringLength ) 360 { 361 //Try to chop on a word boundary 362 int posOfSpace = remainingStr. 363 lastIndexOf( ' ', maxStringLength - 1 ); 364 365 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1; 366 367 printStr = remainingStr.substring( 0, posOfSpace + 1 ); 368 remainingStr = remainingStr.substring( posOfSpace + 1 ); 369 } 370 //else just print 371 else 372 { 373 printStr = remainingStr; 374 remainingStr = ""; 375 } 376 377 instructionsText.append( printStr + "\n" ); 378 379 }// while 380 381 }// for 382 383 }//printInstructions() 384 385 //DO NOT call this directly, go through Sysout 386 public void displayMessage( String messageIn ) 387 { 388 messageText.append( messageIn + "\n" ); 389 System.out.println(messageIn); 390 } 391 392 }// TestDialog class