HiDPIPropertiesUnixTest.java revision 14851:980da45565c8
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
24import java.awt.Dialog;
25import java.awt.Frame;
26import java.awt.Graphics;
27import java.awt.Graphics2D;
28import java.awt.geom.AffineTransform;
29import javax.swing.UIManager;
30
31/*
32 * @test
33 * @key headful
34 * @bug 8137571
35 * @summary Linux HiDPI Graphics support
36 * @author Alexander Scherbatiy
37 * @requires (os.family == "linux" | os.family == "mac")
38 * @run main/othervm -Dsun.java2d.uiScale.enabled=false
39 *                   -Dsun.java2d.uiScale=2
40 *                    HiDPIPropertiesUnixTest UISCALE_DISABLED
41 * @run main/othervm -Dsun.java2d.uiScale.enabled=true
42 *                   -Dsun.java2d.uiScale=3
43 *                    HiDPIPropertiesUnixTest UISCALE_3
44 * @run main/othervm -Dsun.java2d.uiScale=4
45 *                    HiDPIPropertiesUnixTest UISCALE_4
46 */
47public class HiDPIPropertiesUnixTest {
48
49    public static void main(String[] args) throws Exception {
50
51        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
52
53        String testCase = args[0];
54        switch (testCase) {
55            case "UISCALE_DISABLED":
56                testScale(1.0, 1.0);
57                break;
58            case "UISCALE_3":
59                testScale(3.0, 3.0);
60                break;
61            case "UISCALE_4":
62                testScale(4.0, 4.0);
63                break;
64            default:
65                throw new RuntimeException("Unknown test case: " + testCase);
66        }
67    }
68
69    private static void testScale(double scaleX, double scaleY) {
70
71        Dialog dialog = new Dialog((Frame) null, true) {
72
73            @Override
74            public void paint(Graphics g) {
75                super.paint(g);
76                AffineTransform tx = ((Graphics2D) g).getTransform();
77                dispose();
78                if (scaleX != tx.getScaleX() || scaleY != tx.getScaleY()) {
79                    throw new RuntimeException(String.format("Wrong scale:"
80                            + "[%f, %f] instead of [%f, %f].",
81                            tx.getScaleX(), tx.getScaleY(), scaleX, scaleY));
82                }
83            }
84        };
85        dialog.setSize(200, 300);
86        dialog.setVisible(true);
87    }
88}
89