Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JMenu/6538132/bug6538132.java
41153 views
1
/*
2
* Copyright (c) 2011, 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 6538132
28
* @summary Regression: Pressing Escape key don't close the menu items from jdk7.0 b07 onwards
29
* @author Alexander Potochkin
30
* @requires (os.family == "windows")
31
* @library /lib/client
32
* @build ExtendedRobot
33
* @run main bug6538132
34
*/
35
36
import javax.swing.*;
37
import java.awt.*;
38
import java.awt.event.InputEvent;
39
import java.awt.event.KeyEvent;
40
41
public class bug6538132 {
42
private static JMenu menu1;
43
private static JMenu menu2;
44
private static volatile boolean isWinLaf;
45
private static JFrame frame;
46
47
private static void createGui() {
48
try {
49
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
50
isWinLaf = true;
51
} catch (Exception e) {
52
// If we can't set WinLaf it means we are not under Windows
53
// make the test pass
54
isWinLaf = false;
55
return;
56
}
57
frame = new JFrame();
58
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
59
60
JMenuBar menuBar = new JMenuBar();
61
menu1 = createMenu();
62
menuBar.add(menu1);
63
menu2 = createMenu();
64
menuBar.add(menu2);
65
frame.setJMenuBar(menuBar);
66
67
frame.setSize(200, 200);
68
frame.setVisible(true);
69
}
70
71
static JMenu createMenu() {
72
JMenu menu = new JMenu("Menu");
73
menu.add(new JMenuItem("MenuItem"));
74
menu.add(new JMenuItem("MenuItem"));
75
menu.add(new JMenuItem("MenuItem"));
76
return menu;
77
}
78
79
public static void main(String[] args) throws Exception {
80
try {
81
SwingUtilities.invokeAndWait(new Runnable() {
82
public void run() {
83
bug6538132.createGui();
84
}
85
});
86
if(isWinLaf) {
87
ExtendedRobot robot = new ExtendedRobot();
88
robot.setAutoDelay(10);
89
robot.waitForIdle();
90
Point p1 = menu1.getLocationOnScreen();
91
final int x1 = p1.x + menu1.getWidth() / 2;
92
final int y1 = p1.y + menu1.getHeight() / 2;
93
robot.glide(0, 0, x1, y1);
94
robot.mousePress(InputEvent.BUTTON1_MASK);
95
robot.mouseRelease(InputEvent.BUTTON1_MASK);
96
assertPopupOpen();
97
Point p2 = menu2.getLocationOnScreen();
98
final int x2 = p2.x + menu2.getWidth() / 2;
99
final int y2 = p2.y + menu2.getHeight() / 2;
100
robot.glide(x1, y1, x2, y2);
101
assertPopupOpen();
102
robot.keyPress(KeyEvent.VK_ESCAPE);
103
robot.keyRelease(KeyEvent.VK_ESCAPE);
104
assertPopupNotOpen();
105
robot.glide(x2, y2, x1, y1);
106
assertPopupNotOpen();
107
robot.mousePress(InputEvent.BUTTON1_MASK);
108
robot.mouseRelease(InputEvent.BUTTON1_MASK);
109
assertPopupOpen();
110
}
111
} finally {
112
if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());
113
}
114
}
115
116
static void assertPopupOpen() {
117
if (getLastPopup() == null) {
118
throw new RuntimeException("PopupMenu is not open");
119
}
120
}
121
122
static void assertPopupNotOpen() {
123
if (getLastPopup() != null) {
124
throw new RuntimeException("PopupMenu is unexpectedly open");
125
}
126
}
127
128
// copied from BasicPopupMenuUI
129
static JPopupMenu getLastPopup() {
130
MenuSelectionManager msm = MenuSelectionManager.defaultManager();
131
MenuElement[] p = msm.getSelectedPath();
132
JPopupMenu popup = null;
133
134
for (int i = p.length - 1; popup == null && i >= 0; i--) {
135
if (p[i] instanceof JPopupMenu)
136
popup = (JPopupMenu) p[i];
137
}
138
return popup;
139
}
140
}
141
142