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