Skip to content

Commit 8fffc07

Browse files
author
Andrew Lu
committed
8315761: Open source few swing JList and JMenuBar tests
Backport-of: bb6b3f2486b07a6ccdeea18519453e6d9c05c2c3
1 parent f248ea8 commit 8fffc07

File tree

4 files changed

+249
-0
lines changed

4 files changed

+249
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2000, 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 4300224
27+
* @summary BasicListUI.ListDataHandler improperly updates list selection on insertion
28+
* @run main bug4300224
29+
*/
30+
31+
import javax.swing.JList;
32+
import javax.swing.DefaultListModel;
33+
34+
public class bug4300224 {
35+
36+
public static void main(String[] args) throws Exception {
37+
DefaultListModel<String> model = new DefaultListModel<>();
38+
JList<String> list = new JList<>(model);
39+
40+
model.addElement("List Item 1");
41+
model.addElement("List Item 2");
42+
model.addElement("List Item 3");
43+
model.addElement("List Item 4");
44+
list.setSelectedIndex(2);
45+
model.insertElementAt("Inserted Item", 0);
46+
if (list.getSelectedIndex() != 3) {
47+
throw new RuntimeException("Inserted element improperly updates list selection");
48+
}
49+
}
50+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 4487689
27+
* @summary JList.setSelectedValue() throws ArrayIndexOutOfBoundsException on empty list.
28+
* @run main bug4487689
29+
*/
30+
31+
import java.util.Vector;
32+
import javax.swing.JList;
33+
34+
public class bug4487689 {
35+
36+
public static void main(String[] args) throws Exception {
37+
JList<String> list = new JList<>(new Vector<String>());
38+
39+
list.setSelectedIndex(0);
40+
list.getSelectedValue();
41+
42+
int[] indices = {0,1};
43+
list.setSelectedIndices(indices);
44+
list.getSelectedValues();
45+
}
46+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 4832765
27+
* @summary JList vertical scrolling doesn't work properly.
28+
* @run main bug4832765
29+
*/
30+
31+
import java.awt.Dimension;
32+
import java.awt.Rectangle;
33+
import javax.swing.JFrame;
34+
import javax.swing.JList;
35+
import javax.swing.JScrollPane;
36+
import javax.swing.SwingConstants;
37+
import javax.swing.SwingUtilities;
38+
39+
public class bug4832765 {
40+
41+
public static void main(String[] argv) throws Exception {
42+
SwingUtilities.invokeAndWait(() -> {
43+
String[] data = {"One", "Two", "Three", "Four",
44+
"Five", "Six ", "Seven", "Eight",
45+
"Nine", "Ten", "Eleven", "Twelv"};
46+
JList<String> list = new JList<>(data);
47+
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
48+
49+
JScrollPane jsp = new JScrollPane(list);
50+
Rectangle rect = list.getCellBounds(5, 5);
51+
Dimension d = new Dimension(200, rect.height);
52+
jsp.setPreferredSize(d);
53+
jsp.setMinimumSize(d);
54+
55+
list.scrollRectToVisible(rect);
56+
57+
int unit = list.getScrollableUnitIncrement(rect,
58+
SwingConstants.VERTICAL,
59+
-1);
60+
if (unit <= 0) {
61+
throw new RuntimeException("JList scrollable unit increment" +
62+
" should be greate than 0.");
63+
}
64+
});
65+
}
66+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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 4802656
27+
* @summary Problem with keyboard navigation in JMenus JMenuItems if setVisible(false)
28+
* @key headful
29+
* @run main bug4802656
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 bug4802656 {
40+
41+
public static JFrame mainFrame;
42+
public static JMenu menu2;
43+
public static volatile boolean menu2Selected = true;
44+
45+
public static void main(String[] args) throws Exception {
46+
Robot robo = new Robot();
47+
robo.setAutoDelay(100);
48+
try {
49+
SwingUtilities.invokeAndWait(() -> {
50+
mainFrame = new JFrame("Bug4802656");
51+
JMenuBar menuBar = new JMenuBar();
52+
JMenu menu1 = new JMenu("File");
53+
menu2 = new JMenu("Hidden");
54+
JMenu menu3 = new JMenu("Help");
55+
menuBar.add(menu1);
56+
menuBar.add(menu2);
57+
menuBar.add(menu3);
58+
menu2.setVisible(false);
59+
mainFrame.setJMenuBar(menuBar);
60+
mainFrame.setSize(200, 200);
61+
mainFrame.setLocationRelativeTo(null);
62+
mainFrame.setVisible(true);
63+
});
64+
robo.waitForIdle();
65+
robo.delay(1000);
66+
robo.keyPress(KeyEvent.VK_F10);
67+
robo.keyRelease(KeyEvent.VK_F10);
68+
robo.keyPress(KeyEvent.VK_RIGHT);
69+
robo.keyRelease(KeyEvent.VK_RIGHT);
70+
robo.delay(500);
71+
72+
SwingUtilities.invokeAndWait(() -> {
73+
menu2Selected = menu2.isSelected();
74+
});
75+
76+
if (menu2Selected) {
77+
throw new RuntimeException("Test failed");
78+
}
79+
} finally {
80+
SwingUtilities.invokeAndWait(() -> {
81+
if (mainFrame != null) {
82+
mainFrame.dispose();
83+
}
84+
});
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)