Skip to content

Commit 0459ea5

Browse files
committed
8251166: Add automated testcases for changes done in JDK-8214112
Backport-of: a0d6a8a
1 parent b117ecc commit 0459ea5

File tree

3 files changed

+469
-0
lines changed

3 files changed

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

0 commit comments

Comments
 (0)