1 /*
2 * Copyright (c) 2016, 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 import java.awt.Color;
25 import java.awt.Dialog;
26 import java.awt.Frame;
27 import java.awt.Graphics;
28 import java.awt.Graphics2D;
29 import java.awt.Panel;
30 import java.awt.Rectangle;
31 import java.awt.Robot;
32 import java.awt.SplashScreen;
33 import java.awt.TextField;
34 import java.awt.Window;
35 import java.awt.event.KeyEvent;
36 import java.awt.image.BufferedImage;
37 import java.io.BufferedReader;
38 import java.io.File;
39 import java.io.InputStreamReader;
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.List;
43 import java.util.Map;
44 import javax.imageio.ImageIO;
45
46 /**
47 * @test @bug 8145174
48 * @summary HiDPI splash screen support on Linux
49 * @modules java.desktop/sun.java2d
50 * @run main UnixMultiResolutionSplashTest
51 */
52 public class UnixMultiResolutionSplashTest {
53
54 private static final int IMAGE_WIDTH = 300;
55 private static final int IMAGE_HEIGHT = 200;
56 private static int inx = 0;
57 private static final ImageInfo[] tests = {
58 new ImageInfo("splash1.png", "splash1.java-scale2x.png", Color.BLUE, Color.GREEN),
59 new ImageInfo("splash2", "splash2.java-scale2x", Color.WHITE, Color.BLACK),
60 new ImageInfo("splash3.", "splash3.java-scale2x.", Color.YELLOW, Color.RED)
61 };
62
63 public static void main(String[] args) throws Exception {
64
65 if (args.length == 0) {
66 generateImages();
67 for (ImageInfo test : tests) {
68 createChildProcess(test);
69 }
70 } else {
71 int index = Integer.parseInt(args[0]);
72 testSplash(tests[index]);
73 }
74 }
75
76 static void createChildProcess(ImageInfo test) {
77 String javaPath = System.getProperty("java.home");
78 File file = new File(test.name1x);
79 String classPathDir = System.getProperty("java.class.path");
80 Map<String, String> env = new HashMap<String, String>();
81 env.put("GDK_SCALE", "2");
82 int exitValue = doExec(env, javaPath + File.separator + "bin" + File.separator
83 + "java", "-splash:" + file.getAbsolutePath(), "-cp",
84 classPathDir, "UnixMultiResolutionSplashTest", String.valueOf(inx++));
85 if (exitValue != 0) {
86 throw new RuntimeException("Test Failed");
87 }
88 }
89
90 static void testSplash(ImageInfo test) throws Exception {
91 SplashScreen splashScreen = SplashScreen.getSplashScreen();
92 if (splashScreen == null) {
93 throw new RuntimeException("Splash screen is not shown!");
94 }
95 Graphics2D g = splashScreen.createGraphics();
96 Rectangle splashBounds = splashScreen.getBounds();
97 int screenX = (int) splashBounds.getCenterX();
98 int screenY = (int) splashBounds.getCenterY();
99 System.out.println(screenX);
100 System.out.println(screenY);
101 Robot robot = new Robot();
102 Color splashScreenColor = robot.getPixelColor(screenX, screenY);
103
104 float scaleFactor = getScaleFactor();
105 Color testColor = (1 < scaleFactor) ? test.color2x : test.color1x;
106 if (!compare(testColor, splashScreenColor)) {
107 throw new RuntimeException(
108 "Image with wrong resolution is used for splash screen!");
109 }
110 }
111
112 static int doExec(Map<String, String> envToSet, String... cmds) {
113 Process p = null;
114 ProcessBuilder pb = new ProcessBuilder(cmds);
115 Map<String, String> env = pb.environment();
116 for (String cmd : cmds) {
117 System.out.print(cmd + " ");
118 }
119 System.out.println();
120 if (envToSet != null) {
121 env.putAll(envToSet);
122 }
123 BufferedReader rdr = null;
124 try {
125 List<String> outputList = new ArrayList<>();
126 pb.redirectErrorStream(true);
127 p = pb.start();
128 rdr = new BufferedReader(new InputStreamReader(p.getInputStream()));
129 String in = rdr.readLine();
130 while (in != null) {
131 outputList.add(in);
132 in = rdr.readLine();
133 System.out.println(in);
134 }
135 p.waitFor();
136 p.destroy();
137 } catch (Exception ex) {
138 ex.printStackTrace();
139 }
140 return p.exitValue();
141 }
142
143 static void testFocus() throws Exception {
144
145 System.out.println("Focus Test!");
146 Robot robot = new Robot();
147 robot.setAutoDelay(50);
148 Frame frame = new Frame();
149 frame.setSize(100, 100);
150 String test = "123";
151 TextField textField = new TextField(test);
152 textField.selectAll();
153 frame.add(textField);
154 frame.setVisible(true);
155 robot.waitForIdle();
156
157 robot.keyPress(KeyEvent.VK_A);
158 robot.keyRelease(KeyEvent.VK_A);
159 robot.keyPress(KeyEvent.VK_B);
160 robot.keyRelease(KeyEvent.VK_B);
161 robot.waitForIdle();
162
163 frame.dispose();
164 if (!textField.getText().equals("ab")) {
165 throw new RuntimeException("Focus is lost!");
166 }
167 }
168
169 static boolean compare(Color c1, Color c2) {
170 return compare(c1.getRed(), c2.getRed())
171 && compare(c1.getGreen(), c2.getGreen())
172 && compare(c1.getBlue(), c2.getBlue());
173 }
174
175 static boolean compare(int n, int m) {
176 return Math.abs(n - m) <= 50;
177 }
178
179 static float getScaleFactor() {
180
181 final Dialog dialog = new Dialog((Window) null);
182 dialog.setSize(100, 100);
183 dialog.setModal(true);
184 float[] scaleFactors = new float[1];
185 Panel panel = new Panel() {
186
187 @Override
188 public void paint(Graphics g) {
189 String scaleStr = System.getenv("GDK_SCALE");
190 if (scaleStr != null && !scaleStr.equals("")) {
191 try {
192 scaleFactors[0] = Float.valueOf(scaleStr);
193 } catch (NumberFormatException ex) {
194 scaleFactors[0] = 1.0f;
195 }
196 }
197 dialog.setVisible(false);
198 }
199 };
200 dialog.add(panel);
201 dialog.setVisible(true);
202 dialog.dispose();
203 return scaleFactors[0];
204 }
205
206 static void generateImages() throws Exception {
207 for (ImageInfo test : tests) {
208 generateImage(test.name1x, test.color1x, 1);
209 generateImage(test.name2x, test.color2x, 2);
210 }
211 }
212
213 static void generateImage(String name, Color color, int scale) throws Exception {
214 File file = new File(name);
215 if (file.exists()) {
216 return;
217 }
218 BufferedImage image = new BufferedImage(scale * IMAGE_WIDTH, scale * IMAGE_HEIGHT,
219 BufferedImage.TYPE_INT_RGB);
220 Graphics g = image.getGraphics();
221 g.setColor(color);
222 g.fillRect(0, 0, scale * IMAGE_WIDTH, scale * IMAGE_HEIGHT);
223 ImageIO.write(image, "png", file);
224 }
225
226 static class ImageInfo {
227
228 final String name1x;
229 final String name2x;
230 final Color color1x;
231 final Color color2x;
232
233 public ImageInfo(String name1x, String name2x, Color color1x, Color color2x) {
234 this.name1x = name1x;
235 this.name2x = name2x;
236 this.color1x = color1x;
237 this.color2x = color2x;
238 }
239 }
240 }
241