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