Skip to content

Commit ec6528e

Browse files
author
Satyen Subramaniam
committed
8353011: Open source Swing JButton tests - Set 1
Backport-of: f7155183d7f7c6fcea2090f906de69e02973a6d9
1 parent a8d5a40 commit ec6528e

File tree

5 files changed

+454
-0
lines changed

5 files changed

+454
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 4151763
27+
* @summary Tests that button icon is not drawn upon button border
28+
* @library /java/awt/regtesthelpers /test/lib
29+
* @build PassFailJFrame jtreg.SkippedException
30+
* @run main/manual bug4151763
31+
*/
32+
33+
import java.awt.Color;
34+
import java.awt.Dimension;
35+
import java.awt.FlowLayout;
36+
import java.awt.Graphics2D;
37+
import java.awt.image.BufferedImage;
38+
import javax.swing.ImageIcon;
39+
import javax.swing.JButton;
40+
import javax.swing.JFrame;
41+
import javax.swing.UIManager;
42+
import javax.swing.border.CompoundBorder;
43+
import javax.swing.border.EmptyBorder;
44+
import javax.swing.border.LineBorder;
45+
import jtreg.SkippedException;
46+
47+
public class bug4151763 {
48+
private static final int IMAGE_SIZE = 150;
49+
private static final String INSTRUCTIONS = """
50+
Verify that image icon is NOT painted outside
51+
the black rectangle.
52+
53+
If above is true press PASS else FAIL.
54+
""";
55+
56+
public static void main(String[] args) throws Exception {
57+
PassFailJFrame.builder()
58+
.instructions(INSTRUCTIONS)
59+
.columns(40)
60+
.testUI(bug4151763::createAndShowUI)
61+
.build()
62+
.awaitAndCheck();
63+
}
64+
65+
private static JFrame createAndShowUI() {
66+
try {
67+
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
68+
} catch (Exception e) {
69+
throw new SkippedException("Unsupported LaF", e);
70+
}
71+
72+
JFrame frame = new JFrame("bug4151763");
73+
final JButton b = new JButton(createImageIcon());
74+
b.setBorder(new CompoundBorder(
75+
new EmptyBorder(20, 20, 20, 20),
76+
new LineBorder(Color.BLACK)));
77+
b.setPreferredSize(new Dimension(100, 100));
78+
79+
frame.setLayout(new FlowLayout());
80+
frame.add(b);
81+
frame.setSize(400, 300);
82+
return frame;
83+
}
84+
85+
private static ImageIcon createImageIcon() {
86+
BufferedImage redImg = new BufferedImage(IMAGE_SIZE, IMAGE_SIZE,
87+
BufferedImage.TYPE_INT_RGB);
88+
Graphics2D g = redImg.createGraphics();
89+
g.setColor(Color.RED);
90+
g.fillRect(0, 0, IMAGE_SIZE, IMAGE_SIZE);
91+
g.dispose();
92+
return new ImageIcon(redImg);
93+
}
94+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 4415505
27+
* @requires (os.family == "windows")
28+
* @summary Tests JButton appearance under Windows LAF
29+
* @library /java/awt/regtesthelpers
30+
* @build PassFailJFrame
31+
* @run main/manual bug4415505
32+
*/
33+
34+
import java.awt.FlowLayout;
35+
import javax.swing.JButton;
36+
import javax.swing.JFrame;
37+
import javax.swing.JToggleButton;
38+
import javax.swing.UIManager;
39+
40+
public class bug4415505 {
41+
private static final String INSTRUCTIONS = """
42+
<html>
43+
<p>This test is for Windows LaF.
44+
Press the button named "Button" using mouse and check that it has
45+
"pressed" look. It should look like the selected toggle button
46+
near it.</p>
47+
48+
<p>If above is true press PASS else FAIL.</p>
49+
<html>
50+
""";
51+
52+
public static void main(String[] args) throws Exception {
53+
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
54+
55+
PassFailJFrame.builder()
56+
.instructions(INSTRUCTIONS)
57+
.rows(5)
58+
.columns(40)
59+
.testUI(bug4415505::createAndShowUI)
60+
.build()
61+
.awaitAndCheck();
62+
}
63+
64+
private static JFrame createAndShowUI() {
65+
JButton button = new JButton("Button");
66+
button.setFocusPainted(false);
67+
JToggleButton tbutton = new JToggleButton("ToggleButton");
68+
tbutton.setSelected(true);
69+
70+
JFrame jFrame = new JFrame("bug4415505");
71+
jFrame.setLayout(new FlowLayout(FlowLayout.CENTER));
72+
jFrame.add(button);
73+
jFrame.add(tbutton);
74+
jFrame.setSize(300, 100);
75+
return jFrame;
76+
}
77+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright (c) 2004, 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 4978274
27+
* @summary Tests that JButton is painted with same visible height
28+
* as toggle buttons
29+
* @library /java/awt/regtesthelpers
30+
* @build PassFailJFrame
31+
* @run main/manual bug4978274
32+
*/
33+
34+
import java.awt.BorderLayout;
35+
import java.awt.FlowLayout;
36+
import java.awt.event.MouseAdapter;
37+
import java.awt.event.MouseEvent;
38+
import javax.swing.JButton;
39+
import javax.swing.JFrame;
40+
import javax.swing.JPanel;
41+
import javax.swing.JToggleButton;
42+
import javax.swing.UIManager;
43+
import javax.swing.border.EmptyBorder;
44+
import javax.swing.plaf.metal.MetalLookAndFeel;
45+
import javax.swing.plaf.metal.OceanTheme;
46+
47+
public class bug4978274 {
48+
private static final String INSTRUCTIONS = """
49+
The toggle buttons must be painted to the same visible
50+
height as button. In addition to that verify the following:
51+
52+
a) All three buttons - "Button", "Toggle Btn" and
53+
"Selected Toggle Btn" have the same border.
54+
55+
b) Verify that when "Button" is pressed and moused over
56+
it has the EXACT same border as "Toggle Btn" and
57+
"Selected Toggle Btn" on press & mouse over.
58+
59+
c) Click to the test window (panel) to disable/enable all
60+
three buttons. In disabled state verify that all three
61+
buttons have the exact same border.
62+
63+
If all of the above conditions are true press PASS, else FAIL.
64+
""";
65+
66+
public static void main(String[] args) throws Exception {
67+
MetalLookAndFeel.setCurrentTheme(new OceanTheme());
68+
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
69+
70+
PassFailJFrame.builder()
71+
.instructions(INSTRUCTIONS)
72+
.columns(40)
73+
.testUI(createAndShowUI())
74+
.build()
75+
.awaitAndCheck();
76+
}
77+
78+
private static JFrame createAndShowUI() {
79+
JFrame frame = new JFrame("bug4978274");
80+
frame.setLayout(new BorderLayout());
81+
82+
JPanel panel = new JPanel();
83+
panel.setLayout(new FlowLayout());
84+
panel.setBorder(new EmptyBorder(12, 12, 12, 12));
85+
JButton jButton = new JButton("Button");
86+
JToggleButton jToggleButton = new JToggleButton("Selected Toggle Btn");
87+
jToggleButton.setSelected(true);
88+
89+
panel.add(jButton);
90+
panel.add(new JToggleButton("Toggle Btn"));
91+
panel.add(jToggleButton);
92+
93+
panel.addMouseListener(new MouseAdapter() {
94+
@Override
95+
public void mouseClicked(MouseEvent event) {
96+
jButton.setEnabled(!jButton.isEnabled());
97+
jToggleButton.setEnabled(jButton.isEnabled());
98+
for(int i = 0; i < panel.getComponentCount(); i++) {
99+
panel.getComponent(i).setEnabled(jButton.isEnabled());
100+
}
101+
}
102+
});
103+
104+
frame.add(panel);
105+
frame.pack();
106+
return frame;
107+
}
108+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) 2004, 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 4673850
27+
* @summary Tests that JRadioButton and JCheckBox checkmarks are painted entirely
28+
* inside circular/rectangle checkboxes for Motif LaF.
29+
* @library /java/awt/regtesthelpers /test/lib
30+
* @build PassFailJFrame jtreg.SkippedException
31+
* @run main/manual bug4673850
32+
*/
33+
34+
import java.awt.FlowLayout;
35+
import javax.swing.JCheckBox;
36+
import javax.swing.JFrame;
37+
import javax.swing.JRadioButton;
38+
import javax.swing.SwingConstants;
39+
import javax.swing.UIManager;
40+
import jtreg.SkippedException;
41+
42+
public class bug4673850 {
43+
private static final String INSTRUCTIONS = """
44+
<html>
45+
<head>
46+
<style>
47+
p {
48+
font: sans-serif;
49+
}
50+
</style>
51+
</head>
52+
<body>
53+
<p><b>This test is for Motif LaF.</b></p>
54+
55+
<p><b>
56+
When the test starts, you'll see 2 radio buttons and 2 check boxes
57+
with the checkmarks painted.</b></p>
58+
59+
<p><b>
60+
Ensure that all the button's checkmarks are painted entirely
61+
within the circular/rectangle checkbox, NOT over them or outside them.
62+
</b></p>
63+
</body>
64+
""";
65+
66+
public static void main(String[] args) throws Exception {
67+
try {
68+
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
69+
} catch (Exception e) {
70+
throw new SkippedException("Unsupported LaF", e);
71+
}
72+
73+
PassFailJFrame.builder()
74+
.instructions(INSTRUCTIONS)
75+
.rows(10)
76+
.columns(45)
77+
.testUI(createAndShowUI())
78+
.build()
79+
.awaitAndCheck();
80+
}
81+
82+
private static JFrame createAndShowUI() {
83+
JFrame frame = new JFrame("bug4673850");
84+
frame.setLayout(new FlowLayout());
85+
86+
JRadioButton rb = new JRadioButton("RadioButt");
87+
rb.setSelected(true);
88+
frame.add(rb);
89+
JRadioButton rb2 = new JRadioButton("RadioButt");
90+
rb2.setHorizontalTextPosition(SwingConstants.LEFT);
91+
rb2.setSelected(true);
92+
frame.add(rb2);
93+
94+
JCheckBox cb = new JCheckBox("CheckBox");
95+
cb.setSelected(true);
96+
frame.add(cb);
97+
JCheckBox cb2 = new JCheckBox("CheckBox");
98+
cb2.setHorizontalTextPosition(SwingConstants.LEFT);
99+
cb2.setSelected(true);
100+
frame.add(cb2);
101+
frame.setSize(200, 150);
102+
return frame;
103+
}
104+
}

0 commit comments

Comments
 (0)