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