1 /* 2 * Copyright (c) 2015, 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 * @key headful 27 * @bug 8068283 28 * @summary Checks that <Alt>+Char accelerators work when pressed in a text component 29 * @author Anton Nashatyrev 30 * @modules java.desktop/sun.awt 31 * @run main AltCharAcceleratorTest 32 */ 33 34 import sun.awt.SunToolkit; 35 36 import javax.swing.*; 37 import java.awt.*; 38 import java.awt.event.*; 39 import java.util.concurrent.CountDownLatch; 40 import java.util.concurrent.TimeUnit; 41 42 public class AltCharAcceleratorTest { 43 44 boolean action1 = false; 45 boolean action2 = false; 46 47 CountDownLatch focusLatch = new CountDownLatch(1); 48 CountDownLatch actionLatch = new CountDownLatch(2); 49 50 public AltCharAcceleratorTest() throws Exception { 51 SwingUtilities.invokeAndWait(new Runnable() { 52 @Override 53 public void run() { 54 JFrame f = new JFrame("frame"); 55 final JTextField t = new JTextField(); 56 JMenuBar mb = new JMenuBar(); 57 JMenu m1 = new JMenu("File"); 58 JMenuItem i1 = new JMenuItem("Save"); 59 JMenuItem i2 = new JMenuItem("Load"); 60 61 i1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, KeyEvent.ALT_MASK)); 62 i2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, KeyEvent.ALT_MASK)); 63 64 i1.addActionListener(new ActionListener() { 65 @Override 66 public void actionPerformed(ActionEvent e) { 67 System.out.println("Action1!"); 68 action1 = true; 69 actionLatch.countDown(); 70 } 71 }); 72 73 i2.addActionListener(new ActionListener() { 74 @Override 75 public void actionPerformed(ActionEvent e) { 76 System.out.println("Action2!"); 77 action2 = true; 78 actionLatch.countDown(); 79 } 80 }); 81 82 t.addFocusListener(new FocusAdapter() { 83 @Override 84 public void focusGained(FocusEvent e) { 85 System.out.println("Focused!"); 86 focusLatch.countDown(); 87 } 88 }); 89 90 t.setColumns(10); 91 t.requestFocusInWindow(); 92 93 f.setJMenuBar(mb); 94 mb.add(m1); 95 m1.add(i1); 96 m1.add(i2); 97 98 f.setLayout(new FlowLayout()); 99 f.add(t); 100 f.setSize(200, 200); 101 102 f.setVisible(true); 103 } 104 }); 105 } 106 107 void test() throws Exception { 108 ((SunToolkit) Toolkit.getDefaultToolkit()).realSync(); 109 110 focusLatch.await(5, TimeUnit.SECONDS); 111 112 Robot robot = new Robot(); 113 robot.setAutoDelay(100); 114 115 robot.keyPress(KeyEvent.VK_ALT); 116 robot.keyPress(KeyEvent.VK_T); 117 robot.keyRelease(KeyEvent.VK_T); 118 robot.keyRelease(KeyEvent.VK_ALT); 119 120 robot.keyPress(KeyEvent.VK_ALT); 121 robot.keyPress(KeyEvent.VK_F); 122 robot.keyRelease(KeyEvent.VK_F); 123 robot.keyRelease(KeyEvent.VK_ALT); 124 125 actionLatch.await(5, TimeUnit.SECONDS); 126 127 if (!action1 || !action2) { 128 throw new RuntimeException("Actions not performed"); 129 } 130 131 System.out.println("Passed."); 132 } 133 134 public static void main(String[] args) throws Exception { 135 AltCharAcceleratorTest t = new AltCharAcceleratorTest(); 136 t.test(); 137 } 138 }