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 
blockTillDisplayed(Component comp)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 
getMaxColorDiff(Color c1, Color c2)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 
main(String[] args)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 }
152