1 /*
  2  * Copyright (c) 2001, 2023, 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 4403749
 27  * @summary Tests that keyboard accelerator implementation in JMenuBar is
 28             MenuElement aware
 29  * @key headful
 30  * @run main bug4403749
 31  */
 32 
 33 import java.awt.Component;
 34 import java.awt.Dimension;
 35 import java.awt.Point;
 36 import java.awt.Robot;
 37 import java.awt.event.KeyEvent;
 38 import java.awt.event.InputEvent;
 39 import java.awt.event.MouseEvent;
 40 
 41 import javax.swing.JButton;
 42 import javax.swing.JFrame;
 43 import javax.swing.JMenu;
 44 import javax.swing.JMenuBar;
 45 import javax.swing.JPanel;
 46 import javax.swing.KeyStroke;
 47 import javax.swing.MenuElement;
 48 import javax.swing.MenuSelectionManager;
 49 import javax.swing.SwingUtilities;
 50 
 51 public class bug4403749 {
 52     static JFrame frame;
 53     static volatile Point pt;
 54     static volatile Dimension dim;
 55     static volatile boolean passed;
 56 
 57     public static void main(String[] args) throws Exception {
 58         Robot robot = new Robot();
 59         robot.setAutoDelay(100);
 60         try {
 61             SwingUtilities.invokeAndWait(() -> {
 62                 frame = new JFrame("bug4403749");
 63                 JMenuBar mbar = new JMenuBar();
 64                 JMenu menu = new JMenu("Menu");
 65                 JPanel panel = new TestMenuElement();
 66                 menu.add(panel);
 67                 mbar.add(menu);
 68                 frame.setJMenuBar(mbar);
 69 
 70                 frame.getContentPane().add(new JButton(""));
 71                 frame.setSize(200, 200);
 72                 frame.setLocationRelativeTo(null);
 73                 frame.setAlwaysOnTop(true);
 74                 frame.setVisible(true);
 75             });
 76             robot.waitForIdle();
 77             robot.delay(1000);
 78             SwingUtilities.invokeAndWait(() -> {
 79                 pt = frame.getLocationOnScreen();
 80                 dim = frame.getSize();
 81             });
 82             robot.mouseMove(pt.x + dim.width / 2, pt.y + dim.height / 2);
 83             robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 84             robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 85             robot.waitForIdle();
 86             robot.delay(200);
 87             robot.keyPress(KeyEvent.VK_ALT);
 88             robot.keyPress(KeyEvent.VK_A);
 89             robot.keyRelease(KeyEvent.VK_A);
 90             robot.keyRelease(KeyEvent.VK_ALT);
 91             if (!passed) {
 92                 throw new RuntimeException("Failed: processKeyBinding wasn't called");
 93             }
 94         } finally {
 95             SwingUtilities.invokeAndWait(() -> {
 96                 if (frame != null) {
 97                     frame.dispose();
 98                  }
 99             });
100         }
101     }
102 
103     static class TestMenuElement extends JPanel implements MenuElement {
104         public void processMouseEvent(MouseEvent event,
105                                       MenuElement[] path,
106                                       MenuSelectionManager manager) {}
107 
108         public void processKeyEvent(KeyEvent event,
109                                     MenuElement[] path,
110                                     MenuSelectionManager manager) {}
111 
112         public void menuSelectionChanged(boolean isIncluded) {}
113 
114         public MenuElement[] getSubElements() {
115             return new MenuElement[0];
116         }
117 
118         public Component getComponent() {
119             return this;
120         }
121 
122         protected boolean processKeyBinding(KeyStroke ks, KeyEvent e,
123                                             int condition, boolean pressed) {
124             passed = true;
125             return super.processKeyBinding(ks, e, condition, pressed);
126         }
127     }
128 }