|
| 1 | +/* |
| 2 | + * Copyright (c) 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.awt.Button; |
| 25 | +import java.awt.Checkbox; |
| 26 | +import java.awt.Choice; |
| 27 | +import java.awt.Component; |
| 28 | +import java.awt.Dimension; |
| 29 | +import java.awt.EventQueue; |
| 30 | +import java.awt.FlowLayout; |
| 31 | +import java.awt.Frame; |
| 32 | +import java.awt.Label; |
| 33 | +import java.awt.List; |
| 34 | +import java.awt.Panel; |
| 35 | +import java.awt.Point; |
| 36 | +import java.awt.Robot; |
| 37 | +import java.awt.Scrollbar; |
| 38 | +import java.awt.TextArea; |
| 39 | +import java.awt.TextField; |
| 40 | +import java.awt.event.ComponentEvent; |
| 41 | +import java.awt.event.ComponentListener; |
| 42 | +import java.awt.event.InputEvent; |
| 43 | +import java.lang.reflect.InvocationTargetException; |
| 44 | + |
| 45 | +import jdk.test.lib.Platform; |
| 46 | + |
| 47 | +/* |
| 48 | + * @test |
| 49 | + * @key headful |
| 50 | + * @bug 8333403 |
| 51 | + * @summary Test performs various operations to check components events are triggered properly. |
| 52 | + * @library /test/lib |
| 53 | + * @build jdk.test.lib.Platform |
| 54 | + * @run main ComponentEventTest |
| 55 | + */ |
| 56 | +public class ComponentEventTest { |
| 57 | + |
| 58 | + private static final int DELAY = 500; |
| 59 | + |
| 60 | + private static Frame frame; |
| 61 | + private static Robot robot; |
| 62 | + |
| 63 | + private static Component[] components; |
| 64 | + |
| 65 | + private static volatile Point centerPoint; |
| 66 | + |
| 67 | + private static volatile boolean componentHidden; |
| 68 | + private static volatile boolean componentShown; |
| 69 | + private static volatile boolean componentMoved; |
| 70 | + private static volatile boolean componentResized; |
| 71 | + |
| 72 | + private static final ComponentListener componentListener = |
| 73 | + new ComponentListener() { |
| 74 | + |
| 75 | + @Override |
| 76 | + public void componentShown(ComponentEvent e) { |
| 77 | + System.out.println("ComponentShown: " + e.getSource()); |
| 78 | + componentShown = true; |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void componentResized(ComponentEvent e) { |
| 83 | + System.out.println("ComponentResized: " + e.getSource()); |
| 84 | + componentResized = true; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void componentMoved(ComponentEvent e) { |
| 89 | + System.out.println("ComponentMoved: " + e.getSource()); |
| 90 | + componentMoved = true; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void componentHidden(ComponentEvent e) { |
| 95 | + System.out.println("ComponentHidden: " + e.getSource()); |
| 96 | + componentHidden = true; |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + private static void initializeGUI() { |
| 101 | + frame = new Frame("Component Event Test"); |
| 102 | + frame.setLayout(new FlowLayout()); |
| 103 | + |
| 104 | + Panel panel = new Panel(); |
| 105 | + Button button = new Button("Button"); |
| 106 | + Label label = new Label("Label"); |
| 107 | + List list = new List(); |
| 108 | + list.add("One"); |
| 109 | + list.add("Two"); |
| 110 | + list.add("Three"); |
| 111 | + Choice choice = new Choice(); |
| 112 | + choice.add("Red"); |
| 113 | + choice.add("Orange"); |
| 114 | + choice.add("Yellow"); |
| 115 | + Checkbox checkbox = new Checkbox("Checkbox"); |
| 116 | + Scrollbar scrollbar = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 255); |
| 117 | + TextField textfield = new TextField(15); |
| 118 | + TextArea textarea = new TextArea(5, 15); |
| 119 | + |
| 120 | + components = new Component[] { panel, button, label, list, choice, |
| 121 | + checkbox, scrollbar, textfield, textarea, frame }; |
| 122 | + |
| 123 | + for (int i = 0; i < components.length - 1; i++) { |
| 124 | + components[i].addComponentListener(componentListener); |
| 125 | + frame.add(components[i]); |
| 126 | + } |
| 127 | + frame.addComponentListener(componentListener); |
| 128 | + |
| 129 | + frame.pack(); |
| 130 | + frame.setLocationRelativeTo(null); |
| 131 | + frame.setVisible(true); |
| 132 | + } |
| 133 | + |
| 134 | + public static void main(String[] args) throws Exception { |
| 135 | + try { |
| 136 | + robot = new Robot(); |
| 137 | + robot.setAutoWaitForIdle(true); |
| 138 | + |
| 139 | + EventQueue.invokeAndWait(ComponentEventTest::initializeGUI); |
| 140 | + robot.waitForIdle(); |
| 141 | + robot.delay(DELAY); |
| 142 | + |
| 143 | + doTest(); |
| 144 | + |
| 145 | + System.out.println("Test PASSED"); |
| 146 | + } finally { |
| 147 | + EventQueue.invokeAndWait(ComponentEventTest::disposeFrame); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + private static void doTest() |
| 152 | + throws InvocationTargetException, InterruptedException { |
| 153 | + // Click the frame to ensure it gains focus |
| 154 | + clickFrame(); |
| 155 | + |
| 156 | + robot.delay(DELAY); |
| 157 | + |
| 158 | + for (int i = 0; i < components.length; i++) { |
| 159 | + for (boolean state : new boolean[] { true, false }) { |
| 160 | + doTest(components[i], state); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + robot.delay(DELAY); |
| 165 | + |
| 166 | + System.out.println("Iconify frame"); |
| 167 | + resetValues(); |
| 168 | + testIconifyFrame(); |
| 169 | + |
| 170 | + System.out.println("Deiconify frame"); |
| 171 | + resetValues(); |
| 172 | + testDeiconifyFrame(); |
| 173 | + } |
| 174 | + |
| 175 | + private static void clickFrame() |
| 176 | + throws InvocationTargetException, InterruptedException { |
| 177 | + EventQueue.invokeAndWait(() -> { |
| 178 | + Point location = frame.getLocationOnScreen(); |
| 179 | + Dimension size = frame.getSize(); |
| 180 | + centerPoint = new Point(location.x + size.width / 2, |
| 181 | + location.y + size.height / 2); |
| 182 | + }); |
| 183 | + |
| 184 | + robot.mouseMove(centerPoint.x, centerPoint.y); |
| 185 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 186 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 187 | + } |
| 188 | + |
| 189 | + private static void testIconifyFrame() |
| 190 | + throws InvocationTargetException, InterruptedException { |
| 191 | + EventQueue.invokeAndWait(() -> frame.setExtendedState(Frame.ICONIFIED)); |
| 192 | + |
| 193 | + robot.waitForIdle(); |
| 194 | + robot.delay(DELAY); |
| 195 | + if (componentShown || componentHidden || componentMoved |
| 196 | + || componentResized) { |
| 197 | + throw new RuntimeException( |
| 198 | + "ComponentEvent triggered when frame is iconified"); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + private static void testDeiconifyFrame() |
| 203 | + throws InvocationTargetException, InterruptedException { |
| 204 | + EventQueue.invokeAndWait(() -> frame.setExtendedState(Frame.NORMAL)); |
| 205 | + |
| 206 | + robot.waitForIdle(); |
| 207 | + robot.delay(DELAY); |
| 208 | + |
| 209 | + /* |
| 210 | + * Because of the different behavior between MS Windows and other OS, we |
| 211 | + * receive native events WM_SIZE and WM_MOVE on Windows when the frame |
| 212 | + * state changes from iconified to normal. AWT sends these events to |
| 213 | + * components when it receives the events from the native system. See |
| 214 | + * JDK-6754618 for more information. |
| 215 | + */ |
| 216 | + |
| 217 | + if (componentShown || componentHidden) { |
| 218 | + throw new RuntimeException( |
| 219 | + "FAIL: componentShown or componentHidden triggered " |
| 220 | + + "when frame set to normal"); |
| 221 | + } |
| 222 | + |
| 223 | + if (Platform.isWindows() && (!componentMoved || !componentResized)) { |
| 224 | + throw new RuntimeException( |
| 225 | + "FAIL: componentMoved or componentResized wasn't triggered " |
| 226 | + + "when frame set to normal"); |
| 227 | + } |
| 228 | + if (!Platform.isWindows() && (componentMoved || componentResized)) { |
| 229 | + throw new RuntimeException( |
| 230 | + "FAIL: componentMoved or componentResized triggered " |
| 231 | + + "when frame set to normal"); |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + private static void doTest(final Component currentComponent, boolean enable) |
| 236 | + throws InvocationTargetException, InterruptedException { |
| 237 | + |
| 238 | + System.out.println("Component " + currentComponent); |
| 239 | + System.out.println(" enabled " + enable); |
| 240 | + |
| 241 | + EventQueue.invokeAndWait(() -> { |
| 242 | + currentComponent.setEnabled(enable); |
| 243 | + revalidateFrame(); |
| 244 | + }); |
| 245 | + |
| 246 | + robot.delay(DELAY); |
| 247 | + |
| 248 | + resetValues(); |
| 249 | + EventQueue.invokeAndWait(() -> { |
| 250 | + currentComponent.setVisible(false); |
| 251 | + revalidateFrame(); |
| 252 | + }); |
| 253 | + |
| 254 | + robot.delay(DELAY); |
| 255 | + if (!componentHidden) { |
| 256 | + throw new RuntimeException("FAIL: ComponentHidden not triggered for" |
| 257 | + + currentComponent.getClass()); |
| 258 | + } |
| 259 | + |
| 260 | + resetValues(); |
| 261 | + EventQueue.invokeAndWait(() -> { |
| 262 | + currentComponent.setVisible(false); |
| 263 | + revalidateFrame(); |
| 264 | + }); |
| 265 | + |
| 266 | + robot.delay(DELAY); |
| 267 | + if (componentHidden) { |
| 268 | + throw new RuntimeException("FAIL: ComponentHidden triggered when " |
| 269 | + + "setVisible(false) called for a hidden " |
| 270 | + + currentComponent.getClass()); |
| 271 | + } |
| 272 | + |
| 273 | + resetValues(); |
| 274 | + EventQueue.invokeAndWait(() -> { |
| 275 | + currentComponent.setVisible(true); |
| 276 | + revalidateFrame(); |
| 277 | + }); |
| 278 | + |
| 279 | + robot.delay(DELAY); |
| 280 | + if (!componentShown) { |
| 281 | + throw new RuntimeException("FAIL: ComponentShown not triggered for " |
| 282 | + + currentComponent.getClass()); |
| 283 | + } |
| 284 | + |
| 285 | + resetValues(); |
| 286 | + EventQueue.invokeAndWait(() -> { |
| 287 | + currentComponent.setVisible(true); |
| 288 | + revalidateFrame(); |
| 289 | + }); |
| 290 | + |
| 291 | + robot.delay(DELAY); |
| 292 | + if (componentShown) { |
| 293 | + throw new RuntimeException("FAIL: ComponentShown triggered when " |
| 294 | + + "setVisible(true) called for a shown " |
| 295 | + + currentComponent.getClass()); |
| 296 | + } |
| 297 | + |
| 298 | + resetValues(); |
| 299 | + EventQueue.invokeAndWait(() -> { |
| 300 | + currentComponent.setLocation(currentComponent.getLocation().x + 1, |
| 301 | + currentComponent.getLocation().y); |
| 302 | + revalidateFrame(); |
| 303 | + }); |
| 304 | + |
| 305 | + robot.delay(DELAY); |
| 306 | + if (!componentMoved) { |
| 307 | + throw new RuntimeException("FAIL: ComponentMoved not triggered for " |
| 308 | + + currentComponent.getClass()); |
| 309 | + } |
| 310 | + |
| 311 | + resetValues(); |
| 312 | + EventQueue.invokeAndWait(() -> { |
| 313 | + currentComponent.setSize(currentComponent.getSize().width + 1, |
| 314 | + currentComponent.getSize().height); |
| 315 | + revalidateFrame(); |
| 316 | + }); |
| 317 | + |
| 318 | + robot.delay(DELAY); |
| 319 | + if (!componentResized) { |
| 320 | + throw new RuntimeException("FAIL: ComponentResized not triggered " |
| 321 | + + "when size increases for " + currentComponent.getClass()); |
| 322 | + } |
| 323 | + |
| 324 | + resetValues(); |
| 325 | + EventQueue.invokeAndWait(() -> { |
| 326 | + currentComponent.setSize(currentComponent.getSize().width - 1, |
| 327 | + currentComponent.getSize().height); |
| 328 | + revalidateFrame(); |
| 329 | + }); |
| 330 | + |
| 331 | + robot.delay(DELAY); |
| 332 | + if (!componentResized) { |
| 333 | + throw new RuntimeException("FAIL: ComponentResized not triggered " |
| 334 | + + "when size decreases for " + currentComponent.getClass()); |
| 335 | + } |
| 336 | + |
| 337 | + System.out.println("\n"); |
| 338 | + } |
| 339 | + |
| 340 | + private static void revalidateFrame() { |
| 341 | + frame.invalidate(); |
| 342 | + frame.validate(); |
| 343 | + } |
| 344 | + |
| 345 | + private static void resetValues() { |
| 346 | + componentShown = false; |
| 347 | + componentHidden = false; |
| 348 | + componentMoved = false; |
| 349 | + componentResized = false; |
| 350 | + } |
| 351 | + |
| 352 | + private static void disposeFrame() { |
| 353 | + if (frame != null) { |
| 354 | + frame.dispose(); |
| 355 | + } |
| 356 | + } |
| 357 | +} |
0 commit comments