test/java/awt/SplashScreen/MultiResolutionSplash/MultiResolutionSplashTest.java

Print this page




   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.Graphics;
  27 import java.awt.Graphics2D;
  28 import java.awt.Panel;
  29 import java.awt.Rectangle;
  30 import java.awt.Robot;
  31 import java.awt.SplashScreen;

  32 import java.awt.Window;

  33 import java.awt.image.BufferedImage;
  34 import java.io.File;
  35 import javax.imageio.ImageIO;
  36 import sun.java2d.SunGraphics2D;
  37 
  38 /**
  39  * test
  40  * @bug 8043869
  41  * @author Alexander Scherbatiy
  42  * @summary [macosx] java -splash does not honor 2x hi dpi notation for retina
  43  * support
  44  * @run main MultiResolutionSplashTest GENERATE_IMAGES
  45  * @run main/othervm -splash:splash1.png MultiResolutionSplashTest TEST_SPLASH 0
  46  * @run main/othervm -splash:splash2 MultiResolutionSplashTest TEST_SPLASH 1
  47  * @run main/othervm -splash:splash3. MultiResolutionSplashTest TEST_SPLASH 2

  48  */
  49 public class MultiResolutionSplashTest {
  50 
  51     private static final int IMAGE_WIDTH = 300;
  52     private static final int IMAGE_HEIGHT = 200;
  53 
  54     private static final ImageInfo[] tests = {
  55         new ImageInfo("splash1.png", "splash1@2x.png", Color.BLUE, Color.GREEN),
  56         new ImageInfo("splash2", "splash2@2x", Color.WHITE, Color.BLACK),
  57         new ImageInfo("splash3.", "splash3@2x.", Color.YELLOW, Color.RED)
  58     };
  59 
  60     public static void main(String[] args) throws Exception {
  61 
  62         String test = args[0];
  63 
  64         switch (test) {
  65             case "GENERATE_IMAGES":
  66                 generateImages();
  67                 break;
  68             case "TEST_SPLASH":
  69                 int index = Integer.parseInt(args[1]);
  70                 testSplash(tests[index]);
  71                 break;



  72             default:
  73                 throw new RuntimeException("Unknown test: " + test);
  74         }
  75     }
  76 
  77     static void testSplash(ImageInfo test) throws Exception {
  78         SplashScreen splashScreen = SplashScreen.getSplashScreen();
  79 
  80         if (splashScreen == null) {
  81             throw new RuntimeException("Splash screen is not shown!");
  82         }
  83 
  84         Graphics2D g = splashScreen.createGraphics();
  85         Rectangle splashBounds = splashScreen.getBounds();
  86         int screenX = (int) splashBounds.getCenterX();
  87         int screenY = (int) splashBounds.getCenterY();
  88 
  89         Robot robot = new Robot();
  90         Color splashScreenColor = robot.getPixelColor(screenX, screenY);
  91 
  92         float scaleFactor = getScaleFactor();
  93         Color testColor = (1 < scaleFactor) ? test.color2x : test.color1x;
  94 
  95         if (!testColor.equals(splashScreenColor)) {
  96             throw new RuntimeException(
  97                     "Image with wrong resolution is used for splash screen!");
  98         }
  99     }
 100 




































 101     static float getScaleFactor() {
 102 
 103         final Dialog dialog = new Dialog((Window) null);
 104         dialog.setSize(100, 100);
 105         dialog.setModal(true);
 106         final float[] scaleFactors = new float[1];
 107         Panel panel = new Panel() {
 108 
 109             @Override
 110             public void paint(Graphics g) {
 111                 float scaleFactor = 1;
 112                 if (g instanceof SunGraphics2D) {
 113                     scaleFactor = ((SunGraphics2D) g).surfaceData.getDefaultScale();
 114                 }
 115                 scaleFactors[0] = scaleFactor;
 116                 dialog.setVisible(false);
 117             }
 118         };
 119 
 120         dialog.add(panel);




   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
  44  * @author Alexander Scherbatiy
  45  * @summary [macosx] java -splash does not honor 2x hi dpi notation for retina
  46  * support
  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[] tests = {
  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 
  64     public static void main(String[] args) throws Exception {
  65 
  66         String test = args[0];
  67 
  68         switch (test) {
  69             case "GENERATE_IMAGES":
  70                 generateImages();
  71                 break;
  72             case "TEST_SPLASH":
  73                 int index = Integer.parseInt(args[1]);
  74                 testSplash(tests[index]);
  75                 break;
  76             case "TEST_FOCUS":
  77                 testFocus();
  78                 break;
  79             default:
  80                 throw new RuntimeException("Unknown test: " + test);
  81         }
  82     }
  83 
  84     static void testSplash(ImageInfo test) throws Exception {
  85         SplashScreen splashScreen = SplashScreen.getSplashScreen();
  86 
  87         if (splashScreen == null) {
  88             throw new RuntimeException("Splash screen is not shown!");
  89         }
  90 
  91         Graphics2D g = splashScreen.createGraphics();
  92         Rectangle splashBounds = splashScreen.getBounds();
  93         int screenX = (int) splashBounds.getCenterX();
  94         int screenY = (int) splashBounds.getCenterY();
  95 
  96         Robot robot = new Robot();
  97         Color splashScreenColor = robot.getPixelColor(screenX, screenY);
  98 
  99         float scaleFactor = getScaleFactor();
 100         Color testColor = (1 < scaleFactor) ? test.color2x : test.color1x;
 101 
 102         if (!compare(testColor, splashScreenColor)) {
 103             throw new RuntimeException(
 104                     "Image with wrong resolution is used for splash screen!");
 105         }
 106     }
 107 
 108     static void testFocus() throws Exception {
 109 
 110         System.out.println("Focus Test!");
 111         Robot robot = new Robot();
 112         robot.setAutoDelay(50);
 113 
 114         Frame frame = new Frame();
 115         frame.setSize(100, 100);
 116         String test = "123";
 117         TextField textField = new TextField(test);
 118         frame.add(textField);
 119         frame.setVisible(true);
 120 
 121         robot.keyPress(KeyEvent.VK_A);
 122         robot.keyRelease(KeyEvent.VK_A);
 123         robot.keyPress(KeyEvent.VK_B);
 124         robot.keyRelease(KeyEvent.VK_B);
 125         robot.waitForIdle();
 126 
 127         frame.dispose();
 128 
 129         if(!textField.getText().equals("ab")){
 130             throw new RuntimeException("Focus is lost!");
 131         }
 132     }
 133 
 134     static boolean compare(Color c1, Color c2){
 135         return compare(c1.getRed(), c2.getRed())
 136                 && compare(c1.getGreen(), c2.getGreen())
 137                 && compare(c1.getBlue(), c2.getBlue());
 138     }
 139 
 140     static boolean compare(int n, int m){
 141         return Math.abs(n - m) <= 50;
 142     }
 143 
 144     static float getScaleFactor() {
 145 
 146         final Dialog dialog = new Dialog((Window) null);
 147         dialog.setSize(100, 100);
 148         dialog.setModal(true);
 149         final float[] scaleFactors = new float[1];
 150         Panel panel = new Panel() {
 151 
 152             @Override
 153             public void paint(Graphics g) {
 154                 float scaleFactor = 1;
 155                 if (g instanceof SunGraphics2D) {
 156                     scaleFactor = ((SunGraphics2D) g).surfaceData.getDefaultScale();
 157                 }
 158                 scaleFactors[0] = scaleFactor;
 159                 dialog.setVisible(false);
 160             }
 161         };
 162 
 163         dialog.add(panel);