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
|
/*
* Copyright (c) 2002, 2023, 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.
*/
/*
@test
@bug 4525962
@summary Opposite component calculated inaccurately
@key headful
@run main ClearMostRecentFocusOwnerTest
*/
import java.awt.AWTEvent;
import java.awt.AWTException;
import java.awt.Button;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Insets;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.FocusEvent;
import java.awt.event.InputEvent;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.atomic.AtomicBoolean;
public class ClearMostRecentFocusOwnerTest implements AWTEventListener {
final static int ROBOT_DELAY = 50;
volatile Frame firstFrame;
volatile Frame secondFrame;
volatile Button actionButton;
volatile Button btnToRemove;
volatile Button btnToHide;
volatile Button btnToDisable;
volatile Button btnToNonFocusable;
volatile Panel pnlToHide;
volatile Button btnInPanel;
Robot robot;
volatile Component opposite = null;
volatile Component focusOwner = null;
volatile Object monitor = null;
public void init() throws InterruptedException,
InvocationTargetException {
try {
robot = new Robot();
} catch (AWTException e) {
throw new RuntimeException("Can not create awt-robot.");
}
EventQueue.invokeAndWait(() -> {
firstFrame = new Frame("The First Frame");
firstFrame.setName("\"1st Frame\"");
secondFrame = new Frame("The Second Frame");
secondFrame.setName("\"2nd Frame\"");
pnlToHide = new Panel();
pnlToHide.setName("Panel");
actionButton = new Button("Action Button");
actionButton.setName("\"" + actionButton.getLabel() + "\"");
btnToRemove = new Button("To Remove");
btnToRemove.setName("\"" + btnToRemove.getLabel() + "\"");
btnToHide = new Button("ToHide");
btnToHide.setName("\"" + btnToHide.getLabel() + "\"");
btnToDisable = new Button("To Disable");
btnToDisable.setName("\"" + btnToDisable.getLabel() + "\"");
btnToNonFocusable = new Button("To setFocusable(false)");
btnToNonFocusable.setName("\"" + btnToNonFocusable.getLabel() + "\"");
btnInPanel = new Button("Int Panel");
btnInPanel.setName("\"" + btnInPanel.getLabel() + "\"");
firstFrame.add(actionButton);
secondFrame.setLayout(new FlowLayout());
secondFrame.add(btnToRemove);
secondFrame.add(btnToHide);
secondFrame.add(btnToDisable);
secondFrame.add(btnToNonFocusable);
secondFrame.add(pnlToHide);
pnlToHide.add(btnInPanel);
firstFrame.pack();
firstFrame.setVisible(true);
secondFrame.pack();
secondFrame.setLocation(0, firstFrame.getHeight() + 50);
secondFrame.setVisible(true);
});
}
public void start() throws InterruptedException, InvocationTargetException {
try {
Toolkit.getDefaultToolkit().
addAWTEventListener(this,
AWTEvent.FOCUS_EVENT_MASK);
makeFocusOwner(btnToRemove);
EventQueue.invokeAndWait(() -> {
secondFrame.setVisible(false);
secondFrame.remove(btnToRemove);
});
makeFocusOwner(actionButton);
opposite = null;
EventQueue.invokeAndWait(() -> {
secondFrame.setVisible(true);
});
makeActiveFrame(secondFrame);
if (opposite != btnToHide) {
System.out.println("opposite = " + opposite);
throw new RuntimeException("Test FAILED: wrong opposite after Component.remove().");
}
makeFocusOwner(btnToHide);
EventQueue.invokeAndWait(() -> {
secondFrame.setVisible(false);
btnToHide.setVisible(false);
});
makeFocusOwner(actionButton);
opposite = null;
EventQueue.invokeAndWait(() -> {
secondFrame.setVisible(true);
});
makeActiveFrame(secondFrame);
if (opposite != btnToDisable) {
System.out.println("opposite = " + opposite);
throw new RuntimeException("Test FAILED: wrong opposite after Component.setVisible(false).");
}
makeFocusOwner(btnToDisable);
EventQueue.invokeAndWait(() -> {
secondFrame.setVisible(false);
btnToDisable.setEnabled(false);
});
makeFocusOwner(actionButton);
opposite = null;
EventQueue.invokeAndWait(() -> {
secondFrame.setVisible(true);
});
makeActiveFrame(secondFrame);
if (opposite != btnToNonFocusable) {
System.out.println("opposite = " + opposite);
throw new RuntimeException("Test FAILED: wrong opposite after Component.rsetEnabled(false).");
}
makeFocusOwner(btnToNonFocusable);
EventQueue.invokeAndWait(() -> {
secondFrame.setVisible(false);
btnToNonFocusable.setFocusable(false);
});
makeFocusOwner(actionButton);
opposite = null;
EventQueue.invokeAndWait(() -> {
secondFrame.setVisible(true);
});
makeActiveFrame(secondFrame);
if (opposite != btnInPanel) {
System.out.println("opposite = " + opposite);
throw new RuntimeException("Test FAILED: wrong opposite after Component.setFocusable(false).");
}
makeFocusOwner(btnInPanel);
EventQueue.invokeAndWait(() -> {
secondFrame.setVisible(false);
pnlToHide.setVisible(false);
});
makeFocusOwner(actionButton);
opposite = null;
EventQueue.invokeAndWait(() -> {
secondFrame.setVisible(true);
});
makeActiveFrame(secondFrame);
if (opposite == btnInPanel) {
System.out.println("opposite = " + opposite);
throw new RuntimeException("Test FAILED: wrong opposite after Container.setVisible(false).");
}
} finally {
if (firstFrame != null) {
EventQueue.invokeAndWait(firstFrame::dispose);
}
if (secondFrame != null) {
EventQueue.invokeAndWait(secondFrame::dispose);
}
}
}
public void eventDispatched(AWTEvent event) {
switch (event.getID()) {
case FocusEvent.FOCUS_GAINED:
if (focusOwner == ((FocusEvent) event).getComponent()
&& monitor != null) {
synchronized (monitor) {
monitor.notify();
}
}
break;
case FocusEvent.FOCUS_LOST:
opposite = ((FocusEvent) event).getOppositeComponent();
break;
}
System.out.println(event);
}
void clickOnComponent(Component comp) throws InterruptedException,
InvocationTargetException {
System.err.println("clickOnComopnent " + comp.getName());
robot.delay(3000);
int[] point = new int[2];
EventQueue.invokeAndWait(() -> {
Point origin = comp.getLocationOnScreen();
Dimension dim = comp.getSize();
point[0] = origin.x + (int) dim.getWidth() / 2;
point[1] = origin.y + (int) dim.getHeight() / 2;
});
robot.mouseMove(point[0], point[1]);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.delay(ROBOT_DELAY);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
}
void makeFocusOwner(Component comp) throws InterruptedException,
InvocationTargetException {
AtomicBoolean isOwner = new AtomicBoolean(false);
EventQueue.invokeAndWait(() -> {
isOwner.set(comp.isFocusOwner());
});
if (!isOwner.get()) {
clickOnComponent(comp);
try {
EventQueue.invokeAndWait(() -> {
isOwner.set(comp.isFocusOwner());
});
if (!isOwner.get()) {
monitor = new Object();
focusOwner = comp;
synchronized (monitor) {
monitor.wait(3000);
}
}
} catch (InterruptedException ie) {
throw new RuntimeException("Test was interrupted.");
}
}
EventQueue.invokeAndWait(() -> {
isOwner.set(comp.isFocusOwner());
});
if (!isOwner.get()) {
throw new RuntimeException("Test can not make "
+ comp.getName() + " a focus owner.");
}
}
void makeActiveFrame(Frame frame) throws InvocationTargetException,
InterruptedException {
robot.delay(3000);
if (!frame.isActive()) {
System.err.println("frame is not active");
int[] point = new int[2];
EventQueue.invokeAndWait(() -> {
Point origin = frame.getLocationOnScreen();
Insets ins = frame.getInsets();
point[0] = origin.x + frame.getWidth() / 2;
point[1] = origin.y + ins.top / 2;
});
robot.mouseMove(point[0], point[1]);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.delay(ROBOT_DELAY);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
}
robot.delay(3000);
EventQueue.invokeAndWait(() -> {
if (!frame.isActive()) {
throw new RuntimeException("Test can not activate " + frame.getName() + ".");
}
});
}
public static void main(String[] args) throws InterruptedException, InvocationTargetException {
ClearMostRecentFocusOwnerTest test = new ClearMostRecentFocusOwnerTest();
test.init();
test.start();
}
}
|