1 /* 2 * Copyright (c) 2020, 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.GraphicsDevice; 25 import java.awt.GraphicsEnvironment; 26 import java.awt.Insets; 27 import java.awt.Toolkit; 28 import java.util.List; 29 import java.util.concurrent.TimeUnit; 30 31 import jdk.test.lib.process.OutputAnalyzer; 32 import jdk.test.lib.process.ProcessTools; 33 34 /** 35 * @test 36 * @bug 8243925 37 * @key headful 38 * @requires (os.family == "windows") 39 * @summary Verifies Toolkit.getScreenInsets using different DPI 40 * @library /test/lib 41 * @run main/othervm -Dsun.java2d.uiScale=1 ScreenInsetsDPIVariation 42 */ 43 public final class ScreenInsetsDPIVariation { 44 main(String[] args)45 public static void main(String[] args) throws Exception { 46 var ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 47 GraphicsDevice[] screenDevices = ge.getScreenDevices(); 48 if (args.length == 0) { 49 for (int i = 0; i < screenDevices.length; i++) { 50 var gd = screenDevices[i]; 51 var gc = gd.getDefaultConfiguration(); 52 Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc); 53 checkAllDPI(i, insets); 54 } 55 } else { 56 int screen = Integer.parseInt(args[0]); 57 int left = Integer.parseInt(args[1]); 58 int right = Integer.parseInt(args[2]); 59 int top = Integer.parseInt(args[3]); 60 int bottom = Integer.parseInt(args[4]); 61 double scale = Double.parseDouble(args[5]); 62 63 System.err.println("screen = " + screen); 64 System.err.println("scale = " + scale); 65 if (screen >= screenDevices.length) { 66 return; // devices were changed, skip 67 } 68 var gc = screenDevices[screen].getDefaultConfiguration(); 69 Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc); 70 check(insets.left, left / scale); 71 check(insets.right, right / scale); 72 check(insets.top, top / scale); 73 check(insets.bottom, bottom / scale); 74 } 75 } 76 check(int actual, double expected)77 private static void check(int actual, double expected) { 78 if (actual != clipRound(expected)) { 79 System.err.println("Expected: " + expected); 80 System.err.println("Actual: " + actual); 81 throw new RuntimeException("Wrong size"); 82 } 83 } 84 checkAllDPI(int screen, Insets insets)85 private static void checkAllDPI(int screen, Insets insets) 86 throws Exception { 87 for (String dpi : List.of("1", "1.5", "1.75", "2", "2.5", "3", "3.1")) { 88 runProcess(dpi, screen, insets); 89 } 90 } 91 clipRound(double coordinate)92 public static int clipRound(double coordinate) { 93 double newv = coordinate - 0.5; 94 if (newv < Integer.MIN_VALUE) { 95 return Integer.MIN_VALUE; 96 } 97 if (newv > Integer.MAX_VALUE) { 98 return Integer.MAX_VALUE; 99 } 100 return (int) Math.ceil(newv); 101 } 102 runProcess(String dpi, int screen, Insets insets)103 private static void runProcess(String dpi, int screen, Insets insets) 104 throws Exception { 105 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( 106 "-Dsun.java2d.uiScale=" + dpi, 107 ScreenInsetsDPIVariation.class.getSimpleName(), 108 String.valueOf(screen), String.valueOf(insets.left), 109 String.valueOf(insets.right), String.valueOf(insets.top), 110 String.valueOf(insets.bottom), dpi); 111 Process worker = ProcessTools.startProcess("Worker", pb, null, 20, 112 TimeUnit.SECONDS); 113 new OutputAnalyzer(worker).shouldHaveExitValue(0); 114 } 115 } 116