Skip to content

Commit cfc648b

Browse files
committed
8352677: Opensource JMenu tests - series2
Reviewed-by: abhiscxk
1 parent 2ea1557 commit cfc648b

File tree

3 files changed

+356
-0
lines changed

3 files changed

+356
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 4187996
27+
* @summary Tests that Metal submenus overlap menu
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
30+
* @run main/manual bug4187996
31+
*/
32+
33+
import javax.swing.JFrame;
34+
import javax.swing.JMenu;
35+
import javax.swing.JMenuBar;
36+
import javax.swing.JMenuItem;
37+
import javax.swing.UIManager;
38+
39+
public class bug4187996 {
40+
41+
private static final String INSTRUCTIONS = """
42+
Open the menu "Menu", then "Submenu".
43+
The submenu should be top-aligned with the menu,
44+
and slightly overlap it horizontally. Otherwise test fails.""";
45+
46+
public static void main(String[] args) throws Exception {
47+
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
48+
PassFailJFrame.builder()
49+
.title("bug4187996 Instructions")
50+
.instructions(INSTRUCTIONS)
51+
.columns(35)
52+
.testUI(bug4187996::createTestUI)
53+
.build()
54+
.awaitAndCheck();
55+
}
56+
57+
private static JFrame createTestUI() {
58+
JFrame frame = new JFrame("bug4187996");
59+
JMenu submenu = new JMenu("Submenu");
60+
submenu.add(new JMenuItem("Sub 1"));
61+
submenu.add(new JMenuItem("Sub 2"));
62+
63+
JMenu menu = new JMenu("Menu");
64+
menu.add(submenu);
65+
menu.add(new JMenuItem("Item 1"));
66+
menu.add(new JMenuItem("Item 2"));
67+
68+
JMenuBar mbar = new JMenuBar();
69+
mbar.add(menu);
70+
frame.setJMenuBar(mbar);
71+
frame.setSize(300, 100);
72+
return frame;
73+
}
74+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Copyright (c) 2011, 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 6471949
27+
* @summary JMenu should stay selected after escape is pressed
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
30+
* @run main/manual bug6471949
31+
*/
32+
33+
import java.awt.event.ActionListener;
34+
import java.awt.event.ActionEvent;
35+
36+
import javax.swing.JFrame;
37+
import javax.swing.JMenu;
38+
import javax.swing.JMenuBar;
39+
import javax.swing.JMenuItem;
40+
import javax.swing.JTextArea;
41+
import javax.swing.SwingUtilities;
42+
import javax.swing.UIManager;
43+
44+
public class bug6471949 {
45+
46+
private static final String INSTRUCTIONS = """
47+
Test the menu and its submenus for different LaF:
48+
49+
Click on "Menu" and then click on "Inner" submenu
50+
and then click on "One more" submenu.
51+
52+
For Metal, Nimbus and Aqua Laf the Escape key hides the last open submenu,
53+
Press Esc till the last menu "Inner" is closed.
54+
If the last menu is closed then the menu button (in menubar) gets unselected.
55+
56+
For Windows Laf the Escape key hides the last open submenu
57+
if the last menu is closed then the menu button remains selected,
58+
until the Escape key is pressed again or any other key letter pressed.
59+
60+
For GTK and Motif menu, all open submenus must hide when the Escape key is pressed.
61+
62+
If everything works as described, the test passes and fails otherwise.""";
63+
64+
public static void main(String[] args) throws Exception {
65+
PassFailJFrame.builder()
66+
.title("bug6471949 Instructions")
67+
.instructions(INSTRUCTIONS)
68+
.columns(35)
69+
.testUI(bug6471949::createTestUI)
70+
.logArea()
71+
.build()
72+
.awaitAndCheck();
73+
}
74+
75+
private static JFrame createTestUI() {
76+
77+
PassFailJFrame.log("Menu.cancelMode = " +
78+
UIManager.getString("Menu.cancelMode"));
79+
PassFailJFrame.log("Menu.preserveTopLevelSelection = " +
80+
UIManager.getBoolean("Menu.preserveTopLevelSelection"));
81+
PassFailJFrame.log("");
82+
83+
JFrame frame = new JFrame("bug6471949");
84+
JMenuBar bar = new JMenuBar();
85+
JMenu menu = new JMenu("Menu");
86+
menu.setMnemonic('m');
87+
88+
JMenuItem item = new JMenuItem("Item");
89+
menu.add(item);
90+
JMenu inner = new JMenu("Inner");
91+
inner.add(new JMenuItem("Test"));
92+
JMenu oneMore = new JMenu("One more");
93+
oneMore.add(new JMenuItem("Lala"));
94+
inner.add(oneMore);
95+
menu.add(inner);
96+
97+
JMenu lafMenu = new JMenu("Change LaF");
98+
99+
UIManager.LookAndFeelInfo[] lafs = UIManager.getInstalledLookAndFeels();
100+
for (final UIManager.LookAndFeelInfo lafInfo : lafs) {
101+
JMenuItem lafItem = new JMenuItem(lafInfo.getName());
102+
lafItem.addActionListener(new ActionListener() {
103+
public void actionPerformed(ActionEvent e) {
104+
setLaf(frame, lafInfo.getClassName());
105+
}
106+
});
107+
lafMenu.add(lafItem);
108+
}
109+
110+
frame.setJMenuBar(bar);
111+
bar.add(menu);
112+
bar.add(lafMenu);
113+
114+
JTextArea field = new JTextArea();
115+
frame.add(field);
116+
field.requestFocusInWindow();
117+
frame.pack();
118+
return frame;
119+
}
120+
121+
private static void setLaf(JFrame frame, String laf) {
122+
try {
123+
UIManager.setLookAndFeel(laf);
124+
SwingUtilities.updateComponentTreeUI(frame);
125+
PassFailJFrame.log("Menu.cancelMode = " +
126+
UIManager.getString("Menu.cancelMode"));
127+
PassFailJFrame.log("Menu.preserveTopLevelSelection = " +
128+
UIManager.getBoolean("Menu.preserveTopLevelSelection"));
129+
PassFailJFrame.log("");
130+
} catch (Exception e) {
131+
throw new RuntimeException(e);
132+
}
133+
}
134+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
* Copyright (c) 2011, 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 6513492
27+
* @summary Escape key needs to be pressed twice to remove focus from an empty/diabled Menu.
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
30+
* @run main/manual bug6513492
31+
*/
32+
33+
import java.awt.event.ActionEvent;
34+
import java.awt.event.ActionListener;
35+
36+
import javax.swing.JFrame;
37+
import javax.swing.JMenu;
38+
import javax.swing.JMenuBar;
39+
import javax.swing.JMenuItem;
40+
import javax.swing.JTextArea;
41+
import javax.swing.SwingUtilities;
42+
import javax.swing.UIManager;
43+
44+
public class bug6513492 {
45+
46+
private static final String INSTRUCTIONS = """
47+
Test Menu for different LaF:
48+
49+
* For Windows Laf:
50+
Click the editor
51+
Click EmpyMenu, press Escape -> focus must go to the editor
52+
Click EmpyMenu, press right arrow button, press Escape -> focus must go to the editor
53+
Click SubMenuTest, highlight the first disabled submenu, press Escape
54+
-> focus must stay at the topLevelMenu
55+
56+
* For Metal, Nimbus and Aqua Laf
57+
Click the editor
58+
Click SubMenuTest, highlight the EmptySubmenu, press Escape -> focus must go to the editor
59+
Click SubMenuTest, highlight the EnabledItem, press Escape -> focus must go to the editor
60+
61+
* For GTK and Motif
62+
Click the editor
63+
Open any menu or submenu, press Escape -> focus must go to the editor.""";
64+
65+
public static void main(String[] args) throws Exception {
66+
PassFailJFrame.builder()
67+
.title("bug6513492 Instructions")
68+
.instructions(INSTRUCTIONS)
69+
.columns(35)
70+
.testUI(bug6513492::createTestUI)
71+
.logArea()
72+
.build()
73+
.awaitAndCheck();
74+
}
75+
76+
private static JFrame createTestUI() {
77+
PassFailJFrame.log("Menu.cancelMode = " +
78+
UIManager.getString("Menu.cancelMode"));
79+
PassFailJFrame.log("Menu.preserveTopLevelSelection = " +
80+
UIManager.getBoolean("Menu.preserveTopLevelSelection"));
81+
PassFailJFrame.log("");
82+
83+
JFrame frame = new JFrame("bug6513492");
84+
JMenuBar bar = new JMenuBar();
85+
bar.add(new JMenu("EmptyMenu"));
86+
87+
JMenu disabledMenu = new JMenu("NotEmpyButDisabled");
88+
disabledMenu.add(new JMenuItem("item"));
89+
disabledMenu.setEnabled(false);
90+
bar.add(disabledMenu);
91+
92+
JMenu menu = new JMenu("SubMenuTest");
93+
JMenu disabledSubmenu = new JMenu("Submenu");
94+
disabledSubmenu.add(new JMenuItem("item"));
95+
disabledSubmenu.setEnabled(false);
96+
menu.add(disabledSubmenu);
97+
98+
JMenu enabledSubmenu = new JMenu("Submenu");
99+
enabledSubmenu.add(new JMenuItem("item"));
100+
menu.add(enabledSubmenu);
101+
102+
JMenu emptySubmenu = new JMenu("EmptySubmenu");
103+
menu.add(emptySubmenu);
104+
105+
menu.add(new JMenuItem("EnabledItem"));
106+
JMenuItem item = new JMenuItem("DisabledItem");
107+
item.setEnabled(false);
108+
menu.add(item);
109+
bar.add(menu);
110+
111+
JMenu lafMenu = new JMenu("Change LaF");
112+
113+
UIManager.LookAndFeelInfo[] lafs = UIManager.getInstalledLookAndFeels();
114+
for (final UIManager.LookAndFeelInfo lafInfo : lafs) {
115+
JMenuItem lafItem = new JMenuItem(lafInfo.getName());
116+
lafItem.addActionListener(new ActionListener() {
117+
public void actionPerformed(ActionEvent e) {
118+
setLaf(frame, lafInfo.getClassName());
119+
}
120+
});
121+
lafMenu.add(lafItem);
122+
}
123+
124+
frame.setJMenuBar(bar);
125+
bar.add(menu);
126+
bar.add(lafMenu);
127+
128+
JTextArea field = new JTextArea("The editor");
129+
frame.add(field);
130+
field.requestFocusInWindow();
131+
frame.pack();
132+
return frame;
133+
}
134+
135+
private static void setLaf(JFrame frame, String laf) {
136+
try {
137+
UIManager.setLookAndFeel(laf);
138+
SwingUtilities.updateComponentTreeUI(frame);
139+
PassFailJFrame.log("Menu.cancelMode = " +
140+
UIManager.getString("Menu.cancelMode"));
141+
PassFailJFrame.log("Menu.preserveTopLevelSelection = " +
142+
UIManager.getBoolean("Menu.preserveTopLevelSelection"));
143+
PassFailJFrame.log("");
144+
} catch (Exception e) {
145+
throw new RuntimeException(e);
146+
}
147+
}
148+
}

0 commit comments

Comments
 (0)