8306681: Open source more AWT DnD related tests

Reviewed-by: prr, serb
This commit is contained in:
Damon Nguyen 2023-04-28 19:20:16 +00:00
parent ec5c7926f3
commit 05af487a2d
7 changed files with 544 additions and 0 deletions

View File

@ -0,0 +1,82 @@
/*
* 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 4388802
@summary tests that getting clipboard data doesn't crash when there are no
formats on the clipboard
@key headful
@run main ZeroFormatTransferableTest
*/
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
public class ZeroFormatTransferableTest {
public static void main(String[] args) throws InterruptedException, IOException {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(new ZeroFormatTransferable(), null);
String javaPath = System.getProperty("java.home", "");
Process process = new ProcessBuilder(
javaPath + File.separator + "bin" + File.separator + "java",
"-cp",
System.getProperty("test.classes", "."),
"ZeroFormatTransferableTest").start();
process.waitFor();
InputStream errorStream = process.getErrorStream();
int count = errorStream.available();
if (count > 0) {
byte[] b = new byte[count];
errorStream.read(b);
System.err.println("========= Child VM System.err ========");
System.err.print(new String(b));
System.err.println("======================================");
}
}
}
class ZeroFormatTransferable implements Transferable {
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] {};
}
public boolean isDataFlavorSupported(DataFlavor df) {
return false;
}
public Object getTransferData(DataFlavor df)
throws UnsupportedFlavorException {
throw new UnsupportedFlavorException(df);
}
}

View File

@ -0,0 +1,75 @@
/*
* 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 4267393
@summary Ensures minimal amount of paints
@key headful
@run main ComponentResizedTest
*/
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.ComponentEvent;
import java.lang.reflect.InvocationTargetException;
public class ComponentResizedTest extends Frame {
volatile int paintCount = 0;
public static void main(String[] args) throws InterruptedException,
InvocationTargetException {
ComponentResizedTest componentResizedTest = new ComponentResizedTest();
EventQueue.invokeAndWait(componentResizedTest::init);
componentResizedTest.start();
if (componentResizedTest != null) EventQueue.invokeAndWait(()
-> componentResizedTest.dispose());
}
public void paint(Graphics g) {
System.out.println("Paint called");
++paintCount;
}
public void init() {
setTitle("ComponentResizedTest");
setSize(100, 100);
setLocationRelativeTo(null);
setVisible(true);
}
public void start () throws InterruptedException {
Thread.sleep(1000);
paintCount = 0;
dispatchEvent(new ComponentEvent(this, ComponentEvent.COMPONENT_RESIZED));
Thread.sleep(1000);
if (paintCount > 0) {
throw new RuntimeException("ComponentResizedTest failed. " +
"Paint called.");
}
}
}

View File

@ -0,0 +1,58 @@
/*
* 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.
@key headful
@run main ObjectSourceTest
*/
import java.awt.Component;
import java.awt.Frame;
import java.awt.event.ComponentEvent;
public class ObjectSourceTest {
static Frame frame;
public static void main(String[] args) {
frame = new Frame("ObjectSourceTest");
ComponentEvent ce = new ComponentEvent(frame, ComponentEvent.COMPONENT_SHOWN);
Object obj = new Object();
ce.setSource(obj);
Component comp = ce.getComponent();
if (comp != null) {
throw new RuntimeException("ObjectSourceTest failed. comp != null");
}
if (frame != null) {
frame.dispose();
}
}
}

View File

@ -0,0 +1,87 @@
/*
* 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 4523758
@requires (os.family == "windows")
@summary Checks denied setBounds doesn't generate ComponentEvent
@key headful
@run main ResizeDeniedTest
*/
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.lang.reflect.InvocationTargetException;
public class ResizeDeniedTest implements ComponentListener {
static int runs = 0;
static Frame frame;
public static void main(String[] args) throws InterruptedException,
InvocationTargetException {
ResizeDeniedTest resizeDeniedTest = new ResizeDeniedTest();
EventQueue.invokeAndWait(() -> {
frame = new Frame("ResizeDeniedTest");
frame.addComponentListener(resizeDeniedTest);
frame.setSize(1, 1);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
synchronized(resizeDeniedTest) {
resizeDeniedTest.wait(2000);
}
if (frame != null) {
EventQueue.invokeAndWait(() -> frame.dispose());
}
if (runs > 10) {
System.out.println("Infinite loop");
throw new RuntimeException("Infinite loop");
}
}
public void componentHidden(ComponentEvent e) {}
public void componentMoved(ComponentEvent e) {}
public void componentResized(ComponentEvent e) {
frame.setSize(1, 1);
System.out.println("Size " + frame.getSize());
++runs;
if (runs > 10) {
System.out.println("Infinite loop");
synchronized(this) {
this.notify();
}
throw new RuntimeException("Infinite loop");
}
}
public void componentShown(ComponentEvent e) {}
}

View File

@ -0,0 +1,167 @@
/*
* 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 4715486
@summary Tests that FocusEvent.opposite is not serialized
@key headful
@run main OppositeSerialization
*/
import java.awt.Button;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.FocusEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import static java.lang.Integer.valueOf;
/**
* "This is an AUTOMATIC test",
* "however, that's what it does:",
* "1. It tests that FocusEvent.opposite field is written",
* "to serialized stream as null regardless of whether it",
* "is actually null or not. For this purpose, we serialize",
* "a FocusEvent with really huge opposite, and then check",
* "if serialized object is huge or not.",
* "2. It tests that FocusEvent.opposite deserializes as",
* "null, even if it was serialized in the previous version",
* "of JDK. For this purpose, file old.ser is included into",
* "test. It is FocusEvent serialized with 1.4, with non-null",
* "opposite. We check that after deserialization opposite",
* "field is null"
*/
public class OppositeSerialization {
static Button b1;
static Frame b2;
public static void main(String[] args) throws InterruptedException,
InvocationTargetException {
EventQueue.invokeAndWait(() -> {
b1 = new Button("OppositeSerialization - Source");
b2 = new Frame("OppositeSerialization - Opposite");
b2.setLayout(new FlowLayout());
for (int i = 0; i < 10000; i++) {
String s = (valueOf(i)).toString();
b2.add(new Button("Button" + s));
}
});
FocusEvent evt1 = new FocusEvent(b1, FocusEvent.FOCUS_GAINED, false, b2);
/*
* Here we test that opposite component isn't serialized.
* We created a really huge opposite component for a focus
* event evt1 and now we'll see if the size of serialized data
* is big.
*/
try {
FileOutputStream fos = new FileOutputStream("new.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(evt1);
oos.flush();
} catch (Exception e1) {
System.out.println("Sorry! Couldn't write the stream");
System.out.println("The test failed, but the reason is " +
"unrelated to the subject");
throw new RuntimeException("The test couldn't write serialized data");
}
File file = new File("new.ser");
if (file.length() > 50000) {
System.out.println("The test failed: serialized " +
"FocusEvent too huge");
System.err.println("Serialized FocusEvent is too huge.");
System.err.println("Probably opposite field is " +
"serialized incorrectly.");
throw new RuntimeException("Serialized FocusEvent is too huge");
}
/*
* Here we test that opposite is not deserialized even if it is present
* in the stream. old.ser is created with JDK1.4 using the following
* source code:
*
* import java.awt.event.*;
* import java.io.*;
* import java.awt.*;
*
* public class OldFocusSerializer {
*
* public static void main(String[] args) {
*
* Button b1 = new Button("Source");
* Button b2 = new Button("Opposite");
*
* FocusEvent evt1 = new FocusEvent(b1,
* FocusEvent.FOCUS_GAINED,
* false,
* b2);
*
* try {
* FileOutputStream fos = new FileOutputStream("old.ser");
* ObjectOutputStream oos = new ObjectOutputStream(fos);
* oos.writeObject(evt1);
* oos.flush();
* } catch (IOException e) {
* System.out.println("Sorry! Couldn't write the stream");
* }
* }
* }
*/
FocusEvent evt2;
String testPath = System.getProperty("test.src", ".");
try {
FileInputStream fis = new FileInputStream(testPath +
File.separator + "old.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
evt2 = (FocusEvent)ois.readObject();
} catch (Exception e2) {
System.out.println("The test failed as it couldn't read the stream");
throw new RuntimeException("The test couldn't read serialized data");
}
if (evt2.getOppositeComponent() != null) {
System.out.println("The test failed: opposite component " +
"deserialized to non-null value");
System.err.println("FocusEvent stored in old.ser should have " +
"null opposite field.");
throw new RuntimeException("Non-null opposite component " +
"after deserialization");
}
if (b2 != null) {
EventQueue.invokeAndWait(() -> b2.dispose());
}
System.out.println("The test passed");
}
}

View File

@ -0,0 +1,75 @@
/*
* 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 4460376
@summary HierarchyEvents on Frame should be dispatched correctly
when on its child Window this event type enabled
@key headful
@run main HierarchyEventOnWindowTest
*/
import java.awt.AWTEvent;
import java.awt.Button;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.Window;
import java.awt.event.HierarchyBoundsAdapter;
import java.lang.reflect.InvocationTargetException;
public class HierarchyEventOnWindowTest {
static Frame frame;
public static void main(String args[]) throws InterruptedException,
InvocationTargetException {
EventQueue.invokeAndWait(() -> {
frame = new Frame("HierarchyEventOnWindowTest");
CustomWindow window = new CustomWindow(frame);
window.enableEvents();
frame.add(new Button(""));
window.disableEvents();
window.addHierarchyListener(e -> {});
window.addHierarchyBoundsListener(new HierarchyBoundsAdapter(){});
frame.add(new Button(""));
});
if (frame != null) {
EventQueue.invokeAndWait(() -> frame.dispose());
}
}
}
class CustomWindow extends Window {
public CustomWindow(Frame frame) {
super(frame);
}
public void enableEvents() {
enableEvents(AWTEvent.HIERARCHY_EVENT_MASK |
AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK);
}
public void disableEvents() {
disableEvents(AWTEvent.HIERARCHY_EVENT_MASK |
AWTEvent.HIERARCHY_BOUNDS_EVENT_MASK);
}
}