--- /dev/null 2017-09-05 07:53:18.071706981 -0700 +++ new/test/java/awt/image/BaseMultiResolutionImage/MultiResolutionVariantSelectionTest.java 2017-09-06 19:20:21.993891020 -0700 @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + @test + @bug 8148619 + @summary Select the closest resolution variant in BaseMultiResolutionImage + @run main/othervm -Dsun.java2d.uiScale=1 MultiResolutionVariantSelectionTest + @run main/othervm -Dsun.java2d.uiScale=2 MultiResolutionVariantSelectionTest + @run main/othervm -Dsun.java2d.uiScale=3 MultiResolutionVariantSelectionTest + @run main/othervm -Dsun.java2d.uiScale=4 MultiResolutionVariantSelectionTest +*/ + +import java.awt.*; +import java.awt.image.*; + +public class MultiResolutionVariantSelectionTest { + + static Color[] colors = {Color.RED, Color.GREEN, Color.BLUE}; + static int W = 100, H = 100; + + public static void main(String[] args) throws Exception { + Image[] images = new Image[colors.length]; + for (int i = colors.length - 1; i >= 0; i--) { + images[i] = generateImage(i + 1, colors[i]); + } + Image mrImage =new BaseMultiResolutionImage(images); + + Frame frame = new Frame(); + Panel panel = new Panel() { + @Override + public void paint(Graphics g) { + super.paint(g); + g.drawImage(mrImage, 0, 0, W, H, null); + } + }; + panel.setPreferredSize(new Dimension(W, H)); + frame.add(panel); + frame.pack(); + frame.setVisible(true); + + Robot robot = new Robot(); + robot.delay(200); + robot.waitForIdle(); + + Point p = panel.getLocationOnScreen(); + p.translate(W / 2, H / 2); + Color color = robot.getPixelColor(p.x, p.y); + int expected = Math.min( + Integer.parseInt(System.getProperty("sun.java2d.uiScale")), + colors.length) - 1; + if (!color.equals(colors[expected])) { + throw new RuntimeException("Wrong image variant painted " + color); + } + } + + private static BufferedImage generateImage(int scale, Color c) { + BufferedImage image = new BufferedImage( + scale * W, scale * H, BufferedImage.TYPE_INT_RGB); + Graphics g = image.getGraphics(); + g.setColor(c); + g.fillRect(0, 0, scale * W + 1, scale * H + 1); + g.dispose(); + return image; + } +}