Skip to content

Commit d1aa40f

Browse files
author
Satyen Subramaniam
committed
8354214: Open source Swing tests Batch 2
Backport-of: 2be5bc847a444f08a4ebb41b58e8a2bf4553d621
1 parent ad64fe1 commit d1aa40f

File tree

3 files changed

+340
-0
lines changed

3 files changed

+340
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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 4193267
27+
* @summary Tests that JList first and last visible indices are
28+
* updated properly
29+
* @library /java/awt/regtesthelpers
30+
* @build PassFailJFrame
31+
* @run main/manual bug4193267
32+
*/
33+
34+
import java.awt.Color;
35+
import java.awt.FlowLayout;
36+
import java.awt.GridLayout;
37+
import java.util.List;
38+
import javax.swing.JFrame;
39+
import javax.swing.JLabel;
40+
import javax.swing.JList;
41+
import javax.swing.JPanel;
42+
import javax.swing.JScrollPane;
43+
import javax.swing.JTextField;
44+
import javax.swing.event.ChangeEvent;
45+
import javax.swing.event.ChangeListener;
46+
47+
public class bug4193267 {
48+
public static void main(String[] args) throws Exception {
49+
String INSTRUCTIONS = """
50+
Resize the frame "JList" with a different ways and scroll the list
51+
(if it possible). The indices of first and last visible elements
52+
should be indicated in the corresponding fields in "Index" frame.
53+
If the indicated indices is not right then test fails.
54+
55+
Note:
56+
- the first and last visible indices should be -1 if nothing
57+
is visible;
58+
- the first or last visible cells may only be partially visible.
59+
""";
60+
PassFailJFrame.builder()
61+
.title("bug4193267 Instructions")
62+
.instructions(INSTRUCTIONS)
63+
.positionTestUI(WindowLayouts::rightOneRow)
64+
.columns(35)
65+
.testUI(bug4193267::initialize)
66+
.build()
67+
.awaitAndCheck();
68+
}
69+
70+
private static List initialize() {
71+
String[] data = {"000000000000000", "111111111111111",
72+
"222222222222222", "333333333333333",
73+
"444444444444444", "555555555555555",
74+
"666666666666666", "777777777777777",
75+
"888888888888888", "999999999999999"};
76+
77+
JFrame[] fr = new JFrame[2];
78+
fr[0] = new JFrame("JList");
79+
JList lst = new JList(data);
80+
lst.setLayoutOrientation(JList.VERTICAL_WRAP);
81+
lst.setVisibleRowCount(4);
82+
JScrollPane jsp = new JScrollPane(lst);
83+
fr[0].add(jsp);
84+
fr[0].setSize(400, 200);
85+
86+
JPanel pL = new JPanel();
87+
pL.setLayout(new GridLayout(2, 1));
88+
pL.add(new JLabel("First Visible Index"));
89+
pL.add(new JLabel("Last Visible Index"));
90+
91+
JPanel p = new JPanel();
92+
p.setLayout(new GridLayout(2, 1));
93+
JTextField first = new JTextField("0", 2);
94+
first.setEditable(false);
95+
first.setBackground(Color.white);
96+
p.add(first);
97+
JTextField last = new JTextField("9", 2);
98+
last.setEditable(false);
99+
last.setBackground(Color.white);
100+
p.add(last);
101+
102+
fr[1] = new JFrame("Index");
103+
fr[1].setSize(200, 200);
104+
fr[1].setLayout(new FlowLayout());
105+
fr[1].add(pL);
106+
fr[1].add(p);
107+
108+
jsp.getViewport().addChangeListener(new ChangeListener() {
109+
public void stateChanged(ChangeEvent e) {
110+
first.setText(String.valueOf(lst.getFirstVisibleIndex()));
111+
last.setText(String.valueOf(lst.getLastVisibleIndex()));
112+
}
113+
});
114+
List frameList = List.of(fr[0], fr[1]);
115+
return frameList;
116+
}
117+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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 4249161
27+
* @summary Tests that JList.setComponentOrientation() works correctly
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
30+
* @run main/manual bug4249161
31+
*/
32+
33+
import java.awt.BorderLayout;
34+
import java.awt.ComponentOrientation;
35+
import java.awt.event.ActionEvent;
36+
import java.awt.event.ActionListener;
37+
import javax.swing.JButton;
38+
import javax.swing.JFrame;
39+
import javax.swing.JList;
40+
import javax.swing.JScrollPane;
41+
42+
public class bug4249161 {
43+
public static void main(String[] args) throws Exception {
44+
String INSTRUCTIONS = """
45+
1. With a scroll bar, confirm that all words ("one" - "twenty") are
46+
aligned at the left side of a list.
47+
2. Press "Change!" button. All words on the list should be moved
48+
to the right side.
49+
3. Press the same button again. All words should be moved to the
50+
left side.
51+
52+
If all items in a list are moved as soon as "Change!" button is
53+
pressed, test passes.
54+
""";
55+
PassFailJFrame.builder()
56+
.title("bug4249161 Instructions")
57+
.instructions(INSTRUCTIONS)
58+
.columns(35)
59+
.testUI(bug4249161::initialize)
60+
.build()
61+
.awaitAndCheck();
62+
}
63+
64+
private static JFrame initialize() {
65+
JFrame fr = new JFrame("bug4249161");
66+
67+
String[] data = {"one", "two", "three", "four", "five",
68+
"six", "seven", "eight", "nine", "ten",
69+
"eleven", "twelve", "thirteen", "fourteen", "fifteen",
70+
"sixteen", "seventeen", "eighteen", "nineteen", "twenty"
71+
};
72+
final JList list = new JList(data);
73+
list.setSize(200, 200);
74+
list.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
75+
JScrollPane pane = new JScrollPane(list);
76+
fr.add(pane);
77+
78+
JButton button = new JButton("Change!");
79+
button.addActionListener(new ActionListener() {
80+
public void actionPerformed(ActionEvent e) {
81+
if (list.getComponentOrientation() !=
82+
ComponentOrientation.RIGHT_TO_LEFT) {
83+
list.setComponentOrientation
84+
(ComponentOrientation.RIGHT_TO_LEFT);
85+
} else {
86+
list.setComponentOrientation
87+
(ComponentOrientation.LEFT_TO_RIGHT);
88+
}
89+
}
90+
});
91+
fr.add(button, BorderLayout.SOUTH);
92+
fr.setSize(200, 300);
93+
fr.setAlwaysOnTop(true);
94+
return fr;
95+
}
96+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
/*
25+
* @test
26+
* @bug 4618767
27+
* @summary First letter navigation in JList interferes with mnemonics
28+
* @key headful
29+
* @run main bug4618767
30+
*/
31+
32+
import java.awt.Robot;
33+
import java.awt.event.FocusAdapter;
34+
import java.awt.event.FocusEvent;
35+
import java.awt.event.KeyEvent;
36+
import java.util.concurrent.CountDownLatch;
37+
import java.util.concurrent.TimeUnit;
38+
import javax.swing.JFrame;
39+
import javax.swing.JList;
40+
import javax.swing.JMenu;
41+
import javax.swing.JMenuBar;
42+
import javax.swing.JMenuItem;
43+
import javax.swing.SwingUtilities;
44+
import javax.swing.event.MenuEvent;
45+
import javax.swing.event.MenuListener;
46+
47+
public class bug4618767 {
48+
private static JFrame f;
49+
private static final JList list = new
50+
JList(new String[] {"one", "two", "three", "four"});
51+
private static boolean menuSelected;
52+
private static volatile boolean failed;
53+
private static CountDownLatch listGainedFocusLatch = new CountDownLatch(1);
54+
55+
public static void main(String[] args) throws Exception {
56+
try {
57+
createUI();
58+
runTest();
59+
} finally {
60+
SwingUtilities.invokeAndWait(() -> {
61+
if (f != null) {
62+
f.dispose();
63+
}
64+
});
65+
}
66+
}
67+
68+
private static void createUI() throws Exception {
69+
SwingUtilities.invokeAndWait(() -> {
70+
f = new JFrame("bug4618767");
71+
JMenu menu = new JMenu("File");
72+
menu.setMnemonic('F');
73+
JMenuItem menuItem = new JMenuItem("item");
74+
menu.add(menuItem);
75+
JMenuBar menuBar = new JMenuBar();
76+
menuBar.add(menu);
77+
f.setJMenuBar(menuBar);
78+
79+
menu.addMenuListener(new MenuListener() {
80+
public void menuCanceled(MenuEvent e) {}
81+
public void menuDeselected(MenuEvent e) {}
82+
public void menuSelected(MenuEvent e) {
83+
menuSelected = true;
84+
}
85+
});
86+
87+
list.addFocusListener(new FocusAdapter() {
88+
@Override
89+
public void focusGained(FocusEvent e) {
90+
listGainedFocusLatch.countDown();
91+
}
92+
});
93+
f.add(list);
94+
f.pack();
95+
f.setLocationRelativeTo(null);
96+
f.setAlwaysOnTop(true);
97+
f.setVisible(true);
98+
});
99+
}
100+
101+
private static void runTest() throws Exception {
102+
if (!listGainedFocusLatch.await(3, TimeUnit.SECONDS)) {
103+
throw new RuntimeException("Waited too long, but can't gain" +
104+
" focus for list");
105+
}
106+
Robot robot = new Robot();
107+
robot.setAutoDelay(200);
108+
robot.waitForIdle();
109+
robot.keyPress(KeyEvent.VK_O);
110+
robot.keyRelease(KeyEvent.VK_O);
111+
robot.waitForIdle();
112+
robot.keyPress(KeyEvent.VK_ALT);
113+
robot.keyPress(KeyEvent.VK_F);
114+
robot.keyRelease(KeyEvent.VK_F);
115+
robot.keyRelease(KeyEvent.VK_ALT);
116+
117+
SwingUtilities.invokeAndWait(() -> {
118+
if (menuSelected && list.getSelectedIndex()!= 0) {
119+
failed = true;
120+
}
121+
});
122+
if (failed) {
123+
throw new RuntimeException("Mnemonics interferes with Jlist" +
124+
" item selection using KeyEvent");
125+
}
126+
}
127+
}

0 commit comments

Comments
 (0)