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