Skip to content

Commit 555f824

Browse files
Sonia Zaldana Callesshipilev
Sonia Zaldana Calles
authored andcommitted
8315898: Open source swing JMenu tests
Backport-of: fecd2fd8f26d0e8905a519e30e9aa171683c9df1
1 parent bfaec80 commit 555f824

File tree

6 files changed

+357
-0
lines changed

6 files changed

+357
-0
lines changed
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 1999, 2023, 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 4143592
27+
* @summary Tests the method add(Component, int) of JMenu for insertion
28+
the given component to a specified position of menu
29+
* @run main bug4143592
30+
*/
31+
32+
import javax.swing.JMenu;
33+
import javax.swing.JMenuBar;
34+
import javax.swing.JMenuItem;
35+
36+
public class bug4143592 {
37+
38+
public static void main(String[] argv) {
39+
JMenuBar mb = new JMenuBar();
40+
JMenu m = mb.add(new JMenu("Order"));
41+
m.add("beginning");
42+
m.add("middle");
43+
m.add("end");
44+
m.add(new JMenuItem("in between"), 1);
45+
if (!m.getItem(1).getText().equals("in between")) {
46+
throw new RuntimeException("Item was inserted incorrectly.");
47+
}
48+
}
49+
}
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2001, 2023, 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 4148154
27+
* @summary Tests that menu items created by JMenu.add(Action) method
28+
have right HorizontalTextPosition.
29+
* @run main bug4148154
30+
*/
31+
32+
import java.awt.event.ActionEvent;
33+
import javax.swing.AbstractAction;
34+
import javax.swing.JMenu;
35+
import javax.swing.JMenuItem;
36+
37+
public class bug4148154
38+
{
39+
public static void main(String[] args) {
40+
JMenu menu = new JMenu();
41+
JMenuItem mi = menu.add(new AbstractAction() {
42+
public void actionPerformed(ActionEvent ev) {}
43+
});
44+
if (mi.getHorizontalTextPosition() != JMenu.LEADING &&
45+
mi.getHorizontalTextPosition() != JMenu.TRAILING) {
46+
47+
throw new RuntimeException("Failed:");
48+
}
49+
}
50+
}
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 1999, 2023, 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 4156316
27+
* @summary checks if JMenu.add(Component) throws NullPointerException
28+
* @run main bug4156316
29+
*/
30+
31+
import javax.swing.JComponent;
32+
import javax.swing.JMenu;
33+
34+
public class bug4156316 {
35+
36+
public static void main(String[] args) {
37+
JMenu m = new JMenu("test");
38+
m.add(new XComponent());
39+
}
40+
41+
static class XComponent extends JComponent {
42+
}
43+
}
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 1999, 2023, 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 4161866
27+
* @summary Method AccessibleJMenu.removeAccessibleSelection does not
28+
remove selections correctly
29+
* @run main bug4161866
30+
*/
31+
32+
import javax.accessibility.AccessibleSelection;
33+
import javax.swing.JMenu;
34+
import javax.swing.JMenuBar;
35+
36+
public class bug4161866 {
37+
38+
public static void main(String[] argv) {
39+
JMenuBar mb = new JMenuBar();
40+
JMenu mnu = new JMenu();
41+
AccessibleSelection acs = mnu.getAccessibleContext().
42+
getAccessibleSelection();
43+
mb.add(mnu);
44+
JMenu jm = new JMenu();
45+
mnu.add(jm);
46+
jm.setSelected(true);
47+
acs.addAccessibleSelection(0);
48+
if (!jm.isSelected()) {
49+
throw new RuntimeException("Selection should be non-empty...");
50+
}
51+
52+
acs.removeAccessibleSelection(0);
53+
if (jm.isSelected()) {
54+
throw new RuntimeException("Selection still non-empty after " +
55+
"it was removed");
56+
}
57+
}
58+
}
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 1999, 2023, 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 4244796
27+
* @summary Tests that JMenu has JMenu(Action) constructor
28+
* @run main bug4244796
29+
*/
30+
31+
import java.awt.event.ActionEvent;
32+
import java.beans.PropertyChangeListener;
33+
import javax.swing.Action;
34+
import javax.swing.JMenu;
35+
36+
public class bug4244796 {
37+
38+
/**
39+
* Auxilliary class implementing Action
40+
*/
41+
static class NullAction implements Action {
42+
public void addPropertyChangeListener(
43+
PropertyChangeListener listener) {}
44+
public void removePropertyChangeListener(
45+
PropertyChangeListener listener) {}
46+
public void putValue(String key, Object value) {}
47+
public void setEnabled(boolean b) {}
48+
public void actionPerformed(ActionEvent e) {}
49+
50+
public Object getValue(String key) { return null; }
51+
public boolean isEnabled() { return false; }
52+
}
53+
54+
public static void main(String[] argv) {
55+
Action action = new NullAction();
56+
JMenu menu = new JMenu(action);
57+
}
58+
}
+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (c) 2003, 2023, 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 4767393
27+
* @summary Disabled JMenu is selectable via mnemonic
28+
* @key headful
29+
* @run main bug4767393
30+
*/
31+
32+
import java.awt.Robot;
33+
import java.awt.event.KeyEvent;
34+
import javax.swing.JFrame;
35+
import javax.swing.JMenu;
36+
import javax.swing.JMenuBar;
37+
import javax.swing.SwingUtilities;
38+
39+
public class bug4767393 {
40+
41+
public static JFrame mainFrame;
42+
public static JMenuBar menuBar;
43+
public static JMenu menu;
44+
public static JMenu disabled;
45+
public static volatile boolean disabledMenuSelected = true;
46+
47+
public static void main(String[] args) throws Exception {
48+
try {
49+
Robot robo = new Robot();
50+
robo.setAutoDelay(100);
51+
SwingUtilities.invokeAndWait(() -> {
52+
mainFrame = new JFrame("Bug4767393");
53+
menuBar = new JMenuBar();
54+
menu = new JMenu("File");
55+
disabled = new JMenu("Disabled");
56+
menuBar.add(menu);
57+
menu.add("Menu Item 1");
58+
menu.add("Menu Item 2");
59+
disabled.setEnabled(false);
60+
disabled.setMnemonic('D');
61+
disabled.add("Dummy menu item");
62+
menu.add(disabled);
63+
menu.add("Menu Item 3");
64+
menu.add("Menu Item 4");
65+
mainFrame.setJMenuBar(menuBar);
66+
67+
mainFrame.setSize(200, 200);
68+
mainFrame.setLocationRelativeTo(null);
69+
mainFrame.setVisible(true);
70+
});
71+
robo.waitForIdle();
72+
robo.delay(500);
73+
74+
robo.keyPress(KeyEvent.VK_F10);
75+
robo.keyRelease(KeyEvent.VK_F10);
76+
robo.keyPress(KeyEvent.VK_DOWN);
77+
robo.keyRelease(KeyEvent.VK_DOWN);
78+
robo.delay(500);
79+
robo.keyPress(KeyEvent.VK_D);
80+
robo.keyRelease(KeyEvent.VK_D);
81+
robo.delay(100);
82+
83+
SwingUtilities.invokeAndWait(() -> {
84+
disabledMenuSelected = disabled.isSelected();
85+
});
86+
87+
if (disabledMenuSelected) {
88+
throw new RuntimeException("Disabled JMenu is selected" +
89+
" by the mnemonic. Test failed.");
90+
}
91+
} finally {
92+
SwingUtilities.invokeAndWait(() -> {
93+
if (mainFrame != null) {
94+
mainFrame.dispose();
95+
}
96+
});
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)