1 /* 2 * Copyright (c) 2006, 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 @bug 6432665 27 @summary Inputverifier is not executed when focus owner is removed 28 @author oleg.sukhodolsky: area=awt.focus 29 @library ../../regtesthelpers 30 @build Util 31 @run main InputVerifierTest3 32 */ 33 34 /** 35 * InputVerifierTest3.java 36 * 37 * summary: Inputverifier is not executed when focus owner is removed 38 */ 39 40 import java.awt.AWTException; 41 import java.awt.BorderLayout; 42 import java.awt.Component; 43 import java.awt.Dialog; 44 import java.awt.FlowLayout; 45 import java.awt.Frame; 46 import java.awt.KeyboardFocusManager; 47 import java.awt.Point; 48 import java.awt.Robot; 49 import java.awt.TextArea; 50 import java.awt.Toolkit; 51 52 import java.awt.event.InputEvent; 53 54 import javax.swing.InputVerifier; 55 import javax.swing.JComponent; 56 import javax.swing.JFrame; 57 import javax.swing.JTextField; 58 59 import test.java.awt.regtesthelpers.Util; 60 61 public class InputVerifierTest3 62 { 63 static volatile boolean verifier_called = false; 64 65 private static void init() 66 { 67 //*** Create instructions for the user here *** 68 69 String[] instructions = 70 { 71 "This is an AUTOMATIC test, simply wait until it is done.", 72 "The result (passed or failed) will be shown in the", 73 "message window below." 74 }; 75 Sysout.createDialog( ); 76 Sysout.printInstructions( instructions ); 77 78 JFrame frame = new JFrame(); 79 frame.getContentPane().setLayout(new FlowLayout()); 80 JTextField tf1 = new JTextField(10); 81 tf1.setInputVerifier(new InputVerifier() { 82 public boolean verify(JComponent input) { 83 System.err.println("verify on " + input); 84 verifier_called = true; 85 return true; 86 } 87 }); 88 frame.getContentPane().add(tf1); 89 JTextField tf2 = new JTextField(10); 90 frame.getContentPane().add(tf2); 91 92 frame.setSize(200, 200); 93 frame.setVisible(true); 94 95 Robot r = null; 96 try { 97 r = new Robot(); 98 } catch (AWTException e) { 99 InputVerifierTest3.fail(e); 100 } 101 102 103 try { 104 Util.waitForIdle(r); 105 Util.clickOnComp(tf1, r); 106 Util.waitForIdle(r); 107 108 109 if (!tf1.isFocusOwner()) { 110 System.out.println("focus owner = " + KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()); 111 throw new RuntimeException("tf1 is not a focus owner"); 112 } 113 114 frame.getContentPane().remove(tf1); 115 Util.waitForIdle(r); 116 117 if (!tf2.isFocusOwner()) { 118 System.out.println("focus owner = " + KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()); 119 throw new RuntimeException("tf2 is not a focus owner"); 120 } 121 122 if (!verifier_called) { 123 throw new RuntimeException("verifier was not called"); 124 } 125 126 } catch (Exception e) { 127 InputVerifierTest3.fail(e); 128 } 129 130 InputVerifierTest3.pass(); 131 132 }//End init() 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 InputVerifierTest3 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