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