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
24import java.awt.Color;
25import java.awt.Dialog;
26import java.awt.Frame;
27import java.awt.Graphics;
28import java.awt.Graphics2D;
29import java.awt.Panel;
30import java.awt.Rectangle;
31import java.awt.Robot;
32import java.awt.SplashScreen;
33import java.awt.TextField;
34import java.awt.Window;
35import java.awt.event.KeyEvent;
36import java.awt.image.BufferedImage;
37import java.io.BufferedReader;
38import java.io.File;
39import java.io.InputStreamReader;
40import java.util.ArrayList;
41import java.util.HashMap;
42import java.util.List;
43import java.util.Map;
44import javax.imageio.ImageIO;
45
46/**
47 * @test
48 * @bug 8145174 8151787 8168657
49 * @summary HiDPI splash screen support on Linux
50 * @modules java.desktop/sun.java2d
51 * @requires (os.family == "linux")
52 * @run main UnixMultiResolutionSplashTest
53 */
54public class UnixMultiResolutionSplashTest {
55
56    private static final int IMAGE_WIDTH = 300;
57    private static final int IMAGE_HEIGHT = 200;
58    private static int inx = 0;
59    private static final ImageInfo[] tests = {
60        new ImageInfo("splash1.png", "splash1@200pct.png", Color.BLUE, Color.GREEN),
61        new ImageInfo("splash2", "splash2@2x", Color.WHITE, Color.BLACK),
62        new ImageInfo("splash3.", "splash3@200pct.", Color.YELLOW, Color.RED)
63    };
64
65    public static void main(String[] args) throws Exception {
66
67        if (args.length == 0) {
68            generateImages();
69            for (ImageInfo test : tests) {
70                createChildProcess(test);
71            }
72        } else {
73            int index = Integer.parseInt(args[0]);
74            testSplash(tests[index]);
75        }
76    }
77
78    static void createChildProcess(ImageInfo test) {
79        String javaPath = System.getProperty("java.home");
80        File file = new File(test.name1x);
81        String classPathDir = System.getProperty("java.class.path");
82        Map<String, String> env = new HashMap<String, String>();
83        env.put("GDK_SCALE", "2");
84        int exitValue = doExec(env, javaPath + File.separator + "bin" + File.separator
85                + "java", "-splash:" + file.getAbsolutePath(), "-cp",
86                classPathDir, "UnixMultiResolutionSplashTest", String.valueOf(inx++));
87        if (exitValue != 0) {
88            throw new RuntimeException("Test Failed");
89        }
90    }
91
92    static void testSplash(ImageInfo test) throws Exception {
93        SplashScreen splashScreen = SplashScreen.getSplashScreen();
94        if (splashScreen == null) {
95            throw new RuntimeException("Splash screen is not shown!");
96        }
97        Graphics2D g = splashScreen.createGraphics();
98        Rectangle splashBounds = splashScreen.getBounds();
99        int screenX = (int) splashBounds.getCenterX();
100        int screenY = (int) splashBounds.getCenterY();
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
242