Skip to content

Commit e915df8

Browse files
author
Satyen Subramaniam
committed
8352678: Opensource few JMenuItem tests
8357305: Compilation failure in javax/swing/JMenuItem/bug6197830.java Reviewed-by: shade Backport-of: 8a40498d79a18dcf91cf4979d34c3583a6c39322
1 parent 93aa7d2 commit e915df8

File tree

7 files changed

+553
-186
lines changed

7 files changed

+553
-186
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
* Copyright (c) 2005, 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+
import java.awt.BorderLayout;
25+
import java.awt.Color;
26+
import java.awt.Component;
27+
import java.awt.ComponentOrientation;
28+
import java.awt.Graphics;
29+
import java.awt.event.KeyEvent;
30+
import javax.swing.Icon;
31+
import javax.swing.JCheckBoxMenuItem;
32+
import javax.swing.JFrame;
33+
import javax.swing.JLabel;
34+
import javax.swing.JMenu;
35+
import javax.swing.JMenuBar;
36+
import javax.swing.JMenuItem;
37+
import javax.swing.JRadioButtonMenuItem;
38+
import javax.swing.KeyStroke;
39+
import javax.swing.UIManager;
40+
41+
final class MenuItemTestHelper {
42+
43+
public static JFrame getMenuItemTestFrame(boolean isLeft, String lafName) {
44+
boolean applyLookAndFeel = lafName != null;
45+
if (applyLookAndFeel) {
46+
try {
47+
UIManager.setLookAndFeel(lafName);
48+
} catch (Exception e) {
49+
throw new RuntimeException(e);
50+
}
51+
}
52+
53+
Icon myIcon = new ColoredIcon(Color.RED, 10, 10);
54+
Icon myIcon2 = new ColoredIcon(Color.GREEN, 15, 10);
55+
56+
JMenuBar menuBar = new JMenuBar();
57+
menuBar.add(createViewMenu(myIcon, myIcon2));
58+
menuBar.add(createNoNothingMenu());
59+
menuBar.add(createSomeIconsMenu(myIcon, myIcon2));
60+
61+
String title = (isLeft ? "(Left-to-right)" : "(Right-to-left)") + " - Menu Item Test";
62+
JFrame frame = new JFrame(title);
63+
frame.setJMenuBar(menuBar);
64+
frame.applyComponentOrientation(isLeft
65+
? ComponentOrientation.LEFT_TO_RIGHT
66+
: ComponentOrientation.RIGHT_TO_LEFT);
67+
68+
if (applyLookAndFeel) {
69+
String shortName = lafName.substring(lafName.lastIndexOf('.') + 1);
70+
JLabel label = new JLabel("<HTML><H2>" + shortName + "</H2></HTML>");
71+
frame.setLayout(new BorderLayout());
72+
frame.add(label, BorderLayout.CENTER);
73+
}
74+
75+
frame.setSize(300, 300);
76+
return frame;
77+
}
78+
79+
public static JFrame getMenuItemTestFrame(boolean isLeft) {
80+
return getMenuItemTestFrame(isLeft, null);
81+
}
82+
83+
private static JMenu createViewMenu(Icon myIcon, Icon myIcon2) {
84+
JMenu menu = new JMenu("View");
85+
menu.setMnemonic('V');
86+
menu.add(new JMenuItem("Refresh"));
87+
menu.add(new JMenuItem("Customize..."));
88+
menu.add(new JCheckBoxMenuItem("Show Toolbar"));
89+
menu.addSeparator();
90+
menu.add(new JRadioButtonMenuItem("List"));
91+
menu.add(new JRadioButtonMenuItem("Icons"));
92+
93+
JRadioButtonMenuItem rm2 = new JRadioButtonMenuItem("And icon.");
94+
rm2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, KeyEvent.SHIFT_DOWN_MASK));
95+
rm2.setIcon(myIcon2);
96+
menu.add(rm2);
97+
98+
JRadioButtonMenuItem mi3 = new JRadioButtonMenuItem("Radio w/icon");
99+
mi3.setIcon(myIcon);
100+
menu.add(mi3);
101+
102+
menu.add(new JMenuItem(myIcon2));
103+
104+
JMenuItem mi4 = new JMenuItem("Item with icon");
105+
mi4.setIcon(myIcon);
106+
menu.addSeparator();
107+
menu.add(mi4);
108+
109+
return menu;
110+
}
111+
112+
private static JMenu createNoNothingMenu() {
113+
final JMenu noMenu = new JMenu("No nothing");
114+
115+
for (String label : new String[]{"One", "Two", "Threeee"}) {
116+
JMenuItem item = new JMenuItem(label);
117+
item.addActionListener((e) ->
118+
PassFailJFrame.log("menu.width = "
119+
+ noMenu.getPopupMenu().getWidth()));
120+
noMenu.add(item);
121+
}
122+
123+
return noMenu;
124+
}
125+
126+
private static JMenu createSomeIconsMenu(Icon myIcon, Icon myIcon2) {
127+
JMenu someIcons = new JMenu("Some icons");
128+
129+
JMenuItem imi1 = new JMenuItem("Icon!");
130+
imi1.setIcon(myIcon);
131+
someIcons.add(imi1);
132+
133+
JMenuItem imi2 = new JMenuItem("Wide icon!");
134+
imi2.setIcon(myIcon2);
135+
someIcons.add(imi2);
136+
137+
someIcons.add(new JCheckBoxMenuItem("CheckBox"));
138+
someIcons.add(new JRadioButtonMenuItem("RadioButton"));
139+
140+
return someIcons;
141+
}
142+
143+
private record ColoredIcon(Color color, int width, int height)
144+
implements Icon {
145+
@Override
146+
public void paintIcon(Component c, Graphics g, int x, int y) {
147+
Color oldColor = g.getColor();
148+
g.setColor(color);
149+
g.fillRect(x, y, width, height);
150+
g.setColor(oldColor);
151+
}
152+
153+
@Override
154+
public int getIconWidth() {
155+
return width;
156+
}
157+
158+
@Override
159+
public int getIconHeight() {
160+
return height;
161+
}
162+
}
163+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) 2005, 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+
/*
25+
* @test
26+
* @bug 4729669
27+
* @summary 1.4 REGRESSION: Text edge of different types of JMenuItems are not aligned
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
30+
* @run main/manual bug4729669
31+
*/
32+
33+
import java.util.List;
34+
import javax.swing.JFrame;
35+
36+
public class bug4729669 {
37+
38+
private static final String INSTRUCTIONS = """
39+
Two windows should appear: Left-to-right and Right-to-left.
40+
Check that text on all the menu items of all menus
41+
is properly vertically aligned.""";
42+
43+
public static void main(String[] args) throws Exception {
44+
PassFailJFrame.builder()
45+
.title("bug4729669 Instructions")
46+
.instructions(INSTRUCTIONS)
47+
.columns(35)
48+
.testUI(bug4729669::createTestUI)
49+
.positionTestUIRightColumn()
50+
.logArea()
51+
.build()
52+
.awaitAndCheck();
53+
}
54+
55+
private static List<JFrame> createTestUI() {
56+
JFrame f1 = MenuItemTestHelper.getMenuItemTestFrame(true);
57+
JFrame f2 = MenuItemTestHelper.getMenuItemTestFrame(false);
58+
return List.of(f1, f2);
59+
}
60+
}
61+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (c) 2005, 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+
/*
25+
* @test
26+
* @bug 6197830
27+
* @requires (os.family == "linux")
28+
* @summary Fix for 4729669 does not work on Motif and GTK look and feels
29+
* @library /java/awt/regtesthelpers
30+
* @build PassFailJFrame
31+
* @run main/manual bug6197830
32+
*/
33+
34+
import java.util.List;
35+
import javax.swing.JFrame;
36+
37+
public class bug6197830 {
38+
39+
private static final String INSTRUCTIONS = """
40+
Four windows should appear: Left-to-right and Right-to-left for
41+
the two different Look and Feels (Motif and GTK).
42+
Check that text on all the menu items of all menus is properly
43+
vertically aligned.""";
44+
45+
public static void main(String[] args) throws Exception {
46+
PassFailJFrame.builder()
47+
.title("bug6197830 Instructions")
48+
.instructions(INSTRUCTIONS)
49+
.columns(35)
50+
.testUI(bug6197830::createTestUI)
51+
.positionTestUIBottomRowCentered()
52+
.build()
53+
.awaitAndCheck();
54+
}
55+
56+
private static List<JFrame> createTestUI() {
57+
JFrame frame1 = MenuItemTestHelper.getMenuItemTestFrame(true,
58+
"com.sun.java.swing.plaf.motif.MotifLookAndFeel");
59+
JFrame frame2 = MenuItemTestHelper.getMenuItemTestFrame(false,
60+
"com.sun.java.swing.plaf.motif.MotifLookAndFeel");
61+
JFrame frame3 = MenuItemTestHelper.getMenuItemTestFrame(true,
62+
"com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
63+
JFrame frame4 = MenuItemTestHelper.getMenuItemTestFrame(false,
64+
"com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
65+
return List.of(frame1, frame2, frame3, frame4);
66+
}
67+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
/*
25+
* @test
26+
* @bug 4207339
27+
* @summary Verifies HTML label support for MenuItems
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
30+
* @run main/manual bug4207339
31+
*/
32+
33+
import javax.swing.JPanel;
34+
import javax.swing.JMenuItem;
35+
36+
public class bug4207339 {
37+
38+
private static final String INSTRUCTIONS = """
39+
This tests html support in menuItem.
40+
A MenuItem will be shown.
41+
If the MenuItem is showing "big" text bigger than rest
42+
and "red" text in red color and the text are in multiple lines,
43+
and text "Yo" in blue color,
44+
then press Pass else press Fail.""";
45+
46+
public static void main(String[] args) throws Exception {
47+
PassFailJFrame.builder()
48+
.title("bug4207339 Instructions")
49+
.instructions(INSTRUCTIONS)
50+
.columns(35)
51+
.splitUI(bug4207339::createTestUI)
52+
.build()
53+
.awaitAndCheck();
54+
}
55+
56+
private static JPanel createTestUI() {
57+
JPanel panel = new JPanel();
58+
JMenuItem mi = new JMenuItem("<html><center>Is this text <font size=+3>big</font>" +
59+
"and <font color=red>red</font>?" +
60+
"<br>And on multiple lines?<br><br>" +
61+
"<font size=+2 color=blue face=AvantGarde>Yo!</font>" +
62+
"Then press <em><b>PASS</b>!</center></html>");
63+
panel.add(mi);
64+
return panel;
65+
}
66+
67+
}

0 commit comments

Comments
 (0)