|
| 1 | +/* |
| 2 | + * Copyright (c) 2000, 2025, 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 4133768 4363569 |
| 27 | + * @summary Tests how button displays its icons |
| 28 | + * @key headful |
| 29 | + * @run main bug4133768 |
| 30 | + */ |
| 31 | + |
| 32 | +import java.awt.Color; |
| 33 | +import java.awt.Graphics2D; |
| 34 | +import java.awt.GridLayout; |
| 35 | +import java.awt.Point; |
| 36 | +import java.awt.Robot; |
| 37 | +import java.awt.image.BufferedImage; |
| 38 | +import java.io.IOException; |
| 39 | +import javax.swing.AbstractButton; |
| 40 | +import javax.swing.Icon; |
| 41 | +import javax.swing.ImageIcon; |
| 42 | +import javax.swing.JCheckBox; |
| 43 | +import javax.swing.JFrame; |
| 44 | +import javax.swing.JPanel; |
| 45 | +import javax.swing.JRadioButton; |
| 46 | +import javax.swing.JToggleButton; |
| 47 | +import javax.swing.SwingUtilities; |
| 48 | + |
| 49 | +public class bug4133768 { |
| 50 | + private static Icon RED, GREEN; |
| 51 | + private static JFrame f; |
| 52 | + private static AbstractButton[] buttons; |
| 53 | + private static volatile Point buttonLocation; |
| 54 | + private static volatile int buttonWidth; |
| 55 | + private static volatile int buttonHeight; |
| 56 | + private static Robot robot; |
| 57 | + |
| 58 | + public static void main(String[] args) throws Exception { |
| 59 | + try { |
| 60 | + createTestImages(); |
| 61 | + createUI(); |
| 62 | + robot = new Robot(); |
| 63 | + robot.delay(1000); |
| 64 | + for (AbstractButton b : buttons) { |
| 65 | + testEnabledButton(b); |
| 66 | + } |
| 67 | + for (AbstractButton b : buttons) { |
| 68 | + b.setEnabled(false); |
| 69 | + robot.delay(1000); |
| 70 | + testDisabledButton(b); |
| 71 | + } |
| 72 | + } finally { |
| 73 | + SwingUtilities.invokeAndWait(() -> { |
| 74 | + if (f != null) { |
| 75 | + f.dispose(); |
| 76 | + } |
| 77 | + }); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private static void createTestImages() throws IOException { |
| 82 | + int imageWidth = 32; |
| 83 | + int imageHeight = 32; |
| 84 | + BufferedImage redImg = new BufferedImage(imageWidth, imageHeight, |
| 85 | + BufferedImage.TYPE_INT_RGB); |
| 86 | + Graphics2D g = redImg.createGraphics(); |
| 87 | + g.setColor(Color.RED); |
| 88 | + g.fillRect(0, 0, imageWidth, imageHeight); |
| 89 | + g.dispose(); |
| 90 | + RED = new ImageIcon(redImg); |
| 91 | + BufferedImage greenImg = new BufferedImage(imageWidth, imageHeight, |
| 92 | + BufferedImage.TYPE_INT_RGB); |
| 93 | + g = greenImg.createGraphics(); |
| 94 | + g.setColor(Color.GREEN); |
| 95 | + g.fillRect(0, 0, imageWidth, imageHeight); |
| 96 | + g.dispose(); |
| 97 | + GREEN = new ImageIcon(greenImg); |
| 98 | + } |
| 99 | + |
| 100 | + private static void createUI() throws Exception { |
| 101 | + SwingUtilities.invokeAndWait(() -> { |
| 102 | + f = new JFrame("ButtonIconsTest"); |
| 103 | + buttons = new AbstractButton[] { |
| 104 | + new JToggleButton(), |
| 105 | + new JRadioButton(), |
| 106 | + new JCheckBox() |
| 107 | + }; |
| 108 | + |
| 109 | + JPanel buttonPanel = new JPanel(); |
| 110 | + for (int i = 0; i < buttons.length; i++) { |
| 111 | + AbstractButton b = buttons[i]; |
| 112 | + b.setIcon(RED); |
| 113 | + b.setSelected(true); |
| 114 | + b.setRolloverSelectedIcon(GREEN); |
| 115 | + buttonPanel.add(b); |
| 116 | + } |
| 117 | + f.setLayout(new GridLayout(2, 1)); |
| 118 | + f.add(buttonPanel); |
| 119 | + f.pack(); |
| 120 | + f.setLocationRelativeTo(null); |
| 121 | + f.setAlwaysOnTop(true); |
| 122 | + f.setVisible(true); |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + private static void testEnabledButton(AbstractButton button) throws Exception { |
| 127 | + robot.waitForIdle(); |
| 128 | + SwingUtilities.invokeAndWait(() -> { |
| 129 | + buttonLocation = button.getLocationOnScreen(); |
| 130 | + buttonWidth = button.getWidth(); |
| 131 | + buttonHeight = button.getHeight(); |
| 132 | + }); |
| 133 | + robot.mouseMove(buttonLocation.x + buttonWidth / 2, |
| 134 | + buttonLocation.y + buttonHeight / 2 ); |
| 135 | + robot.delay(1000); |
| 136 | + Color buttonColor = robot.getPixelColor(buttonLocation.x + |
| 137 | + buttonWidth / 2, buttonLocation.y + buttonHeight / 2); |
| 138 | + if (!buttonColor.equals(Color.GREEN)) { |
| 139 | + throw new RuntimeException("Button roll over color is : " + |
| 140 | + buttonColor + " but it should be : " + Color.GREEN); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + private static void testDisabledButton(AbstractButton button) throws Exception { |
| 145 | + robot.waitForIdle(); |
| 146 | + SwingUtilities.invokeAndWait(() -> { |
| 147 | + buttonLocation = button.getLocationOnScreen(); |
| 148 | + buttonWidth = button.getWidth(); |
| 149 | + buttonHeight = button.getHeight(); |
| 150 | + }); |
| 151 | + robot.mouseMove(buttonLocation.x + buttonWidth / 2, |
| 152 | + buttonLocation.y + buttonHeight / 2 ); |
| 153 | + robot.delay(1000); |
| 154 | + Color buttonColor = robot.getPixelColor(buttonLocation.x + |
| 155 | + buttonWidth / 2, buttonLocation.y + buttonHeight / 2); |
| 156 | + if (buttonColor.equals(Color.GREEN) || |
| 157 | + buttonColor.equals(Color.RED)) { |
| 158 | + throw new RuntimeException("Disabled button color should not be : " |
| 159 | + + buttonColor); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments