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.Dialog; 25 import java.awt.Dimension; 26 import java.awt.Frame; 27 import java.awt.Window; 28 import java.util.List; 29 import java.util.concurrent.TimeUnit; 30 31 import jdk.test.lib.Platform; 32 import jdk.test.lib.process.OutputAnalyzer; 33 import jdk.test.lib.process.ProcessTools; 34 35 /** 36 * @test 37 * @bug 8221823 38 * @key headful 39 * @summary Verifies TOP level component's minimumSize using different DPI 40 * @library /test/lib 41 * @run main/othervm -Dsun.java2d.uiScale=1 MinimumSizeDPIVariation 42 */ 43 public final class MinimumSizeDPIVariation { 44 45 public static void main(String[] args) throws Exception { 46 if (args.length == 0) { 47 Dimension minimumSize = test(new Frame()); 48 checkAllDPI("frame", minimumSize.width, minimumSize.height); 49 minimumSize = test(new Window(null)); 50 checkAllDPI("window", minimumSize.width, minimumSize.height); 51 minimumSize = test(new Dialog((Frame) null)); 52 checkAllDPI("dialog", minimumSize.width, minimumSize.height); 53 } else { 54 String comp = args[0]; 55 int w = Integer.parseInt(args[1]); 56 int h = Integer.parseInt(args[2]); 57 double scale = Double.parseDouble(args[3]); 58 59 System.err.println("comp = " + comp); 60 System.err.println("scale = " + scale); 61 62 Dimension minimumSize = switch (comp) { 63 case "frame" -> test(new Frame()); 64 case "window" -> test(new Window(null)); 65 case "dialog" -> test(new Dialog((Frame) null)); 66 default -> throw new java.lang.IllegalStateException( 67 "Unexpected value: " + comp); 68 }; 69 check(minimumSize.width, Math.max(w / scale, 1)); 70 check(minimumSize.height, Math.max(h / scale, 1)); 71 } 72 } 73 74 private static Dimension test(Window window) { 75 try { 76 window.setLayout(null); // trigger use the minimum size of the peer 77 window.setSize(new Dimension(1, 1)); 78 window.pack(); 79 Dimension minimumSize = window.getMinimumSize(); 80 Dimension size = window.getSize(); 81 if (!minimumSize.equals(size)) { 82 System.err.println(window); 83 System.err.println("Expected: " + minimumSize); 84 System.err.println("Actual: " + size); 85 throw new RuntimeException("Wrong size"); 86 } 87 return minimumSize; 88 } finally { 89 window.dispose(); 90 } 91 } 92 93 private static void check(int actual, double expected) { 94 double i = 100 * (actual - expected) / expected; 95 if (Math.abs(i) > 10) { // no more than 10% variation 96 System.err.println("Expected: " + expected); 97 System.err.println("Actual: " + actual); 98 throw new RuntimeException("Difference is too big: " + i); 99 } 100 } 101 102 private static void checkAllDPI(String comp, int w, int h) 103 throws Exception { 104 if (!Platform.isOSX()) { 105 for (String dpi : List.of("1.5", "1.75", "2", "2.5")) { 106 runPocess(dpi, comp, w, h); 107 } 108 } 109 } 110 111 private static void runPocess(String dpi, String comp, int w, int h) 112 throws Exception { 113 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder( 114 "-Dsun.java2d.uiScale=" + dpi, 115 MinimumSizeDPIVariation.class.getSimpleName(), comp, 116 String.valueOf(w), String.valueOf(h), dpi); 117 Process worker = ProcessTools.startProcess("Worker", pb, null, 20, 118 TimeUnit.SECONDS); 119 new OutputAnalyzer(worker).shouldHaveExitValue(0); 120 } 121 }