1 /*
   2  * Copyright (c) 2015, 2017, 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 7172652
  28  * @summary With JDK 1.7 text field does not obtain focus when using mnemonic Alt/Key combin
  29  * @author Semyon Sadetsky
  30  * @requires (os.family == "windows")
  31  * @library /lib/testlibrary
  32  * @build jdk.testlibrary.OSInfo
  33  * @run main bug7172652
  34  */
  35 
  36 import javax.swing.*;
  37 import javax.swing.event.ChangeEvent;
  38 import javax.swing.event.ChangeListener;
  39 import java.awt.*;
  40 import java.awt.event.KeyEvent;
  41 import jdk.testlibrary.OSInfo;
  42 
  43 public class bug7172652  {
  44 
  45     private static JMenu menu;
  46     private static JFrame frame;
  47     private static Boolean selected;
  48 
  49     public static void main(String[] args) throws Exception {
  50         if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) {
  51             System.out.println("ok");
  52             return;
  53         }
  54         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  55         SwingUtilities.invokeAndWait(new Runnable() {
  56             @Override
  57             public void run() {
  58                 setup();
  59             }
  60         });
  61 
  62         test();
  63         SwingUtilities.invokeLater(new Runnable() {
  64             @Override
  65             public void run() {
  66                 frame.dispose();
  67             }
  68         });
  69     }
  70 
  71     private static void test() throws Exception {
  72         SwingUtilities.invokeAndWait(new Runnable() {
  73             @Override
  74             public void run() {
  75                 menu.getModel().addChangeListener(new ChangeListener() {
  76                     @Override
  77                     public void stateChanged(ChangeEvent e) {
  78                         selected = menu.isSelected();
  79                     }
  80                 });
  81             }
  82         });
  83 
  84         Robot robot = new Robot();
  85         robot.setAutoDelay(200);
  86 
  87         robot.keyPress(KeyEvent.VK_ALT);
  88         robot.keyPress(KeyEvent.VK_F);
  89         robot.keyRelease(KeyEvent.VK_F);
  90         robot.keyRelease(KeyEvent.VK_ALT);
  91 
  92         robot.waitForIdle();
  93         if( selected != null ) {
  94             throw new RuntimeException("Menu is notified selected= " + selected);
  95         }
  96 
  97         robot.keyPress(KeyEvent.VK_ALT);
  98         robot.keyPress(KeyEvent.VK_F);
  99         robot.keyRelease(KeyEvent.VK_F);
 100         robot.keyRelease(KeyEvent.VK_ALT);
 101         if( selected != null ) {
 102             throw new RuntimeException("Menu is notified selected= " + selected);
 103         }
 104 
 105         robot.waitForIdle();
 106 
 107         robot.keyPress(KeyEvent.VK_ALT);
 108         robot.keyPress(KeyEvent.VK_F);
 109         robot.keyRelease(KeyEvent.VK_F);
 110         robot.keyRelease(KeyEvent.VK_ALT);
 111         if( selected != null ) {
 112             throw new RuntimeException("Menu is notified selected= " + selected);
 113         }
 114 
 115         robot.waitForIdle();
 116 
 117         robot.keyPress(KeyEvent.VK_ALT);
 118         robot.keyPress(KeyEvent.VK_F);
 119         robot.keyRelease(KeyEvent.VK_F);
 120         robot.keyRelease(KeyEvent.VK_ALT);
 121         if( selected != null ) {
 122             throw new RuntimeException("Menu is notified selected= " + selected);
 123         }
 124 
 125         robot.waitForIdle();
 126 
 127         System.out.printf("ok");
 128     }
 129 
 130     private static void setup() {
 131         JLabel firstLbl = new JLabel("First name");
 132         JLabel lastLbl = new JLabel("Last name");
 133         JMenuBar menuBar = new JMenuBar();
 134 
 135         JTextField firstTxtFld = new JTextField(20);
 136         JTextField lastTxtFld = new JTextField(20);
 137         JDesktopPane desktopPane = new JDesktopPane();
 138         JInternalFrame iframe = new JInternalFrame("A frame", true, true, true, true);
 139 
 140         // Set an initial size
 141         iframe.setSize(200, 220);
 142 
 143         // By default, internal frames are not visible; make it visible
 144         iframe.setVisible(true);
 145 
 146         JPanel pane = new JPanel();
 147         pane.setLayout(new FlowLayout());
 148 
 149         pane.add(firstLbl);
 150         pane.add(firstTxtFld);
 151         pane.add(lastLbl);
 152         pane.add(lastTxtFld);
 153 
 154         firstLbl.setLabelFor(firstTxtFld);
 155         firstLbl.setDisplayedMnemonic('F');
 156 
 157         lastLbl.setLabelFor(lastTxtFld);
 158         lastLbl.setDisplayedMnemonic('L');
 159 
 160         iframe.getContentPane().add(pane);
 161         iframe.setJMenuBar(menuBar);
 162         menu = new JMenu("FirstMenu");
 163         //m.setMnemonic('i');
 164         menuBar.add(menu);
 165         desktopPane.add(iframe);
 166 
 167         frame = new JFrame();
 168         frame.setUndecorated(true);
 169         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 170         frame.getContentPane().add(desktopPane);
 171         frame.setSize(300, 300);
 172         frame.setVisible(true);
 173     }
 174 
 175 }