Skip to content

Commit 1a7a9d8

Browse files
committed
8251254: Add automated test for fix done in JDK-8218472
Backport-of: 8bf5b1d
1 parent 0459ea5 commit 1a7a9d8

File tree

1 file changed

+146
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)