Skip to content

Commit 5592894

Browse files
author
Damon Nguyen
committedOct 4, 2024
8340417: Open source some MenuBar tests - Set1
Reviewed-by: psadhukhan
1 parent bade041 commit 5592894

File tree

4 files changed

+407
-0
lines changed

4 files changed

+407
-0
lines changed
 
+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/*
2+
* Copyright (c) 2011, 2024, 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 6502052
27+
* @summary Menu cells must resize if font changes (XToolkit)
28+
* @requires os.family == "linux"
29+
* @library /java/awt/regtesthelpers
30+
* @build PassFailJFrame
31+
* @run main/manual CellsResize
32+
*/
33+
34+
import java.awt.Button;
35+
import java.awt.Font;
36+
import java.awt.Frame;
37+
import java.awt.GridLayout;
38+
import java.awt.Menu;
39+
import java.awt.MenuBar;
40+
import java.awt.MenuComponent;
41+
import java.awt.MenuItem;
42+
import java.awt.Panel;
43+
import java.awt.PopupMenu;
44+
import java.awt.Toolkit;
45+
import java.awt.event.MouseAdapter;
46+
import java.awt.event.MouseEvent;
47+
48+
public class CellsResize {
49+
private static Frame frame;
50+
private static MenuBar menuBar;
51+
private static PopupMenu popupMenu;
52+
private static Menu barSubMenu;
53+
private static Menu popupSubMenu;
54+
private static boolean fontMultiplied = false;
55+
56+
public static void main(String[] args) throws Exception {
57+
String INSTRUCTIONS = """
58+
1. Open all nested menus in menu bar.
59+
2. Click on "popup-menu" button to show popup-menus.
60+
3. Open all nested menus in popup-menu.
61+
4. Click on "big-font" button (to make all menus have a
62+
bigger font).
63+
5. Open all nested menus again (as described in 1, 2, 3).
64+
6. If all menu items use a bigger font now and their labels fit
65+
into menu-item size, press "pass", otherwise press "fail".
66+
""";
67+
68+
PassFailJFrame.builder()
69+
.title("Test Instructions")
70+
.instructions(INSTRUCTIONS)
71+
.rows((int) INSTRUCTIONS.lines().count() + 2)
72+
.columns(35)
73+
.testUI(CellsResize::createUI)
74+
.logArea(5)
75+
.build()
76+
.awaitAndCheck();
77+
}
78+
79+
public static Frame createUI () {
80+
if (!checkToolkit()) {
81+
new RuntimeException("Toolkit check failed.");
82+
}
83+
frame = new Frame("MenuBar Cell Resize Test");
84+
85+
popupMenu = new PopupMenu();
86+
popupMenu.add(createMenu(false));
87+
88+
frame.add(popupMenu);
89+
90+
menuBar = new MenuBar();
91+
menuBar.add(createMenu(true));
92+
93+
frame.setMenuBar(menuBar);
94+
95+
Button bp = new Button("popup-menu");
96+
bp.addMouseListener(new MouseAdapter() {
97+
public void mouseReleased(MouseEvent e) {
98+
popupMenu.show(e.getComponent(), e.getX(), e.getY());
99+
}
100+
});
101+
102+
Button bf = new Button("big-font");
103+
bf.addMouseListener(new MouseAdapter() {
104+
public void mouseReleased(MouseEvent e) {
105+
bigFont();
106+
}
107+
});
108+
109+
Panel panel = new Panel();
110+
panel.setLayout(new GridLayout(2, 1));
111+
panel.add(bp);
112+
panel.add(bf);
113+
114+
frame.add(panel);
115+
frame.setSize(300, 300);
116+
return frame;
117+
}
118+
119+
static boolean checkToolkit() {
120+
String toolkitName = Toolkit.getDefaultToolkit().getClass().getName();
121+
return toolkitName.equals("sun.awt.X11.XToolkit");
122+
}
123+
124+
static Menu createMenu(boolean bar) {
125+
Menu menu1 = new Menu("Menu-1");
126+
Menu menu11 = new Menu("Menu-11");
127+
menu1.add(menu11);
128+
if (bar) {
129+
barSubMenu = menu11;
130+
} else {
131+
popupSubMenu = menu11;
132+
}
133+
menu11.add(new MenuItem("MenuItem"));
134+
return menu1;
135+
}
136+
137+
static void bigFont() {
138+
if (fontMultiplied) {
139+
return;
140+
} else {
141+
fontMultiplied = true;
142+
}
143+
144+
multiplyFont(barSubMenu, 7);
145+
multiplyFont(popupSubMenu, 7);
146+
147+
// NOTE: if previous two are moved below following
148+
// two, they get their font multiplied twice.
149+
150+
multiplyFont(menuBar, 5);
151+
multiplyFont(popupMenu, 5);
152+
}
153+
154+
static void multiplyFont(MenuComponent comp, int times) {
155+
Font font = comp.getFont();
156+
float size = font.getSize() * times;
157+
comp.setFont(font.deriveFont(size));
158+
}
159+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (c) 1999, 2024, 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 4275848
27+
* @summary Tests that MenuBar is painted correctly after its submenu is removed
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
30+
* @run main/manual MenuBarRemoveMenuTest
31+
*/
32+
33+
import java.awt.Button;
34+
import java.awt.Frame;
35+
import java.awt.GridLayout;
36+
import java.awt.Menu;
37+
import java.awt.MenuBar;
38+
import java.awt.event.ActionEvent;
39+
import java.awt.event.ActionListener;
40+
41+
public class MenuBarRemoveMenuTest implements ActionListener {
42+
private static MenuBar menubar;
43+
private static Button removeButton;
44+
private static Button addButton;
45+
46+
public static void main(String[] args) throws Exception {
47+
String INSTRUCTIONS = """
48+
Press "Remove menu" button. If you see that both menus
49+
disappeared, the test failed. Otherwise try to add and remove
50+
menu several times to verify that the test passed. Every time
51+
you press "Remove menu" button only one menu should go away.
52+
""";
53+
54+
PassFailJFrame.builder()
55+
.title("Test Instructions")
56+
.instructions(INSTRUCTIONS)
57+
.rows((int) INSTRUCTIONS.lines().count() + 2)
58+
.columns(35)
59+
.testUI(MenuBarRemoveMenuTest::createUI)
60+
.build()
61+
.awaitAndCheck();
62+
}
63+
64+
private static Frame createUI() {
65+
Frame frame = new Frame();
66+
menubar = new MenuBar();
67+
removeButton = new Button("Remove menu");
68+
addButton = new Button("Add menu");
69+
removeButton.addActionListener(new MenuBarRemoveMenuTest());
70+
addButton.addActionListener(new MenuBarRemoveMenuTest());
71+
addButton.setEnabled(false);
72+
menubar.add(new Menu("menu"));
73+
menubar.add(new Menu("menu"));
74+
frame.setMenuBar(menubar);
75+
frame.setLayout(new GridLayout(1, 2));
76+
frame.add(removeButton);
77+
frame.add(addButton);
78+
frame.pack();
79+
return frame;
80+
}
81+
82+
public void actionPerformed(ActionEvent e) {
83+
if (e.getSource() == removeButton) {
84+
menubar.remove(0);
85+
removeButton.setEnabled(false);
86+
addButton.setEnabled(true);
87+
} else {
88+
menubar.add(new Menu("menu"));
89+
removeButton.setEnabled(true);
90+
addButton.setEnabled(false);
91+
}
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2004, 2024, 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 5005194
27+
* @summary Frame.remove(getMenuBar()) throws NPE if the frame doesn't
28+
* have a menu bar
29+
* @key headful
30+
* @run main MenuNPE
31+
*/
32+
33+
import java.awt.Frame;
34+
import java.awt.Menu;
35+
import java.awt.MenuBar;
36+
import java.awt.MenuItem;
37+
38+
public class MenuNPE {
39+
private static Frame frame;
40+
public static void main(String[] args) throws Exception {
41+
try {
42+
frame = new Frame("Menu NPE");
43+
MenuBar menuBar = new MenuBar();
44+
Menu menu1 = new Menu("Menu 01");
45+
MenuItem menuLabel = new MenuItem("Item 01");
46+
menu1.add(menuLabel);
47+
menuBar.add(menu1);
48+
frame.setMenuBar(menuBar);
49+
frame.setSize(200, 200);
50+
frame.setVisible(true);
51+
frame.validate();
52+
frame.remove(frame.getMenuBar());
53+
frame.remove(frame.getMenuBar());
54+
System.out.println("Test passed.");
55+
} catch (Exception e) {
56+
e.printStackTrace();
57+
} finally {
58+
if (frame != null) {
59+
frame.dispose();
60+
}
61+
}
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright (c) 1999, 2024, 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 4105881
27+
* @summary Sets the menu bar while frame window is hidden, then shows
28+
frame again
29+
* @key headful
30+
* @run main SetMBarWhenHidden
31+
*/
32+
33+
import java.awt.EventQueue;
34+
import java.awt.Frame;
35+
import java.awt.Menu;
36+
import java.awt.MenuBar;
37+
import java.awt.Rectangle;
38+
39+
// test case for 4105881: FRAME.SETSIZE() DOESN'T WORK FOR SOME SOLARIS WITH
40+
// JDK115+CASES ON
41+
public class SetMBarWhenHidden {
42+
private static Frame f;
43+
private static Rectangle startBounds;
44+
private static Rectangle endBounds;
45+
46+
public static void main(String[] args) throws Exception {
47+
try {
48+
EventQueue.invokeAndWait(() -> {
49+
f = new Frame("Set MenuBar When Hidden Test");
50+
Menu file;
51+
Menu edit;
52+
MenuBar menubar = new MenuBar();
53+
file = new Menu("File");
54+
menubar.add(file);
55+
edit = new Menu("Edit");
56+
menubar.add(edit);
57+
edit.setEnabled(false);
58+
f.setMenuBar(menubar);
59+
f.setSize(200, 200);
60+
startBounds = f.getBounds();
61+
System.out.println("About to call setVisible(false)");
62+
f.setVisible(false);
63+
System.out.println("About to call setSize(500, 500)");
64+
f.setSize(500, 500);
65+
// create a new menubar and add
66+
MenuBar menubar1 = new MenuBar();
67+
menubar1.add(file);
68+
menubar1.add(edit);
69+
System.out.println("About to call setMenuBar");
70+
f.setMenuBar(menubar1);
71+
System.out.println("About to call setVisible(true)");
72+
f.setVisible(true);
73+
endBounds = f.getBounds();
74+
});
75+
if (startBounds.getHeight() > endBounds.getHeight() &&
76+
startBounds.getWidth() > endBounds.getWidth()) {
77+
throw new RuntimeException("Test failed. Frame size didn't " +
78+
"change.\nStart: " + startBounds + "\n" +
79+
"End: " + endBounds);
80+
} else {
81+
System.out.println("Test passed.\nStart: " + startBounds +
82+
"\nEnd: " + endBounds);
83+
}
84+
} finally {
85+
EventQueue.invokeAndWait(() -> {
86+
if (f != null) {
87+
f.dispose();
88+
}
89+
});
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)
Please sign in to comment.