|
| 1 | +/* |
| 2 | + * Copyright (c) 1999, 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 4219344 |
| 27 | + * @summary tests that WINDOW_ACTIVATED events are generated properly |
| 28 | + * @key headful |
| 29 | + * @library /test/jdk/java/awt/regtesthelpers |
| 30 | + * @build Util |
| 31 | + * @run main WindowActivatedEventTest |
| 32 | + */ |
| 33 | + |
| 34 | +import test.java.awt.regtesthelpers.Util; |
| 35 | + |
| 36 | +import java.awt.Dialog; |
| 37 | +import java.awt.EventQueue; |
| 38 | +import java.awt.Frame; |
| 39 | +import java.awt.Robot; |
| 40 | +import java.awt.Window; |
| 41 | +import java.awt.event.WindowEvent; |
| 42 | +import java.awt.event.WindowAdapter; |
| 43 | +import java.lang.reflect.InvocationTargetException; |
| 44 | +import java.util.Arrays; |
| 45 | +import java.util.concurrent.atomic.AtomicInteger; |
| 46 | + |
| 47 | +public class WindowActivatedEventTest { |
| 48 | + |
| 49 | + static Robot robot; |
| 50 | + static Frame frame; |
| 51 | + static Dialog dialog; |
| 52 | + |
| 53 | + public static void main(String[] args) throws Exception { |
| 54 | + robot = new Robot(); |
| 55 | + |
| 56 | + try { |
| 57 | + EventQueue.invokeAndWait(WindowActivatedEventTest::createAndShowGUI); |
| 58 | + robot.waitForIdle(); |
| 59 | + robot.delay(500); |
| 60 | + |
| 61 | + Util.clickOnComp(dialog, robot); |
| 62 | + |
| 63 | + robot.waitForIdle(); |
| 64 | + robot.delay(500); |
| 65 | + |
| 66 | + for (int i = 0; i < 3 ; i++) { |
| 67 | + clickAndCheck(frame); |
| 68 | + clickAndCheck(dialog); |
| 69 | + } |
| 70 | + } finally { |
| 71 | + EventQueue.invokeAndWait(() -> { |
| 72 | + if (frame != null) { |
| 73 | + frame.dispose(); |
| 74 | + } |
| 75 | + if (dialog != null) { |
| 76 | + dialog.dispose(); |
| 77 | + } |
| 78 | + }); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private static void clickAndCheck(Window windowToFocus) |
| 83 | + throws InterruptedException, InvocationTargetException { |
| 84 | + Window oppositeWindow = (windowToFocus == frame) ? dialog : frame; |
| 85 | + |
| 86 | + System.out.println("Clicking on " + windowToFocus); |
| 87 | + |
| 88 | + EventQueue.invokeAndWait(() -> { |
| 89 | + if (windowToFocus.isFocused() || !oppositeWindow.isFocused()) { |
| 90 | + throw new RuntimeException("%s isFocused %b, %s isFocused %b".formatted( |
| 91 | + windowToFocus.getName(), windowToFocus.isFocused(), |
| 92 | + oppositeWindow.getName(), oppositeWindow.isFocused() |
| 93 | + )); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + WindowEventLogger windowLogger = WindowEventLogger.getFromWindow(windowToFocus); |
| 98 | + WindowEventLogger oppositeWindowLogger = WindowEventLogger.getFromWindow(oppositeWindow); |
| 99 | + |
| 100 | + windowLogger.resetCounters(); |
| 101 | + oppositeWindowLogger.resetCounters(); |
| 102 | + |
| 103 | + Util.clickOnComp(windowToFocus, robot); |
| 104 | + |
| 105 | + robot.delay(500); |
| 106 | + |
| 107 | + int windowActivatedCount = windowLogger.activatedCount.get(); |
| 108 | + int windowDeactivatedCount = windowLogger.deactivatedCount.get(); |
| 109 | + int oppositeWindowActivatedCount = oppositeWindowLogger.activatedCount.get(); |
| 110 | + int oppositeWindowDeactivatedCount = oppositeWindowLogger.deactivatedCount.get(); |
| 111 | + |
| 112 | + if (windowActivatedCount != 1 |
| 113 | + || windowDeactivatedCount != 0 |
| 114 | + || oppositeWindowActivatedCount != 0 |
| 115 | + || oppositeWindowDeactivatedCount != 1) { |
| 116 | + throw new RuntimeException( |
| 117 | + "Invalid activated/deactivated count: %s (%d/%d) / %s (%d/%d)" |
| 118 | + .formatted( |
| 119 | + windowToFocus.getName(), |
| 120 | + windowActivatedCount, |
| 121 | + windowDeactivatedCount, |
| 122 | + oppositeWindow.getName(), |
| 123 | + oppositeWindowActivatedCount, |
| 124 | + oppositeWindowDeactivatedCount |
| 125 | + )); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private static void createAndShowGUI() { |
| 130 | + frame = new Frame("frame WindowActivatedEventTest"); |
| 131 | + dialog = new Dialog(frame, "dialog WindowActivatedEventTest"); |
| 132 | + |
| 133 | + frame.addWindowListener(new WindowEventLogger()); |
| 134 | + dialog.addWindowListener(new WindowEventLogger()); |
| 135 | + |
| 136 | + frame.setBounds(400, 0, 200, 200); |
| 137 | + frame.setVisible(true); |
| 138 | + |
| 139 | + dialog.setBounds(400, 200, 200, 200); |
| 140 | + dialog.setVisible(true); |
| 141 | + } |
| 142 | + |
| 143 | + private static class WindowEventLogger extends WindowAdapter { |
| 144 | + final AtomicInteger activatedCount = new AtomicInteger(0); |
| 145 | + final AtomicInteger deactivatedCount = new AtomicInteger(0); |
| 146 | + |
| 147 | + public void windowActivated(WindowEvent e) { |
| 148 | + activatedCount.incrementAndGet(); |
| 149 | + System.out.println(e); |
| 150 | + } |
| 151 | + |
| 152 | + public void windowDeactivated(WindowEvent e) { |
| 153 | + deactivatedCount.incrementAndGet(); |
| 154 | + System.out.println(e); |
| 155 | + } |
| 156 | + |
| 157 | + public void resetCounters() { |
| 158 | + activatedCount.set(0); |
| 159 | + deactivatedCount.set(0); |
| 160 | + } |
| 161 | + |
| 162 | + public static WindowEventLogger getFromWindow(Window window) { |
| 163 | + return (WindowEventLogger) Arrays |
| 164 | + .stream(window.getWindowListeners()) |
| 165 | + .filter(listener -> listener instanceof WindowEventLogger) |
| 166 | + .findFirst().get(); |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments