Skip to content

Commit 8342287

Browse files
Abhishek Kumarpull[bot]
authored andcommitted
8340809: Open source few more AWT PopupMenu tests
Reviewed-by: prr, aivanov
1 parent 52bad3f commit 8342287

File tree

5 files changed

+680
-0
lines changed

5 files changed

+680
-0
lines changed
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
/*
2+
* Copyright (c) 1999, 2024, 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.BorderLayout;
25+
import java.awt.Button;
26+
import java.awt.Component;
27+
import java.awt.Dimension;
28+
import java.awt.EventQueue;
29+
import java.awt.Frame;
30+
import java.awt.Label;
31+
import java.awt.Menu;
32+
import java.awt.MenuBar;
33+
import java.awt.MenuItem;
34+
import java.awt.Panel;
35+
import java.awt.Point;
36+
import java.awt.PopupMenu;
37+
import java.awt.Robot;
38+
39+
import java.awt.event.ActionEvent;
40+
import java.awt.event.ActionListener;
41+
import java.awt.event.InputEvent;
42+
import java.awt.event.MouseAdapter;
43+
import java.awt.event.MouseEvent;
44+
45+
import java.util.Hashtable;
46+
47+
/*
48+
* @test
49+
* @bug 4214550
50+
* @summary Tests that there is no seg fault on repeatedly showing
51+
* PopupMenu by right-clicking Label, Panel or Button
52+
* @key headful
53+
* @run main ActivePopupCrashTest
54+
*/
55+
56+
public class ActivePopupCrashTest {
57+
private static Frame f;
58+
private static Label l;
59+
private static Button b;
60+
private static Panel p;
61+
62+
private static volatile Point labelCenter;
63+
private static volatile Point buttonCenter;
64+
private static volatile Point panelCenter;
65+
66+
public static void main(String[] args) throws Exception {
67+
final int REPEAT_COUNT = 5;
68+
try {
69+
Robot robot = new Robot();
70+
robot.setAutoDelay(50);
71+
EventQueue.invokeAndWait(ActivePopupCrashTest::createAndShowUI);
72+
robot.delay(1000);
73+
74+
EventQueue.invokeAndWait(() -> {
75+
labelCenter = getCenterPoint(l);
76+
buttonCenter = getCenterPoint(b);
77+
panelCenter = getCenterPoint(p);
78+
});
79+
80+
for (int i = 0; i < REPEAT_COUNT; i++) {
81+
robot.mouseMove(labelCenter.x, labelCenter.y);
82+
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
83+
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
84+
85+
robot.mouseMove(buttonCenter.x, buttonCenter.y);
86+
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
87+
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
88+
89+
robot.mouseMove(panelCenter.x, panelCenter.y);
90+
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
91+
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
92+
}
93+
94+
// To close the popup, otherwise test fails on windows with timeout error
95+
robot.mouseMove(panelCenter.x - 5, panelCenter.y - 5);
96+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
97+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
98+
} finally {
99+
EventQueue.invokeAndWait(() -> {
100+
if (f != null) {
101+
f.dispose();
102+
}
103+
});
104+
}
105+
}
106+
107+
private static Point getCenterPoint(Component component) {
108+
Point p = component.getLocationOnScreen();
109+
Dimension size = component.getSize();
110+
return new Point(p.x + size.width / 2, p.y + size.height / 2);
111+
}
112+
113+
public static void createAndShowUI() {
114+
f = new Frame("ActivePopupCrashTest Test");
115+
MenuItem item = new MenuItem("file-1");
116+
item.addActionListener(ActivePopupCrashTest::logActionEvent);
117+
Menu m = new Menu("file");
118+
m.add(item);
119+
item = new MenuItem("file-2");
120+
m.add(item);
121+
MenuBar mb = new MenuBar();
122+
mb.add(m);
123+
124+
f.setMenuBar(mb);
125+
f.setSize(200, 200);
126+
f.setLayout(new BorderLayout());
127+
128+
l = new Label("label");
129+
addPopup(l, "label");
130+
f.add(l, BorderLayout.NORTH);
131+
132+
p = new Panel();
133+
addPopup(p, "panel");
134+
f.add(p, BorderLayout.CENTER);
135+
136+
b = new Button("button");
137+
addPopup(b, "button");
138+
f.add(b, BorderLayout.SOUTH);
139+
140+
f.setSize(400, 300);
141+
f.setLocationRelativeTo(null);
142+
f.setVisible(true);
143+
}
144+
145+
static void addPopup(Component c, String name) {
146+
PopupMenu pm = new PopupMenu();
147+
MenuItem mi = new MenuItem(name + "-1");
148+
mi.addActionListener(ActivePopupCrashTest::logActionEvent);
149+
pm.add(mi);
150+
151+
mi = new MenuItem(name + "-2");
152+
pm.add(mi);
153+
154+
setHash(c, pm);
155+
c.add(pm);
156+
c.addMouseListener(new MouseAdapter() {
157+
@Override
158+
public void mouseClicked(MouseEvent e) {
159+
mouseAction("mouseClicked", e);
160+
}
161+
162+
@Override
163+
public void mousePressed(MouseEvent e) {
164+
mouseAction("mousePressed", e);
165+
}
166+
167+
@Override
168+
public void mouseReleased(MouseEvent e) {
169+
mouseAction("mouseReleased", e);
170+
}
171+
});
172+
}
173+
174+
static void logActionEvent(ActionEvent e) {
175+
System.out.println("actionPerformed, event=" + e + ", mod=" + getMods(e));
176+
System.out.println("command=" + e.getActionCommand());
177+
System.out.println("param=" + e.paramString());
178+
System.out.println("source=" + e.getSource());
179+
}
180+
181+
static String getMods(ActionEvent e) { return getMods(e.getModifiers()); }
182+
183+
static String getMods(MouseEvent e) { return getMods(e.getModifiers()); }
184+
185+
static String getMods(int mods) {
186+
String modstr = "";
187+
if ((mods & ActionEvent.SHIFT_MASK) == ActionEvent.SHIFT_MASK) {
188+
modstr += (" SHIFT");
189+
} else if ((mods & ActionEvent.ALT_MASK) == ActionEvent.ALT_MASK) {
190+
modstr += (" ALT");
191+
} else if ((mods & ActionEvent.CTRL_MASK) == ActionEvent.CTRL_MASK) {
192+
modstr += (" CTRL");
193+
} else if ((mods & ActionEvent.META_MASK) == ActionEvent.META_MASK) {
194+
modstr += (" META");
195+
}
196+
return modstr;
197+
}
198+
199+
static void mouseAction(String which, MouseEvent e) {
200+
Component c = e.getComponent();
201+
System.out.println(which + " e = " + e + " , mods = " + getMods(e) +
202+
" , component = " + c);
203+
if (e.isPopupTrigger()) {
204+
System.out.println("isPopup");
205+
PopupMenu pm = getHash(c);
206+
pm.show(c, c.getWidth() / 2, c.getHeight() / 2);
207+
}
208+
}
209+
210+
static Hashtable<Component, PopupMenu> popupTable = new Hashtable<>();
211+
212+
static void setHash(Component c, PopupMenu p) {
213+
popupTable.put(c, p);
214+
}
215+
216+
static PopupMenu getHash(Component c) {
217+
return popupTable.get(c);
218+
}
219+
220+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Copyright (c) 2004, 2024, 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.Dimension;
25+
import java.awt.EventQueue;
26+
import java.awt.Frame;
27+
import java.awt.Label;
28+
import java.awt.Menu;
29+
import java.awt.MenuItem;
30+
import java.awt.Point;
31+
import java.awt.PopupMenu;
32+
import java.awt.Robot;
33+
34+
import java.awt.event.InputEvent;
35+
import java.awt.event.KeyEvent;
36+
import java.awt.event.MouseAdapter;
37+
import java.awt.event.MouseEvent;
38+
39+
/*
40+
* @test
41+
* @bug 5021183
42+
* @summary Tests Key Traversal doesn't crash PopupMenu
43+
* @key headful
44+
* @run main KeyTraversalCrash
45+
*/
46+
47+
public class KeyTraversalCrash {
48+
private static Frame f;
49+
private static Label label;
50+
51+
private static volatile Point loc;
52+
private static volatile Dimension dim;
53+
54+
public static void main(String[] args) throws Exception {
55+
try {
56+
Robot robot = new Robot();
57+
robot.setAutoDelay(100);
58+
EventQueue.invokeAndWait(KeyTraversalCrash::createAndShowUI);
59+
robot.delay(1000);
60+
61+
EventQueue.invokeAndWait(() -> {
62+
loc = label.getLocationOnScreen();
63+
dim = label.getSize();
64+
});
65+
66+
robot.mouseMove(loc.x + 20, loc.y + 20);
67+
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
68+
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
69+
70+
robot.mouseMove(loc.x + 25, loc.y + 25);
71+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
72+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
73+
74+
robot.keyPress(KeyEvent.VK_LEFT);
75+
robot.keyRelease(KeyEvent.VK_LEFT);
76+
77+
robot.keyPress(KeyEvent.VK_DOWN);
78+
robot.keyRelease(KeyEvent.VK_DOWN);
79+
80+
// To close the popup, otherwise test fails on windows with timeout error
81+
robot.mouseMove(loc.x + dim.width - 20, loc.y + dim.height - 20);
82+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
83+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
84+
} finally {
85+
EventQueue.invokeAndWait(() -> {
86+
if (f != null) {
87+
f.dispose();
88+
}
89+
});
90+
}
91+
}
92+
93+
public static void createAndShowUI() {
94+
f = new Frame("KeyTraversalCrash Test");
95+
final PopupMenu popup = new PopupMenu();
96+
for (int i = 0; i < 10; i++) {
97+
Menu menu = new Menu("Menu " + i);
98+
for(int j = 0; j < 10; j++) {
99+
MenuItem menuItem = new MenuItem("MenuItem " + j);
100+
menu.add(menuItem);
101+
}
102+
popup.add(menu);
103+
}
104+
label = new Label("Label");
105+
f.add(label);
106+
f.add(popup);
107+
label.addMouseListener(new MouseAdapter() {
108+
@Override
109+
public void mousePressed(MouseEvent me) {
110+
if (me.isPopupTrigger()) {
111+
popup.show(me.getComponent(), me.getX(), me.getY());
112+
}
113+
}
114+
115+
@Override
116+
public void mouseReleased(MouseEvent me) {
117+
if (me.isPopupTrigger()) {
118+
popup.show(me.getComponent(), me.getX(), me.getY());
119+
}
120+
}
121+
});
122+
f.setSize(200, 200);
123+
f.setLocationRelativeTo(null);
124+
f.setVisible(true);
125+
}
126+
}

0 commit comments

Comments
 (0)