8354214: Open source Swing tests Batch 2

Reviewed-by: abhiscxk, honkar
This commit is contained in:
Jayathirth D V 2025-04-16 04:48:25 +00:00
parent 55afcb57a5
commit 2be5bc847a
3 changed files with 340 additions and 0 deletions

View File

@ -0,0 +1,117 @@
/*
* Copyright (c) 2001, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4193267
* @summary Tests that JList first and last visible indices are
* updated properly
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual bug4193267
*/
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class bug4193267 {
public static void main(String[] args) throws Exception {
String INSTRUCTIONS = """
Resize the frame "JList" with a different ways and scroll the list
(if it possible). The indices of first and last visible elements
should be indicated in the corresponding fields in "Index" frame.
If the indicated indices is not right then test fails.
Note:
- the first and last visible indices should be -1 if nothing
is visible;
- the first or last visible cells may only be partially visible.
""";
PassFailJFrame.builder()
.title("bug4193267 Instructions")
.instructions(INSTRUCTIONS)
.positionTestUI(WindowLayouts::rightOneRow)
.columns(35)
.testUI(bug4193267::initialize)
.build()
.awaitAndCheck();
}
private static List initialize() {
String[] data = {"000000000000000", "111111111111111",
"222222222222222", "333333333333333",
"444444444444444", "555555555555555",
"666666666666666", "777777777777777",
"888888888888888", "999999999999999"};
JFrame[] fr = new JFrame[2];
fr[0] = new JFrame("JList");
JList lst = new JList(data);
lst.setLayoutOrientation(JList.VERTICAL_WRAP);
lst.setVisibleRowCount(4);
JScrollPane jsp = new JScrollPane(lst);
fr[0].add(jsp);
fr[0].setSize(400, 200);
JPanel pL = new JPanel();
pL.setLayout(new GridLayout(2, 1));
pL.add(new JLabel("First Visible Index"));
pL.add(new JLabel("Last Visible Index"));
JPanel p = new JPanel();
p.setLayout(new GridLayout(2, 1));
JTextField first = new JTextField("0", 2);
first.setEditable(false);
first.setBackground(Color.white);
p.add(first);
JTextField last = new JTextField("9", 2);
last.setEditable(false);
last.setBackground(Color.white);
p.add(last);
fr[1] = new JFrame("Index");
fr[1].setSize(200, 200);
fr[1].setLayout(new FlowLayout());
fr[1].add(pL);
fr[1].add(p);
jsp.getViewport().addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
first.setText(String.valueOf(lst.getFirstVisibleIndex()));
last.setText(String.valueOf(lst.getLastVisibleIndex()));
}
});
List frameList = List.of(fr[0], fr[1]);
return frameList;
}
}

View File

@ -0,0 +1,96 @@
/*
* Copyright (c) 2001, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4249161
* @summary Tests that JList.setComponentOrientation() works correctly
* @library /java/awt/regtesthelpers
* @build PassFailJFrame
* @run main/manual bug4249161
*/
import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
public class bug4249161 {
public static void main(String[] args) throws Exception {
String INSTRUCTIONS = """
1. With a scroll bar, confirm that all words ("one" - "twenty") are
aligned at the left side of a list.
2. Press "Change!" button. All words on the list should be moved
to the right side.
3. Press the same button again. All words should be moved to the
left side.
If all items in a list are moved as soon as "Change!" button is
pressed, test passes.
""";
PassFailJFrame.builder()
.title("bug4249161 Instructions")
.instructions(INSTRUCTIONS)
.columns(35)
.testUI(bug4249161::initialize)
.build()
.awaitAndCheck();
}
private static JFrame initialize() {
JFrame fr = new JFrame("bug4249161");
String[] data = {"one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen", "twenty"
};
final JList list = new JList(data);
list.setSize(200, 200);
list.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
JScrollPane pane = new JScrollPane(list);
fr.add(pane);
JButton button = new JButton("Change!");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (list.getComponentOrientation() !=
ComponentOrientation.RIGHT_TO_LEFT) {
list.setComponentOrientation
(ComponentOrientation.RIGHT_TO_LEFT);
} else {
list.setComponentOrientation
(ComponentOrientation.LEFT_TO_RIGHT);
}
}
});
fr.add(button, BorderLayout.SOUTH);
fr.setSize(200, 300);
fr.setAlwaysOnTop(true);
return fr;
}
}

View File

@ -0,0 +1,127 @@
/*
* Copyright (c) 1999, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 4618767
* @summary First letter navigation in JList interferes with mnemonics
* @key headful
* @run main bug4618767
*/
import java.awt.Robot;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
public class bug4618767 {
private static JFrame f;
private static final JList list = new
JList(new String[] {"one", "two", "three", "four"});
private static boolean menuSelected;
private static volatile boolean failed;
private static CountDownLatch listGainedFocusLatch = new CountDownLatch(1);
public static void main(String[] args) throws Exception {
try {
createUI();
runTest();
} finally {
SwingUtilities.invokeAndWait(() -> {
if (f != null) {
f.dispose();
}
});
}
}
private static void createUI() throws Exception {
SwingUtilities.invokeAndWait(() -> {
f = new JFrame("bug4618767");
JMenu menu = new JMenu("File");
menu.setMnemonic('F');
JMenuItem menuItem = new JMenuItem("item");
menu.add(menuItem);
JMenuBar menuBar = new JMenuBar();
menuBar.add(menu);
f.setJMenuBar(menuBar);
menu.addMenuListener(new MenuListener() {
public void menuCanceled(MenuEvent e) {}
public void menuDeselected(MenuEvent e) {}
public void menuSelected(MenuEvent e) {
menuSelected = true;
}
});
list.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
listGainedFocusLatch.countDown();
}
});
f.add(list);
f.pack();
f.setLocationRelativeTo(null);
f.setAlwaysOnTop(true);
f.setVisible(true);
});
}
private static void runTest() throws Exception {
if (!listGainedFocusLatch.await(3, TimeUnit.SECONDS)) {
throw new RuntimeException("Waited too long, but can't gain" +
" focus for list");
}
Robot robot = new Robot();
robot.setAutoDelay(200);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_O);
robot.keyRelease(KeyEvent.VK_O);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_F);
robot.keyRelease(KeyEvent.VK_F);
robot.keyRelease(KeyEvent.VK_ALT);
SwingUtilities.invokeAndWait(() -> {
if (menuSelected && list.getSelectedIndex()!= 0) {
failed = true;
}
});
if (failed) {
throw new RuntimeException("Mnemonics interferes with Jlist" +
" item selection using KeyEvent");
}
}
}