Skip to content

Commit 98dac46

Browse files
committed
8353589: Open source a few Swing menu-related tests
Reviewed-by: jdv, honkar
1 parent e433fa2 commit 98dac46

File tree

3 files changed

+294
-0
lines changed

3 files changed

+294
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright (c) 1999, 2025, 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+
/* @test
25+
* @bug 4119993
26+
* @library /java/awt/regtesthelpers
27+
* @build PassFailJFrame
28+
* @summary Check that mouse button 3 is reserved for popup invocation not selection.
29+
* @run main/manual bug4119993
30+
*/
31+
32+
import java.awt.BorderLayout;
33+
import java.awt.Dimension;
34+
import javax.swing.BoxLayout;
35+
import javax.swing.JFrame;
36+
import javax.swing.JLabel;
37+
import javax.swing.JList;
38+
import javax.swing.JPanel;
39+
import javax.swing.JScrollPane;
40+
import javax.swing.JTable;
41+
import javax.swing.JTextArea;
42+
import javax.swing.border.BevelBorder;
43+
44+
/*
45+
* This is a sort of negative test. Mouse Button 3 is not supposed to cause selections.
46+
* If it did, then it would not be useable to invoke popup menus.
47+
* So this popup menu test .. does not popup menus.
48+
*/
49+
50+
public class bug4119993 {
51+
52+
static final String INSTRUCTIONS = """
53+
<html>
54+
The test window contains a text area, a table, and a list.
55+
<p>
56+
For each component, try to select text/cells/rows/items as appropriate
57+
using the <font size=+2 color=red>RIGHT</font> mouse button (Mouse Button 3).
58+
<p>
59+
If the selection changes, then press <em><b>FAIL</b></em>.
60+
<p>
61+
If the selection does not change, press <em><b>PASS</b></em></html
62+
</html>
63+
""";
64+
65+
public static void main(String[] args) throws Exception {
66+
PassFailJFrame.builder()
67+
.instructions(INSTRUCTIONS)
68+
.columns(60)
69+
.testUI(bug4119993::createUI)
70+
.build()
71+
.awaitAndCheck();
72+
}
73+
74+
static JFrame createUI() {
75+
JFrame frame = new JFrame("bug4119993");
76+
JPanel p = new JPanel();
77+
p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
78+
frame.add(p);
79+
80+
String text = "This is some text that you should try to select using the right mouse button";
81+
JTextArea area = new JTextArea(text, 5, 40);
82+
JScrollPane scrollpane0 = new JScrollPane(area);
83+
scrollpane0.setBorder(new BevelBorder(BevelBorder.LOWERED));
84+
scrollpane0.setPreferredSize(new Dimension(430, 200));
85+
p.add(scrollpane0);
86+
87+
String[][] data = new String[5][5];
88+
String[] cols = new String[5];
89+
for (int r = 0; r < 5; r ++) {
90+
cols[r] = "col " + r;
91+
for (int c = 0; c < 5; c ++) {
92+
data[r][c] = "(" + r + "," + c + ")";
93+
}
94+
}
95+
96+
JTable tableView = new JTable(data, cols);
97+
JScrollPane scrollpane = new JScrollPane(tableView);
98+
scrollpane.setBorder(new BevelBorder(BevelBorder.LOWERED));
99+
scrollpane.setPreferredSize(new Dimension(430, 200));
100+
p.add(scrollpane);
101+
102+
String[] s = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
103+
JList listView = new JList(s);
104+
JScrollPane scrollpane2 = new JScrollPane(listView);
105+
scrollpane2.setBorder(new BevelBorder(BevelBorder.LOWERED));
106+
scrollpane2.setPreferredSize(new Dimension(430, 200));
107+
p.add(scrollpane2);
108+
109+
frame.pack();
110+
return frame;
111+
}
112+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (c) 1999, 2025, 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+
/* @test
25+
@bug 4187004
26+
@summary test that title/label is show on menu for Motif L&F
27+
@key headful
28+
*/
29+
30+
import java.awt.Dimension;
31+
import java.awt.Robot;
32+
import javax.swing.JFrame;
33+
import javax.swing.JPopupMenu;
34+
import javax.swing.SwingUtilities;
35+
import javax.swing.UIManager;
36+
37+
public class bug4187004 {
38+
39+
static volatile JPopupMenu m;
40+
static volatile Dimension d1, d2;
41+
42+
public static void main(String[] args) throws Exception {
43+
try {
44+
SwingUtilities.invokeAndWait(bug4187004::createUI);
45+
46+
Robot robot = new Robot();
47+
robot.waitForIdle();
48+
robot.delay(1000);
49+
d1 = m.getSize();
50+
SwingUtilities.invokeAndWait(bug4187004::hideMenu);
51+
robot.waitForIdle();
52+
robot.delay(1000);
53+
SwingUtilities.invokeAndWait(bug4187004::updateUI);
54+
robot.waitForIdle();
55+
robot.delay(1000);
56+
SwingUtilities.invokeAndWait(bug4187004::showMenu);
57+
robot.waitForIdle();
58+
robot.delay(1000);
59+
d2 = m.getSize();
60+
} finally {
61+
SwingUtilities.invokeAndWait(bug4187004::hideMenu);
62+
}
63+
System.out.println(d1);
64+
System.out.println(d2);
65+
if (d1.width <= d2.width) {
66+
throw new RuntimeException("Menu not updated");
67+
}
68+
}
69+
70+
static void createUI() {
71+
try {
72+
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
73+
} catch (Exception e) {
74+
throw new RuntimeException(e);
75+
}
76+
m = new JPopupMenu("One really long menu title");
77+
m.add("Item 1");
78+
m.add("Item 2");
79+
m.add("Item 3");
80+
m.add("Item 4");
81+
m.pack();
82+
m.setVisible(true);
83+
}
84+
85+
static void hideMenu() {
86+
m.setVisible(false);
87+
}
88+
89+
static void showMenu() {
90+
m.setVisible(true);
91+
}
92+
93+
static void updateUI() {
94+
m.setLabel("short");
95+
m.pack();
96+
}
97+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) 2002, 2025, 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+
/* @test
25+
* @bug 4530303
26+
* @library /java/awt/regtesthelpers
27+
* @build PassFailJFrame
28+
* @summary Tests JPopupMenu.pack()
29+
* @run main/manual bug4530303
30+
*/
31+
32+
import java.awt.event.MouseAdapter;
33+
import java.awt.event.MouseEvent;
34+
import javax.swing.JFrame;
35+
import javax.swing.JMenu;
36+
import javax.swing.JMenuBar;
37+
import javax.swing.JMenuItem;
38+
import javax.swing.JPopupMenu;
39+
40+
public class bug4530303 {
41+
42+
static final String INSTRUCTIONS = """
43+
The test window has a menu bar.
44+
Open the menu "Menu" and place the mouse pointer over the first menu item, "Point here".
45+
The second menu item, "Ghost", should be replaced with another item, "Fixed!".
46+
If the item just disappears and no new item appears in the empty space, the test FAILS.
47+
""";
48+
49+
static volatile JMenu menu;
50+
51+
public static void main(String[] args) throws Exception {
52+
PassFailJFrame.builder()
53+
.instructions(INSTRUCTIONS)
54+
.columns(60)
55+
.testUI(bug4530303::createUI)
56+
.build()
57+
.awaitAndCheck();
58+
}
59+
60+
static JFrame createUI() {
61+
JFrame frame = new JFrame("bug4530303");
62+
menu = new JMenu("Menu");
63+
JMenuItem item = new JMenuItem("Point here");
64+
item.addMouseListener(new MenuBuilder());
65+
menu.add(item);
66+
menu.add(new JMenuItem("Ghost"));
67+
68+
JMenuBar mbar = new JMenuBar();
69+
mbar.add(menu);
70+
frame.setJMenuBar(mbar);
71+
frame.setSize(300, 300);
72+
return frame;
73+
}
74+
75+
static class MenuBuilder extends MouseAdapter {
76+
public void mouseEntered(MouseEvent ev) {
77+
menu.remove(1);
78+
menu.add(new JMenuItem("Fixed!"));
79+
80+
JPopupMenu pm = menu.getPopupMenu();
81+
pm.pack();
82+
pm.paintImmediately(pm.getBounds());
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)