Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JMenu/4515762/bug4515762.java
41153 views
1
/*
2
* Copyright (c) 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
import java.awt.*;
25
import java.awt.event.*;
26
import javax.swing.*;
27
28
/**
29
* @test
30
* @key headful
31
* @bug 4515762
32
* @author Mark Davidson
33
* @summary Tests the ability to support duplicate mnemonics
34
* @library ../../regtesthelpers
35
* @build Util
36
* @run main bug4515762
37
*/
38
public class bug4515762 {
39
40
private static volatile boolean actionExpected = false;
41
private static volatile boolean actionRecieved = false;
42
private static JFrame frame;
43
44
/**
45
* @param str name of Menu
46
*/
47
private static JMenuBar createMenuBar() {
48
JMenuBar menubar = new JMenuBar();
49
50
// Duplicate menu item test for 4515762
51
JMenu menu = new JMenu("Duplicate Menu");
52
menu.setMnemonic('D');
53
menu.add(createMenuItem("Sunday", 'S'));
54
menu.add(createMenuItem("Monday", 'M'));
55
56
menu.add(createMenuItem("Tuesday", 'S'));
57
menu.add(createMenuItem("Wednesday", 'S'));
58
menu.add(createMenuItem("Thursday", 'S'));
59
menu.add(createMenuItem("Friday", 'F'));
60
menu.add(createMenuItem("Saturday", 'S'));
61
62
// Control with unique menu
63
JMenu menu2 = new JMenu("Unique Menu");
64
menu2.setMnemonic('U');
65
menu2.add(createMenuItem("Sunday", 'S'));
66
menu2.add(createMenuItem("Monday", 'M'));
67
68
menu2.add(createMenuItem("Tuesday", 'T'));
69
menu2.add(createMenuItem("Wednesday", 'W'));
70
menu2.add(createMenuItem("Thursday", 'U'));
71
menu2.add(createMenuItem("Friday", 'F'));
72
menu2.add(createMenuItem("Saturday", 'A'));
73
74
menubar.add(menu);
75
menubar.add(menu2);
76
77
return menubar;
78
}
79
80
/**
81
* Creates and returns the menu item.
82
*/
83
private static JMenuItem createMenuItem(String name, char mnemonic) {
84
JMenuItem menuItem = new JMenuItem(name, mnemonic);
85
menuItem.addActionListener(new ActionListener() {
86
87
@Override
88
public void actionPerformed(ActionEvent evt) {
89
JMenuItem item = (JMenuItem) evt.getSource();
90
if (actionExpected == false) {
91
throw new RuntimeException("Menu Action: "
92
+ item.getText() + " should not be called");
93
} else {
94
actionRecieved = true;
95
}
96
}
97
});
98
99
return menuItem;
100
}
101
102
public static void checkAction() {
103
if (actionRecieved == true) {
104
actionRecieved = false;
105
} else {
106
throw new RuntimeException("Action has not been received");
107
}
108
}
109
110
public static void main(String[] args) throws Throwable {
111
try {
112
Robot robot = new Robot();
113
robot.setAutoDelay(250);
114
115
SwingUtilities.invokeAndWait(new Runnable() {
116
117
@Override
118
public void run() {
119
frame = new JFrame("Test");
120
frame.setJMenuBar(createMenuBar());
121
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
122
frame.pack();
123
frame.setVisible(true);
124
frame.toFront();
125
}
126
});
127
128
robot.waitForIdle();
129
130
Util.hitMnemonics(robot, KeyEvent.VK_D);
131
robot.waitForIdle();
132
133
// Press the S key many times (should not cause an action peformed)
134
int TIMES = 5;
135
for (int i = 0; i < TIMES; i++) {
136
Util.hitKeys(robot, KeyEvent.VK_S);
137
}
138
robot.waitForIdle();
139
140
// Unique menu items.
141
actionExpected = true;
142
Util.hitMnemonics(robot, KeyEvent.VK_U);
143
144
robot.waitForIdle();
145
robot.keyPress(KeyEvent.VK_S);
146
robot.keyRelease(KeyEvent.VK_S);
147
robot.waitForIdle();
148
149
checkAction();
150
151
Util.hitMnemonics(robot, KeyEvent.VK_U);
152
robot.waitForIdle();
153
154
robot.keyPress(KeyEvent.VK_M);
155
robot.keyRelease(KeyEvent.VK_M);
156
robot.waitForIdle();
157
158
checkAction();
159
160
Util.hitMnemonics(robot, KeyEvent.VK_U);
161
robot.waitForIdle();
162
Util.hitKeys(robot, KeyEvent.VK_T);
163
robot.waitForIdle();
164
165
checkAction();
166
167
Util.hitMnemonics(robot, KeyEvent.VK_U);
168
robot.waitForIdle();
169
Util.hitKeys(robot, KeyEvent.VK_W);
170
robot.waitForIdle();
171
172
checkAction();
173
174
Util.hitMnemonics(robot, KeyEvent.VK_U);
175
robot.waitForIdle();
176
Util.hitKeys(robot, KeyEvent.VK_U);
177
robot.waitForIdle();
178
179
checkAction();
180
} finally {
181
if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
182
}
183
}
184
}
185
186