Skip to content

Commit db16554

Browse files
elifaslan1Paul Hohensee
authored andcommitted
8353661: Open source several swing tests batch5
Backport-of: 924638c471b0bf4a00a890ce6a3fd7e118cdd578
1 parent 634d3fa commit db16554

File tree

4 files changed

+436
-0
lines changed

4 files changed

+436
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 4382876
27+
* @summary Tests if JSlider fires ChangeEvents when thumb is clicked and not moved
28+
* @key headful
29+
* @run main bug4186062
30+
*/
31+
32+
import java.awt.Point;
33+
import java.awt.Robot;
34+
import java.awt.event.InputEvent;
35+
36+
import javax.swing.JFrame;
37+
import javax.swing.JLabel;
38+
import javax.swing.JPanel;
39+
import javax.swing.JSlider;
40+
import javax.swing.SwingUtilities;
41+
import javax.swing.event.ChangeListener;
42+
43+
public class bug4186062 {
44+
private static JFrame f;
45+
private static JSlider slider;
46+
private static volatile Point loc;
47+
private static volatile int labelNum;
48+
49+
public static void main(String[] args) throws Exception {
50+
try {
51+
SwingUtilities.invokeAndWait(() -> {
52+
f = new JFrame("JSlider Click Value Test");
53+
f.setSize(400, 200);
54+
f.setLocationRelativeTo(null);
55+
f.setVisible(true);
56+
JPanel panel = new JPanel();
57+
slider = new JSlider();
58+
final JLabel label = new JLabel("0");
59+
labelNum = 0;
60+
61+
ChangeListener listener = e -> {
62+
labelNum++;
63+
label.setText("" + labelNum);
64+
};
65+
slider.addChangeListener(listener);
66+
67+
panel.add(slider);
68+
panel.add(label);
69+
f.add(panel);
70+
});
71+
72+
Robot r = new Robot();
73+
r.setAutoDelay(100);
74+
r.waitForIdle();
75+
r.delay(1000);
76+
77+
SwingUtilities.invokeAndWait(() -> {
78+
loc = slider.getLocationOnScreen();
79+
loc.setLocation(loc.x + (slider.getWidth() / 2),
80+
loc.y + (slider.getHeight() / 2));
81+
});
82+
83+
r.mouseMove(loc.x, loc.y);
84+
r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
85+
r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
86+
87+
if (labelNum > 0) {
88+
throw new RuntimeException(labelNum + " ChangeEvents fired. " +
89+
"Test failed");
90+
}
91+
} finally {
92+
SwingUtilities.invokeAndWait(() -> {
93+
if (f != null) {
94+
f.dispose();
95+
}
96+
});
97+
}
98+
}
99+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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 4275631
27+
* @summary Tests if vertical JSlider is properly aligned in large container
28+
* @key headful
29+
* @run main bug4275631
30+
*/
31+
32+
import java.awt.BorderLayout;
33+
import java.awt.Dimension;
34+
import java.awt.GridBagConstraints;
35+
import java.awt.GridBagLayout;
36+
import java.awt.Point;
37+
import java.awt.Robot;
38+
39+
import javax.swing.BorderFactory;
40+
import javax.swing.Box;
41+
import javax.swing.JFrame;
42+
import javax.swing.JPanel;
43+
import javax.swing.JSlider;
44+
import javax.swing.SwingUtilities;
45+
46+
public class bug4275631 {
47+
private static final int OFFSET = 1;
48+
private static JFrame f;
49+
private static JSlider slider1;
50+
private static JSlider slider2;
51+
private static volatile Point loc1;
52+
private static volatile Point loc2;
53+
54+
public static void main(String[] args) throws Exception {
55+
try {
56+
SwingUtilities.invokeAndWait(() -> {
57+
f = new JFrame("JSlider Alignment Test");
58+
f.setSize(400, 200);
59+
f.setLocationRelativeTo(null);
60+
61+
// Create two sliders, verify the alignment on the slider to be
62+
// used in the border layout
63+
slider1 = new JSlider(JSlider.VERTICAL, 0, 99, 50);
64+
slider1.setInverted(true);
65+
slider1.setMajorTickSpacing(10);
66+
slider1.setMinorTickSpacing(1);
67+
slider1.setPaintTicks(true);
68+
slider1.setPaintLabels(true);
69+
slider2 = new JSlider(JSlider.VERTICAL, 0, 99, 50);
70+
slider2.setInverted(true);
71+
slider2.setMajorTickSpacing(10);
72+
slider2.setMinorTickSpacing(1);
73+
slider2.setPaintTicks(true);
74+
slider2.setPaintLabels(true);
75+
76+
// Try to center the natural way, using a border layout in the "Center"
77+
JPanel borderPanel = new JPanel();
78+
borderPanel.setLayout(new BorderLayout());
79+
borderPanel.setBorder(BorderFactory.createTitledBorder("BorderLayout"));
80+
borderPanel.add(slider1, BorderLayout.CENTER);
81+
borderPanel.setPreferredSize(new Dimension(200, 200));
82+
83+
// Try to center using GridBagLayout, with glue on left
84+
// and right to squeeze slider into place
85+
JPanel gridBagPanel = new JPanel(new GridBagLayout());
86+
gridBagPanel.setBorder(BorderFactory.createTitledBorder("GridBagLayout"));
87+
GridBagConstraints c = new GridBagConstraints();
88+
c.gridx = 1;
89+
c.fill = GridBagConstraints.VERTICAL;
90+
c.weighty = 1.0;
91+
gridBagPanel.add(slider2, c);
92+
c.gridx = 0;
93+
c.fill = GridBagConstraints.BOTH;
94+
c.weighty = 0.0;
95+
gridBagPanel.add(Box.createHorizontalGlue(), c);
96+
c.gridx = 2;
97+
c.fill = GridBagConstraints.BOTH;
98+
gridBagPanel.add(Box.createHorizontalGlue(), c);
99+
gridBagPanel.setPreferredSize(new Dimension(200, 200));
100+
101+
f.add(borderPanel, BorderLayout.WEST);
102+
f.add(gridBagPanel, BorderLayout.EAST);
103+
f.setVisible(true);
104+
});
105+
106+
Robot r = new Robot();
107+
r.setAutoDelay(100);
108+
r.waitForIdle();
109+
r.delay(1000);
110+
111+
SwingUtilities.invokeAndWait(() -> {
112+
loc1 = slider1.getLocationOnScreen();
113+
loc1.setLocation(loc1.x + (slider1.getWidth() / 2),
114+
loc1.y + (slider1.getHeight() / 2));
115+
116+
loc2 = slider2.getLocationOnScreen();
117+
loc2.setLocation(loc2.x + (slider2.getWidth() / 2),
118+
loc2.y + (slider2.getHeight() / 2));
119+
});
120+
121+
if (loc1.y > loc2.y + OFFSET || loc1.y < loc2.y - OFFSET) {
122+
throw new RuntimeException("JSlider position is not aligned!");
123+
}
124+
} finally {
125+
SwingUtilities.invokeAndWait(() -> {
126+
if (f != null) {
127+
f.dispose();
128+
}
129+
});
130+
}
131+
}
132+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 4382876
27+
* @summary Tests how PgUp and PgDn keys work with JSlider
28+
* @key headful
29+
* @run main bug4382876
30+
*/
31+
32+
import java.awt.BorderLayout;
33+
import java.awt.ComponentOrientation;
34+
import java.awt.GraphicsConfiguration;
35+
import java.awt.GraphicsEnvironment;
36+
import java.awt.Robot;
37+
import java.awt.event.KeyEvent;
38+
import java.awt.image.BufferedImage;
39+
import java.io.File;
40+
import java.io.IOException;
41+
42+
import javax.imageio.ImageIO;
43+
import javax.swing.JFrame;
44+
import javax.swing.JSlider;
45+
import javax.swing.SwingUtilities;
46+
47+
public class bug4382876 {
48+
private static Robot r;
49+
private static JFrame f;
50+
private static JSlider slider;
51+
private static boolean upFail;
52+
private static boolean downFail;
53+
54+
public static void main(String[] args) throws Exception {
55+
try {
56+
SwingUtilities.invokeAndWait(() -> {
57+
f = new JFrame("JSlider PageUp/Down Test");
58+
f.setSize(300, 200);
59+
f.setLocationRelativeTo(null);
60+
f.setVisible(true);
61+
slider = new JSlider(-1000, -900, -1000);
62+
slider.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
63+
slider.putClientProperty("JSlider.isFilled", Boolean.TRUE);
64+
f.add(slider, BorderLayout.CENTER);
65+
});
66+
67+
r = new Robot();
68+
r.setAutoDelay(100);
69+
r.waitForIdle();
70+
r.delay(1000);
71+
72+
r.keyPress(KeyEvent.VK_PAGE_UP);
73+
SwingUtilities.invokeAndWait(() -> {
74+
if (slider.getValue() < -1000) {
75+
System.out.println("PAGE_UP VAL: " + slider.getValue());
76+
upFail = true;
77+
}
78+
});
79+
if (upFail) {
80+
writeFailImage();
81+
throw new RuntimeException("Slider value did NOT change with PAGE_UP");
82+
}
83+
r.keyPress(KeyEvent.VK_PAGE_DOWN);
84+
SwingUtilities.invokeAndWait(() -> {
85+
if (slider.getValue() > -1000) {
86+
System.out.println("PAGE_DOWN VAL: " + slider.getValue());
87+
downFail = true;
88+
}
89+
});
90+
if (downFail) {
91+
writeFailImage();
92+
throw new RuntimeException("Slider value did NOT change with PAGE_DOWN");
93+
}
94+
} finally {
95+
SwingUtilities.invokeAndWait(() -> {
96+
if (f != null) {
97+
f.dispose();
98+
}
99+
});
100+
}
101+
}
102+
103+
private static void writeFailImage() throws IOException {
104+
GraphicsConfiguration ge = GraphicsEnvironment
105+
.getLocalGraphicsEnvironment().getDefaultScreenDevice()
106+
.getDefaultConfiguration();
107+
BufferedImage failImage = r.createScreenCapture(ge.getBounds());
108+
ImageIO.write(failImage, "png", new File("failImage.png"));
109+
}
110+
}

0 commit comments

Comments
 (0)