Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JMenuItem/8216971/DoubleActionTest.java
41153 views
1
/*
2
* Copyright (c) 2019, 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 8216971
28
* @summary For JCheckBoxMenuItem actionPerformed() is called twice, when
29
* apple.laf.useScreenMenuBar=true and modifier is InputEvent.META_DOWN_MASK
30
* @library /test/lib
31
* @run main DoubleActionTest
32
*/
33
34
import javax.swing.JFrame;
35
import javax.swing.JMenu;
36
import javax.swing.JMenuBar;
37
import javax.swing.JCheckBoxMenuItem;
38
import javax.swing.SwingUtilities;
39
import javax.swing.AbstractAction;
40
import javax.swing.Action;
41
import javax.swing.KeyStroke;
42
import java.awt.event.ActionEvent;
43
import java.awt.event.InputEvent;
44
import java.awt.event.KeyEvent;
45
import java.awt.Robot;
46
import jdk.test.lib.Platform;
47
48
public class DoubleActionTest {
49
50
private static int metaDownCount = 0;
51
private static JFrame frame;
52
private static final int ORIGIN_X = 200;
53
private static final int ORIGIN_Y = 200;
54
55
public static void main(String[] args) throws Exception {
56
if (!System.getProperty("os.name").startsWith("Mac")) {
57
System.out.println("This test is only for Mac OS, passed " +
58
"automatically on other platforms.");
59
return;
60
}
61
62
try {
63
System.setProperty("apple.laf.useScreenMenuBar", "true");
64
65
SwingUtilities.invokeAndWait(DoubleActionTest::createAndShowGUI);
66
67
Robot robot = new Robot();
68
robot.setAutoDelay(100);
69
testKeyPress(robot);
70
robot.delay(1000);
71
72
} finally {
73
SwingUtilities.invokeAndWait(()->frame.dispose());
74
if (metaDownCount != 1) {
75
throw new RuntimeException("Test Failed: actionPerformed is called twice");
76
}
77
}
78
}
79
80
private static void createAndShowGUI() {
81
frame = new JFrame();
82
final JMenuBar menubar = new JMenuBar();
83
final JMenu fileMenu = new JMenu("OPEN ME");
84
final MyAction myAction = new MyAction();
85
final JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(myAction);
86
87
fileMenu.add(menuItem);
88
menubar.add(fileMenu);
89
frame.setJMenuBar(menubar);
90
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
91
frame.setBounds(ORIGIN_X, ORIGIN_X, 200, 200);
92
frame.setVisible(true);
93
}
94
95
private static class MyAction extends AbstractAction {
96
97
MyAction() {
98
putValue(Action.NAME, "HIT MY ACCELERATOR KEY");
99
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.META_DOWN_MASK));
100
}
101
102
@Override
103
public void actionPerformed(final ActionEvent e) {
104
System.out.println("Action! called with modifiers: " + e.getModifiers() + "\n" + e);
105
metaDownCount++;
106
}
107
}
108
109
private static void testKeyPress(Robot robot) throws Exception {
110
robot.mouseMove(ORIGIN_X + 50, ORIGIN_Y + 50);
111
robot.waitForIdle();
112
robot.keyPress(KeyEvent.VK_META);
113
robot.keyPress(KeyEvent.VK_E);
114
robot.keyRelease(KeyEvent.VK_E);
115
robot.keyRelease(KeyEvent.VK_META);
116
robot.waitForIdle();
117
}
118
}
119
120