Skip to content

Commit 6a31061

Browse files
author
Tejesh R
committed
8354248: Open source several AWT GridBagLayout and List tests
Reviewed-by: abhiscxk
1 parent 1889dac commit 6a31061

File tree

4 files changed

+368
-0
lines changed

4 files changed

+368
-0
lines changed

test/jdk/ProblemList.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ java/awt/TrayIcon/RightClickWhenBalloonDisplayed/RightClickWhenBalloonDisplayed.
464464
java/awt/PopupMenu/PopupMenuLocation.java 8259913,8315878 windows-all,macosx-aarch64
465465
java/awt/GridLayout/ComponentPreferredSize/ComponentPreferredSize.java 8238720,8324782 windows-all,macosx-all
466466
java/awt/GridLayout/ChangeGridSize/ChangeGridSize.java 8238720,8324782 windows-all,macosx-all
467+
java/awt/GridBagLayout/ComponentShortage.java 8355280 windows-all,linux-all
467468
java/awt/event/MouseEvent/FrameMouseEventAbsoluteCoordsTest/FrameMouseEventAbsoluteCoordsTest.java 8238720 windows-all
468469

469470
# Several tests which fail sometimes on macos11
@@ -815,6 +816,7 @@ java/awt/PopupMenu/PopupHangTest.java 8340022 windows-all
815816
java/awt/Focus/MinimizeNonfocusableWindowTest.java 8024487 windows-all
816817
java/awt/Focus/InactiveFocusRace.java 8023263 linux-all
817818
java/awt/List/HandlingKeyEventIfMousePressedTest.java 6848358 macosx-all,windows-all
819+
java/awt/List/ListScrollbarCursorTest.java 8066410 generic-all
818820
java/awt/Checkbox/CheckboxBoxSizeTest.java 8340870 windows-all
819821
java/awt/Checkbox/CheckboxIndicatorSizeTest.java 8340870 windows-all
820822
java/awt/Checkbox/CheckboxNullLabelTest.java 8340870 windows-all
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (c) 2008, 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 4238932
27+
* @summary JTextField in gridBagLayout does not properly set MinimumSize
28+
* @key headful
29+
* @run main ComponentShortage
30+
*/
31+
32+
import java.awt.Dimension;
33+
import java.awt.EventQueue;
34+
import java.awt.GridBagConstraints;
35+
import java.awt.GridBagLayout;
36+
import java.awt.Robot;
37+
import javax.swing.JFrame;
38+
import javax.swing.JTextField;
39+
40+
public class ComponentShortage {
41+
static final int WIDTH_REDUCTION = 50;
42+
static JFrame frame;
43+
static JTextField jtf;
44+
static volatile Dimension size;
45+
static volatile Dimension fSize;
46+
47+
public static void main(String[] args) throws Exception {
48+
Robot robot = new Robot();
49+
try {
50+
EventQueue.invokeAndWait(() -> {
51+
frame = new JFrame();
52+
frame.setLayout(new GridBagLayout());
53+
GridBagConstraints gBC = new GridBagConstraints();
54+
55+
gBC.gridx = 1;
56+
gBC.gridy = 0;
57+
gBC.gridwidth = 1;
58+
gBC.gridheight = 1;
59+
gBC.weightx = 1.0;
60+
gBC.weighty = 0.0;
61+
gBC.fill = GridBagConstraints.NONE;
62+
gBC.anchor = GridBagConstraints.NORTHWEST;
63+
jtf = new JTextField(16);
64+
frame.add(jtf, gBC);
65+
frame.pack();
66+
frame.setVisible(true);
67+
});
68+
robot.waitForIdle();
69+
robot.delay(1000);
70+
71+
EventQueue.invokeAndWait(() -> {
72+
size = jtf.getSize();
73+
});
74+
System.out.println("TextField size before Frame's width reduction : " + size);
75+
76+
EventQueue.invokeAndWait(() -> {
77+
frame.setSize(frame.getSize().width - WIDTH_REDUCTION, frame.getSize().height);
78+
});
79+
frame.repaint();
80+
81+
EventQueue.invokeAndWait(() -> {
82+
size = jtf.getSize();
83+
fSize = frame.getSize();
84+
});
85+
System.out.println("TextField size after Frame's width reduction : " + size);
86+
87+
if (size.width < fSize.width - WIDTH_REDUCTION) {
88+
throw new RuntimeException("Width of JTextField is too small to be visible.");
89+
}
90+
System.out.println("Test passed.");
91+
} finally {
92+
EventQueue.invokeAndWait(() -> {
93+
if (frame != null) {
94+
frame.dispose();
95+
}
96+
});
97+
}
98+
}
99+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2000, 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 4290684
27+
* @summary Tests that cursor on the scrollbar of the list is set to default.
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
30+
* @run main/manual ListScrollbarCursorTest
31+
*/
32+
33+
import java.awt.Cursor;
34+
import java.awt.Frame;
35+
import java.awt.List;
36+
import java.awt.Panel;
37+
38+
public class ListScrollbarCursorTest {
39+
public static void main(String[] args) throws Exception {
40+
String INSTRUCTIONS = """
41+
1. You see the list in the middle of the panel.
42+
This list has two scrollbars.
43+
2. The cursor should have a shape of hand over the main area
44+
and a shape of arrow over scrollbars.
45+
3. Move the mouse cursor to either horizontal or vertical scrollbar.
46+
4. Press PASS if you see the default arrow cursor else press FAIL.
47+
""";
48+
PassFailJFrame.builder()
49+
.instructions(INSTRUCTIONS)
50+
.columns(35)
51+
.testUI(ListScrollbarCursorTest::initialize)
52+
.build()
53+
.awaitAndCheck();
54+
}
55+
56+
static Frame initialize() {
57+
Frame frame = new Frame("List Scrollbar Cursor Test");
58+
Panel panel = new Panel();
59+
List list = new List(3);
60+
list.add("List item with a very long name" +
61+
"(just to make the horizontal scrollbar visible)");
62+
list.add("Item 2");
63+
list.add("Item 3");
64+
list.setCursor(new Cursor(Cursor.HAND_CURSOR));
65+
panel.add(list);
66+
frame.add(panel);
67+
frame.setSize(200, 200);
68+
return frame;
69+
}
70+
}
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
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 4096445
27+
* @summary Test to verify List Scollbar appears/disappears automatically
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
30+
* @run main/manual ListScrollbarTest
31+
*/
32+
33+
import java.awt.Button;
34+
import java.awt.Component;
35+
import java.awt.Event;
36+
import java.awt.Frame;
37+
import java.awt.GridBagConstraints;
38+
import java.awt.GridBagLayout;
39+
import java.awt.List;
40+
41+
public class ListScrollbarTest extends Frame {
42+
static final int ITEMS = 10;
43+
List ltList;
44+
List rtList;
45+
46+
public static void main(String[] args) throws Exception {
47+
String INSTRUCTIONS = """
48+
1. There are two lists added to the Frame separated by
49+
a column of buttons
50+
2. Double click on any item(s) on the left list, you would see
51+
a '*' added at the end of the item
52+
3. Keep double clicking on the same item till the length of the
53+
item exceeds the width of the list
54+
4. Now, if you don't get the horizontal scrollbar on
55+
the left list click FAIL.
56+
5. If you get horizontal scrollbar, select the item
57+
(that you double clicked) and press the '>' button
58+
to move the item to the right list.
59+
6. If horizontal scroll bar appears on the right list
60+
as well as disappears from the left list [only if both
61+
happen] proceed with step 8 else click FAIL
62+
7. Now move the same item to the left list, by pressing
63+
'<' button
64+
8. If the horizontal scrollbar appears on the left list
65+
and disappears from the right list[only if both happen]
66+
click PASS else click FAIL.
67+
""";
68+
PassFailJFrame.builder()
69+
.instructions(INSTRUCTIONS)
70+
.columns(35)
71+
.testUI(ListScrollbarTest::new)
72+
.build()
73+
.awaitAndCheck();
74+
}
75+
76+
public ListScrollbarTest() {
77+
super("List scroll bar test");
78+
GridBagLayout gbl = new GridBagLayout();
79+
ltList = new List(ITEMS, true);
80+
rtList = new List(0, true);
81+
setLayout(gbl);
82+
add(ltList, 0, 0, 1, 5, 1.0, 1.0);
83+
add(rtList, 2, 0, 1, 5, 1.0, 1.0);
84+
add(new Button(">"), 1, 0, 1, 1, 0, 1.0);
85+
add(new Button(">>"), 1, 1, 1, 1, 0, 1.0);
86+
add(new Button("<"), 1, 2, 1, 1, 0, 1.0);
87+
add(new Button("<<"), 1, 3, 1, 1, 0, 1.0);
88+
add(new Button("!"), 1, 4, 1, 1, 0, 1.0);
89+
90+
for (int i = 0; i < ITEMS; i++) {
91+
ltList.addItem("item " + i);
92+
}
93+
setSize(220, 250);
94+
}
95+
96+
void add(Component comp, int x, int y, int w, int h, double weightx, double weighty) {
97+
GridBagLayout gbl = (GridBagLayout) getLayout();
98+
GridBagConstraints c = new GridBagConstraints();
99+
c.fill = GridBagConstraints.BOTH;
100+
c.gridx = x;
101+
c.gridy = y;
102+
c.gridwidth = w;
103+
c.gridheight = h;
104+
c.weightx = weightx;
105+
c.weighty = weighty;
106+
add(comp);
107+
gbl.setConstraints(comp, c);
108+
}
109+
110+
void reverseSelections(List l) {
111+
for (int i = 0; i < l.countItems(); i++) {
112+
if (l.isSelected(i)) {
113+
l.deselect(i);
114+
} else {
115+
l.select(i);
116+
}
117+
}
118+
}
119+
120+
void deselectAll(List l) {
121+
for (int i = 0; i < l.countItems(); i++) {
122+
l.deselect(i);
123+
}
124+
}
125+
126+
void replaceItem(List l, String item) {
127+
for (int i = 0; i < l.countItems(); i++) {
128+
if (l.getItem(i).equals(item)) {
129+
l.replaceItem(item + "*", i);
130+
}
131+
}
132+
}
133+
134+
void move(List l1, List l2, boolean all) {
135+
136+
// if all the items are to be moved
137+
if (all) {
138+
for (int i = 0; i < l1.countItems(); i++) {
139+
l2.addItem(l1.getItem(i));
140+
}
141+
l1.delItems(0, l1.countItems() - 1);
142+
} else { // else move the selected items
143+
String[] items = l1.getSelectedItems();
144+
int[] itemIndexes = l1.getSelectedIndexes();
145+
146+
deselectAll(l2);
147+
for (int i = 0; i < items.length; i++) {
148+
l2.addItem(items[i]);
149+
l2.select(l2.countItems() - 1);
150+
if (i == 0) {
151+
l2.makeVisible(l2.countItems() - 1);
152+
}
153+
}
154+
for (int i = itemIndexes.length - 1; i >= 0; i--) {
155+
l1.delItem(itemIndexes[i]);
156+
}
157+
}
158+
}
159+
160+
@Override
161+
public boolean action(Event evt, Object arg) {
162+
if (">".equals(arg)) {
163+
move(ltList, rtList, false);
164+
} else if (">>".equals(arg)) {
165+
move(ltList, rtList, true);
166+
} else if ("<".equals(arg)) {
167+
move(rtList, ltList, false);
168+
} else if ("<<".equals(arg)) {
169+
move(rtList, ltList, true);
170+
} else if ("!".equals(arg)) {
171+
if (ltList.getSelectedItems().length > 0) {
172+
reverseSelections(ltList);
173+
} else if (rtList.getSelectedItems().length > 0) {
174+
reverseSelections(rtList);
175+
}
176+
} else if (evt.target == rtList || evt.target == ltList) {
177+
replaceItem((List) evt.target, (String) arg);
178+
} else {
179+
return false;
180+
}
181+
return true;
182+
}
183+
184+
@Override
185+
public boolean handleEvent(Event evt) {
186+
if (evt.id == Event.LIST_SELECT
187+
|| evt.id == Event.LIST_DESELECT) {
188+
if (evt.target == ltList) {
189+
deselectAll(rtList);
190+
} else if (evt.target == rtList) {
191+
deselectAll(ltList);
192+
}
193+
return true;
194+
}
195+
return super.handleEvent(evt);
196+
}
197+
}

0 commit comments

Comments
 (0)