Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JPopupMenu/4966112/bug4966112.java
41153 views
1
/*
2
* Copyright (c) 2011, 2012, 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 4966112
28
* @summary Some Composite components does not show the Context Popup.
29
* @library ../../regtesthelpers
30
* @build Util
31
* @author Alexander Zuev
32
* @run main bug4966112
33
*/
34
import javax.swing.JButton;
35
import javax.swing.JComponent;
36
import javax.swing.JFileChooser;
37
import javax.swing.JFrame;
38
import javax.swing.JPanel;
39
import javax.swing.JPopupMenu;
40
import javax.swing.JSpinner;
41
import javax.swing.JSplitPane;
42
import javax.swing.SwingUtilities;
43
import javax.swing.UIManager;
44
import javax.swing.event.PopupMenuListener;
45
import javax.swing.event.PopupMenuEvent;
46
47
import java.awt.BorderLayout;
48
import java.awt.Dimension;
49
import java.awt.Robot;
50
import java.awt.Point;
51
import java.awt.event.InputEvent;
52
import java.awt.event.KeyEvent;
53
import java.awt.event.MouseAdapter;
54
import java.awt.event.MouseEvent;
55
56
public class bug4966112 {
57
58
private static final int NO_MOUSE_BUTTON = -1;
59
private static volatile boolean shown = false;
60
private static volatile int popupButton = NO_MOUSE_BUTTON;
61
private static volatile JButton testButton;
62
private static volatile JSplitPane jsp;
63
private static volatile JSpinner spin;
64
private static volatile JFileChooser filec;
65
private static int buttonMask;
66
private static Robot robot;
67
private static boolean isAquaFileChooser;
68
69
public static void main(String[] args) throws Exception {
70
robot = new Robot();
71
robot.setAutoDelay(100);
72
73
createAndShowButton();
74
robot.waitForIdle();
75
76
setClickPoint(testButton);
77
clickMouse(InputEvent.BUTTON1_MASK);
78
clickMouse(InputEvent.BUTTON2_MASK);
79
clickMouse(InputEvent.BUTTON3_MASK);
80
81
robot.waitForIdle();
82
closeFrame();
83
84
if (popupButton == NO_MOUSE_BUTTON) {
85
System.out.println("Test can't identify the popup trigger button. Test skipped");
86
return;
87
}
88
89
setButtonMask();
90
91
// Test Split Pane
92
createAndShowSplitPane();
93
robot.waitForIdle();
94
95
clickMouse(jsp);
96
robot.waitForIdle();
97
closeFrame();
98
99
if (!shown) {
100
throw new RuntimeException("Popup was not shown on splitpane");
101
}
102
103
// Test Spinner
104
createAndShowSpinner();
105
robot.waitForIdle();
106
107
clickMouse(spin);
108
robot.waitForIdle();
109
closeFrame();
110
111
if (!shown) {
112
throw new RuntimeException("Popup was not shown on spinner");
113
}
114
115
// Test File Chooser
116
createAndShowFileChooser();
117
robot.waitForIdle();
118
119
if ((UIManager.getLookAndFeel().getID()).equals("Aqua")) {
120
isAquaFileChooser = true;
121
} else {
122
isAquaFileChooser = false;
123
}
124
clickMouse(filec);
125
robot.waitForIdle();
126
127
Util.hitKeys(robot, KeyEvent.VK_ESCAPE);
128
robot.waitForIdle();
129
130
Util.hitKeys(robot, KeyEvent.VK_ESCAPE);
131
robot.waitForIdle();
132
closeFrame();
133
134
if (!shown) {
135
throw new RuntimeException("Popup was not shown on filechooser");
136
}
137
}
138
139
private static void clickMouse(JComponent c) throws Exception {
140
setClickPoint(c);
141
clickMouse(buttonMask);
142
}
143
144
private static void clickMouse(int buttons) {
145
robot.mousePress(buttons);
146
robot.mouseRelease(buttons);
147
}
148
149
private static void setButtonMask() {
150
switch (popupButton) {
151
case MouseEvent.BUTTON1:
152
buttonMask = InputEvent.BUTTON1_MASK;
153
break;
154
case MouseEvent.BUTTON2:
155
buttonMask = InputEvent.BUTTON2_MASK;
156
break;
157
case MouseEvent.BUTTON3:
158
buttonMask = InputEvent.BUTTON3_MASK;
159
break;
160
}
161
}
162
163
private static void setClickPoint(final JComponent c) throws Exception {
164
final Point[] result = new Point[1];
165
SwingUtilities.invokeAndWait(new Runnable() {
166
167
@Override
168
public void run() {
169
Point p = c.getLocationOnScreen();
170
Dimension size = c.getSize();
171
if (isAquaFileChooser) {
172
result[0] = new Point(p.x + size.width / 2, p.y + 5);
173
} else {
174
result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2);
175
}
176
}
177
});
178
179
robot.mouseMove(result[0].x, result[0].y);
180
}
181
182
private static JPopupMenu createJPopupMenu() {
183
JPopupMenu jpm = new JPopupMenu();
184
jpm.add("One");
185
jpm.add("Two");
186
jpm.add("Three");
187
jpm.addPopupMenuListener(new PopupMenuListener() {
188
189
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
190
shown = true;
191
}
192
193
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
194
}
195
196
public void popupMenuCanceled(PopupMenuEvent e) {
197
}
198
});
199
200
AutoClosable.INSTANCE.setPopup(jpm);
201
return jpm;
202
}
203
204
private static void createAndShowButton() throws Exception {
205
SwingUtilities.invokeAndWait(new Runnable() {
206
207
@Override
208
public void run() {
209
JFrame frame = new JFrame("Button Frame");
210
frame.setLayout(new BorderLayout());
211
testButton = new JButton("Popup Tester");
212
213
testButton.addMouseListener(new MouseAdapter() {
214
215
void setPopupTrigger(MouseEvent e) {
216
if (e.isPopupTrigger()) {
217
popupButton = e.getButton();
218
}
219
}
220
221
public void mouseClicked(MouseEvent e) {
222
setPopupTrigger(e);
223
}
224
225
public void mousePressed(MouseEvent e) {
226
setPopupTrigger(e);
227
}
228
229
public void mouseReleased(MouseEvent e) {
230
setPopupTrigger(e);
231
}
232
});
233
234
frame.add(testButton, BorderLayout.CENTER);
235
frame.pack();
236
frame.setVisible(true);
237
AutoClosable.INSTANCE.setFrame(frame);
238
}
239
});
240
}
241
242
private static void createAndShowSplitPane() throws Exception {
243
SwingUtilities.invokeAndWait(new Runnable() {
244
245
@Override
246
public void run() {
247
JFrame frame = new JFrame("Test SplitPane");
248
frame.setSize(250, 200);
249
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
250
frame.setLayout(new BorderLayout());
251
252
shown = false;
253
jsp = new JSplitPane();
254
jsp.setRightComponent(new JPanel());
255
jsp.setLeftComponent(new JPanel());
256
jsp.setComponentPopupMenu(createJPopupMenu());
257
258
frame.add(jsp, BorderLayout.CENTER);
259
260
jsp.setDividerLocation(150);
261
262
frame.setLocation(400, 300);
263
frame.setVisible(true);
264
AutoClosable.INSTANCE.setFrame(frame);
265
}
266
});
267
}
268
269
private static void createAndShowSpinner() throws Exception {
270
SwingUtilities.invokeAndWait(new Runnable() {
271
272
@Override
273
public void run() {
274
JFrame frame = new JFrame("JSpinner Test");
275
frame.setLayout(new BorderLayout());
276
frame.setSize(200, 100);
277
shown = false;
278
spin = new JSpinner();
279
spin.setComponentPopupMenu(createJPopupMenu());
280
frame.add(spin, BorderLayout.CENTER);
281
frame.setVisible(true);
282
AutoClosable.INSTANCE.setFrame(frame);
283
}
284
});
285
}
286
287
private static void createAndShowFileChooser() throws Exception {
288
SwingUtilities.invokeLater(new Runnable() {
289
290
@Override
291
public void run() {
292
JFrame frame = new JFrame("FileChooser test dialog");
293
frame.setSize(100, 100);
294
295
shown = false;
296
filec = new JFileChooser();
297
filec.setComponentPopupMenu(createJPopupMenu());
298
filec.showOpenDialog(frame);
299
300
frame.setVisible(true);
301
AutoClosable.INSTANCE.setFrame(frame);
302
}
303
});
304
}
305
306
private static void closeFrame() {
307
SwingUtilities.invokeLater(new Runnable() {
308
309
@Override
310
public void run() {
311
AutoClosable.INSTANCE.close();
312
}
313
});
314
}
315
316
private static class AutoClosable {
317
318
static final AutoClosable INSTANCE = new AutoClosable();
319
private JFrame frame;
320
private JPopupMenu popup;
321
322
public void setFrame(JFrame frame) {
323
this.frame = frame;
324
}
325
326
public void setPopup(JPopupMenu popup) {
327
this.popup = popup;
328
}
329
330
public void close() {
331
frame.dispose();
332
if (popup != null) {
333
popup.setVisible(false);
334
}
335
}
336
}
337
}
338
339