1 /*
   2  * Copyright (c) 2016, 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 /*
  25  * @test
  26  * @bug 8150724
  27  * @author a.stepanov
  28  * @summary Check that correct resolution variants are chosen for icons
  29  *          when multiresolution image is used for their construction.
  30  *
  31  * @library ../../../../lib/testlibrary/
  32  * @build ExtendedRobot
  33  * @run main/othervm -Dsun.java2d.uiScale=1 MultiresolutionIconTest
  34  * @run main/othervm -Dsun.java2d.uiScale=2 MultiresolutionIconTest
  35  */
  36 
  37 import java.awt.*;
  38 import java.awt.geom.AffineTransform;
  39 import java.awt.image.BaseMultiResolutionImage;
  40 import java.awt.image.BufferedImage;
  41 import javax.swing.*;
  42 
  43 public class MultiresolutionIconTest extends JFrame {
  44 
  45     private final static int SZ = 100;
  46     private final static int N = 5; // number of components
  47 
  48     private final static String SCALE = "sun.java2d.uiScale";
  49     private final static Color C1X = Color.RED;
  50     private final static Color C2X = Color.BLUE;
  51 
  52     private JLabel lbl;
  53     private JTabbedPane tabbedPane;
  54 
  55     private final ExtendedRobot r;
  56 
  57     private static BufferedImage generateImage(int scale, Color c) {
  58 
  59         int x = (SZ / 2) * scale;
  60         BufferedImage img = new BufferedImage(x, x, BufferedImage.TYPE_INT_RGB);
  61         Graphics g = img.getGraphics();
  62         g.setColor(c);
  63         g.fillRect(0, 0, x, x);
  64         return img;
  65     }
  66 
  67     public MultiresolutionIconTest() throws Exception {
  68 
  69         r = new ExtendedRobot();
  70         SwingUtilities.invokeAndWait(this::UI);
  71     }
  72 
  73     private void UI() {
  74 
  75         setUndecorated(true);
  76 
  77         BufferedImage img1x = generateImage(1, C1X);
  78         BufferedImage img2x = generateImage(2, C2X);
  79         BaseMultiResolutionImage mri = new BaseMultiResolutionImage(
  80             new BufferedImage[]{img1x, img2x});
  81         Icon icon = new ImageIcon(mri);
  82 
  83         setSize(N * SZ, SZ);
  84         setLocation(50, 50);
  85         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  86         getContentPane().setLayout(new GridLayout(1, 1));
  87 
  88         JPanel p = new JPanel();
  89         p.setLayout(new GridLayout(1, N));
  90 
  91         JButton btn = new JButton(icon);
  92         p.add(btn);
  93 
  94         JToggleButton tbn = new JToggleButton(icon);
  95         p.add(tbn);
  96 
  97         JRadioButton rbn = new JRadioButton(icon);
  98         rbn.setHorizontalAlignment(SwingConstants.CENTER);
  99         p.add(rbn);
 100 
 101         JCheckBox cbx = new JCheckBox(icon);
 102         cbx.setHorizontalAlignment(SwingConstants.CENTER);
 103         p.add(cbx);
 104 
 105         lbl = new JLabel(icon);
 106         p.add(lbl);
 107 
 108         tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
 109         tabbedPane.addTab("", p);
 110         tabbedPane.setTabComponentAt(0, new JLabel(icon));
 111         getContentPane().add(tabbedPane);
 112 
 113         setVisible(true);
 114     }
 115 
 116     private static boolean is2x() {
 117 
 118         AffineTransform tr = GraphicsEnvironment.getLocalGraphicsEnvironment().
 119             getDefaultScreenDevice().getDefaultConfiguration().
 120             getDefaultTransform();
 121         return (Math.min(tr.getScaleX(), tr.getScaleY()) > 1.001);
 122     }
 123 
 124     private void doTest() {
 125 
 126         r.waitForIdle(2000);
 127         String scale = System.getProperty(SCALE);
 128         System.out.println("scale = " + scale);
 129         Color expected = is2x() ? C2X : C1X;
 130 
 131         Point p = lbl.getLocationOnScreen();
 132         int x = p.x + lbl.getWidth() / 2;
 133         int y = p.y + lbl.getHeight() / 2;
 134         int w = lbl.getWidth();
 135 
 136         boolean ok = true;
 137         Color c;
 138         String components[] = new String[]{
 139             "JLabel", "JCheckBox", "JRadioButton", "JToggleButton", "JButton"};
 140         for (int i = 0; i < N; ++i) {
 141             c = r.getPixelColor(x - i * w, y);
 142             System.out.print(components[i] + " icon: ");
 143             if (c.equals(expected)) {
 144                 System.out.println("ok");
 145             } else {
 146                 System.out.println("nok");
 147                 ok = false;
 148             }
 149             r.waitForIdle();
 150         }
 151 
 152         Component comp = tabbedPane.getTabComponentAt(0);
 153         p = comp.getLocationOnScreen();
 154         c = r.getPixelColor(p.x + comp.getWidth() / 2, p.y + comp.getHeight()/ 2);
 155         if (c.equals(expected)) {
 156             System.out.println("JTabbedPane label icon: ok");
 157         } else {
 158             System.out.println("JTabbedPane label icon: nok");
 159             ok = false;
 160         }
 161 
 162         if (!ok) { throw new RuntimeException("test failed"); }
 163 
 164         r.waitForIdle();
 165         dispose();
 166     }
 167 
 168     public static void main(String[] args) throws Exception {
 169 
 170         // TODO: remove is2x() check after JDK-8150844 fix
 171         if (is2x() == "2".equals(System.getProperty(SCALE))) {
 172             (new MultiresolutionIconTest()).doTest();
 173         }
 174     }
 175 }