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 8151060 8151303
  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  * @requires (os.family != "mac")
  32  *
  33  * @library ../../../../lib/testlibrary/
  34  * @build ExtendedRobot
  35  * @run main/othervm -Dsun.java2d.uiScale=1 MultiresolutionIconTest
  36  * @run main/othervm -Dsun.java2d.uiScale=2 MultiresolutionIconTest
  37  */
  38 
  39 
  40 // TODO: please remove the "@requires" tag after 8151060 and 8151303 fix
  41 
  42 
  43 import java.awt.*;
  44 import java.awt.event.InputEvent;
  45 import java.awt.geom.AffineTransform;
  46 import java.awt.image.BaseMultiResolutionImage;
  47 import java.awt.image.BufferedImage;
  48 import javax.swing.*;
  49 
  50 public class MultiresolutionIconTest extends JFrame {
  51 
  52     private final static int SZ = 100;
  53     private final static int N = 5; // number of components
  54 
  55     private final static String SCALE = "sun.java2d.uiScale";
  56     private final static Color C1X = Color.RED;
  57     private final static Color C2X = Color.BLUE;
  58 
  59     private JLabel lbl;
  60     private JTabbedPane tabbedPane;
  61 
  62     private final ExtendedRobot r;
  63 
  64     private static BufferedImage generateImage(int scale, Color c) {
  65 
  66         int x = (SZ - 20) * scale;
  67         BufferedImage img = new BufferedImage(x, x, BufferedImage.TYPE_INT_RGB);
  68         Graphics g = img.getGraphics();
  69         g.setColor(c);
  70         g.fillRect(0, 0, x, x);
  71         return img;
  72     }
  73 
  74     public MultiresolutionIconTest() throws Exception {
  75 
  76         r = new ExtendedRobot();
  77         SwingUtilities.invokeAndWait(this::UI);
  78     }
  79 
  80     private void UI() {
  81 
  82         setUndecorated(true);
  83 
  84         BufferedImage img1x = generateImage(1, C1X);
  85         BufferedImage img2x = generateImage(2, C2X);
  86         BaseMultiResolutionImage mri = new BaseMultiResolutionImage(
  87             new BufferedImage[]{img1x, img2x});
  88         Icon icon = new ImageIcon(mri);
  89 
  90         setSize((N + 1) * SZ, SZ);
  91         setLocation(50, 50);
  92         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  93         getContentPane().setLayout(new GridLayout(1, 1));
  94 
  95         JPanel p = new JPanel();
  96         p.setLayout(new GridLayout(1, N));
  97 
  98         JButton btn = new JButton(icon);
  99         p.add(btn);
 100 
 101         JToggleButton tbn = new JToggleButton(icon);
 102         p.add(tbn);
 103 
 104         JRadioButton rbn = new JRadioButton(icon);
 105         rbn.setHorizontalAlignment(SwingConstants.CENTER);
 106         p.add(rbn);
 107 
 108         JCheckBox cbx = new JCheckBox(icon);
 109         cbx.setHorizontalAlignment(SwingConstants.CENTER);
 110         p.add(cbx);
 111 
 112         lbl = new JLabel(icon);
 113         p.add(lbl);
 114 
 115         tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
 116         tabbedPane.addTab("", icon, p);
 117         tabbedPane.addTab("", new JPanel());
 118         tabbedPane.setTabComponentAt(1, new JLabel());
 119         getContentPane().add(tabbedPane);
 120 
 121         setResizable(false);
 122         setVisible(true);
 123     }
 124 
 125     private static boolean is2x() {
 126 
 127         AffineTransform tr = GraphicsEnvironment.getLocalGraphicsEnvironment().
 128             getDefaultScreenDevice().getDefaultConfiguration().
 129             getDefaultTransform();
 130         return (Math.min(tr.getScaleX(), tr.getScaleY()) > 1.001);
 131     }
 132 
 133     private void doTest() {
 134 
 135         r.waitForIdle(2000);
 136         String scale = System.getProperty(SCALE);
 137         System.out.println("scale = " + scale);
 138         Color expected = is2x() ? C2X : C1X;
 139 
 140         Point p = lbl.getLocationOnScreen();
 141         int x = p.x + lbl.getWidth() / 2;
 142         int y = p.y + lbl.getHeight() / 3;
 143         int w = lbl.getWidth();
 144 
 145         boolean ok = true, curr;
 146         Color c;
 147         String components[] = new String[]{
 148             "JLabel", "JCheckBox", "JRadioButton", "JToggleButton", "JButton"};
 149         for (int i = 0; i < N; ++i) {
 150 
 151             curr = true;
 152             int t = x - i * w;
 153 
 154             // check icon color
 155             c = r.getPixelColor(t, y);
 156             System.out.print(components[i] + " icon: ");
 157             if (!c.equals(expected)) {
 158                 curr = false;
 159             } else {
 160                 // check icon color when mouse button pressed - see JDK-8151303
 161                 r.mouseMove(t, y);
 162                 r.waitForIdle();
 163                 r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 164                 r.waitForIdle(100);
 165                 c = r.getPixelColor(t, y);
 166                 r.waitForIdle(100);
 167                 r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 168                 r.waitForIdle(100);
 169                 if (!c.equals(expected)) {
 170                     curr = false;
 171                 }
 172                 // check the icon's color hasn't changed
 173                 // after the mouse was released
 174                 c = r.getPixelColor(t, y);
 175                 if (!c.equals(expected)) {
 176                     curr = false;
 177                 }
 178             }
 179 
 180             System.out.println(curr ? "ok" : "nok");
 181             ok = ok && curr;
 182 
 183             r.waitForIdle();
 184         }
 185 
 186         curr = true;
 187         Component tc = tabbedPane.getTabComponentAt(1);
 188         x = tc.getLocationOnScreen().x + tc.getWidth() / 2;
 189         c = r.getPixelColor(x, y);
 190         if (!c.equals(expected)) {
 191             curr = false;
 192         } else {
 193             // just in case
 194             r.mouseMove(x, y);
 195             r.waitForIdle();
 196 
 197             r.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 198             r.waitForIdle(100);
 199             c = r.getPixelColor(x, y);
 200             r.waitForIdle(100);
 201             r.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 202             r.waitForIdle(100);
 203             if (!c.equals(expected)) {
 204                 curr = false;
 205             } else {
 206                 c = r.getPixelColor(x, y);
 207                 curr = c.equals(expected);
 208             }
 209         }
 210 
 211         System.out.println("JTabbedPane icon: " + (curr ? "ok" : "nok"));
 212         ok = ok && curr;
 213 
 214         if (!ok) { throw new RuntimeException("test failed"); }
 215 
 216         r.waitForIdle();
 217         dispose();
 218     }
 219 
 220     public static void main(String[] args) throws Exception {
 221 
 222         // TODO: remove is2x() check after JDK-8150844 fix
 223         if (is2x() == "2".equals(System.getProperty(SCALE))) {
 224             (new MultiresolutionIconTest()).doTest();
 225         }
 226     }
 227 }