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