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 8151787
  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@200pct.png", Color.BLUE, Color.GREEN),
  59         new ImageInfo("splash2", "splash2@2x", Color.WHITE, Color.BLACK),
  60         new ImageInfo("splash3.", "splash3@200pct.", 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         Robot robot = new Robot();
 100         Color splashScreenColor = robot.getPixelColor(screenX, screenY);
 101 
 102         float scaleFactor = getScaleFactor();
 103         Color testColor = (1 < scaleFactor) ? test.color2x : test.color1x;
 104         if (!compare(testColor, splashScreenColor)) {
 105             throw new RuntimeException(
 106                     "Image with wrong resolution is used for splash screen!");
 107         }
 108     }
 109 
 110     static int doExec(Map<String, String> envToSet, String... cmds) {
 111         Process p = null;
 112         ProcessBuilder pb = new ProcessBuilder(cmds);
 113         Map<String, String> env = pb.environment();
 114         for (String cmd : cmds) {
 115             System.out.print(cmd + " ");
 116         }
 117         System.out.println();
 118         if (envToSet != null) {
 119             env.putAll(envToSet);
 120         }
 121         BufferedReader rdr = null;
 122         try {
 123             List<String> outputList = new ArrayList<>();
 124             pb.redirectErrorStream(true);
 125             p = pb.start();
 126             rdr = new BufferedReader(new InputStreamReader(p.getInputStream()));
 127             String in = rdr.readLine();
 128             while (in != null) {
 129                 outputList.add(in);
 130                 in = rdr.readLine();
 131                 System.out.println(in);
 132             }
 133             p.waitFor();
 134             p.destroy();
 135         } catch (Exception ex) {
 136             ex.printStackTrace();
 137         }
 138         return p.exitValue();
 139     }
 140 
 141     static void testFocus() throws Exception {
 142 
 143         System.out.println("Focus Test!");
 144         Robot robot = new Robot();
 145         robot.setAutoDelay(50);
 146         Frame frame = new Frame();
 147         frame.setSize(100, 100);
 148         String test = "123";
 149         TextField textField = new TextField(test);
 150         textField.selectAll();
 151         frame.add(textField);
 152         frame.setVisible(true);
 153         robot.waitForIdle();
 154 
 155         robot.keyPress(KeyEvent.VK_A);
 156         robot.keyRelease(KeyEvent.VK_A);
 157         robot.keyPress(KeyEvent.VK_B);
 158         robot.keyRelease(KeyEvent.VK_B);
 159         robot.waitForIdle();
 160 
 161         frame.dispose();
 162         if (!textField.getText().equals("ab")) {
 163             throw new RuntimeException("Focus is lost!");
 164         }
 165     }
 166 
 167     static boolean compare(Color c1, Color c2) {
 168         return compare(c1.getRed(), c2.getRed())
 169                 && compare(c1.getGreen(), c2.getGreen())
 170                 && compare(c1.getBlue(), c2.getBlue());
 171     }
 172 
 173     static boolean compare(int n, int m) {
 174         return Math.abs(n - m) <= 50;
 175     }
 176 
 177     static float getScaleFactor() {
 178 
 179         final Dialog dialog = new Dialog((Window) null);
 180         dialog.setSize(100, 100);
 181         dialog.setModal(true);
 182         float[] scaleFactors = new float[1];
 183         Panel panel = new Panel() {
 184 
 185             @Override
 186             public void paint(Graphics g) {
 187                 String scaleStr = System.getenv("GDK_SCALE");
 188                 if (scaleStr != null && !scaleStr.equals("")) {
 189                     try {
 190                         scaleFactors[0] = Float.valueOf(scaleStr);
 191                     } catch (NumberFormatException ex) {
 192                         scaleFactors[0] = 1.0f;
 193                     }
 194                 }
 195                 dialog.setVisible(false);
 196             }
 197         };
 198         dialog.add(panel);
 199         dialog.setVisible(true);
 200         dialog.dispose();
 201         return scaleFactors[0];
 202     }
 203 
 204     static void generateImages() throws Exception {
 205         for (ImageInfo test : tests) {
 206             generateImage(test.name1x, test.color1x, 1);
 207             generateImage(test.name2x, test.color2x, 2);
 208         }
 209     }
 210 
 211     static void generateImage(String name, Color color, int scale) throws Exception {
 212         File file = new File(name);
 213         if (file.exists()) {
 214             return;
 215         }
 216         BufferedImage image = new BufferedImage(scale * IMAGE_WIDTH, scale * IMAGE_HEIGHT,
 217                 BufferedImage.TYPE_INT_RGB);
 218         Graphics g = image.getGraphics();
 219         g.setColor(color);
 220         g.fillRect(0, 0, scale * IMAGE_WIDTH, scale * IMAGE_HEIGHT);
 221         ImageIO.write(image, "png", file);
 222     }
 223 
 224     static class ImageInfo {
 225 
 226         final String name1x;
 227         final String name2x;
 228         final Color color1x;
 229         final Color color2x;
 230 
 231         public ImageInfo(String name1x, String name2x, Color color1x, Color color2x) {
 232             this.name1x = name1x;
 233             this.name2x = name2x;
 234             this.color1x = color1x;
 235             this.color2x = color2x;
 236         }
 237     }
 238 }
 239 
--- EOF ---