test/jdk/javax/swing/event/RightAltKeyTest.java
changeset 50837 86897f8a6598
equal deleted inserted replaced
50836:b9456394d24f 50837:86897f8a6598
       
     1 /*
       
     2  * Copyright (c) 2018, 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 8194873
       
    28  * @requires (os.family == "Windows")
       
    29  * @summary Checks that right ALT (ALT_GRAPH) key works on Swing components
       
    30  * @run main RightAltKeyTest
       
    31  */
       
    32 
       
    33 import javax.swing.JFrame;
       
    34 import javax.swing.JMenu;
       
    35 import javax.swing.JMenuBar;
       
    36 import javax.swing.JMenuItem;
       
    37 import javax.swing.JOptionPane;
       
    38 import javax.swing.JPanel;
       
    39 import javax.swing.JTabbedPane;
       
    40 import javax.swing.JTextField;
       
    41 import javax.swing.JLabel;
       
    42 import javax.swing.SwingUtilities;
       
    43 import javax.swing.UIManager;
       
    44 import javax.swing.KeyStroke;
       
    45 import javax.swing.event.MenuEvent;
       
    46 import javax.swing.event.MenuListener;
       
    47 import java.awt.event.ActionEvent;
       
    48 import java.awt.event.ActionListener;
       
    49 import java.awt.event.FocusEvent;
       
    50 import java.awt.event.FocusListener;
       
    51 import java.awt.event.InputEvent;
       
    52 import java.awt.event.KeyEvent;
       
    53 import java.awt.GridLayout;
       
    54 import java.awt.Robot;
       
    55 import java.lang.reflect.InvocationTargetException;
       
    56 
       
    57 public class RightAltKeyTest {
       
    58 
       
    59     boolean action = false;
       
    60     JFrame frame;
       
    61 
       
    62     void testJMenu() {
       
    63         frame = new JFrame("Menu Frame");
       
    64         JMenuBar mb = new JMenuBar();
       
    65         JMenu m1 = new JMenu("File");
       
    66         JMenuItem i1 = new JMenuItem("Save");
       
    67         JMenuItem i2 = new JMenuItem("Load");
       
    68 
       
    69         m1.setMnemonic(KeyEvent.VK_F);
       
    70 
       
    71         m1.addMenuListener(new MenuListener() {
       
    72             @Override
       
    73             public void menuSelected(MenuEvent e) {
       
    74                 action = true;
       
    75                 disposeUI();
       
    76             }
       
    77 
       
    78             @Override
       
    79             public void menuDeselected(MenuEvent e) {
       
    80             }
       
    81 
       
    82             @Override
       
    83             public void menuCanceled(MenuEvent e) {
       
    84             }
       
    85         });
       
    86 
       
    87         frame.setJMenuBar(mb);
       
    88         mb.add(m1);
       
    89         m1.add(i1);
       
    90         m1.add(i2);
       
    91 
       
    92         frame.setSize(200, 200);
       
    93         frame.setVisible(true);
       
    94     }
       
    95 
       
    96     void testJMenuItem() {
       
    97         frame = new JFrame("Menu Frame");
       
    98         JMenuBar mb = new JMenuBar();
       
    99         JMenu m1 = new JMenu("File");
       
   100         JMenuItem i1 = new JMenuItem("Save");
       
   101         i1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
       
   102                 InputEvent.ALT_GRAPH_DOWN_MASK));
       
   103         i1.addActionListener((e) -> {
       
   104             action = true;
       
   105             disposeUI();
       
   106         });
       
   107 
       
   108         frame.setJMenuBar(mb);
       
   109         mb.add(m1);
       
   110         m1.add(i1);
       
   111 
       
   112         frame.setSize(200, 200);
       
   113         frame.setVisible(true);
       
   114     }
       
   115 
       
   116     void testJOptionPane() {
       
   117         int selection =  JOptionPane.showConfirmDialog(null, "Do you wish " +
       
   118                 "to save file?","Confirm", JOptionPane.YES_NO_CANCEL_OPTION);
       
   119         //Pressed Yes
       
   120         if (selection == 0) {
       
   121             action = true;
       
   122         }
       
   123     }
       
   124 
       
   125     void testJTabbedPane() {
       
   126         frame =new JFrame();
       
   127         JPanel p1=new JPanel();
       
   128         JPanel p2=new JPanel();
       
   129         JTabbedPane tp=new JTabbedPane();
       
   130         tp.add("Main",p1);
       
   131         tp.add("Visit",p2);
       
   132         tp.setMnemonicAt(0, KeyEvent.VK_M);
       
   133         tp.setMnemonicAt(1, KeyEvent.VK_V);
       
   134 
       
   135         tp.addChangeListener((e) -> {
       
   136             if (tp.getSelectedIndex() == 1)
       
   137                 action = true;
       
   138             disposeUI();
       
   139         });
       
   140 
       
   141         frame.add(tp);
       
   142         frame.setSize(200,200);
       
   143         frame.setVisible(true);
       
   144     }
       
   145 
       
   146     void testJTextArea() {
       
   147         JTextField firstField = new JTextField(10);
       
   148         JTextField lastField = new JTextField(10);
       
   149 
       
   150         JLabel firstLabel = new JLabel("First Name", JLabel.RIGHT);
       
   151         firstLabel.setDisplayedMnemonic('F');
       
   152         firstLabel.setLabelFor(firstField);
       
   153 
       
   154         JLabel lastLabel = new JLabel("Last Name", JLabel.RIGHT);
       
   155         lastLabel.setDisplayedMnemonic('L');
       
   156         lastLabel.setLabelFor(lastField);
       
   157 
       
   158         JPanel p = new JPanel();
       
   159         p.setLayout(new GridLayout(2, 2, 5, 5));
       
   160         p.add(firstLabel);
       
   161         p.add(firstField);
       
   162         p.add(lastLabel);
       
   163         p.add(lastField);
       
   164 
       
   165         frame = new JFrame("MnemonicLabels");
       
   166         lastField.addFocusListener(new FocusListener() {
       
   167             @Override
       
   168             public void focusGained(FocusEvent e) {
       
   169                 action = true;
       
   170                 disposeUI();
       
   171             }
       
   172 
       
   173             @Override
       
   174             public void focusLost(FocusEvent e) {
       
   175 
       
   176             }
       
   177         });
       
   178 
       
   179         frame.add(p);
       
   180         frame.setSize(200,200);
       
   181         frame.setVisible(true);
       
   182     }
       
   183 
       
   184     void test() throws Exception {
       
   185         UIManager.LookAndFeelInfo[] lookAndFeels = UIManager
       
   186                 .getInstalledLookAndFeels();
       
   187         for (UIManager.LookAndFeelInfo lookAndFeel : lookAndFeels) {
       
   188             UIManager.setLookAndFeel(lookAndFeel.getClassName());
       
   189 
       
   190             Robot robot = new Robot();
       
   191             robot.setAutoDelay(100);
       
   192             robot.waitForIdle();
       
   193 
       
   194             action = false;
       
   195             SwingUtilities.invokeLater(this::testJMenu);
       
   196             robot.waitForIdle();
       
   197             robot.keyPress(KeyEvent.VK_ALT_GRAPH);
       
   198             robot.keyPress(KeyEvent.VK_F);
       
   199             robot.keyRelease(KeyEvent.VK_F);
       
   200             robot.keyRelease(KeyEvent.VK_ALT_GRAPH);
       
   201             robot.waitForIdle();
       
   202             if (!action)
       
   203                 errLog("JMenu", lookAndFeel.getClassName());
       
   204 
       
   205             action = false;
       
   206             SwingUtilities.invokeLater(this::testJMenuItem);
       
   207             robot.waitForIdle();
       
   208             robot.keyPress(KeyEvent.VK_ALT_GRAPH);
       
   209             robot.keyPress(KeyEvent.VK_S);
       
   210             robot.keyRelease(KeyEvent.VK_S);
       
   211             robot.keyRelease(KeyEvent.VK_ALT_GRAPH);
       
   212             robot.waitForIdle();
       
   213             if (!action)
       
   214                 errLog("JMenuItem", lookAndFeel.getClassName());
       
   215 
       
   216             action = false;
       
   217             SwingUtilities.invokeLater(this::testJOptionPane);
       
   218             robot.waitForIdle();
       
   219             robot.keyPress(KeyEvent.VK_ALT_GRAPH);
       
   220             robot.keyPress(KeyEvent.VK_Y);
       
   221             robot.keyRelease(KeyEvent.VK_Y);
       
   222             robot.keyRelease(KeyEvent.VK_ALT_GRAPH);
       
   223             robot.waitForIdle();
       
   224             if (!action)
       
   225                 errLog("JOptionPane", lookAndFeel.getClassName());
       
   226 
       
   227             action = false;
       
   228             SwingUtilities.invokeLater(this::testJTabbedPane);
       
   229             robot.waitForIdle();
       
   230             robot.keyPress(KeyEvent.VK_ALT_GRAPH);
       
   231             robot.keyPress(KeyEvent.VK_V);
       
   232             robot.keyRelease(KeyEvent.VK_V);
       
   233             robot.keyRelease(KeyEvent.VK_ALT_GRAPH);
       
   234             robot.waitForIdle();
       
   235             if (!action)
       
   236                 errLog("JTabbedPane", lookAndFeel.getClassName());
       
   237 
       
   238             action = false;
       
   239             SwingUtilities.invokeLater(this::testJTextArea);
       
   240             robot.waitForIdle();
       
   241             robot.keyPress(KeyEvent.VK_ALT_GRAPH);
       
   242             robot.keyPress(KeyEvent.VK_L);
       
   243             robot.keyRelease(KeyEvent.VK_L);
       
   244             robot.keyRelease(KeyEvent.VK_ALT_GRAPH);
       
   245             robot.waitForIdle();
       
   246             if (!action)
       
   247                 errLog("JTextArea", lookAndFeel.getClassName());
       
   248         }
       
   249         System.out.println("Passed.");
       
   250     }
       
   251 
       
   252     void disposeUI() {
       
   253         frame.setVisible(false);
       
   254         frame.dispose();
       
   255     }
       
   256 
       
   257     void errLog(String componentName, String lookAndFeel)
       
   258             throws InvocationTargetException, InterruptedException
       
   259     {
       
   260         SwingUtilities.invokeAndWait(this::disposeUI);
       
   261         throw new RuntimeException("Actions are not performed for "+
       
   262                 componentName + " with " + lookAndFeel + " look and feel.");
       
   263     }
       
   264 
       
   265     public static void main(String[] args) throws Exception {
       
   266         RightAltKeyTest t = new RightAltKeyTest();
       
   267         t.test();
       
   268     }
       
   269 }