1/*
2 * Copyright (c) 2012, 2017, 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 * @key headful
27 * @bug 7160951 8152492 8178448
28 * @summary [macosx] ActionListener called twice for JMenuItem using ScreenMenuBar
29 * @author vera.akulova@oracle.com
30 * @modules java.desktop/java.awt:open
31 * @library ../../../../lib/testlibrary
32 * @build jdk.testlibrary.OSInfo
33 * @run main ActionListenerCalledTwiceTest
34 */
35
36import jdk.testlibrary.OSInfo;
37import java.awt.*;
38import java.awt.event.*;
39import javax.swing.*;
40
41public class ActionListenerCalledTwiceTest {
42
43    static String menuItems[] = {"Item1", "Item2", "Item3",
44                                    "Item4", "Item5", "Item6"};
45    static KeyStroke keyStrokes[] = {
46        KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.META_MASK),
47        KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0),
48        KeyStroke.getKeyStroke(KeyEvent.VK_UP, InputEvent.SHIFT_MASK),
49        KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.META_MASK),
50        KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.CTRL_MASK),
51        KeyStroke.getKeyStroke(KeyEvent.VK_EQUALS, InputEvent.META_MASK)
52    };
53
54    static JMenuBar bar;
55    static JFrame frame;
56    static volatile int listenerCallCounter = 0;
57
58    public static void main(String[] args) throws Exception {
59        if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
60            System.out.println("This test is for MacOS only." +
61                    " Automatically passed on other platforms.");
62            return;
63        }
64
65        try {
66
67            System.setProperty("apple.laf.useScreenMenuBar", "true");
68            SwingUtilities.invokeAndWait(
69                    ActionListenerCalledTwiceTest::createAndShowGUI);
70
71            Robot robot = new Robot();
72            robot.setAutoDelay(100);
73
74            testForTwice(robot, "");
75
76            SwingUtilities.invokeAndWait(
77                    ActionListenerCalledTwiceTest::testDefaultMenuBar);
78            robot.waitForIdle();
79
80            testForTwice(robot, "DefaultMenuBar");
81        } finally {
82            SwingUtilities.invokeAndWait(() -> frame.dispose());
83        }
84    }
85
86    private static void createAndShowGUI() {
87        JMenu menu = new JMenu("Menu");
88
89        for (int i = 0; i < menuItems.length; ++i) {
90            JMenuItem newItem = new JMenuItem(menuItems[i]);
91            newItem.setAccelerator(keyStrokes[i]);
92            newItem.addActionListener(e -> listenerCallCounter++);
93            menu.add(newItem);
94        }
95
96        bar = new JMenuBar();
97        bar.add(menu);
98        frame = new JFrame("Test");
99        frame.setJMenuBar(bar);
100        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
101        frame.pack();
102        frame.setVisible(true);
103    }
104
105    private static int getModKeyCode(int mod) {
106        if ((mod & (InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK)) != 0) {
107            return KeyEvent.VK_SHIFT;
108        }
109
110        if ((mod & (InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)) != 0) {
111            return KeyEvent.VK_CONTROL;
112        }
113
114        if ((mod & (InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK)) != 0) {
115            return KeyEvent.VK_ALT;
116        }
117
118        if ((mod & (InputEvent.META_DOWN_MASK | InputEvent.META_MASK)) != 0) {
119            return KeyEvent.VK_META;
120        }
121
122        return 0;
123    }
124
125    private static void testForTwice(Robot robot, String exceptionPrefix)
126                                        throws Exception{
127        for (int i = 0; i < menuItems.length; ++i) {
128            KeyStroke ks = keyStrokes[i];
129            int modKeyCode = getModKeyCode(ks.getModifiers());
130
131            if (modKeyCode != 0) {
132                robot.keyPress(modKeyCode);
133            }
134
135            robot.keyPress(ks.getKeyCode());
136            robot.keyRelease(ks.getKeyCode());
137
138            if (modKeyCode != 0) {
139                robot.keyRelease(modKeyCode);
140            }
141
142            robot.waitForIdle();
143
144            if (listenerCallCounter != 1) {
145                throw new Exception(exceptionPrefix
146                        + " Test failed: ActionListener for " + menuItems[i]
147                        + " called " + listenerCallCounter + " times instead of 1!");
148            }
149
150            listenerCallCounter = 0;
151        }
152    }
153
154    private static void testDefaultMenuBar() {
155        if (Desktop.getDesktop().isSupported(Desktop.Action.APP_MENU_BAR)) {
156            Desktop.getDesktop().setDefaultMenuBar(bar);
157            frame.setExtendedState(JFrame.ICONIFIED);
158        }
159    }
160}
161