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