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