|
| 1 | +/* |
| 2 | + * Copyright (c) 2001, 2022, 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 | + * @test |
| 25 | + * @key headful |
| 26 | + * @bug 4314194 8075916 |
| 27 | + * @summary Verifies disabled color for JCheckbox and JRadiobutton is honored in all L&F |
| 28 | + * @run main bug4314194 |
| 29 | + */ |
| 30 | + |
| 31 | +import java.awt.BorderLayout; |
| 32 | +import java.awt.Color; |
| 33 | +import java.awt.Component; |
| 34 | +import java.awt.Point; |
| 35 | +import java.awt.Rectangle; |
| 36 | +import java.awt.Robot; |
| 37 | +import javax.swing.JFrame; |
| 38 | +import javax.swing.JCheckBox; |
| 39 | +import javax.swing.JRadioButton; |
| 40 | +import javax.swing.SwingUtilities; |
| 41 | +import javax.swing.UIManager; |
| 42 | +import javax.swing.UnsupportedLookAndFeelException; |
| 43 | + |
| 44 | +public class bug4314194 { |
| 45 | + private static JFrame frame; |
| 46 | + private static JRadioButton radioButton; |
| 47 | + private static JCheckBox checkBox; |
| 48 | + private static Point point; |
| 49 | + private static Rectangle rect; |
| 50 | + private static Robot robot; |
| 51 | + private static final Color radioButtonColor = Color.RED; |
| 52 | + private static final Color checkboxColor = Color.GREEN; |
| 53 | + private static final int tolerance = 20; |
| 54 | + |
| 55 | + private static boolean checkComponent(Component comp, Color c) throws Exception { |
| 56 | + int correctColoredPixels = 0; |
| 57 | + int totalPixels = 0; |
| 58 | + |
| 59 | + SwingUtilities.invokeAndWait(() -> { |
| 60 | + point = comp.getLocationOnScreen(); |
| 61 | + rect = comp.getBounds(); |
| 62 | + }); |
| 63 | + |
| 64 | + int y = point.y + rect.height / 2; |
| 65 | + for (int x = point.x; x < point.x + rect.width; x++) { |
| 66 | + Color color = robot.getPixelColor(x, y); |
| 67 | + robot.waitForIdle(); |
| 68 | + |
| 69 | + if (color.equals(c)) { |
| 70 | + correctColoredPixels++; |
| 71 | + } |
| 72 | + totalPixels++; |
| 73 | + } |
| 74 | + |
| 75 | + return ((double)correctColoredPixels/totalPixels*100) >= tolerance; |
| 76 | + } |
| 77 | + |
| 78 | + private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) { |
| 79 | + try { |
| 80 | + UIManager.setLookAndFeel(laf.getClassName()); |
| 81 | + } catch (UnsupportedLookAndFeelException ignored) { |
| 82 | + System.out.println("Unsupported L&F: " + laf.getClassName()); |
| 83 | + } catch (ClassNotFoundException | InstantiationException |
| 84 | + | IllegalAccessException e) { |
| 85 | + throw new RuntimeException(e); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private static void createUI() { |
| 90 | + UIManager.getDefaults().put("CheckBox.disabledText", checkboxColor); |
| 91 | + UIManager.getDefaults().put("RadioButton.disabledText", radioButtonColor); |
| 92 | + |
| 93 | + checkBox = new JCheckBox("WWWWW"); |
| 94 | + radioButton = new JRadioButton("WWWWW"); |
| 95 | + checkBox.setFont(checkBox.getFont().deriveFont(50.0f)); |
| 96 | + radioButton.setFont(radioButton.getFont().deriveFont(50.0f)); |
| 97 | + checkBox.setEnabled(false); |
| 98 | + radioButton.setEnabled(false); |
| 99 | + |
| 100 | + frame = new JFrame("bug4314194"); |
| 101 | + frame.getContentPane().add(radioButton, BorderLayout.SOUTH); |
| 102 | + frame.getContentPane().add(checkBox, BorderLayout.NORTH); |
| 103 | + frame.pack(); |
| 104 | + frame.setAlwaysOnTop(true); |
| 105 | + frame.setLocationRelativeTo(null); |
| 106 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 107 | + frame.setVisible(true); |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + public static void main(String[] args) throws Exception { |
| 112 | + robot = new Robot(); |
| 113 | + robot.setAutoDelay(100); |
| 114 | + |
| 115 | + for (UIManager.LookAndFeelInfo laf : |
| 116 | + UIManager.getInstalledLookAndFeels()) { |
| 117 | + if (laf.getClassName().contains("Motif")) { |
| 118 | + System.out.println("Skipping Motif L&F as it is deprecated"); |
| 119 | + continue; |
| 120 | + } |
| 121 | + System.out.println("Testing L&F: " + laf.getClassName()); |
| 122 | + SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf)); |
| 123 | + try { |
| 124 | + SwingUtilities.invokeAndWait(() -> createUI()); |
| 125 | + robot.waitForIdle(); |
| 126 | + robot.delay(1000); |
| 127 | + |
| 128 | + if (!checkComponent(checkBox, checkboxColor)) { |
| 129 | + throw new RuntimeException("Correct color not set for Checkbox"); |
| 130 | + } |
| 131 | + |
| 132 | + if (!checkComponent(radioButton, radioButtonColor)) { |
| 133 | + throw new RuntimeException("Correct color not set for RadioButton"); |
| 134 | + } |
| 135 | + } finally { |
| 136 | + if (frame != null) { |
| 137 | + SwingUtilities.invokeAndWait(() -> frame.dispose()); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | + |
0 commit comments