1 /*
   2  * Copyright (c) 2015, 2017, 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.BorderLayout;
  25 import java.awt.Canvas;
  26 import java.awt.Color;
  27 import java.awt.Frame;
  28 import java.awt.Graphics;
  29 import java.awt.Panel;
  30 import java.awt.Rectangle;
  31 import java.awt.Robot;
  32 import java.awt.image.BufferedImage;
  33 
  34 import jdk.testlibrary.OSInfo;
  35 
  36 /* @test
  37  * @bug 8073320
  38  * @summary  Windows HiDPI support
  39  * @library ../../../../lib/testlibrary
  40  * @build jdk.testlibrary.OSInfo
  41  * @run main/othervm HiDPIRobotScreenCaptureTest
  42  * @run main/othervm -Dsun.java2d.uiScale=1 HiDPIRobotScreenCaptureTest
  43  * @run main/othervm -Dsun.java2d.uiScale=2 HiDPIRobotScreenCaptureTest
  44  * @run main/othervm -Dsun.java2d.win.uiScaleX=3 -Dsun.java2d.win.uiScaleY=2
  45  *                    HiDPIRobotScreenCaptureTest
  46  * @run main/othervm -Dsun.java2d.win.uiScaleX=2 -Dsun.java2d.win.uiScaleY=3
  47  *                    HiDPIRobotScreenCaptureTest
  48  */
  49 public class HiDPIRobotScreenCaptureTest {
  50 
  51     private static final Color[] COLORS = {
  52         Color.GREEN, Color.BLUE, Color.ORANGE, Color.RED};
  53 
  54     public static void main(String[] args) throws Exception {
  55         if (!OSInfo.getOSType().equals(OSInfo.OSType.WINDOWS)
  56                 && System.getProperty("sun.java2d.win.uiScaleX") != null) {
  57             return;
  58         }
  59 
  60         Frame frame = new Frame();
  61         frame.setBounds(97, 87, 101, 123);
  62         frame.setUndecorated(true);
  63 
  64         Panel panel = new Panel(new BorderLayout());
  65         Canvas canvas = new Canvas() {
  66             @Override
  67             public void paint(Graphics g) {
  68                 super.paint(g);
  69                 int w = getWidth();
  70                 int h = getHeight();
  71                 g.setColor(COLORS[0]);
  72                 g.fillRect(0, 0, w / 2, h / 2);
  73                 g.setColor(COLORS[1]);
  74                 g.fillRect(w / 2, 0, w / 2, h / 2);
  75                 g.setColor(COLORS[2]);
  76                 g.fillRect(0, h / 2, w / 2, h / 2);
  77                 g.setColor(COLORS[3]);
  78                 g.fillRect(w / 2, h / 2, w / 2, h / 2);
  79             }
  80         };
  81 
  82         panel.add(canvas);
  83         frame.add(panel);
  84         frame.setVisible(true);
  85         Robot robot = new Robot();
  86         robot.waitForIdle();
  87         Thread.sleep(200);
  88 
  89         Rectangle rect = canvas.getBounds();
  90         rect.setLocation(canvas.getLocationOnScreen());
  91 
  92         BufferedImage image = robot.createScreenCapture(rect);
  93         frame.dispose();
  94 
  95         int w = image.getWidth();
  96         int h = image.getHeight();
  97 
  98         if (w != frame.getWidth() || h != frame.getHeight()) {
  99             throw new RuntimeException("Wrong image size!");
 100         }
 101 
 102         if (image.getRGB(w / 4, h / 4) != COLORS[0].getRGB()) {
 103             throw new RuntimeException("Wrong image color!");
 104         }
 105 
 106         if (image.getRGB(3 * w / 4, h / 4) != COLORS[1].getRGB()) {
 107             throw new RuntimeException("Wrong image color!");
 108         }
 109 
 110         if (image.getRGB(w / 4, 3 * h / 4) != COLORS[2].getRGB()) {
 111             throw new RuntimeException("Wrong image color!");
 112         }
 113 
 114         if (image.getRGB(3 * w / 4, 3 * h / 4) != COLORS[3].getRGB()) {
 115             throw new RuntimeException("Wrong image color!");
 116         }
 117     }
 118 }