--- /dev/null 2016-02-29 12:51:35.719143579 +0300 +++ new/test/java/awt/image/multiresolution/MultiresolutionIconTest.java 2016-03-03 17:38:25.590730320 +0300 @@ -0,0 +1,175 @@ +/* + * Copyright (c) 2016, 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 8150724 + * @author a.stepanov + * @summary Check that correct resolution variants are chosen for icons + * when multiresolution image is used for their construction. + * + * @library ../../../../lib/testlibrary/ + * @build ExtendedRobot + * @run main/othervm -Dsun.java2d.uiScale=1 MultiresolutionIconTest + * @run main/othervm -Dsun.java2d.uiScale=2 MultiresolutionIconTest + */ + +import java.awt.*; +import java.awt.geom.AffineTransform; +import java.awt.image.BaseMultiResolutionImage; +import java.awt.image.BufferedImage; +import javax.swing.*; + +public class MultiresolutionIconTest extends JFrame { + + private final static int SZ = 100; + private final static int N = 5; // number of components + + private final static String SCALE = "sun.java2d.uiScale"; + private final static Color C1X = Color.RED; + private final static Color C2X = Color.BLUE; + + private JLabel lbl; + private JTabbedPane tabbedPane; + + private final ExtendedRobot r; + + private static BufferedImage generateImage(int scale, Color c) { + + int x = (SZ / 2) * scale; + BufferedImage img = new BufferedImage(x, x, BufferedImage.TYPE_INT_RGB); + Graphics g = img.getGraphics(); + g.setColor(c); + g.fillRect(0, 0, x, x); + return img; + } + + public MultiresolutionIconTest() throws Exception { + + r = new ExtendedRobot(); + SwingUtilities.invokeAndWait(this::UI); + } + + private void UI() { + + setUndecorated(true); + + BufferedImage img1x = generateImage(1, C1X); + BufferedImage img2x = generateImage(2, C2X); + BaseMultiResolutionImage mri = new BaseMultiResolutionImage( + new BufferedImage[]{img1x, img2x}); + Icon icon = new ImageIcon(mri); + + setSize(N * SZ, SZ); + setLocation(50, 50); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + getContentPane().setLayout(new GridLayout(1, 1)); + + JPanel p = new JPanel(); + p.setLayout(new GridLayout(1, N)); + + JButton btn = new JButton(icon); + p.add(btn); + + JToggleButton tbn = new JToggleButton(icon); + p.add(tbn); + + JRadioButton rbn = new JRadioButton(icon); + rbn.setHorizontalAlignment(SwingConstants.CENTER); + p.add(rbn); + + JCheckBox cbx = new JCheckBox(icon); + cbx.setHorizontalAlignment(SwingConstants.CENTER); + p.add(cbx); + + lbl = new JLabel(icon); + p.add(lbl); + + tabbedPane = new JTabbedPane(JTabbedPane.LEFT); + tabbedPane.addTab("", p); + tabbedPane.setTabComponentAt(0, new JLabel(icon)); + getContentPane().add(tabbedPane); + + setVisible(true); + } + + private static boolean is2x() { + + AffineTransform tr = GraphicsEnvironment.getLocalGraphicsEnvironment(). + getDefaultScreenDevice().getDefaultConfiguration(). + getDefaultTransform(); + return (Math.min(tr.getScaleX(), tr.getScaleY()) > 1.001); + } + + private void doTest() { + + r.waitForIdle(2000); + String scale = System.getProperty(SCALE); + System.out.println("scale = " + scale); + Color expected = is2x() ? C2X : C1X; + + Point p = lbl.getLocationOnScreen(); + int x = p.x + lbl.getWidth() / 2; + int y = p.y + lbl.getHeight() / 2; + int w = lbl.getWidth(); + + boolean ok = true; + Color c; + String components[] = new String[]{ + "JLabel", "JCheckBox", "JRadioButton", "JToggleButton", "JButton"}; + for (int i = 0; i < N; ++i) { + c = r.getPixelColor(x - i * w, y); + System.out.print(components[i] + " icon: "); + if (c.equals(expected)) { + System.out.println("ok"); + } else { + System.out.println("nok"); + ok = false; + } + r.waitForIdle(); + } + + Component comp = tabbedPane.getTabComponentAt(0); + p = comp.getLocationOnScreen(); + c = r.getPixelColor(p.x + comp.getWidth() / 2, p.y + comp.getHeight()/ 2); + if (c.equals(expected)) { + System.out.println("JTabbedPane label icon: ok"); + } else { + System.out.println("JTabbedPane label icon: nok"); + ok = false; + } + + if (!ok) { throw new RuntimeException("test failed"); } + + r.waitForIdle(); + dispose(); + } + + public static void main(String[] args) throws Exception { + + // TODO: remove is2x() check after JDK-8150844 fix + if (is2x() == "2".equals(System.getProperty(SCALE))) { + (new MultiresolutionIconTest()).doTest(); + } + } +}