Skip to content

Commit 9a5daf1

Browse files
committed
8352676: Opensource JMenu tests - series1
Backport-of: c002b97ee99c1889aa89e0a8853beafaf0969e9c
1 parent a696fc0 commit 9a5daf1

File tree

3 files changed

+297
-0
lines changed

3 files changed

+297
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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 4140643
27+
* @summary Tests that Motif menus open with both Alt-key and Meta-key
28+
* @key headful
29+
* @run main bug4140643
30+
*/
31+
32+
import java.awt.Robot;
33+
import java.awt.event.KeyEvent;
34+
import javax.swing.JButton;
35+
import javax.swing.JFrame;
36+
import javax.swing.JMenu;
37+
import javax.swing.JMenuBar;
38+
import javax.swing.JMenuItem;
39+
import javax.swing.SwingUtilities;
40+
import javax.swing.UIManager;
41+
import javax.swing.UnsupportedLookAndFeelException;
42+
43+
public class bug4140643 {
44+
private static JFrame frame;
45+
private static JMenu menu;
46+
private static volatile boolean isPopMenuVisible;
47+
48+
public static void main(String[] args) throws Exception {
49+
Robot robot = new Robot();
50+
try {
51+
SwingUtilities.invokeAndWait(() -> {
52+
try {
53+
UIManager.setLookAndFeel(
54+
"com.sun.java.swing.plaf.motif.MotifLookAndFeel");
55+
} catch (ClassNotFoundException | InstantiationException
56+
| UnsupportedLookAndFeelException
57+
| IllegalAccessException e) {
58+
throw new RuntimeException(e);
59+
}
60+
frame = new JFrame("bug4140643");
61+
62+
menu = new JMenu("File");
63+
menu.setMnemonic(KeyEvent.VK_F);
64+
menu.add(new JMenuItem("Open..."));
65+
menu.add(new JMenuItem("Save"));
66+
67+
JMenuBar mbar = new JMenuBar();
68+
mbar.add(menu);
69+
frame.setJMenuBar(mbar);
70+
71+
frame.add(new JButton("Click Here"));
72+
frame.pack();
73+
frame.setLocationRelativeTo(null);
74+
frame.setVisible(true);
75+
});
76+
robot.waitForIdle();
77+
robot.delay(1000);
78+
if (System.getProperty("os.name").contains("OS X")) {
79+
robot.keyPress(KeyEvent.VK_CONTROL);
80+
robot.keyPress(KeyEvent.VK_ALT);
81+
robot.keyPress(KeyEvent.VK_F);
82+
robot.keyRelease(KeyEvent.VK_F);
83+
robot.keyRelease(KeyEvent.VK_ALT);
84+
robot.keyRelease(KeyEvent.VK_CONTROL);
85+
} else {
86+
robot.keyPress(KeyEvent.VK_ALT);
87+
robot.keyPress(KeyEvent.VK_F);
88+
robot.keyRelease(KeyEvent.VK_F);
89+
robot.keyRelease(KeyEvent.VK_ALT);
90+
}
91+
robot.waitForIdle();
92+
robot.delay(1000);
93+
SwingUtilities.invokeAndWait(() -> {
94+
isPopMenuVisible = menu.isPopupMenuVisible();
95+
});
96+
if (!isPopMenuVisible) {
97+
throw new RuntimeException("Menu popup is not shown");
98+
}
99+
} finally {
100+
SwingUtilities.invokeAndWait(() -> {
101+
if (frame != null) {
102+
frame.dispose();
103+
}
104+
});
105+
}
106+
}
107+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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 4146588
27+
* @summary JMenu.setMenuLocation has no effect
28+
* @key headful
29+
* @run main bug4146588
30+
*/
31+
32+
import java.awt.Point;
33+
import java.awt.Rectangle;
34+
import java.awt.Robot;
35+
import javax.swing.JFrame;
36+
import javax.swing.JMenu;
37+
import javax.swing.JMenuBar;
38+
import javax.swing.SwingUtilities;
39+
40+
public class bug4146588 {
41+
42+
private static JFrame fr;
43+
private static JMenu menu;
44+
private static volatile Point loc;
45+
private static volatile Point popupLoc;
46+
private static volatile Point menuLoc;
47+
private static volatile Rectangle frameBounds;
48+
private static volatile Rectangle popupBounds;
49+
50+
public static void main(String[] args) throws Exception {
51+
Robot robot = new Robot();
52+
try {
53+
SwingUtilities.invokeAndWait(() -> {
54+
fr = new JFrame("bug4146588 frame");
55+
56+
JMenuBar menubar = new JMenuBar();
57+
menu = new JMenu("Menu");
58+
menu.add("Item 1");
59+
menu.add("Item 2");
60+
menu.add("Item 3");
61+
menu.setMenuLocation(150, 150);
62+
menubar.add(menu);
63+
fr.setJMenuBar(menubar);
64+
65+
fr.setSize(400, 400);
66+
fr.setLocationRelativeTo(null);
67+
fr.setVisible(true);
68+
});
69+
robot.waitForIdle();
70+
robot.delay(1000);
71+
SwingUtilities.invokeAndWait(() -> {
72+
menu.doClick(0);
73+
});
74+
robot.waitForIdle();
75+
robot.delay(200);
76+
SwingUtilities.invokeAndWait(() -> {
77+
popupLoc = menu.getPopupMenu().getLocationOnScreen();
78+
menuLoc = menu.getLocationOnScreen();
79+
frameBounds = fr.getBounds();
80+
popupBounds = menu.getPopupMenu().getBounds();
81+
});
82+
System.out.println(popupLoc);
83+
System.out.println(popupBounds);
84+
System.out.println(frameBounds);
85+
System.out.println(menuLoc);
86+
if (!(popupLoc.getX()
87+
> ((frameBounds.getX() + frameBounds.getWidth() / 2) - popupBounds.getWidth()))
88+
&& (popupLoc.getY()
89+
> ((frameBounds.getY() + frameBounds.getHeight() / 2) - popupBounds.getHeight()))) {
90+
throw new RuntimeException("popup is not at center of frame");
91+
}
92+
} finally {
93+
SwingUtilities.invokeAndWait(() -> {
94+
if (fr != null) {
95+
fr.dispose();
96+
}
97+
});
98+
}
99+
}
100+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 4342646
27+
* @summary Tests that very long menus are properly placed on the screen.
28+
* @key headful
29+
* @run main bug4342646
30+
*/
31+
32+
import java.awt.Point;
33+
import java.awt.Robot;
34+
35+
import javax.swing.JFrame;
36+
import javax.swing.JMenu;
37+
import javax.swing.JMenuBar;
38+
import javax.swing.JMenuItem;
39+
import javax.swing.SwingUtilities;
40+
41+
public class bug4342646 {
42+
43+
private static JFrame frame;
44+
private static JMenu menu;
45+
private static volatile Point popupLoc;
46+
private static volatile Point menuLoc;
47+
48+
public static void main(String[] args) throws Exception {
49+
Robot robot = new Robot();
50+
try {
51+
SwingUtilities.invokeAndWait(() -> {
52+
JMenuBar mbar = new JMenuBar();
53+
menu = new JMenu("Menu");
54+
menu.add(new JMenuItem(
55+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
56+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
57+
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
58+
"AAAAAAAAAAAAAAAAAAAAAAA"));
59+
mbar.add(menu);
60+
61+
frame = new JFrame("4342646");
62+
frame.setJMenuBar(mbar);
63+
frame.setBounds(10, 10, 200, 100);
64+
frame.setVisible(true);
65+
});
66+
robot.waitForIdle();
67+
robot.delay(1000);
68+
SwingUtilities.invokeAndWait(() -> {
69+
menu.doClick();
70+
});
71+
robot.waitForIdle();
72+
robot.delay(200);
73+
SwingUtilities.invokeAndWait(() -> {
74+
popupLoc = menu.getPopupMenu().getLocationOnScreen();
75+
menuLoc = menu.getLocationOnScreen();
76+
});
77+
System.out.println(menuLoc);
78+
System.out.println(popupLoc);
79+
if (popupLoc.getX() < menuLoc.getX()) {
80+
throw new RuntimeException("PopupMenu is incorrectly placed at left of menu");
81+
}
82+
} finally {
83+
SwingUtilities.invokeAndWait(() -> {
84+
if (frame != null) {
85+
frame.dispose();
86+
}
87+
});
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)