Skip to content

Commit 7c5f2a2

Browse files
author
Tejesh R
committed
8315669: Open source several Swing PopupMenu related tests
Reviewed-by: dnguyen, psadhukhan
1 parent cf74b8c commit 7c5f2a2

File tree

6 files changed

+750
-0
lines changed

6 files changed

+750
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright (c) 1999, 2023, 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 javax.swing.Action;
25+
import javax.swing.JMenuItem;
26+
import javax.swing.JPopupMenu;
27+
import javax.swing.MenuElement;
28+
import javax.swing.SwingUtilities;
29+
import java.awt.event.ActionEvent;
30+
import java.beans.PropertyChangeListener;
31+
32+
/*
33+
* @test
34+
* @bug 4236750
35+
* @summary Tests presence of JPopupMenu.insert(Action, int)
36+
* @run main bug4236750
37+
*/
38+
39+
public class bug4236750 {
40+
private static MenuElement[] elements;
41+
private static volatile boolean passed = true;
42+
43+
/**
44+
* Auxilliary class implementing Action
45+
*/
46+
static class NullAction implements Action {
47+
public void addPropertyChangeListener(
48+
PropertyChangeListener listener) {
49+
}
50+
51+
public void removePropertyChangeListener(
52+
PropertyChangeListener listener) {
53+
}
54+
55+
public void setEnabled(boolean b) {
56+
}
57+
58+
public boolean isEnabled() {
59+
return true;
60+
}
61+
62+
public void actionPerformed(ActionEvent e) {
63+
}
64+
65+
private String name;
66+
67+
public NullAction(String s) {
68+
name = s;
69+
}
70+
71+
public void putValue(String key, Object value) {
72+
if (key.equals(Action.NAME)) {
73+
name = (String) value;
74+
}
75+
}
76+
77+
public Object getValue(String key) {
78+
if (key.equals(Action.NAME)) {
79+
return name;
80+
}
81+
return null;
82+
}
83+
}
84+
85+
public static void main(String[] args) throws Exception {
86+
SwingUtilities.invokeAndWait(() -> {
87+
JPopupMenu popup;
88+
popup = new JPopupMenu("Test Popup");
89+
JMenuItem item0 = popup.add(new NullAction("0"));
90+
JMenuItem item2 = popup.add(new NullAction("2"));
91+
popup.insert(new NullAction("1"), 1);
92+
elements = popup.getSubElements();
93+
for (int i = 0; i < 3; i++) {
94+
JMenuItem mi = (JMenuItem) elements[i];
95+
if (!mi.getText().equals("" + i)) {
96+
passed = false;
97+
}
98+
}
99+
});
100+
101+
if (!passed) {
102+
throw new RuntimeException("Failed: wrong order of menuitems");
103+
}
104+
System.out.println("Test Passed!");
105+
}
106+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) 2002, 2023, 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 javax.swing.JFrame;
25+
import javax.swing.JMenu;
26+
import javax.swing.JMenuBar;
27+
import javax.swing.JMenuItem;
28+
import javax.swing.SwingUtilities;
29+
import java.awt.Robot;
30+
import java.io.ByteArrayOutputStream;
31+
import java.io.ObjectOutputStream;
32+
33+
34+
/*
35+
* @test
36+
* @bug 4321273
37+
* @summary NotSerializableException during the menu serialization
38+
* @key headful
39+
* @run main bug4321273
40+
*/
41+
42+
public class bug4321273 {
43+
public static JFrame frame;
44+
public static JMenu menu;
45+
46+
public static void main(String[] args) throws Exception {
47+
try {
48+
Robot robot = new Robot();
49+
SwingUtilities.invokeAndWait(() -> {
50+
JMenuBar menuBar = new JMenuBar();
51+
frame = new JFrame();
52+
frame.setJMenuBar(menuBar);
53+
menu = new JMenu("Menu");
54+
menuBar.add(menu);
55+
menu.add(new JMenuItem("item 1"));
56+
menu.add(new JMenuItem("item 2"));
57+
menu.add(new JMenuItem("item 3"));
58+
frame.pack();
59+
frame.setVisible(true);
60+
});
61+
robot.waitForIdle();
62+
robot.delay(1000);
63+
SwingUtilities.invokeAndWait(() -> {
64+
menu.doClick();
65+
try {
66+
ByteArrayOutputStream byteArrayOutputStream =
67+
new ByteArrayOutputStream();
68+
ObjectOutputStream oos =
69+
new ObjectOutputStream(byteArrayOutputStream);
70+
oos.writeObject(menu);
71+
} catch (Exception se) {
72+
throw new RuntimeException("NotSerializableException " +
73+
"during the menu serialization", se);
74+
}
75+
});
76+
77+
robot.waitForIdle();
78+
robot.delay(100);
79+
System.out.println("Test Passed!");
80+
} finally {
81+
SwingUtilities.invokeAndWait(() -> {
82+
if (frame != null) {
83+
frame.dispose();
84+
}
85+
});
86+
}
87+
}
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Copyright (c) 2002, 2023, 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 javax.swing.JFrame;
25+
import javax.swing.JMenuItem;
26+
import javax.swing.JPopupMenu;
27+
import javax.swing.SwingUtilities;
28+
import java.awt.Component;
29+
import java.awt.Dimension;
30+
import java.awt.GraphicsConfiguration;
31+
import java.awt.GraphicsDevice;
32+
import java.awt.GraphicsEnvironment;
33+
import java.awt.Insets;
34+
import java.awt.Robot;
35+
import java.awt.Window;
36+
import java.awt.Toolkit;
37+
import java.awt.event.ActionEvent;
38+
import java.awt.event.ActionListener;
39+
import java.awt.event.MouseAdapter;
40+
import java.awt.event.MouseEvent;
41+
import java.awt.event.InputEvent;
42+
43+
/*
44+
* @test
45+
* @bug 4711693
46+
* @summary Pop-up doesn't stay up
47+
* @key headful
48+
* @run main bug4711693
49+
*/
50+
51+
public class bug4711693 {
52+
static JFrame fr;
53+
static Robot robot;
54+
static volatile boolean passed = true;
55+
static volatile Dimension scr;
56+
57+
public static void main(String[] args) throws Exception {
58+
try {
59+
robot = new Robot();
60+
SwingUtilities.invokeAndWait(() -> {
61+
fr = new JFrame("Test 4711693");
62+
scr = new Dimension();
63+
fr.setSize(600, 600);
64+
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
65+
GraphicsDevice[] gs = ge.getScreenDevices();
66+
GraphicsConfiguration gc = null;
67+
68+
for (int j = 0; j < gs.length; j++) {
69+
GraphicsDevice gd = gs[j];
70+
gc = gd.getDefaultConfiguration();
71+
if (gc.getBounds().contains(100, 100)) break;
72+
}
73+
scr = Toolkit.getDefaultToolkit().getScreenSize();
74+
Insets ins = Toolkit.getDefaultToolkit().getScreenInsets(gc);
75+
scr.width -= ins.right;
76+
scr.height -= ins.bottom;
77+
fr.setLocation(scr.width - 400, scr.height - 400);
78+
fr.setVisible(true);
79+
});
80+
robot.waitForIdle();
81+
robot.delay(1000);
82+
SwingUtilities.invokeAndWait(() -> {
83+
final JPopupMenu popupMenu = new JPopupMenu();
84+
final Component pane = fr.getContentPane();
85+
for (int i = 1; i < 10; i++) {
86+
final String itemName = "Item " + i;
87+
JMenuItem it = popupMenu.add(new JMenuItem(itemName));
88+
it.addActionListener(new ActionListener() {
89+
public void actionPerformed(ActionEvent a) {
90+
passed = false;
91+
}
92+
});
93+
}
94+
95+
pane.addMouseListener(new MouseAdapter() {
96+
public void mousePressed(MouseEvent e) {
97+
if ((e.isAltDown() ||
98+
((e.getModifiersEx() &
99+
InputEvent.BUTTON3_DOWN_MASK) != 0))) {
100+
Component parent = e.getComponent();
101+
while (parent != null && !(parent instanceof Window)) {
102+
parent = parent.getParent();
103+
}
104+
popupMenu.show(pane, e.getX(), e.getY());
105+
}
106+
}
107+
});
108+
});
109+
110+
robot.mouseMove(scr.width - 55, scr.height - 55);
111+
robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
112+
robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
113+
} finally {
114+
SwingUtilities.invokeAndWait(() -> {
115+
if (fr != null) {
116+
fr.dispose();
117+
}
118+
});
119+
}
120+
if (!passed) {
121+
throw new RuntimeException("Test failed. Popup disposed on mouse release.");
122+
} else {
123+
System.out.println("Test Passed!");
124+
}
125+
}
126+
}

0 commit comments

Comments
 (0)