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