Skip to content

Commit 8a40498

Browse files
committed
8352678: Opensource few JMenuItem tests
Reviewed-by: abhiscxk
1 parent 66b5dba commit 8a40498

File tree

4 files changed

+336
-0
lines changed

4 files changed

+336
-0
lines changed
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+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright (c) 2001, 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 4327146
27+
* @summary Tests menu width after removeAll()
28+
* @key headful
29+
* @run main bug4327146
30+
*/
31+
32+
import java.awt.Dimension;
33+
import java.awt.Point;
34+
import java.awt.Rectangle;
35+
import java.awt.Robot;
36+
import java.awt.event.InputEvent;
37+
import javax.swing.JButton;
38+
import javax.swing.JFrame;
39+
import javax.swing.JMenu;
40+
import javax.swing.JMenuBar;
41+
import javax.swing.JMenuItem;
42+
import javax.swing.SwingUtilities;
43+
44+
public class bug4327146 {
45+
private static JButton b;
46+
private static JMenu m;
47+
private static JFrame frame;
48+
private static volatile Point loc;
49+
private static volatile Dimension dim;
50+
private static volatile Rectangle old_popupBounds;
51+
private static volatile Rectangle new_popupBounds;
52+
53+
public static void main(String[] args) throws Exception {
54+
Robot robot = new Robot();
55+
try {
56+
SwingUtilities.invokeAndWait(() -> {
57+
frame = new JFrame("bug4327146");
58+
m = new JMenu("Menu");
59+
m.add(new JMenuItem("I'm an ugly bug, fix me right now please!"));
60+
61+
JMenuBar mbar = new JMenuBar();
62+
mbar.add(m);
63+
frame.setJMenuBar(mbar);
64+
65+
b = new JButton("Cut");
66+
b.addActionListener(e -> {
67+
m.removeAll();
68+
m.add(new JMenuItem("Fixed :)"));
69+
});
70+
mbar.add(b);
71+
frame.pack();
72+
frame.setLocationRelativeTo(null);
73+
frame.setVisible(true);
74+
});
75+
robot.waitForIdle();
76+
robot.delay(1000);
77+
SwingUtilities.invokeAndWait(() -> {
78+
loc = b.getLocationOnScreen();
79+
dim = b.getSize();
80+
m.doClick();
81+
});
82+
robot.waitForIdle();
83+
robot.delay(500);
84+
SwingUtilities.invokeAndWait(() -> {
85+
old_popupBounds = m.getPopupMenu().getBounds();
86+
});
87+
robot.mouseMove(loc.x + dim.width / 2, loc.y + dim.height / 2);
88+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
89+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
90+
robot.waitForIdle();
91+
robot.delay(500);
92+
SwingUtilities.invokeAndWait(() -> {
93+
m.doClick();
94+
});
95+
robot.waitForIdle();
96+
robot.delay(500);
97+
SwingUtilities.invokeAndWait(() -> {
98+
new_popupBounds = m.getPopupMenu().getBounds();
99+
});
100+
if (new_popupBounds.getWidth() >= old_popupBounds.getWidth()) {
101+
System.out.println("before cut popup Bounds " + old_popupBounds);
102+
System.out.println("after cut popupBounds " + new_popupBounds);
103+
throw new RuntimeException("JMenu popup width is wrong");
104+
}
105+
} finally {
106+
SwingUtilities.invokeAndWait(() -> {
107+
if (frame != null) {
108+
frame.dispose();
109+
}
110+
});
111+
}
112+
}
113+
114+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) 2001, 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 4402082
27+
* @requires (os.family == "windows")
28+
* @summary Tests that JMenuItem accelerator is rendered correctly.
29+
* @library /java/awt/regtesthelpers
30+
* @build PassFailJFrame
31+
* @run main/manual bug4402082
32+
*/
33+
34+
import java.awt.Container;
35+
import java.awt.GridLayout;
36+
import java.awt.event.KeyEvent;
37+
import javax.swing.JFrame;
38+
import javax.swing.JMenuItem;
39+
import javax.swing.KeyStroke;
40+
import javax.swing.UIManager;
41+
42+
public class bug4402082 {
43+
44+
private static final String INSTRUCTIONS = """
45+
You see three menu items, each having different look-and-feels.
46+
Each menu item should display accelerator "F1" on its right side.
47+
The accelerator should be fully visible. If it is partially
48+
offscreen, or not visible at all, test fails.""";
49+
50+
public static void main(String[] args) throws Exception {
51+
PassFailJFrame.builder()
52+
.title("bug4402082 Instructions")
53+
.instructions(INSTRUCTIONS)
54+
.columns(35)
55+
.testUI(bug4402082::createTestUI)
56+
.build()
57+
.awaitAndCheck();
58+
}
59+
60+
static JMenuItem getMenuItem(String lnf) {
61+
try {
62+
UIManager.setLookAndFeel(lnf);
63+
} catch (Exception exc) {
64+
System.err.println("Could not load LookAndFeel: " + lnf);
65+
}
66+
JMenuItem mi = new JMenuItem("Bad Item");
67+
mi.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0));
68+
return mi;
69+
}
70+
71+
private static JFrame createTestUI() {
72+
JFrame frame = new JFrame("bug4402082");
73+
Container pane = frame.getContentPane();
74+
pane.setLayout(new GridLayout(3,1));
75+
pane.add(getMenuItem("javax.swing.plaf.metal.MetalLookAndFeel"));
76+
pane.add(getMenuItem("com.sun.java.swing.plaf.motif.MotifLookAndFeel"));
77+
pane.add(getMenuItem("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"));
78+
frame.pack();
79+
return frame;
80+
}
81+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
public class bug6197830 {
35+
36+
private static final String INSTRUCTIONS = """
37+
Four windows should appear: Left-to-right and Right-to-left for
38+
the two different Look and Feels (Motif and GTK).
39+
Check that text on all the menu items of all menus is properly
40+
vertically aligned.""";
41+
42+
public static void main(String[] args) throws Exception {
43+
PassFailJFrame.builder()
44+
.title("bug6197830 Instructions")
45+
.instructions(INSTRUCTIONS)
46+
.columns(35)
47+
.testUI(bug6197830::createTestUI)
48+
.position(PassFailJFrame.Position.TOP_LEFT_CORNER)
49+
.build()
50+
.awaitAndCheck();
51+
}
52+
53+
private static List<JFrame> createTestUI() {
54+
JFrame frame1 = MenuItemTest.doMenuItemTest(true,
55+
"com.sun.java.swing.plaf.motif.MotifLookAndFeel",
56+
20);
57+
frame1.setLocation(300, 300);
58+
JFrame frame2 = MenuItemTest.doMenuItemTest(false,
59+
"com.sun.java.swing.plaf.motif.MotifLookAndFeel",
60+
20);
61+
frame2.setLocation((int)(frame1.getLocation().getX() + frame1.getWidth()
62+
+ 100), 300);
63+
JFrame frame3 = MenuItemTest.doMenuItemTest(true,
64+
"com.sun.java.swing.plaf.gtk.GTKLookAndFeel", 420);
65+
frame3.setLocation(300, (int)(frame1.getLocation().getY()
66+
+ frame1.getHeight() + 100));
67+
JFrame frame4 = MenuItemTest.doMenuItemTest(false,
68+
"com.sun.java.swing.plaf.gtk.GTKLookAndFeel", 420);
69+
frame4.setLocation((int)(frame3.getLocation().getX() + frame3.getWidth()
70+
+ 100),
71+
(int)frame3.getLocation().getY());
72+
return List.of(frame1, frame2, frame3, frame4);
73+
}
74+
}

0 commit comments

Comments
 (0)