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