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