Skip to content

Commit a769f38

Browse files
author
Satyen Subramaniam
committed
8354163: Open source Swing tests Batch 1
Backport-of: b783784
1 parent d08aff5 commit a769f38

File tree

5 files changed

+668
-0
lines changed

5 files changed

+668
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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 4133768 4363569
27+
* @summary Tests how button displays its icons
28+
* @key headful
29+
* @run main bug4133768
30+
*/
31+
32+
import java.awt.Color;
33+
import java.awt.Graphics2D;
34+
import java.awt.GridLayout;
35+
import java.awt.Point;
36+
import java.awt.Robot;
37+
import java.awt.image.BufferedImage;
38+
import java.io.IOException;
39+
import javax.swing.AbstractButton;
40+
import javax.swing.Icon;
41+
import javax.swing.ImageIcon;
42+
import javax.swing.JCheckBox;
43+
import javax.swing.JFrame;
44+
import javax.swing.JPanel;
45+
import javax.swing.JRadioButton;
46+
import javax.swing.JToggleButton;
47+
import javax.swing.SwingUtilities;
48+
49+
public class bug4133768 {
50+
private static Icon RED, GREEN;
51+
private static JFrame f;
52+
private static AbstractButton[] buttons;
53+
private static volatile Point buttonLocation;
54+
private static volatile int buttonWidth;
55+
private static volatile int buttonHeight;
56+
private static Robot robot;
57+
58+
public static void main(String[] args) throws Exception {
59+
try {
60+
createTestImages();
61+
createUI();
62+
robot = new Robot();
63+
robot.delay(1000);
64+
for (AbstractButton b : buttons) {
65+
testEnabledButton(b);
66+
}
67+
for (AbstractButton b : buttons) {
68+
b.setEnabled(false);
69+
robot.delay(1000);
70+
testDisabledButton(b);
71+
}
72+
} finally {
73+
SwingUtilities.invokeAndWait(() -> {
74+
if (f != null) {
75+
f.dispose();
76+
}
77+
});
78+
}
79+
}
80+
81+
private static void createTestImages() throws IOException {
82+
int imageWidth = 32;
83+
int imageHeight = 32;
84+
BufferedImage redImg = new BufferedImage(imageWidth, imageHeight,
85+
BufferedImage.TYPE_INT_RGB);
86+
Graphics2D g = redImg.createGraphics();
87+
g.setColor(Color.RED);
88+
g.fillRect(0, 0, imageWidth, imageHeight);
89+
g.dispose();
90+
RED = new ImageIcon(redImg);
91+
BufferedImage greenImg = new BufferedImage(imageWidth, imageHeight,
92+
BufferedImage.TYPE_INT_RGB);
93+
g = greenImg.createGraphics();
94+
g.setColor(Color.GREEN);
95+
g.fillRect(0, 0, imageWidth, imageHeight);
96+
g.dispose();
97+
GREEN = new ImageIcon(greenImg);
98+
}
99+
100+
private static void createUI() throws Exception {
101+
SwingUtilities.invokeAndWait(() -> {
102+
f = new JFrame("ButtonIconsTest");
103+
buttons = new AbstractButton[] {
104+
new JToggleButton(),
105+
new JRadioButton(),
106+
new JCheckBox()
107+
};
108+
109+
JPanel buttonPanel = new JPanel();
110+
for (int i = 0; i < buttons.length; i++) {
111+
AbstractButton b = buttons[i];
112+
b.setIcon(RED);
113+
b.setSelected(true);
114+
b.setRolloverSelectedIcon(GREEN);
115+
buttonPanel.add(b);
116+
}
117+
f.setLayout(new GridLayout(2, 1));
118+
f.add(buttonPanel);
119+
f.pack();
120+
f.setLocationRelativeTo(null);
121+
f.setAlwaysOnTop(true);
122+
f.setVisible(true);
123+
});
124+
}
125+
126+
private static void testEnabledButton(AbstractButton button) throws Exception {
127+
robot.waitForIdle();
128+
SwingUtilities.invokeAndWait(() -> {
129+
buttonLocation = button.getLocationOnScreen();
130+
buttonWidth = button.getWidth();
131+
buttonHeight = button.getHeight();
132+
});
133+
robot.mouseMove(buttonLocation.x + buttonWidth / 2,
134+
buttonLocation.y + buttonHeight / 2 );
135+
robot.delay(1000);
136+
Color buttonColor = robot.getPixelColor(buttonLocation.x +
137+
buttonWidth / 2, buttonLocation.y + buttonHeight / 2);
138+
if (!buttonColor.equals(Color.GREEN)) {
139+
throw new RuntimeException("Button roll over color is : " +
140+
buttonColor + " but it should be : " + Color.GREEN);
141+
}
142+
}
143+
144+
private static void testDisabledButton(AbstractButton button) throws Exception {
145+
robot.waitForIdle();
146+
SwingUtilities.invokeAndWait(() -> {
147+
buttonLocation = button.getLocationOnScreen();
148+
buttonWidth = button.getWidth();
149+
buttonHeight = button.getHeight();
150+
});
151+
robot.mouseMove(buttonLocation.x + buttonWidth / 2,
152+
buttonLocation.y + buttonHeight / 2 );
153+
robot.delay(1000);
154+
Color buttonColor = robot.getPixelColor(buttonLocation.x +
155+
buttonWidth / 2, buttonLocation.y + buttonHeight / 2);
156+
if (buttonColor.equals(Color.GREEN) ||
157+
buttonColor.equals(Color.RED)) {
158+
throw new RuntimeException("Disabled button color should not be : "
159+
+ buttonColor);
160+
}
161+
}
162+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
import java.awt.Color;
25+
import java.awt.Graphics2D;
26+
import java.awt.Point;
27+
import java.awt.Robot;
28+
import java.awt.event.ActionEvent;
29+
import java.awt.event.InputEvent;
30+
import java.awt.image.BufferedImage;
31+
import java.io.IOException;
32+
import javax.swing.AbstractAction;
33+
import javax.swing.Action;
34+
import javax.swing.Icon;
35+
import javax.swing.ImageIcon;
36+
import javax.swing.JButton;
37+
import javax.swing.JFrame;
38+
import javax.swing.JToolBar;
39+
import javax.swing.SwingUtilities;
40+
41+
/*
42+
* @test
43+
* @bug 4391622
44+
* @summary The toolbar's button which is added as action should ignore text
45+
* @key headful
46+
* @run main bug4391622
47+
*/
48+
49+
public class bug4391622 {
50+
private static Icon RED, GREEN;
51+
private static JButton bt;
52+
private static JFrame f;
53+
private static volatile Point buttonLocation;
54+
private static volatile int buttonWidth;
55+
private static volatile int buttonHeight;
56+
57+
public static void main(String[] args) throws Exception {
58+
try {
59+
createTestImages();
60+
createUI();
61+
runTest();
62+
verifyTest();
63+
} finally {
64+
SwingUtilities.invokeAndWait(() -> {
65+
if (f != null) {
66+
f.dispose();
67+
}
68+
});
69+
}
70+
}
71+
72+
private static void createTestImages() throws IOException {
73+
int imageWidth = 32;
74+
int imageHeight = 32;
75+
BufferedImage redImg = new BufferedImage(imageWidth, imageHeight,
76+
BufferedImage.TYPE_INT_RGB);
77+
Graphics2D g = redImg.createGraphics();
78+
g.setColor(Color.RED);
79+
g.fillRect(0, 0, imageWidth, imageHeight);
80+
g.dispose();
81+
RED = new ImageIcon(redImg);
82+
BufferedImage greenImg = new BufferedImage(imageWidth, imageHeight,
83+
BufferedImage.TYPE_INT_RGB);
84+
g = greenImg.createGraphics();
85+
g.setColor(Color.GREEN);
86+
g.fillRect(0, 0, imageWidth, imageHeight);
87+
g.dispose();
88+
GREEN = new ImageIcon(greenImg);
89+
}
90+
91+
private static void createUI() throws Exception {
92+
SwingUtilities.invokeAndWait(() -> {
93+
f = new JFrame("bug4391622");
94+
Action changeIt = new ChangeIt();
95+
96+
JToolBar toolbar = new JToolBar();
97+
bt = toolbar.add(changeIt);
98+
f.add(bt);
99+
f.pack();
100+
f.setLocationRelativeTo(null);
101+
f.setVisible(true);
102+
});
103+
}
104+
105+
private static void runTest() throws Exception {
106+
Robot robot = new Robot();
107+
robot.setAutoDelay(500);
108+
robot.waitForIdle();
109+
SwingUtilities.invokeAndWait(() -> {
110+
buttonLocation = bt.getLocationOnScreen();
111+
buttonWidth = bt.getWidth();
112+
buttonHeight = bt.getHeight();
113+
});
114+
robot.mouseMove(buttonLocation.x + buttonWidth / 2,
115+
buttonLocation.y + buttonHeight / 2 );
116+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
117+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
118+
robot.waitForIdle();
119+
}
120+
121+
private static void verifyTest() {
122+
if (bt.getText() != null) {
123+
throw new RuntimeException("The toolbar's button shouldn't" +
124+
" have any text.");
125+
}
126+
}
127+
128+
public static class ChangeIt extends AbstractAction {
129+
private boolean c = true;
130+
131+
public ChangeIt() {
132+
putValue(Action.NAME, "Red");
133+
putValue(Action.SMALL_ICON, RED);
134+
}
135+
136+
public void actionPerformed(ActionEvent event) {
137+
c = !c;
138+
putValue(Action.NAME, c ? "Red" : "Green");
139+
putValue(Action.SMALL_ICON, c ? RED : GREEN);
140+
}
141+
}
142+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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 4183379
27+
* @summary JList has wrong scrolling behavior when you click in the "troth"
28+
* of a scrollbar, in a scrollpane.
29+
* @library /java/awt/regtesthelpers
30+
* @build PassFailJFrame
31+
* @run main/manual bug4183379
32+
*/
33+
34+
import javax.swing.JFrame;
35+
import javax.swing.JList;
36+
import javax.swing.JScrollPane;
37+
38+
public class bug4183379 {
39+
public static void main(String[] args) throws Exception {
40+
String INSTRUCTIONS = """
41+
Click mouse several times in the "troth" of a scrollbars
42+
in a scrollpane containing a list.
43+
The list should scrolls by one block, i.e.:
44+
45+
For vertical scrolling:
46+
- if scrolling down the last visible element should become the
47+
first completely visible element
48+
- if scrolling up, the first visible element should become the
49+
last completely visible element
50+
51+
For horizontal scrolling:
52+
- for scrolling left if the beginning of the first column is not
53+
visible it should become visible, otherwise the beginning of the
54+
previous column should become visible;
55+
- for scrolling right the next colunm after first visible column
56+
should become visible.
57+
""";
58+
PassFailJFrame.builder()
59+
.title("bug4183379 Instructions")
60+
.instructions(INSTRUCTIONS)
61+
.columns(35)
62+
.testUI(bug4183379::initialize)
63+
.build()
64+
.awaitAndCheck();
65+
}
66+
67+
private static JFrame initialize() {
68+
JFrame fr = new JFrame("bug4183379");
69+
70+
String[] data = new String[90];
71+
for (int i=0; i<90; i++) {
72+
data[i] = "item number "+i;
73+
}
74+
75+
JList lst = new JList(data);
76+
lst.setLayoutOrientation(JList.VERTICAL_WRAP);
77+
lst.setVisibleRowCount(20);
78+
79+
JScrollPane jsp = new JScrollPane(lst);
80+
fr.add(jsp);
81+
fr.setSize(210,200);
82+
fr.setAlwaysOnTop(true);
83+
return fr;
84+
}
85+
}

0 commit comments

Comments
 (0)