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