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