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