1 /*
   2   test
   3   @bug       6346690
   4   @summary   Tests that key_typed is consumed after mnemonic key_pressed is handled for a menu item.
   5   @author    anton.tarasov@sun.com: area=awt-focus
   6   @run       applet ConsumeNextMnemonicKeyTypedTest.html
   7 */
   8 
   9 import java.awt.*;
  10 import javax.swing.*;
  11 import java.awt.event.*;
  12 import java.applet.Applet;
  13 import sun.awt.SunToolkit;
  14 
  15 
  16 public class ConsumeNextMnemonicKeyTypedTest extends Applet {
  17     Robot robot;
  18     JFrame frame = new JFrame("Test Frame");
  19     JTextField text = new JTextField();
  20     JMenuBar bar = new JMenuBar();
  21     JMenu menu = new JMenu("Menu");
  22     JMenuItem item = new JMenuItem("item");
  23 
  24     public static void main(String[] args) {
  25         ConsumeNextMnemonicKeyTypedTest app = new ConsumeNextMnemonicKeyTypedTest();
  26         app.init();
  27         app.start();
  28     }
  29 
  30     public void init() {
  31         try {
  32             robot = new Robot();
  33         } catch (AWTException e) {
  34             throw new RuntimeException("Error: unable to create robot", e);
  35         }
  36         // Create instructions for the user here, as well as set up
  37         // the environment -- set the layout manager, add buttons,
  38         // etc.
  39         this.setLayout (new BorderLayout ());
  40         Sysout.createDialogWithInstructions(new String[]
  41             {"Automatic test. Simply wait until it's done."});
  42     }
  43 
  44     public void start() {
  45         menu.setMnemonic('f');
  46         item.setMnemonic('i');
  47         menu.add(item);
  48         bar.add(menu);
  49 
  50         frame.add(text);
  51         frame.setJMenuBar(bar);
  52         frame.pack();
  53 
  54         frame.setLocation(800, 0);
  55         frame.setVisible(true);
  56 
  57         test();
  58     }
  59 
  60     void test() {
  61         SunToolkit toolkit = (SunToolkit)Toolkit.getDefaultToolkit();
  62 
  63         toolkit.realSync();
  64 
  65         if (!text.isFocusOwner()) {
  66             robot.mouseMove(text.getLocationOnScreen().x + 5, text.getLocationOnScreen().y + 5);
  67             robot.delay(100);
  68             robot.mousePress(MouseEvent.BUTTON1_MASK);
  69             robot.delay(100);
  70             robot.mouseRelease(MouseEvent.BUTTON1_MASK);
  71 
  72             int iter = 10;
  73             while (!text.isFocusOwner() && iter-- > 0) {
  74                 robot.delay(200);
  75             }
  76             if (iter <= 0) {
  77                 Sysout.println("Test: text field couldn't be focused!");
  78                 return;
  79             }
  80         }
  81 
  82         robot.keyPress(KeyEvent.VK_A);
  83         robot.delay(100);
  84         robot.keyRelease(KeyEvent.VK_A);
  85 
  86         toolkit.realSync();
  87 
  88         String charA = text.getText();
  89         System.err.println("Test: character typed with VK_A: " + charA);
  90 
  91         robot.keyPress(KeyEvent.VK_BACK_SPACE);
  92         robot.delay(100);
  93         robot.keyRelease(KeyEvent.VK_BACK_SPACE);
  94 
  95         toolkit.realSync();
  96 
  97         robot.keyPress(KeyEvent.VK_ALT);
  98         robot.keyPress(KeyEvent.VK_F);
  99         robot.delay(100);
 100         robot.keyRelease(KeyEvent.VK_F);
 101         robot.keyRelease(KeyEvent.VK_ALT);
 102 
 103         toolkit.realSync();
 104 
 105         String string = text.getText();
 106 
 107         robot.keyPress(KeyEvent.VK_I);
 108         robot.delay(100);
 109         robot.keyRelease(KeyEvent.VK_I);
 110 
 111         toolkit.realSync();
 112 
 113         Sysout.println("Test: character typed after mnemonic key press: " + text.getText());
 114 
 115         if (!text.getText().equals(string)) {
 116             throw new RuntimeException("Test failed!");
 117         }
 118 
 119         robot.keyPress(KeyEvent.VK_A);
 120         robot.delay(100);
 121         robot.keyRelease(KeyEvent.VK_A);
 122 
 123         toolkit.realSync();
 124 
 125         System.err.println("Test: chracter typed with VK_A: " + text.getText());
 126 
 127         if (!charA.equals(text.getText())) {
 128             throw new RuntimeException("Test failed!");
 129         }
 130 
 131         Sysout.println("Test passed.");
 132     }
 133 }
 134 
 135 /****************************************************
 136  Standard Test Machinery
 137  DO NOT modify anything below -- it's a standard
 138   chunk of code whose purpose is to make user
 139   interaction uniform, and thereby make it simpler
 140   to read and understand someone else's test.
 141  ****************************************************/
 142 
 143 /**
 144  This is part of the standard test machinery.
 145  It creates a dialog (with the instructions), and is the interface
 146   for sending text messages to the user.
 147  To print the instructions, send an array of strings to Sysout.createDialog
 148   WithInstructions method.  Put one line of instructions per array entry.
 149  To display a message for the tester to see, simply call Sysout.println
 150   with the string to be displayed.
 151  This mimics System.out.println but works within the test harness as well
 152   as standalone.
 153  */
 154 
 155 class Sysout
 156 {
 157     static TestDialog dialog;
 158 
 159     public static void createDialogWithInstructions( String[] instructions )
 160     {
 161         dialog = new TestDialog( new Frame(), "Instructions" );
 162         dialog.printInstructions( instructions );
 163         dialog.setVisible(true);
 164         println( "Any messages for the tester will display here." );
 165     }
 166 
 167     public static void createDialog( )
 168     {
 169         dialog = new TestDialog( new Frame(), "Instructions" );
 170         String[] defInstr = { "Instructions will appear here. ", "" } ;
 171         dialog.printInstructions( defInstr );
 172         dialog.setVisible(true);
 173         println( "Any messages for the tester will display here." );
 174     }
 175 
 176 
 177     public static void printInstructions( String[] instructions )
 178     {
 179         dialog.printInstructions( instructions );
 180     }
 181 
 182 
 183     public static void println( String messageIn )
 184     {
 185         dialog.displayMessage( messageIn );
 186     }
 187 
 188 }// Sysout  class
 189 
 190 /**
 191   This is part of the standard test machinery.  It provides a place for the
 192    test instructions to be displayed, and a place for interactive messages
 193    to the user to be displayed.
 194   To have the test instructions displayed, see Sysout.
 195   To have a message to the user be displayed, see Sysout.
 196   Do not call anything in this dialog directly.
 197   */
 198 class TestDialog extends Dialog
 199 {
 200 
 201     TextArea instructionsText;
 202     TextArea messageText;
 203     int maxStringLength = 80;
 204 
 205     //DO NOT call this directly, go through Sysout
 206     public TestDialog( Frame frame, String name )
 207     {
 208         super( frame, name );
 209         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 210         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 211         add( "North", instructionsText );
 212 
 213         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 214         add("Center", messageText);
 215 
 216         pack();
 217 
 218         setVisible(true);
 219     }// TestDialog()
 220 
 221     //DO NOT call this directly, go through Sysout
 222     public void printInstructions( String[] instructions )
 223     {
 224         //Clear out any current instructions
 225         instructionsText.setText( "" );
 226 
 227         //Go down array of instruction strings
 228 
 229         String printStr, remainingStr;
 230         for( int i=0; i < instructions.length; i++ )
 231         {
 232             //chop up each into pieces maxSringLength long
 233             remainingStr = instructions[ i ];
 234             while( remainingStr.length() > 0 )
 235             {
 236                 //if longer than max then chop off first max chars to print
 237                 if( remainingStr.length() >= maxStringLength )
 238                 {
 239                     //Try to chop on a word boundary
 240                     int posOfSpace = remainingStr.
 241                         lastIndexOf( ' ', maxStringLength - 1 );
 242 
 243                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 244 
 245                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
 246                     remainingStr = remainingStr.substring( posOfSpace + 1 );
 247                 }
 248                 //else just print
 249                 else
 250                 {
 251                     printStr = remainingStr;
 252                     remainingStr = "";
 253                 }
 254 
 255                 instructionsText.append( printStr + "\n" );
 256 
 257             }// while
 258 
 259         }// for
 260 
 261     }//printInstructions()
 262 
 263     //DO NOT call this directly, go through Sysout
 264     public void displayMessage( String messageIn )
 265     {
 266         messageText.append( messageIn + "\n" );
 267         System.out.println(messageIn);
 268     }
 269 
 270 }// TestDialog  class