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