Skip to content

Commit 55c7e76

Browse files
author
Pankaj Bansal
committed
8252825: Add automated test for fix done in JDK-8218479
Reviewed-by: serb, prr
1 parent 5f76deb commit 55c7e76

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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 8218479
29+
* @summary Tests JTextPane background color
30+
* @run main TestJTextPaneBackgroundColor
31+
*/
32+
33+
import javax.swing.JFrame;
34+
import javax.swing.JPanel;
35+
import javax.swing.JTextPane;
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 TestJTextPaneBackgroundColor {
47+
private static JFrame frame;
48+
private static JTextPane textPane;
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+
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+
public static void main(String[] args) throws Exception {
69+
if (!System.getProperty("os.name").startsWith("Linux")) {
70+
System.out.println("This test is meant for Linux platform only");
71+
return;
72+
}
73+
74+
for (UIManager.LookAndFeelInfo lookAndFeelInfo :
75+
UIManager.getInstalledLookAndFeels()) {
76+
if (lookAndFeelInfo.getClassName().contains(GTK_LAF_CLASS)) {
77+
try {
78+
UIManager.setLookAndFeel(lookAndFeelInfo.getClassName());
79+
} catch (final UnsupportedLookAndFeelException ignored) {
80+
System.out.println("GTK L&F could not be set, so this " +
81+
"test can not be run in this scenario ");
82+
return;
83+
}
84+
}
85+
}
86+
87+
robot = new Robot();
88+
robot.setAutoDelay(100);
89+
90+
try {
91+
SwingUtilities.invokeAndWait(new Runnable() {
92+
public void run() {
93+
JPanel panel = new JPanel();
94+
textPane = new JTextPane();
95+
textPane.setText(" ");
96+
panel.add(textPane, BorderLayout.CENTER);
97+
frame = new JFrame("TestJTextPaneBackgroundColor");
98+
frame.add(panel);
99+
frame.setSize(200, 200);
100+
frame.setAlwaysOnTop(true);
101+
frame.setLocationRelativeTo(null);
102+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
103+
frame.setVisible(true);
104+
}
105+
});
106+
107+
robot.waitForIdle();
108+
robot.delay(500);
109+
110+
blockTillDisplayed(textPane);
111+
SwingUtilities.invokeAndWait(() -> {
112+
point = textPane.getLocationOnScreen();
113+
rect = textPane.getBounds();
114+
});
115+
robot.waitForIdle();
116+
robot.delay(500);
117+
118+
Color textpaneBackgroundColor = robot
119+
.getPixelColor(point.x+rect.width/2, point.y+rect.height/2);
120+
robot.waitForIdle();
121+
robot.delay(500);
122+
123+
Color panelColor = robot
124+
.getPixelColor(point.x-rect.width/2, point.y+rect.height/2);
125+
robot.waitForIdle();
126+
robot.delay(500);
127+
128+
System.out.println(textpaneBackgroundColor);
129+
System.out.println(panelColor);
130+
if (textpaneBackgroundColor.equals(panelColor)) {
131+
throw new RuntimeException("The expected background color for " +
132+
"TextPane was not found");
133+
}
134+
} finally {
135+
if (frame != null) {
136+
SwingUtilities.invokeAndWait(frame::dispose);
137+
}
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)