Skip to content

Commit 1ae6b53

Browse files
author
Pankaj Bansal
committedSep 28, 2020
8252194: Add automated test for fix done in JDK-8218469
Reviewed-by: serb, jdv, kizune
1 parent 77a0f39 commit 1ae6b53

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
* Copyright (c) 2020, 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+
* @requires (os.family == "linux")
27+
* @key headful
28+
* @bug 8218469
29+
* @summary Tests JSlider is rendered properly with gtk3
30+
* @run main TestJSliderRendering
31+
*/
32+
33+
import javax.swing.JFrame;
34+
import javax.swing.JPanel;
35+
import javax.swing.JSlider;
36+
import javax.swing.SwingUtilities;
37+
import javax.swing.UIManager;
38+
import javax.swing.UnsupportedLookAndFeelException;
39+
import java.awt.Color;
40+
import java.awt.Component;
41+
import java.awt.Point;
42+
import java.awt.Rectangle;
43+
import java.awt.Robot;
44+
45+
public class TestJSliderRendering {
46+
private static JFrame frame;
47+
private static JSlider slider;
48+
private static Point point;
49+
private static Rectangle rect;
50+
private static Robot robot;
51+
private static final String GTK_LAF_CLASS = "GTKLookAndFeel";
52+
private static int minColorDifference = 50;
53+
54+
private static void blockTillDisplayed(Component comp) {
55+
Point p = null;
56+
while (p == null) {
57+
try {
58+
p = comp.getLocationOnScreen();
59+
} catch (IllegalStateException e) {
60+
try {
61+
Thread.sleep(500);
62+
} catch (InterruptedException ie) {
63+
}
64+
}
65+
}
66+
}
67+
68+
private static int getMaxColorDiff(Color c1, Color c2) {
69+
return Math.max(Math.abs(c1.getRed() - c2.getRed()),
70+
Math.max(Math.abs(c1.getGreen() - c2.getGreen()),
71+
Math.abs(c1.getBlue() - c2.getBlue())));
72+
}
73+
74+
public static void main(String[] args) throws Exception {
75+
if (!System.getProperty("os.name").startsWith("Linux")) {
76+
System.out.println("This test is meant for Linux platform only");
77+
return;
78+
}
79+
80+
for (UIManager.LookAndFeelInfo lookAndFeelInfo :
81+
UIManager.getInstalledLookAndFeels()) {
82+
if (lookAndFeelInfo.getClassName().contains(GTK_LAF_CLASS)) {
83+
try {
84+
UIManager.setLookAndFeel(lookAndFeelInfo.getClassName());
85+
} catch (final UnsupportedLookAndFeelException ignored) {
86+
System.out.println("GTK L&F could not be set, so this " +
87+
"test can not be run in this scenario ");
88+
return;
89+
}
90+
}
91+
}
92+
93+
robot = new Robot();
94+
robot.setAutoDelay(100);
95+
96+
try {
97+
SwingUtilities.invokeAndWait(new Runnable() {
98+
public void run() {
99+
JPanel panel = new JPanel();
100+
slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 50);
101+
panel.add(slider);
102+
frame = new JFrame("TestJSliderRendering");
103+
frame.add(panel);
104+
frame.setSize(200, 200);
105+
frame.setAlwaysOnTop(true);
106+
frame.setLocationRelativeTo(null);
107+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
108+
frame.setVisible(true);
109+
}
110+
});
111+
112+
robot.waitForIdle();
113+
robot.delay(500);
114+
115+
blockTillDisplayed(slider);
116+
SwingUtilities.invokeAndWait(() -> {
117+
point = slider.getLocationOnScreen();
118+
rect = slider.getBounds();
119+
});
120+
robot.waitForIdle();
121+
robot.delay(500);
122+
123+
int h = point.y+rect.height*6/7;
124+
125+
Color backgroundColor = robot
126+
.getPixelColor(point.x+rect.width/4, h);
127+
robot.waitForIdle();
128+
129+
boolean knobFound = false;
130+
for (int i=point.x+rect.width/4;i<point.x+rect.width*3/4;i+=2) {
131+
Color highlightColor = robot.getPixelColor(i, h);
132+
if (getMaxColorDiff(backgroundColor, highlightColor)
133+
> minColorDifference) {
134+
knobFound = true;
135+
break;
136+
}
137+
robot.waitForIdle();
138+
}
139+
if (!knobFound) {
140+
throw new RuntimeException("The slider is not rendered properly");
141+
}
142+
} finally {
143+
if (frame != null) {
144+
SwingUtilities.invokeAndWait(frame::dispose);
145+
}
146+
}
147+
}
148+
}

0 commit comments

Comments
 (0)
Please sign in to comment.