Path: blob/master/test/jdk/javax/swing/JMenuItem/8216971/DoubleActionTest.java
41153 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @key headful26* @bug 821697127* @summary For JCheckBoxMenuItem actionPerformed() is called twice, when28* apple.laf.useScreenMenuBar=true and modifier is InputEvent.META_DOWN_MASK29* @library /test/lib30* @run main DoubleActionTest31*/3233import javax.swing.JFrame;34import javax.swing.JMenu;35import javax.swing.JMenuBar;36import javax.swing.JCheckBoxMenuItem;37import javax.swing.SwingUtilities;38import javax.swing.AbstractAction;39import javax.swing.Action;40import javax.swing.KeyStroke;41import java.awt.event.ActionEvent;42import java.awt.event.InputEvent;43import java.awt.event.KeyEvent;44import java.awt.Robot;45import jdk.test.lib.Platform;4647public class DoubleActionTest {4849private static int metaDownCount = 0;50private static JFrame frame;51private static final int ORIGIN_X = 200;52private static final int ORIGIN_Y = 200;5354public static void main(String[] args) throws Exception {55if (!System.getProperty("os.name").startsWith("Mac")) {56System.out.println("This test is only for Mac OS, passed " +57"automatically on other platforms.");58return;59}6061try {62System.setProperty("apple.laf.useScreenMenuBar", "true");6364SwingUtilities.invokeAndWait(DoubleActionTest::createAndShowGUI);6566Robot robot = new Robot();67robot.setAutoDelay(100);68testKeyPress(robot);69robot.delay(1000);7071} finally {72SwingUtilities.invokeAndWait(()->frame.dispose());73if (metaDownCount != 1) {74throw new RuntimeException("Test Failed: actionPerformed is called twice");75}76}77}7879private static void createAndShowGUI() {80frame = new JFrame();81final JMenuBar menubar = new JMenuBar();82final JMenu fileMenu = new JMenu("OPEN ME");83final MyAction myAction = new MyAction();84final JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(myAction);8586fileMenu.add(menuItem);87menubar.add(fileMenu);88frame.setJMenuBar(menubar);89frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);90frame.setBounds(ORIGIN_X, ORIGIN_X, 200, 200);91frame.setVisible(true);92}9394private static class MyAction extends AbstractAction {9596MyAction() {97putValue(Action.NAME, "HIT MY ACCELERATOR KEY");98putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.META_DOWN_MASK));99}100101@Override102public void actionPerformed(final ActionEvent e) {103System.out.println("Action! called with modifiers: " + e.getModifiers() + "\n" + e);104metaDownCount++;105}106}107108private static void testKeyPress(Robot robot) throws Exception {109robot.mouseMove(ORIGIN_X + 50, ORIGIN_Y + 50);110robot.waitForIdle();111robot.keyPress(KeyEvent.VK_META);112robot.keyPress(KeyEvent.VK_E);113robot.keyRelease(KeyEvent.VK_E);114robot.keyRelease(KeyEvent.VK_META);115robot.waitForIdle();116}117}118119120