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.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.File; 38 import javax.imageio.ImageIO; 39 import sun.java2d.SunGraphics2D; 40 41 /** 42 * @test 43 * @bug 8043869 8075244 8078082 44 * @author Alexander Scherbatiy 45 * @summary [macosx] java -splash does not honor 2x hi dpi notation for retina 46 * support 47 * @run main MultiResolutionSplashTest GENERATE_IMAGES 48 * @run main/othervm -splash:splash1.png MultiResolutionSplashTest TEST_SPLASH 0 49 * @run main/othervm -splash:splash2 MultiResolutionSplashTest TEST_SPLASH 1 50 * @run main/othervm -splash:splash3. MultiResolutionSplashTest TEST_SPLASH 2 51 * @run main/othervm -splash:splash1.png MultiResolutionSplashTest TEST_FOCUS 52 */ 53 public class MultiResolutionSplashTest { 54 55 private static final int IMAGE_WIDTH = 300; 56 private static final int IMAGE_HEIGHT = 200; 57 58 private static final ImageInfo[] tests = { 59 new ImageInfo("splash1.png", "splash1@2x.png", Color.BLUE, Color.GREEN), 60 new ImageInfo("splash2", "splash2@2x", Color.WHITE, Color.BLACK), 61 new ImageInfo("splash3.", "splash3@2x.", Color.YELLOW, Color.RED) 62 }; 63 64 public static void main(String[] args) throws Exception { 65 66 String test = args[0]; 67 68 switch (test) { 69 case "GENERATE_IMAGES": 70 generateImages(); 71 break; 72 case "TEST_SPLASH": 73 int index = Integer.parseInt(args[1]); 74 testSplash(tests[index]); 75 break; 76 case "TEST_FOCUS": 77 testFocus(); 78 break; 79 default: 80 throw new RuntimeException("Unknown test: " + test); 81 } 82 } 83 84 static void testSplash(ImageInfo test) throws Exception { 85 SplashScreen splashScreen = SplashScreen.getSplashScreen(); 86 87 if (splashScreen == null) { 88 throw new RuntimeException("Splash screen is not shown!"); 89 } 90 91 Graphics2D g = splashScreen.createGraphics(); 92 Rectangle splashBounds = splashScreen.getBounds(); 93 int screenX = (int) splashBounds.getCenterX(); 94 int screenY = (int) splashBounds.getCenterY(); 95 96 Robot robot = new Robot(); 97 Color splashScreenColor = robot.getPixelColor(screenX, screenY); 98 99 float scaleFactor = getScaleFactor(); 100 Color testColor = (1 < scaleFactor) ? test.color2x : test.color1x; 101 102 if (!compare(testColor, splashScreenColor)) { 103 throw new RuntimeException( 104 "Image with wrong resolution is used for splash screen!"); 105 } 106 } 107 108 static void testFocus() throws Exception { 109 110 System.out.println("Focus Test!"); 111 Robot robot = new Robot(); 112 robot.setAutoDelay(50); 113 114 Frame frame = new Frame(); 115 frame.setSize(100, 100); 116 String test = "123"; 117 TextField textField = new TextField(test); 118 textField.selectAll(); 119 frame.add(textField); 120 frame.setVisible(true); 121 robot.waitForIdle(); 122 123 robot.keyPress(KeyEvent.VK_A); 124 robot.keyRelease(KeyEvent.VK_A); 125 robot.keyPress(KeyEvent.VK_B); 126 robot.keyRelease(KeyEvent.VK_B); 127 robot.waitForIdle(); 128 129 frame.dispose(); 130 131 if(!textField.getText().equals("ab")){ 132 throw new RuntimeException("Focus is lost!"); 133 } 134 } 135 136 static boolean compare(Color c1, Color c2){ 137 return compare(c1.getRed(), c2.getRed()) 138 && compare(c1.getGreen(), c2.getGreen()) 139 && compare(c1.getBlue(), c2.getBlue()); 140 } 141 142 static boolean compare(int n, int m){ 143 return Math.abs(n - m) <= 50; 144 } 145 146 static float getScaleFactor() { 147 148 final Dialog dialog = new Dialog((Window) null); 149 dialog.setSize(100, 100); 150 dialog.setModal(true); 151 final float[] scaleFactors = new float[1]; 152 Panel panel = new Panel() { 153 154 @Override 155 public void paint(Graphics g) { 156 float scaleFactor = 1; 157 if (g instanceof SunGraphics2D) { 158 scaleFactor = ((SunGraphics2D) g).surfaceData.getDefaultScale(); 159 } 160 scaleFactors[0] = scaleFactor; 161 dialog.setVisible(false); 162 } 163 }; 164 165 dialog.add(panel); 166 dialog.setVisible(true); 167 dialog.dispose(); 168 169 return scaleFactors[0]; 170 } 171 172 static void generateImages() throws Exception { 173 for (ImageInfo test : tests) { 174 generateImage(test.name1x, test.color1x, 1); 175 generateImage(test.name2x, test.color2x, 2); 176 } 177 } 178 179 static void generateImage(String name, Color color, int scale) throws Exception { 180 File file = new File(name); 181 if (file.exists()) { 182 return; 183 } 184 BufferedImage image = new BufferedImage(scale * IMAGE_WIDTH, scale * IMAGE_HEIGHT, 185 BufferedImage.TYPE_INT_RGB); 186 Graphics g = image.getGraphics(); 187 g.setColor(color); 188 g.fillRect(0, 0, scale * IMAGE_WIDTH, scale * IMAGE_HEIGHT); 189 ImageIO.write(image, "png", file); 190 } 191 192 static class ImageInfo { 193 194 final String name1x; 195 final String name2x; 196 final Color color1x; 197 final Color color2x; 198 199 public ImageInfo(String name1x, String name2x, Color color1x, Color color2x) { 200 this.name1x = name1x; 201 this.name2x = name2x; 202 this.color1x = color1x; 203 this.color2x = color2x; 204 } 205 } 206 }