8306871: Open source more AWT Drag & Drop tests

Reviewed-by: prr
This commit is contained in:
Damon Nguyen 2023-05-05 16:28:03 +00:00
parent 47422be2d1
commit b5a48277ab
6 changed files with 1915 additions and 0 deletions

@ -0,0 +1,56 @@
/*
* Copyright (c) 2001, 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 4420658
@summary No ClassCastException should be thrown when getComponent()
is called on an event with a non-Component source.
The result should be null.
@run main ObjectSourceTest
*/
import java.awt.Component;
import java.awt.Panel;
import java.awt.event.HierarchyEvent;
import java.lang.reflect.InvocationTargetException;
public class ObjectSourceTest {
static Panel panel;
public static void main(String args[]) throws InterruptedException,
InvocationTargetException {
panel = new Panel();
HierarchyEvent he = new HierarchyEvent(panel, HierarchyEvent.ANCESTOR_MOVED,
panel, panel);
Object obj = new Object();
he.setSource(obj);
Component comp = he.getComponent();
if (comp != null) {
throw new RuntimeException("ObjectSourceTest failed. comp != null");
}
}
}

File diff suppressed because it is too large Load Diff

@ -0,0 +1,286 @@
/*
* Copyright (c) 2001, 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 4353201
@summary Wrong modifiers on InputEvent
@key headful
@run main MouseModsTest
*/
import java.awt.AWTEvent;
import java.awt.AWTException;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.MouseInfo;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.lang.reflect.InvocationTargetException;
public class MouseModsTest {
static volatile int testCtrl = 0;
static volatile int testBtn = 0;
static volatile boolean passed = true;
static final int BUTTONS = Math.min(3, MouseInfo.getNumberOfButtons());
static final int KEYS = 2;
static Frame frame;
static Panel panel;
static Canvas button1;
static Canvas button2;
static volatile Point pt1;
static volatile Point pt2;
public static void main(String args[]) throws AWTException,
InterruptedException, InvocationTargetException {
EventQueue.invokeAndWait(() -> {
frame = new Frame("MouseModsTest");
panel = new Panel();
button1 = new TestCanvas();
button2 = new TestCanvas();
frame.setSize(300, 200);
panel.add(button1);
panel.add(button2);
frame.add(panel);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
try {
Robot robot;
robot = new Robot();
robot.setAutoWaitForIdle(true);
robot.setAutoDelay(50);
robot.delay(1000);
EventQueue.invokeAndWait(() -> {
pt1 = button1.getLocationOnScreen();
pt2 = button2.getLocationOnScreen();
pt1.x += button1.getSize().width / 2;
pt1.y += button1.getSize().height / 2;
pt2.x += button2.getSize().width / 2;
pt2.y += button2.getSize().height / 2;
});
robot.mouseMove(pt2.x, pt2.y);
//Keyboard to Mouse Test
for (int ctrl = 1; ctrl <= KEYS; ++ctrl) {
testCtrl = ctrl;
robot.keyPress(getKeycode(ctrl));
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
for (int btn = 2; btn <= BUTTONS; ++btn) {
testBtn = btn;
robot.mousePress(getMouseModifier(btn));
robot.mouseMove(pt1.x, pt1.y);
robot.mouseMove(pt2.x, pt2.y);
robot.mouseRelease(getMouseModifier(btn));
}
robot.keyRelease(getKeycode(ctrl));
}
//Mouse to Mouse Test
for (int btn1 = 1; btn1 <= BUTTONS; ++btn1) {
testBtn = btn1;
robot.mousePress(getMouseModifier(btn1));
for (int btn = 1; btn <= BUTTONS; ++btn) {
if (btn == btn1) continue;
testBtn = btn;
robot.mousePress(getMouseModifier(btn));
robot.mouseMove(pt1.x, pt1.y);
robot.mouseMove(pt2.x, pt2.y);
robot.mouseRelease(getMouseModifier(btn));
}
testBtn = btn1;
robot.mouseRelease(getMouseModifier(btn1));
}
testBtn = 0;
} finally {
EventQueue.invokeAndWait(() -> {
if (frame != null) {
frame.dispose();
}
});
}
if (!passed) {
throw new RuntimeException("Test Failed");
}
}
static int getKeycode(int ctrl) {
switch (ctrl) {
case 1: return KeyEvent.VK_SHIFT;
case 2: return KeyEvent.VK_CONTROL;
case 3: return KeyEvent.VK_ALT;
default: return 0;
}
}
static int getKeyModifier(int ctrl) {
switch (ctrl) {
case 1: return InputEvent.SHIFT_MASK;
case 2: return InputEvent.CTRL_MASK;
case 3: return InputEvent.ALT_MASK;
default: return 0;
}
}
static int getMouseModifier(int btn) {
switch (btn) {
case 1: return InputEvent.BUTTON1_MASK;
case 2: return InputEvent.BUTTON2_MASK;
case 3: return InputEvent.BUTTON3_MASK;
default: return 0;
}
}
static final int allKeyMods =
InputEvent.SHIFT_MASK
| InputEvent.CTRL_MASK
| InputEvent.ALT_MASK;
static final int allMouseMods =
InputEvent.BUTTON1_MASK
| InputEvent.BUTTON2_MASK
| InputEvent.BUTTON3_MASK;
static void printInputEvent(InputEvent e) {
System.out.println(e);
if (e.isAltDown()) {
System.out.println("Alt is Down");
}
if (e.isControlDown()) {
System.out.println("Ctrl is Down");
}
if (e.isShiftDown()) {
System.out.println("Shift is Down");
}
if (e.isMetaDown()) {
System.out.println("Meta is Down");
}
}
static class TestCanvas extends Canvas {
public TestCanvas() {
setSize(100, 100);
setBackground(Color.blue);
enableEvents(AWTEvent.MOUSE_EVENT_MASK
| AWTEvent.MOUSE_MOTION_EVENT_MASK
| AWTEvent.KEY_EVENT_MASK);
}
protected void processMouseEvent(MouseEvent e) {
try {
if (testBtn == 0) {
return;
}
if (e.getID() == MouseEvent.MOUSE_ENTERED
|| e.getID() == MouseEvent.MOUSE_EXITED)
{
if ((e.getModifiers() & getMouseModifier(testBtn)) != 0) {
System.out.println("Mouse modifiers on MOUSE_ENTERED, MOUSE_EXITED are set");
} else {
printInputEvent(e);
System.out.println("Cur mods = " + (e.getModifiers() & allMouseMods) + " Wanted = " +
getMouseModifier(testBtn));
passed = false;
throw new RuntimeException("Mouse modifiers on MOUSE_ENTERED, MOUSE_EXITED aren't set");
}
}
if (e.getID() == MouseEvent.MOUSE_PRESSED
|| e.getID() == MouseEvent.MOUSE_RELEASED)
{
if ((e.getModifiers() & getMouseModifier(testBtn)) != 0) {
System.out.println("Right Mouse modifiers on MOUSE_PRESSED, MOUSE_RELEASED");
} else {
printInputEvent(e);
System.out.println("Cur mods = " + (e.getModifiers() & allMouseMods) + " Wanted = " +
getMouseModifier(testBtn));
passed = false;
throw new RuntimeException("Wrong Mouse modifiers on MOUSE_PRESSED, MOUSE_RELEASED");
}
}
} finally {
synchronized (frame) {
frame.notify();
}
}
}
protected void processMouseMotionEvent(MouseEvent e) {
try {
if (testBtn == 0) {
return;
}
if (e.getID() == MouseEvent.MOUSE_DRAGGED) {
if ((e.getModifiers() & getMouseModifier(testBtn)) != 0) {
System.out.println("Mouse modifiers on MOUSE_DRAGGED are set");
} else {
printInputEvent(e);
System.out.println("Cur mods = " + (e.getModifiers() & allMouseMods) + " Wanted = " +
getMouseModifier(testBtn));
passed = false;
throw new RuntimeException("Mouse modifiers on MOUSE_DRAGGED aren't set");
}
}
} finally {
synchronized (frame) {
frame.notify();
}
}
}
protected void processKeyEvent(KeyEvent e) {
try {
if (e.getKeyCode() != KeyEvent.VK_A) {
return;
}
if ((e.getModifiers() & getKeyModifier(testCtrl)) != 0) {
System.out.println("Right Key modifiers on KeyEvent");
} else {
printInputEvent(e);
passed = false;
throw new RuntimeException("Wrong Key modifiers on KeyEvent");
}
} finally {
synchronized (frame) {
frame.notify();
}
}
}
}
}

@ -0,0 +1,47 @@
/*
* Copyright (c) 2003, 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 4403712
@summary Error thrown in InvokeAndWait runnable causes hang
@run main CatchingThrowableTest
*/
import java.awt.EventQueue;
public class CatchingThrowableTest {
public static void main(String args[]) {
try {
EventQueue.invokeAndWait(() -> {
throw new RuntimeException("My Error");
});
} catch (InterruptedException ex) {
ex.printStackTrace();
} catch (java.lang.reflect.InvocationTargetException ex) {
ex.printStackTrace();
}
System.err.println("Test passed.");
}
}

@ -0,0 +1,140 @@
/*
* Copyright (c) 2000, 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 4343344
@summary Tests key modifiers when ALT_GRAPH key is pressed by Robot.
@key headful
@requires (os.family != "mac")
@run main AltGraphModifier
*/
import java.awt.AWTException;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.lang.reflect.InvocationTargetException;
public class AltGraphModifier {
static Frame frame;
static final int[] modifierKeys = {
KeyEvent.VK_ALT_GRAPH
};
static final int[] inputMasks = {
InputEvent.ALT_GRAPH_DOWN_MASK
};
static boolean[] modifierPress = new boolean[modifierKeys.length];
static volatile boolean modKeys;
static int modKeyCount;
static volatile boolean failed = false;
public static void main (String args[]) throws
InterruptedException, InvocationTargetException, AWTException {
EventQueue.invokeAndWait(() -> {
frame = new Frame("Modifier Robot Key BUG");
frame.setLayout(new FlowLayout());
frame.setSize(200, 200);
frame.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent kp){
System.out.println(kp);
if (modKeys == true) {
for (int i=0; i < modifierKeys.length; i++) {
if (modifierPress[i] == true) {
if ((kp.getModifiersEx() & inputMasks[i]) == inputMasks[i]) {
} else {
failed = true;
}
}
}
}
}
@Override
public void keyReleased(KeyEvent kr){
}
});
frame.setBackground(Color.blue);
frame.setVisible(true);
});
try {
Robot robot = new Robot();
robot.delay(1000);
robot.mouseMove((int) (frame.getLocationOnScreen().getX()
+ frame.getWidth() / 2),
(int) (frame.getLocationOnScreen().getY()
+ frame.getHeight() / 2));
robot.delay(1000);
robot.setAutoDelay(1000);
//Imbed operations here
modKeys = true;
for (modKeyCount = 0; modKeyCount < modifierKeys.length; modKeyCount++) {
//Press the Modifier Key
modifierPress[modKeyCount] = true;
robot.keyPress(modifierKeys[modKeyCount]);
frame.requestFocus();
robot.delay(1000);
//Press the Modifier Key
modifierPress[modKeyCount] = false;
robot.keyRelease(modifierKeys[modKeyCount]);
robot.delay(1000);
}
} finally {
EventQueue.invokeAndWait(() -> {
if (frame != null) {
frame.dispose();
}
});
}
if (failed) {
throw new RuntimeException("FAIL MESSAGE ---- Modifier "
+" Mask is not set when the Key : "
+"AltGraph"
+ " Key is pressed by Robot.\n");
}
}
}

@ -0,0 +1,138 @@
/*
* 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 4754155
* @summary Tests that KeyTyped events are fired for the Cancel key
* and that no extraneous characters are entered as a result
* @key headful
* @run main CancelKeyTyped
*/
import java.awt.AWTException;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Robot;
import java.awt.TextField;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.lang.reflect.InvocationTargetException;
public class CancelKeyTyped {
static volatile boolean cancelKeyTypedReceived = false;
static Frame frame;
static TextField tf;
static final String ORIGINAL = "0123456789";
public static void main(String[] args) throws AWTException,
InterruptedException, InvocationTargetException {
try {
Robot robot = new Robot();
robot.setAutoWaitForIdle(true);
robot.setAutoDelay(100);
EventQueue.invokeAndWait(CancelKeyTyped::init);
robot.waitForIdle();
robot.delay(1000);
// Press and release Cancel
robot.keyPress(KeyEvent.VK_CANCEL);
robot.keyRelease(KeyEvent.VK_CANCEL);
if (cancelKeyTypedReceived) {
if (tf.getText().equals(ORIGINAL)) {
System.out.println("Test PASSED");
} else {
System.out.println("Test FAILED: wrong string");
throw new RuntimeException("The test failed: wrong string: "
+ tf.getText());
}
}
} finally {
EventQueue.invokeAndWait(() -> {
if (frame != null) {
frame.dispose();
}
});
}
}
public static void init() {
frame = new Frame("CancelKeyTyped");
tf = new TextField(ORIGINAL, 20);
frame.add(tf);
frame.setSize(300, 100);
frame.setVisible(true);
frame.validate();
tf.requestFocusInWindow();
tf.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent evt) {
printKey(evt);
int keychar = evt.getKeyChar();
if (keychar == 24) { // Cancel character is 24 or \u0018
cancelKeyTypedReceived = true;
}
}
@Override
public void keyPressed(KeyEvent evt) {
printKey(evt);
}
@Override
public void keyReleased(KeyEvent evt) {
printKey(evt);
}
protected void printKey(KeyEvent evt) {
switch (evt.getID()) {
case KeyEvent.KEY_TYPED:
case KeyEvent.KEY_PRESSED:
case KeyEvent.KEY_RELEASED:
break;
default:
return;
}
System.out.println("params= " + evt.paramString() + " \n" +
"KeyChar: " + evt.getKeyChar() + " = " +
(int) evt.getKeyChar() +
" KeyCode: " + evt.getKeyCode() +
" Modifiers: " + evt.getModifiersEx());
if (evt.isActionKey()) {
System.out.println("Action Key");
}
System.out.println("keyText= " +
evt.getKeyText(evt.getKeyCode()) + "\n");
}
});
}
}