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