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