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