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 /* @test
  25    @bug 8211987
  26    @requires (os.family == "windows")
  27    @summary Verify if Menu bar gets input focus even if Alt-released event is consumed.
  28    @modules java.desktop/com.sun.java.swing.plaf.windows
  29    @run main AltFocusIssueTest
  30 */
  31 
  32 import java.awt.event.KeyAdapter;
  33 import java.awt.event.KeyEvent;
  34 import java.lang.reflect.InvocationTargetException;
  35 import java.awt.Robot;
  36 import javax.swing.JFrame;
  37 import javax.swing.JMenu;
  38 import javax.swing.JMenuBar;
  39 import javax.swing.JMenuItem;
  40 import javax.swing.JTextArea;
  41 import javax.swing.SwingUtilities;
  42 import javax.swing.UIManager;
  43 import javax.swing.UnsupportedLookAndFeelException;
  44 
  45 /**
  46  * Try to demonstrate the wrong behavior
  47  */
  48 public class AltFocusIssueTest {
  49 
  50     /**
  51      * Menu inside menu bar of the frame
  52      */
  53     private static JMenu menu;
  54 
  55     /**
  56      * Text area to test on.
  57      */
  58     private static JTextArea ta;
  59 
  60     private static JFrame frame;
  61 
  62     /**
  63      * Test that the text area loses input focus although Alt-released event is consumed.
  64      *
  65      * @throws InterruptedException
  66      * @throws InvocationTargetException
  67      */
  68     public static void testAltEvents() throws Exception {
  69         Robot robot = new Robot();
  70         SwingUtilities.invokeAndWait(() -> {
  71             try {
  72                 createUI();
  73             } catch (Exception e) {
  74                 throw new RuntimeException(e);
  75             }
  76         });
  77         robot.waitForIdle();
  78         SwingUtilities.invokeAndWait(() -> ta.requestFocusInWindow());
  79         robot.waitForIdle();
  80         if (!ta.isFocusOwner()) {
  81             throw new RuntimeException("textarea should have input focus");
  82         }
  83         if (menu.isSelected()) {
  84             throw new RuntimeException("menu is selected...");
  85         }
  86 
  87         // Simulate an Alt-typed event
  88         robot.keyPress(KeyEvent.VK_ALT);
  89         robot.keyRelease(KeyEvent.VK_ALT);
  90         robot.waitForIdle();
  91 
  92         // Since the event is consumed, I expect the input focus to be in the text area
  93         if (!ta.isFocusOwner()) {
  94             throw new RuntimeException("textarea should still have input focus");
  95         }
  96         // OR
  97         if (SwingUtilities.getRootPane(ta).isFocusOwner()) {
  98             throw new RuntimeException("Focus should not be changed from the text area");
  99         }
 100         // OR
 101         if (menu.isSelected()) {
 102             throw new RuntimeException("Menu must not be selected");
 103         }
 104     }
 105 
 106     /**
 107      * Builds UI to test.
 108      *
 109      */
 110     private static void createUI() throws Exception {
 111         // Install Windows L&F
 112         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
 113 
 114         frame = new JFrame();
 115         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 116 
 117         JMenuBar menuBar = new JMenuBar();
 118         frame.setJMenuBar(menuBar);
 119 
 120         menu = new JMenu("Menu");
 121         menu.add(new JMenuItem("Menu item"));
 122         menuBar.add(menu);
 123 
 124         ta = new JTextArea();
 125         frame.getContentPane().add(ta);
 126 
 127         ta.addKeyListener( new KeyAdapter() {
 128             @Override
 129             public void keyReleased(KeyEvent e) {
 130                 if (e.getKeyCode() == KeyEvent.VK_ALT) {
 131                     /*
 132                      * This is where I need to do special handling of the Alt-released event.
 133                      * After, nobody else must react to this event, thus I consume it.
 134                      */
 135                     e.consume();
 136                 }
 137             }
 138         });
 139 
 140         frame.setSize(400, 300);
 141         frame.setVisible(true);
 142     }
 143 
 144     public static void main(String[] args) throws Exception {
 145         try {
 146             testAltEvents();
 147         } finally {
 148             SwingUtilities.invokeAndWait(() -> frame.dispose());
 149         }
 150     }
 151 }