Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JMenuItem/8139169/ScreenMenuBarInputTwice.java
41153 views
1
/*
2
* Copyright (c) 2015, 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 8139169 8158390
28
* @summary verifies if TextArea gets input twice due to Apple's Screen Menubar
29
* @requires (os.family=="mac")
30
* @library ../../regtesthelpers
31
* @build Util
32
* @run main ScreenMenuBarInputTwice
33
*/
34
35
import java.awt.BorderLayout;
36
import java.awt.Point;
37
import java.awt.Robot;
38
import java.awt.event.ActionEvent;
39
import java.awt.event.InputEvent;
40
import java.awt.event.KeyEvent;
41
import static java.awt.event.KeyEvent.VK_COMMA;
42
import static java.awt.event.KeyEvent.VK_META;
43
import static java.awt.event.KeyEvent.VK_SHIFT;
44
import javax.swing.AbstractAction;
45
import javax.swing.Action;
46
import javax.swing.JFrame;
47
import javax.swing.JMenu;
48
import javax.swing.JMenuBar;
49
import javax.swing.JMenuItem;
50
import javax.swing.JPanel;
51
import javax.swing.JScrollPane;
52
import javax.swing.JTextArea;
53
import javax.swing.KeyStroke;
54
import javax.swing.SwingUtilities;
55
import javax.swing.text.BadLocationException;
56
57
public class ScreenMenuBarInputTwice {
58
59
public static final String TEST_STRING = "Check string";
60
61
private static Robot robot;
62
private static JFrame frame;
63
private static JPanel content;
64
private static JTextArea textArea;
65
private static JMenuBar menuBar;
66
private static JMenu menu;
67
private static JMenuItem menuItem;
68
69
public static void main(String[] args) throws Exception {
70
robot = new Robot();
71
robot.setAutoDelay(200);
72
robot.setAutoWaitForIdle(true);
73
createUIWithSeperateMenuBar();
74
robot.waitForIdle();
75
robot.delay(500);
76
shortcutTestCase();
77
robot.waitForIdle();
78
robot.delay(250);
79
cleanUp();
80
robot.waitForIdle();
81
robot.delay(250);
82
createUIWithIntegratedMenuBar();
83
robot.waitForIdle();
84
robot.delay(500);
85
menuTestCase();
86
robot.waitForIdle();
87
robot.delay(250);
88
cleanUp();
89
}
90
91
private static void createUIWithSeperateMenuBar() throws Exception {
92
SwingUtilities.invokeAndWait(new Runnable() {
93
94
@Override
95
public void run() {
96
System.setProperty(
97
"com.apple.mrj.application.apple.menu.about.name",
98
"A test frame");
99
System.setProperty("apple.laf.useScreenMenuBar", "true");
100
frame = new JFrame("Text input twice check");
101
content = new JPanel(new BorderLayout());
102
textArea = new JTextArea();
103
content.add(new JScrollPane(textArea,
104
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
105
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),
106
BorderLayout.CENTER);
107
menuBar = new JMenuBar();
108
frame.setJMenuBar(menuBar);
109
Action a = new AbstractAction("Insert some text") {
110
@Override
111
public void actionPerformed(ActionEvent arg0) {
112
try {
113
114
textArea.getDocument()
115
.insertString(0, TEST_STRING, null);
116
} catch (BadLocationException e) {
117
frame.dispose();
118
throw new RuntimeException("Bad location: ", e);
119
}
120
}
121
};
122
KeyStroke keyStroke = KeyStroke.getKeyStroke(
123
"meta shift COMMA");
124
a.putValue(Action.ACCELERATOR_KEY, keyStroke);
125
textArea.getInputMap().put(keyStroke, "myAction");
126
textArea.getActionMap().put("myAction", a);
127
menu = new JMenu("The Menu");
128
menuItem = new JMenuItem(a);
129
menuItem.setAccelerator((KeyStroke) a.getValue(
130
Action.ACCELERATOR_KEY));
131
menu.add(menuItem);
132
menuBar.add(menu);
133
frame.getContentPane().add(content);
134
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
135
frame.setLocationRelativeTo(null);
136
frame.setSize(500, 500);
137
frame.setVisible(true);
138
frame.toFront();
139
}
140
});
141
}
142
143
private static void createUIWithIntegratedMenuBar() throws Exception {
144
SwingUtilities.invokeAndWait(new Runnable() {
145
146
@Override
147
public void run() {
148
System.setProperty(
149
"com.apple.mrj.application.apple.menu.about.name",
150
"A test frame");
151
System.setProperty("apple.laf.useScreenMenuBar", "false");
152
frame = new JFrame("Text input twice check");
153
content = new JPanel(new BorderLayout());
154
textArea = new JTextArea();
155
content.add(new JScrollPane(textArea,
156
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
157
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),
158
BorderLayout.CENTER);
159
menuBar = new JMenuBar();
160
frame.setJMenuBar(menuBar);
161
Action a = new AbstractAction("Insert some text") {
162
@Override
163
public void actionPerformed(ActionEvent arg0) {
164
try {
165
166
textArea.getDocument()
167
.insertString(0, TEST_STRING, null);
168
} catch (BadLocationException e) {
169
frame.dispose();
170
throw new RuntimeException("Bad location: ", e);
171
}
172
}
173
};
174
KeyStroke keyStroke = KeyStroke.getKeyStroke(
175
"meta shift COMMA");
176
a.putValue(Action.ACCELERATOR_KEY, keyStroke);
177
textArea.getInputMap().put(keyStroke, "myAction");
178
textArea.getActionMap().put("myAction", a);
179
menu = new JMenu("The Menu");
180
menuItem = new JMenuItem(a);
181
menuItem.setAccelerator((KeyStroke) a.getValue(
182
Action.ACCELERATOR_KEY));
183
menu.add(menuItem);
184
menuBar.add(menu);
185
frame.getContentPane().add(content);
186
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
187
frame.setSize(500, 500);
188
frame.setLocationRelativeTo(null);
189
frame.setVisible(true);
190
frame.toFront();
191
}
192
});
193
}
194
195
private static void shortcutTestCase() throws Exception {
196
robot.keyPress(KeyEvent.VK_META);
197
robot.keyPress(KeyEvent.VK_SHIFT);
198
robot.keyPress(KeyEvent.VK_COMMA);
199
robot.keyRelease(VK_COMMA);
200
robot.keyRelease(VK_SHIFT);
201
robot.keyRelease(VK_META);
202
checkText(textArea.getText());
203
}
204
205
private static void menuTestCase() throws Exception {
206
Point mousePoint;
207
mousePoint = Util.getCenterPoint(menu);
208
robot.mouseMove(mousePoint.x, mousePoint.y);
209
robot.mousePress(InputEvent.BUTTON1_MASK);
210
robot.mouseRelease(InputEvent.BUTTON1_MASK);
211
mousePoint = Util.getCenterPoint(menuItem);
212
robot.mouseMove(mousePoint.x, mousePoint.y);
213
robot.mousePress(InputEvent.BUTTON1_MASK);
214
robot.mouseRelease(InputEvent.BUTTON1_MASK);
215
checkText(textArea.getText());
216
}
217
218
private static void checkText(String text) throws Exception {
219
SwingUtilities.invokeAndWait(new Runnable() {
220
@Override
221
public void run() {
222
if (TEST_STRING.equals(text)) {
223
textArea.setText("");
224
} else {
225
frame.dispose();
226
throw new RuntimeException("Failed. "
227
+ " Menu item shortcut invoked twice");
228
}
229
}
230
});
231
}
232
233
private static void cleanUp() throws Exception {
234
SwingUtilities.invokeAndWait(new Runnable() {
235
@Override
236
public void run() {
237
frame.dispose();
238
}
239
});
240
}
241
}
242
243