|
| 1 | +/* |
| 2 | + * Copyright (c) 2001, 2025, 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 4140643 |
| 27 | + * @summary Tests that Motif menus open with both Alt-key and Meta-key |
| 28 | + * @key headful |
| 29 | + * @run main bug4140643 |
| 30 | + */ |
| 31 | + |
| 32 | +import java.awt.Robot; |
| 33 | +import java.awt.event.KeyEvent; |
| 34 | +import javax.swing.JButton; |
| 35 | +import javax.swing.JFrame; |
| 36 | +import javax.swing.JMenu; |
| 37 | +import javax.swing.JMenuBar; |
| 38 | +import javax.swing.JMenuItem; |
| 39 | +import javax.swing.SwingUtilities; |
| 40 | +import javax.swing.UIManager; |
| 41 | +import javax.swing.UnsupportedLookAndFeelException; |
| 42 | + |
| 43 | +public class bug4140643 { |
| 44 | + private static JFrame frame; |
| 45 | + private static JMenu menu; |
| 46 | + private static volatile boolean isPopMenuVisible; |
| 47 | + |
| 48 | + public static void main(String[] args) throws Exception { |
| 49 | + Robot robot = new Robot(); |
| 50 | + try { |
| 51 | + SwingUtilities.invokeAndWait(() -> { |
| 52 | + try { |
| 53 | + UIManager.setLookAndFeel( |
| 54 | + "com.sun.java.swing.plaf.motif.MotifLookAndFeel"); |
| 55 | + } catch (ClassNotFoundException | InstantiationException |
| 56 | + | UnsupportedLookAndFeelException |
| 57 | + | IllegalAccessException e) { |
| 58 | + throw new RuntimeException(e); |
| 59 | + } |
| 60 | + frame = new JFrame("bug4140643"); |
| 61 | + |
| 62 | + menu = new JMenu("File"); |
| 63 | + menu.setMnemonic(KeyEvent.VK_F); |
| 64 | + menu.add(new JMenuItem("Open...")); |
| 65 | + menu.add(new JMenuItem("Save")); |
| 66 | + |
| 67 | + JMenuBar mbar = new JMenuBar(); |
| 68 | + mbar.add(menu); |
| 69 | + frame.setJMenuBar(mbar); |
| 70 | + |
| 71 | + frame.add(new JButton("Click Here")); |
| 72 | + frame.pack(); |
| 73 | + frame.setLocationRelativeTo(null); |
| 74 | + frame.setVisible(true); |
| 75 | + }); |
| 76 | + robot.waitForIdle(); |
| 77 | + robot.delay(1000); |
| 78 | + if (System.getProperty("os.name").contains("OS X")) { |
| 79 | + robot.keyPress(KeyEvent.VK_CONTROL); |
| 80 | + robot.keyPress(KeyEvent.VK_ALT); |
| 81 | + robot.keyPress(KeyEvent.VK_F); |
| 82 | + robot.keyRelease(KeyEvent.VK_F); |
| 83 | + robot.keyRelease(KeyEvent.VK_ALT); |
| 84 | + robot.keyRelease(KeyEvent.VK_CONTROL); |
| 85 | + } else { |
| 86 | + robot.keyPress(KeyEvent.VK_ALT); |
| 87 | + robot.keyPress(KeyEvent.VK_F); |
| 88 | + robot.keyRelease(KeyEvent.VK_F); |
| 89 | + robot.keyRelease(KeyEvent.VK_ALT); |
| 90 | + } |
| 91 | + robot.waitForIdle(); |
| 92 | + robot.delay(1000); |
| 93 | + SwingUtilities.invokeAndWait(() -> { |
| 94 | + isPopMenuVisible = menu.isPopupMenuVisible(); |
| 95 | + }); |
| 96 | + if (!isPopMenuVisible) { |
| 97 | + throw new RuntimeException("Menu popup is not shown"); |
| 98 | + } |
| 99 | + } finally { |
| 100 | + SwingUtilities.invokeAndWait(() -> { |
| 101 | + if (frame != null) { |
| 102 | + frame.dispose(); |
| 103 | + } |
| 104 | + }); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments