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:splash1.png MultiResolutionSplashTest TEST_SPLASH 0 46 * @run main/othervm -splash:splash2 MultiResolutionSplashTest TEST_SPLASH 1 47 * @run main/othervm -splash:splash3. MultiResolutionSplashTest TEST_SPLASH 2 48 */ 49 public class MultiResolutionSplashTest { 50 51 private static final int IMAGE_WIDTH = 300; 52 private static final int IMAGE_HEIGHT = 200; 53 54 private static final ImageInfo[] tests = { 55 new ImageInfo("splash1.png", "splash1@2x.png", Color.BLUE, Color.GREEN), 56 new ImageInfo("splash2", "splash2@2x", Color.WHITE, Color.BLACK), 57 new ImageInfo("splash3.", "splash3@2x.", Color.YELLOW, Color.RED) 58 }; 59 60 public static void main(String[] args) throws Exception { 61 62 String test = args[0]; 63 64 switch (test) { 65 case "GENERATE_IMAGES": 66 generateImages(); 67 break; 68 case "TEST_SPLASH": 69 int index = Integer.parseInt(args[1]); 70 testSplash(tests[index]); 71 break; 72 default: 73 throw new RuntimeException("Unknown test: " + test); 74 } 75 } 76 77 static void testSplash(ImageInfo test) throws Exception { 78 SplashScreen splashScreen = SplashScreen.getSplashScreen(); 79 80 if (splashScreen == null) { 81 throw new RuntimeException("Splash screen is not shown!"); 82 } 83 84 Graphics2D g = splashScreen.createGraphics(); 85 Rectangle splashBounds = splashScreen.getBounds(); 86 int screenX = (int) splashBounds.getCenterX(); 87 int screenY = (int) splashBounds.getCenterY(); 88 89 Robot robot = new Robot(); 90 Color splashScreenColor = robot.getPixelColor(screenX, screenY); 91 92 float scaleFactor = getScaleFactor(); 93 Color testColor = (1 < scaleFactor) ? test.color2x : test.color1x; 94 95 if (!testColor.equals(splashScreenColor)) { 96 throw new RuntimeException( 97 "Image with wrong resolution is used for splash screen!"); 98 } 99 } 100 101 static float getScaleFactor() { 102 103 final Dialog dialog = new Dialog((Window) null); 104 dialog.setSize(100, 100); 105 dialog.setModal(true); 106 final float[] scaleFactors = new float[1]; 107 Panel panel = new Panel() { 108 109 @Override 110 public void paint(Graphics g) { 111 float scaleFactor = 1; 112 if (g instanceof SunGraphics2D) { 113 scaleFactor = ((SunGraphics2D) g).surfaceData.getDefaultScale(); 114 } 115 scaleFactors[0] = scaleFactor; 116 dialog.setVisible(false); 117 } 118 }; 119 120 dialog.add(panel); 121 dialog.setVisible(true); 122 dialog.dispose(); 123 124 return scaleFactors[0]; 125 } 126 127 static void generateImages() throws Exception { 128 for (ImageInfo test : tests) { 129 generateImage(test.name1x, test.color1x, 1); 130 generateImage(test.name2x, test.color2x, 2); 131 } 132 } 133 134 static void generateImage(String name, Color color, int scale) throws Exception { 135 File file = new File(name); 136 if (file.exists()) { 137 return; 138 } 139 BufferedImage image = new BufferedImage(scale * IMAGE_WIDTH, scale * IMAGE_HEIGHT, 140 BufferedImage.TYPE_INT_RGB); 141 Graphics g = image.getGraphics(); 142 g.setColor(color); 143 g.fillRect(0, 0, scale * IMAGE_WIDTH, scale * IMAGE_HEIGHT); 144 ImageIO.write(image, "png", file); 145 } 146 147 static class ImageInfo { 148 149 final String name1x; 150 final String name2x; 151 final Color color1x; 152 final Color color2x; 153 154 public ImageInfo(String name1x, String name2x, Color color1x, Color color2x) { 155 this.name1x = name1x; 156 this.name2x = name2x; 157 this.color1x = color1x; 158 this.color2x = color2x; 159 } 160 } 161 }