|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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 7093691 |
| 27 | + * @summary Tests if JComboBox has correct font color when disabled/enabled |
| 28 | + * @run main/othervm -Dsun.java2d.uiScale=1 DisabledComboBoxFontTestAuto |
| 29 | + */ |
| 30 | + |
| 31 | +import java.awt.Color; |
| 32 | +import java.awt.Graphics2D; |
| 33 | +import java.awt.image.BufferedImage; |
| 34 | +import java.io.File; |
| 35 | +import java.io.IOException; |
| 36 | +import java.nio.file.Path; |
| 37 | + |
| 38 | +import javax.imageio.ImageIO; |
| 39 | +import javax.swing.DefaultListCellRenderer; |
| 40 | +import javax.swing.JComboBox; |
| 41 | +import javax.swing.SwingUtilities; |
| 42 | +import javax.swing.UIManager; |
| 43 | +import javax.swing.UnsupportedLookAndFeelException; |
| 44 | + |
| 45 | +import static java.awt.image.BufferedImage.TYPE_INT_ARGB; |
| 46 | + |
| 47 | +public class DisabledComboBoxFontTestAuto { |
| 48 | + private static JComboBox combo, combo2; |
| 49 | + private static BufferedImage enabledImage, disabledImage, enabledImage2, disabledImage2; |
| 50 | + private static Path testDir; |
| 51 | + private static String lafName; |
| 52 | + private static StringBuffer failingLafs; |
| 53 | + private static int COMBO_HEIGHT, COMBO_WIDTH, COMBO2_HEIGHT, COMBO2_WIDTH; |
| 54 | + |
| 55 | + private static void createCombo() { |
| 56 | + combo = new JComboBox(); |
| 57 | + combo.addItem("Simple JComboBox"); |
| 58 | + combo.setRenderer(new DefaultListCellRenderer()); |
| 59 | + combo2 = new JComboBox(); |
| 60 | + combo2.addItem("Simple JComboBox"); |
| 61 | + COMBO_WIDTH = (int) combo.getPreferredSize().getWidth(); |
| 62 | + COMBO_HEIGHT = (int) combo.getPreferredSize().getHeight(); |
| 63 | + COMBO2_WIDTH = (int) combo2.getPreferredSize().getWidth(); |
| 64 | + COMBO2_HEIGHT = (int) combo2.getPreferredSize().getHeight(); |
| 65 | + combo.setSize(COMBO_WIDTH, COMBO_HEIGHT); |
| 66 | + combo2.setSize(COMBO2_WIDTH, COMBO2_HEIGHT); |
| 67 | + } |
| 68 | + |
| 69 | + private static void paintCombo() { |
| 70 | + combo.setEnabled(true); |
| 71 | + enabledImage = new BufferedImage(COMBO_WIDTH, COMBO_HEIGHT, TYPE_INT_ARGB); |
| 72 | + Graphics2D graphics2D = enabledImage.createGraphics(); |
| 73 | + combo.paint(graphics2D); |
| 74 | + graphics2D.dispose(); |
| 75 | + combo.setEnabled(false); |
| 76 | + disabledImage = new BufferedImage(COMBO_WIDTH, COMBO_HEIGHT, TYPE_INT_ARGB); |
| 77 | + graphics2D = disabledImage.createGraphics(); |
| 78 | + combo.paint(graphics2D); |
| 79 | + graphics2D.dispose(); |
| 80 | + combo2.setEnabled(true); |
| 81 | + enabledImage2 = new BufferedImage(COMBO2_WIDTH, COMBO2_HEIGHT, TYPE_INT_ARGB); |
| 82 | + graphics2D = enabledImage2.createGraphics(); |
| 83 | + combo2.paint(graphics2D); |
| 84 | + graphics2D.dispose(); |
| 85 | + combo2.setEnabled(false); |
| 86 | + disabledImage2 = new BufferedImage(COMBO2_WIDTH, COMBO2_HEIGHT, TYPE_INT_ARGB); |
| 87 | + graphics2D = disabledImage2.createGraphics(); |
| 88 | + combo2.paint(graphics2D); |
| 89 | + graphics2D.dispose(); |
| 90 | + } |
| 91 | + |
| 92 | + private static void testMethod() throws IOException { |
| 93 | + ImageIO.write(enabledImage, "png", new File(testDir |
| 94 | + + "/" + lafName + "Enabled.png")); |
| 95 | + ImageIO.write(disabledImage, "png", new File(testDir |
| 96 | + + "/" + lafName + "Disabled.png")); |
| 97 | + ImageIO.write(enabledImage2, "png", new File(testDir |
| 98 | + + "/" + lafName + "EnabledDLCR.png")); |
| 99 | + ImageIO.write(disabledImage2, "png", new File(testDir |
| 100 | + + "/" + lafName + "DisabledDLCR.png")); |
| 101 | + |
| 102 | + boolean isIdentical = true; |
| 103 | + Color eColor1, eColor2, dColor1, dColor2; |
| 104 | + |
| 105 | + // Use center line to compare RGB values |
| 106 | + int y = 10; |
| 107 | + for (int x = (enabledImage.getWidth() / 2) - 20; |
| 108 | + x < (enabledImage.getWidth() / 2) + 20; x++) { |
| 109 | + // Nimbus has a pixel offset in coordinates since Nimbus is 2px |
| 110 | + // smaller in width than other L&F's |
| 111 | + if (lafName.equals("Nimbus")) { |
| 112 | + eColor1 = new Color(enabledImage.getRGB(x + 1, y)); |
| 113 | + eColor2 = new Color(enabledImage2.getRGB(x, y)); |
| 114 | + dColor1 = new Color(disabledImage.getRGB(x + 1, y)); |
| 115 | + dColor2 = new Color(disabledImage2.getRGB(x, y)); |
| 116 | + } else { |
| 117 | + eColor1 = new Color(enabledImage.getRGB(x, y)); |
| 118 | + eColor2 = new Color(enabledImage2.getRGB(x, y)); |
| 119 | + dColor1 = new Color(disabledImage.getRGB(x, y)); |
| 120 | + dColor2 = new Color(disabledImage2.getRGB(x, y)); |
| 121 | + } |
| 122 | + if ((!isColorMatching(eColor1, eColor2)) || (!isColorMatching(dColor1, dColor2))) { |
| 123 | + isIdentical = false; |
| 124 | + break; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + if (isIdentical) { |
| 129 | + System.out.println("PASSED"); |
| 130 | + } else { |
| 131 | + failingLafs.append(lafName + ", "); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private static boolean isColorMatching(Color c1, Color c2) { |
| 136 | + if ((c1.getRed() != c2.getRed()) |
| 137 | + || (c1.getBlue() != c2.getBlue()) |
| 138 | + || (c1.getGreen() != c2.getGreen())) { |
| 139 | + |
| 140 | + System.out.println(lafName + " Enabled RGB failure: " |
| 141 | + + c1.getRed() + ", " |
| 142 | + + c1.getBlue() + ", " |
| 143 | + + c1.getGreen() + " vs " |
| 144 | + + c2.getRed() + ", " |
| 145 | + + c2.getBlue() + ", " |
| 146 | + + c2.getGreen()); |
| 147 | + return false; |
| 148 | + } |
| 149 | + return true; |
| 150 | + } |
| 151 | + |
| 152 | + private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) { |
| 153 | + try { |
| 154 | + UIManager.setLookAndFeel(laf.getClassName()); |
| 155 | + } catch (UnsupportedLookAndFeelException ignored){ |
| 156 | + System.out.println("Unsupported LookAndFeel: " + laf.getClassName()); |
| 157 | + } catch (ClassNotFoundException | InstantiationException | |
| 158 | + IllegalAccessException e) { |
| 159 | + throw new RuntimeException(e); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + public static void main(String[] args) throws Exception { |
| 164 | + lafName = "null"; |
| 165 | + failingLafs = new StringBuffer(); |
| 166 | + testDir = Path.of(System.getProperty("test.classes", ".")); |
| 167 | + for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) { |
| 168 | + // Change Motif LAF name to avoid using slash in saved image file path |
| 169 | + lafName = laf.getName().equals("CDE/Motif") ? "Motif" : laf.getName(); |
| 170 | + SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf)); |
| 171 | + SwingUtilities.invokeAndWait(DisabledComboBoxFontTestAuto::createCombo); |
| 172 | + SwingUtilities.invokeAndWait(DisabledComboBoxFontTestAuto::paintCombo); |
| 173 | + testMethod(); |
| 174 | + } |
| 175 | + if (!failingLafs.isEmpty()) { |
| 176 | + // Remove trailing comma and whitespace |
| 177 | + failingLafs.setLength(failingLafs.length() - 2); |
| 178 | + throw new RuntimeException("FAIL - Enabled and disabled ComboBox " + |
| 179 | + "does not match in these LAFs: " + failingLafs); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments