|
| 1 | +/* |
| 2 | + * Copyright (c) 2014, 2024, 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 | +import java.io.File; |
| 25 | +import java.io.IOException; |
| 26 | +import java.awt.Color; |
| 27 | +import java.awt.Graphics2D; |
| 28 | +import java.awt.image.BufferedImage; |
| 29 | +import javax.imageio.ImageIO; |
| 30 | +import javax.swing.Icon; |
| 31 | +import javax.swing.JLabel; |
| 32 | +import javax.swing.JTree; |
| 33 | +import javax.swing.SwingUtilities; |
| 34 | +import javax.swing.plaf.basic.BasicTreeUI; |
| 35 | +import javax.swing.UIManager; |
| 36 | +import javax.swing.UnsupportedLookAndFeelException; |
| 37 | + |
| 38 | +import static java.awt.image.BufferedImage.TYPE_INT_RGB; |
| 39 | + |
| 40 | +/* @test |
| 41 | + * @key headful |
| 42 | + * @bug 8038113 8258979 |
| 43 | + * @summary [macosx] JTree icon is not rendered in high resolution on Retina, |
| 44 | + * collapsed icon is not rendered for GTK LAF as well. |
| 45 | + * @run main bug8038113 |
| 46 | + */ |
| 47 | + |
| 48 | +public class bug8038113 { |
| 49 | + public static void main(String[] args) throws Exception { |
| 50 | + for (UIManager.LookAndFeelInfo laf : |
| 51 | + UIManager.getInstalledLookAndFeels()) { |
| 52 | + if (!laf.getName().contains("Motif")) { |
| 53 | + System.out.println("Testing LAF: " + laf.getName()); |
| 54 | + SwingUtilities.invokeAndWait(() -> test(laf)); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public static void test(UIManager.LookAndFeelInfo laf) { |
| 60 | + setLookAndFeel(laf); |
| 61 | + final JTree tree = new JTree(); |
| 62 | + final BasicTreeUI treeUI = (BasicTreeUI) tree.getUI(); |
| 63 | + |
| 64 | + Icon collapsedIcon = treeUI.getCollapsedIcon(); |
| 65 | + Icon expandedIcon = treeUI.getExpandedIcon(); |
| 66 | + BufferedImage img1 = paintToImage(expandedIcon); |
| 67 | + BufferedImage img2 = paintToImage(collapsedIcon); |
| 68 | + |
| 69 | + if (!isImgRendered(img1)) { |
| 70 | + try { |
| 71 | + ImageIO.write(img1, "png", new File("Expanded_Icon_" + laf.getName() + ".png")); |
| 72 | + } catch (IOException ignored) { |
| 73 | + } |
| 74 | + throw new RuntimeException("Test Failed, Expanded not rendered for: "+laf.getName()); |
| 75 | + } |
| 76 | + |
| 77 | + if (!isImgRendered(img2)) { |
| 78 | + try { |
| 79 | + ImageIO.write(img2, "png", new File("Collapsed_Icon_" + laf.getName() + ".png")); |
| 80 | + } catch (IOException ignored) { |
| 81 | + } |
| 82 | + throw new RuntimeException("Test Failed, Collapsed Icon not rendered for: "+laf.getName()); |
| 83 | + } |
| 84 | + System.out.println("Test Passed"); |
| 85 | + } |
| 86 | + |
| 87 | + private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) { |
| 88 | + try { |
| 89 | + UIManager.setLookAndFeel(laf.getClassName()); |
| 90 | + } catch (UnsupportedLookAndFeelException ignored) { |
| 91 | + System.out.println("Unsupported LAF: " + laf.getClassName()); |
| 92 | + } catch (ClassNotFoundException | InstantiationException |
| 93 | + | IllegalAccessException e) { |
| 94 | + throw new RuntimeException(e); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private static BufferedImage paintToImage(Icon content) { |
| 99 | + BufferedImage im = new BufferedImage(content.getIconWidth(), |
| 100 | + content.getIconHeight(), TYPE_INT_RGB); |
| 101 | + Graphics2D g = (Graphics2D) im.getGraphics(); |
| 102 | + g.setBackground(Color.WHITE); |
| 103 | + g.clearRect(0, 0, content.getIconWidth(), content.getIconHeight()); |
| 104 | + content.paintIcon(new JLabel(), g, 0, 0); |
| 105 | + g.dispose(); |
| 106 | + return im; |
| 107 | + } |
| 108 | + |
| 109 | + private static boolean isImgRendered(BufferedImage img) { |
| 110 | + Color white = new Color(255, 255, 255); |
| 111 | + for (int x = 0; x < img.getWidth(); ++x) { |
| 112 | + for (int y = 0; y < img.getHeight(); ++y) { |
| 113 | + if (img.getRGB(x, y) != white.getRGB()) { |
| 114 | + return true; |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + return false; |
| 119 | + } |
| 120 | +} |
0 commit comments