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