1 /*
   2  * Copyright (c) 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.Dialog;
  25 import java.awt.Frame;
  26 import java.awt.Graphics;
  27 import java.awt.Graphics2D;
  28 import java.awt.geom.AffineTransform;
  29 import javax.swing.UIManager;
  30 
  31 /* @test
  32  * @bug 8073320
  33  * @summary  Windows HiDPI support
  34  * @author Alexander Scherbatiy
  35  * @requires (os.family == "windows")
  36  * @run main/othervm -Dsun.java2d.uiScale.enabled=false
  37  *                   -Dsun.java2d.win.uiScaleX=3 -Dsun.java2d.win.uiScaleY=2
  38  *                    HiDPIPropertiesTest UISCALE_DISABLED
  39  * @run main/othervm -Dsun.java2d.uiScale.enabled=false
  40  *                   -Dsun.java2d.win.uiScale=3
  41  *                    HiDPIPropertiesTest UISCALE_DISABLED
  42  * @run main/othervm -Dsun.java2d.uiScale.enabled=false
  43  *                   -Dsun.java2d.win.uiScale=3
  44  *                   -Dsun.java2d.win.uiScaleX=5 -Dsun.java2d.win.uiScaleY=6
  45  *                    HiDPIPropertiesTest UISCALE_DISABLED
  46  * @run main/othervm -Dsun.java2d.uiScale.enabled=true
  47  *                   -Dsun.java2d.win.uiScale=3
  48  *                    HiDPIPropertiesTest UISCALE_3
  49  * @run main/othervm -Dsun.java2d.uiScale.enabled=true
  50  *                   -Dsun.java2d.win.uiScale=4
  51  *                   -Dsun.java2d.win.uiScaleX=2 -Dsun.java2d.win.uiScaleY=3
  52  *                    HiDPIPropertiesTest UISCALE_2X3
  53  * @run main/othervm -Dsun.java2d.uiScale.enabled=true
  54  *                   -Dsun.java2d.win.uiScaleX=3 -Dsun.java2d.win.uiScaleY=2
  55  *                    HiDPIPropertiesTest UISCALE_3X2
  56  * @run main/othervm -Dsun.java2d.win.uiScale=4
  57  *                    HiDPIPropertiesTest UISCALE_4
  58  * @run main/othervm -Dsun.java2d.win.uiScale=4
  59  *                   -Dsun.java2d.win.uiScaleX=2 -Dsun.java2d.win.uiScaleY=3
  60  *                    HiDPIPropertiesTest UISCALE_2X3
  61  * @run main/othervm -Dsun.java2d.win.uiScaleX=4 -Dsun.java2d.win.uiScaleY=5
  62  *                    HiDPIPropertiesTest UISCALE_4X5
  63  * @run main/othervm -Dsun.java2d.win.uiScale=3
  64  *                   -Dsun.java2d.win.uiScaleX=0 -Dsun.java2d.win.uiScaleY=0
  65  *                    HiDPIPropertiesTest UISCALE_3
  66  * @run main/othervm -Dsun.java2d.win.uiScale=4
  67  *                   -Dsun.java2d.win.uiScaleX=-7 -Dsun.java2d.win.uiScaleY=-8
  68  *                    HiDPIPropertiesTest UISCALE_4
  69  * @run main/othervm -Dsun.java2d.win.uiScale=384dpi
  70  *                    HiDPIPropertiesTest UISCALE_4
  71  * @run main/othervm -Dsun.java2d.win.uiScale=300%
  72  *                    HiDPIPropertiesTest UISCALE_3
  73  * @run main/othervm -Dsun.java2d.win.uiScaleX=400% -Dsun.java2d.win.uiScaleY=500%
  74  *                    HiDPIPropertiesTest UISCALE_4X5
  75  * @run main/othervm -Dsun.java2d.win.uiScaleX=288dpi -Dsun.java2d.win.uiScaleY=192dpi
  76  *                    HiDPIPropertiesTest UISCALE_3X2
  77  * @run main/othervm -Dsun.java2d.win.uiScaleX=200% -Dsun.java2d.win.uiScaleY=288dpi
  78  *                    HiDPIPropertiesTest UISCALE_2X3
  79  */
  80 public class HiDPIPropertiesTest {
  81 
  82     public static void main(String[] args) throws Exception {
  83 
  84         try {
  85             UIManager.setLookAndFeel(
  86                     "com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  87         } catch (Exception e) {
  88             return;
  89         }
  90 
  91         String testCase = args[0];
  92         switch (testCase) {
  93             case "UISCALE_DISABLED":
  94                 testScale(1.0, 1.0);
  95                 break;
  96             case "UISCALE_3":
  97                 testScale(3.0, 3.0);
  98                 break;
  99             case "UISCALE_4":
 100                 testScale(4.0, 4.0);
 101                 break;
 102             case "UISCALE_2X3":
 103                 testScale(2.0, 3.0);
 104                 break;
 105             case "UISCALE_3X2":
 106                 testScale(3.0, 2.0);
 107                 break;
 108             case "UISCALE_4X5":
 109                 testScale(4.0, 5.0);
 110                 break;
 111             default:
 112                 throw new RuntimeException("Unknown test case: " + testCase);
 113         }
 114     }
 115 
 116     private static void testScale(double scaleX, double scaleY) {
 117 
 118         Dialog dialog = new Dialog((Frame) null, true) {
 119 
 120             @Override
 121             public void paint(Graphics g) {
 122                 super.paint(g);
 123                 AffineTransform tx = ((Graphics2D) g).getTransform();
 124                 dispose();
 125                 if (scaleX != tx.getScaleX() || scaleY != tx.getScaleY()) {
 126                     throw new RuntimeException(String.format("Wrong scale:"
 127                             + "[%f, %f] instead of [%f, %f].",
 128                             tx.getScaleX(), tx.getScaleY(), scaleX, scaleY));
 129                 }
 130             }
 131         };
 132         dialog.setSize(200, 300);
 133         dialog.setVisible(true);
 134     }
 135 }