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