Skip to content

Commit 3e3dff6

Browse files
committed
8354451: Open source some more Swing popup menu tests
Reviewed-by: jdv, kizune
1 parent e163a76 commit 3e3dff6

File tree

3 files changed

+466
-0
lines changed

3 files changed

+466
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
* @summary Test that medium weight submenus are not hidden by a heavyweight canvas.
26+
* @bug 4188832
27+
* @library /java/awt/regtesthelpers
28+
* @build PassFailJFrame
29+
* @run main/manual bug4188832
30+
*/
31+
32+
import java.awt.Color;
33+
import java.awt.Panel;
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 bug4188832 {
41+
42+
static final String INSTRUCTIONS = """
43+
Select the File menu, then select the "Save As..." submenu.
44+
If you can see the submenu items displayed, press PASS, else press FAIL
45+
""";
46+
47+
public static void main(String[] args) throws Exception {
48+
PassFailJFrame.builder()
49+
.instructions(INSTRUCTIONS)
50+
.columns(40)
51+
.testUI(bug4188832::createUI)
52+
.build()
53+
.awaitAndCheck();
54+
}
55+
56+
static JFrame createUI() {
57+
// for Medium Weight menus
58+
JPopupMenu.setDefaultLightWeightPopupEnabled(false);
59+
JFrame frame = new JFrame("bug4188832");
60+
61+
// Create the menus
62+
JMenuBar menuBar = new JMenuBar();
63+
JMenu fileMenu = new JMenu("File");
64+
menuBar.add(fileMenu);
65+
fileMenu.add(new JMenuItem("New"));
66+
fileMenu.add(new JMenuItem("Open"));
67+
fileMenu.add(new JMenuItem("Save"));
68+
JMenu sm = new JMenu("Save As...");
69+
// these guys don't show up
70+
sm.add(new JMenuItem("This"));
71+
sm.add(new JMenuItem("That"));
72+
fileMenu.add(sm);
73+
fileMenu.add(new JMenuItem("Exit"));
74+
frame.setJMenuBar(menuBar);
75+
76+
Panel field = new Panel(); // a heavyweight container
77+
field.setBackground(Color.blue);
78+
frame.add(field);
79+
frame.setSize(400, 400);
80+
return frame;
81+
}
82+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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 4212464
26+
* @library /java/awt/regtesthelpers
27+
* @build PassFailJFrame
28+
* @summary Verify popup menu borders are drawn correctly when switching L&Fs
29+
* @run main/manual bug4212464
30+
*/
31+
32+
import java.awt.BorderLayout;
33+
import java.awt.FlowLayout;
34+
import java.awt.Font;
35+
import java.awt.event.ActionEvent;
36+
import java.awt.event.ActionListener;
37+
import java.awt.event.MouseAdapter;
38+
import java.awt.event.MouseEvent;
39+
import javax.swing.JApplet;
40+
import javax.swing.JButton;
41+
import javax.swing.JFrame;
42+
import javax.swing.JLabel;
43+
import javax.swing.JPanel;
44+
import javax.swing.JPopupMenu;
45+
import javax.swing.SwingUtilities;
46+
import javax.swing.UIManager;
47+
48+
public class bug4212464 extends JFrame implements ActionListener {
49+
50+
static String strMotif = "Motif";
51+
static String motifClassName = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
52+
53+
static String strMetal = "Metal";
54+
static String metalClassName = "javax.swing.plaf.metal.MetalLookAndFeel";
55+
56+
static bug4212464 frame;
57+
static JPopupMenu popup;
58+
59+
static final String INSTRUCTIONS = """
60+
This test is to see whether popup menu borders behave properly when switching
61+
back and forth between Motif and Metal L&F. The initial L&F is Metal.
62+
63+
Pressing the mouse button on the label in the center of the test window brings
64+
up a popup menu.
65+
66+
In order to test, use the labeled buttons to switch the look and feel.
67+
Clicking a button will cause the menu to be hidden. This is OK. Just click the label again.
68+
Switch back and forth and verify that the popup menu border changes consistently
69+
and there is a title for the menu when using Motif L&F (Metal won't have a title).
70+
71+
Make sure you switch back and forth several times.
72+
If the change is consistent, press PASS otherwise press FAIL.
73+
""";
74+
75+
public static void main(String[] args) throws Exception {
76+
PassFailJFrame.builder()
77+
.instructions(INSTRUCTIONS)
78+
.columns(50)
79+
.testUI(bug4212464::createUI)
80+
.build()
81+
.awaitAndCheck();
82+
}
83+
84+
static JFrame createUI() {
85+
try {
86+
UIManager.setLookAndFeel(metalClassName); // initialize to Metal.
87+
} catch (Exception e) {
88+
throw new RuntimeException(e);
89+
}
90+
frame = new bug4212464("bug4212464");
91+
popup = new JPopupMenu("Test");
92+
popup.add("Item 1");
93+
popup.add("Item 2");
94+
popup.add("Item 3");
95+
popup.add("Item 4");
96+
97+
JPanel p = new JPanel();
98+
p.setLayout(new FlowLayout());
99+
JButton motif = (JButton)p.add(new JButton(strMotif));
100+
JButton metal = (JButton)p.add(new JButton(strMetal));
101+
motif.setActionCommand(motifClassName);
102+
metal.setActionCommand(metalClassName);
103+
motif.addActionListener(frame);
104+
metal.addActionListener(frame);
105+
frame.add(BorderLayout.NORTH, p);
106+
107+
JLabel l = new JLabel("Click any mouse button on this big label");
108+
l.setFont(new Font(Font.DIALOG, Font.PLAIN, 20));
109+
l.addMouseListener(new MouseAdapter() {
110+
public void mousePressed(MouseEvent e) {
111+
popup.show(e.getComponent(), e.getX(), e.getY());
112+
}
113+
});
114+
frame.add(BorderLayout.CENTER, l);
115+
frame.setSize(500, 400);
116+
return frame;
117+
}
118+
119+
public bug4212464(String title) {
120+
super(title);
121+
}
122+
123+
public void actionPerformed(ActionEvent e) {
124+
String str = e.getActionCommand();
125+
if (str.equals(metalClassName) || str.equals(motifClassName)) {
126+
changeLNF(str);
127+
} else {
128+
System.out.println("ActionEvent: " + str);
129+
}
130+
}
131+
132+
public void changeLNF(String str) {
133+
System.out.println("Changing LNF to " + str);
134+
try {
135+
UIManager.setLookAndFeel(str);
136+
SwingUtilities.updateComponentTreeUI(frame);
137+
SwingUtilities.updateComponentTreeUI(popup);
138+
} catch (Exception e) {
139+
throw new RuntimeException(e);
140+
}
141+
}
142+
}

0 commit comments

Comments
 (0)