1 /*
   2  * Copyright (c) 2014, 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.Graphics;
  27 import java.awt.Graphics2D;
  28 import java.awt.Panel;
  29 import java.awt.Rectangle;
  30 import java.awt.Robot;
  31 import java.awt.SplashScreen;
  32 import java.awt.Window;
  33 import java.awt.image.BufferedImage;
  34 import java.io.File;
  35 import javax.imageio.ImageIO;
  36 import sun.java2d.SunGraphics2D;
  37 
  38 /**
  39  * @test
  40  * @bug 8043869
  41  * @author Alexander Scherbatiy
  42  * @summary [macosx] java -splash does not honor 2x hi dpi notation for retina
  43  * support
  44  * @run main MultiResolutionSplashTest GENERATE_IMAGES
  45  * @run main/othervm -splash:image.png MultiResolutionSplashTest TEST_SPLASH
  46  */
  47 public class MultiResolutionSplashTest {
  48 
  49     private static final int IMAGE_WIDTH = 300;
  50     private static final int IMAGE_HEIGHT = 200;
  51     private static final Color COLOR_1X = Color.GREEN;
  52     private static final Color COLOR_2X = Color.BLUE;
  53     private static final String IMAGE_NAME_1X = "image.png";
  54     private static final String IMAGE_NAME_2X = "image@2x.png";
  55 
  56     public static void main(String[] args) throws Exception {
  57 
  58         String test = args[0];
  59 
  60         switch (test) {
  61             case "GENERATE_IMAGES":
  62                 generateImages();
  63                 break;
  64             case "TEST_SPLASH":
  65                 testSplash();
  66                 break;
  67             default:
  68                 throw new RuntimeException("Unknown test: " + test);
  69         }
  70     }
  71 
  72     static void testSplash() throws Exception {
  73         SplashScreen splashScreen = SplashScreen.getSplashScreen();
  74 
  75         if (splashScreen == null) {
  76             throw new RuntimeException("Splash screen is not shown!");
  77         }
  78 
  79         Graphics2D g = splashScreen.createGraphics();
  80         Rectangle splashBounds = splashScreen.getBounds();
  81         int screenX = (int) splashBounds.getCenterX();
  82         int screenY = (int) splashBounds.getCenterY();
  83 
  84         Robot robot = new Robot();
  85         Color splashScreenColor = robot.getPixelColor(screenX, screenY);
  86 
  87         float scaleFactor = getScaleFactor();
  88         Color testColor = (1 < scaleFactor) ? COLOR_2X : COLOR_1X;
  89 
  90         if (!testColor.equals(splashScreenColor)) {
  91             throw new RuntimeException(
  92                     "Image with wrong resolution is used for splash screen!");
  93         }
  94     }
  95 
  96     static float getScaleFactor() {
  97 
  98         final Dialog dialog = new Dialog((Window) null);
  99         dialog.setSize(100, 100);
 100         dialog.setModal(true);
 101         final float[] scaleFactors = new float[1];
 102         Panel panel = new Panel() {
 103 
 104             @Override
 105             public void paint(Graphics g) {
 106                 float scaleFactor = 1;
 107                 if (g instanceof SunGraphics2D) {
 108                     scaleFactor = ((SunGraphics2D) g).surfaceData.getDefaultScale();
 109                 }
 110                 scaleFactors[0] = scaleFactor;
 111                 dialog.setVisible(false);
 112             }
 113         };
 114 
 115         dialog.add(panel);
 116         dialog.setVisible(true);
 117         dialog.dispose();
 118 
 119         return scaleFactors[0];
 120     }
 121 
 122     static void generateImages() throws Exception {
 123         if (!new File(IMAGE_NAME_1X).exists()) {
 124             generateImage(1);
 125         }
 126 
 127         if (!new File(IMAGE_NAME_2X).exists()) {
 128             generateImage(2);
 129         }
 130     }
 131 
 132     static void generateImage(int scale) throws Exception {
 133         BufferedImage image = new BufferedImage(scale * IMAGE_WIDTH, scale * IMAGE_HEIGHT,
 134                 BufferedImage.TYPE_INT_RGB);
 135         Graphics g = image.getGraphics();
 136         g.setColor(scale == 1 ? COLOR_1X : COLOR_2X);
 137         g.fillRect(0, 0, scale * IMAGE_WIDTH, scale * IMAGE_HEIGHT);
 138         File file = new File(scale == 1 ? IMAGE_NAME_1X : IMAGE_NAME_2X);
 139         ImageIO.write(image, "png", file);
 140     }
 141 }