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