Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/SplashScreen/MultiResolutionSplash/MultiResolutionSplashTest.java
38821 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.Color;24import java.awt.Dialog;25import java.awt.Frame;26import java.awt.Graphics;27import java.awt.Graphics2D;28import java.awt.Panel;29import java.awt.Rectangle;30import java.awt.Robot;31import java.awt.SplashScreen;32import java.awt.TextField;33import java.awt.Window;34import java.awt.event.KeyEvent;35import java.awt.image.BufferedImage;36import java.io.File;37import javax.imageio.ImageIO;38import sun.java2d.SunGraphics2D;3940/**41* @test42* @bug 8043869 8075244 807808243* @author Alexander Scherbatiy44* @summary [macosx] java -splash does not honor 2x hi dpi notation for retina45* support46* @run main MultiResolutionSplashTest GENERATE_IMAGES47* @run main/othervm -splash:splash1.png MultiResolutionSplashTest TEST_SPLASH 048* @run main/othervm -splash:splash2 MultiResolutionSplashTest TEST_SPLASH 149* @run main/othervm -splash:splash3. MultiResolutionSplashTest TEST_SPLASH 250* @run main/othervm -splash:splash1.png MultiResolutionSplashTest TEST_FOCUS51*/52public class MultiResolutionSplashTest {5354private static final int IMAGE_WIDTH = 300;55private static final int IMAGE_HEIGHT = 200;5657private static final ImageInfo[] tests = {58new ImageInfo("splash1.png", "[email protected]", Color.BLUE, Color.GREEN),59new ImageInfo("splash2", "splash2@2x", Color.WHITE, Color.BLACK),60new ImageInfo("splash3.", "splash3@2x.", Color.YELLOW, Color.RED)61};6263public static void main(String[] args) throws Exception {6465String test = args[0];6667switch (test) {68case "GENERATE_IMAGES":69generateImages();70break;71case "TEST_SPLASH":72int index = Integer.parseInt(args[1]);73testSplash(tests[index]);74break;75case "TEST_FOCUS":76testFocus();77break;78default:79throw new RuntimeException("Unknown test: " + test);80}81}8283static void testSplash(ImageInfo test) throws Exception {84SplashScreen splashScreen = SplashScreen.getSplashScreen();8586if (splashScreen == null) {87throw new RuntimeException("Splash screen is not shown!");88}8990Graphics2D g = splashScreen.createGraphics();91Rectangle splashBounds = splashScreen.getBounds();92int screenX = (int) splashBounds.getCenterX();93int screenY = (int) splashBounds.getCenterY();9495if(splashBounds.width != IMAGE_WIDTH){96throw new RuntimeException(97"SplashScreen#getBounds has wrong width");98}99if(splashBounds.height != IMAGE_HEIGHT){100throw new RuntimeException(101"SplashScreen#getBounds has wrong height");102}103104Robot robot = new Robot();105Color splashScreenColor = robot.getPixelColor(screenX, screenY);106107float scaleFactor = getScaleFactor();108Color testColor = (1 < scaleFactor) ? test.color2x : test.color1x;109110if (!compare(testColor, splashScreenColor)) {111throw new RuntimeException(112"Image with wrong resolution is used for splash screen!");113}114}115116static void testFocus() throws Exception {117118System.out.println("Focus Test!");119Robot robot = new Robot();120robot.setAutoDelay(50);121122Frame frame = new Frame();123frame.setSize(100, 100);124String test = "123";125TextField textField = new TextField(test);126textField.selectAll();127frame.add(textField);128frame.setVisible(true);129robot.waitForIdle();130131robot.keyPress(KeyEvent.VK_A);132robot.keyRelease(KeyEvent.VK_A);133robot.keyPress(KeyEvent.VK_B);134robot.keyRelease(KeyEvent.VK_B);135robot.waitForIdle();136137frame.dispose();138139if(!textField.getText().equals("ab")){140throw new RuntimeException("Focus is lost!");141}142}143144static boolean compare(Color c1, Color c2){145return compare(c1.getRed(), c2.getRed())146&& compare(c1.getGreen(), c2.getGreen())147&& compare(c1.getBlue(), c2.getBlue());148}149150static boolean compare(int n, int m){151return Math.abs(n - m) <= 50;152}153154static float getScaleFactor() {155156final Dialog dialog = new Dialog((Window) null);157dialog.setSize(100, 100);158dialog.setModal(true);159final float[] scaleFactors = new float[1];160Panel panel = new Panel() {161162@Override163public void paint(Graphics g) {164float scaleFactor = 1;165if (g instanceof SunGraphics2D) {166scaleFactor = ((SunGraphics2D) g).surfaceData.getDefaultScale();167}168scaleFactors[0] = scaleFactor;169dialog.setVisible(false);170}171};172173dialog.add(panel);174dialog.setVisible(true);175dialog.dispose();176177return scaleFactors[0];178}179180static void generateImages() throws Exception {181for (ImageInfo test : tests) {182generateImage(test.name1x, test.color1x, 1);183generateImage(test.name2x, test.color2x, 2);184}185}186187static void generateImage(String name, Color color, int scale) throws Exception {188File file = new File(name);189if (file.exists()) {190return;191}192BufferedImage image = new BufferedImage(scale * IMAGE_WIDTH, scale * IMAGE_HEIGHT,193BufferedImage.TYPE_INT_RGB);194Graphics g = image.getGraphics();195g.setColor(color);196g.fillRect(0, 0, scale * IMAGE_WIDTH, scale * IMAGE_HEIGHT);197ImageIO.write(image, "png", file);198}199200static class ImageInfo {201202final String name1x;203final String name2x;204final Color color1x;205final Color color2x;206207public ImageInfo(String name1x, String name2x, Color color1x, Color color2x) {208this.name1x = name1x;209this.name2x = name2x;210this.color1x = color1x;211this.color2x = color2x;212}213}214}215216217