Skip to content

Commit

Permalink
8306718: Optimize and opensource some old AWT tests
Browse files Browse the repository at this point in the history
Reviewed-by: prr
  • Loading branch information
Alexander Zuev committed Apr 25, 2023
1 parent 36d61c3 commit 9beae21
Show file tree
Hide file tree
Showing 4 changed files with 410 additions and 0 deletions.
118 changes: 118 additions & 0 deletions test/jdk/java/awt/EventDispatchThread/StoppingEdtOnPushPopTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* 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 4848555
@summary Popping an event queue could cause its thread to restart inadvertently
@run main StoppingEdtOnPushPopTest
*/

import java.awt.AWTEvent;
import java.awt.ActiveEvent;
import java.awt.EventQueue;
import java.awt.Toolkit;

public class StoppingEdtOnPushPopTest implements Runnable {
public void start() {
int before = countEventQueues();
try {
for (int i = 0; i < 10; i++) {
EventQueue.invokeAndWait(this);
}
} catch (InterruptedException e) {
e.printStackTrace();
throw new RuntimeException("Test was interrupted");
} catch (java.lang.reflect.InvocationTargetException e) {
e.printStackTrace();
throw new RuntimeException("InvocationTargetException occurred");
}
pause(1000);
int after = countEventQueues();
if (before < after && after > 1) {
throw new RuntimeException("Test failed (before=" + before
+ "; after=" + after + ")");
}
System.out.println("Test passed");
}

public void run() {
System.out.println("push/pop");
MyEventQueue queue = new MyEventQueue();
Toolkit.getDefaultToolkit().getSystemEventQueue().push(queue);
Toolkit.getDefaultToolkit().getSystemEventQueue()
.postEvent(new EmptyEvent());
queue.pop();
}

public int countEventQueues() {
int count = 0;
System.out.println("All threads currently running in the system");
Thread threads[] = new Thread[Thread.activeCount()];
Thread.enumerate(threads);
for (int i = 0; i < threads.length; ++i) {
Thread thread = threads[i];
if (thread != null) {
System.out.println(thread.getName());
if (thread.getName().startsWith("AWT-EventQueue")) {
count++;
}
}
}
return count;
}

public void pause(long aMillis) {
try {
Thread.sleep(aMillis);
} catch (InterruptedException e) {
e.printStackTrace();
throw new RuntimeException("Test was interrupted");
}
}

public static void main(String[] args) {
StoppingEdtOnPushPopTest test = new StoppingEdtOnPushPopTest();
test.start();
}
}

class MyEventQueue extends EventQueue {
public MyEventQueue() {
super();
}

public void pop() {
super.pop();
}
}

class EmptyEvent extends AWTEvent implements ActiveEvent {
public EmptyEvent() {
super(new Object(), 0);
}

public void dispatch() {
System.out.println("one more EmptyEvent");
}
}
116 changes: 116 additions & 0 deletions test/jdk/java/awt/FileDialog/ExceptionAfterSetDirectory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright (c) 2005, 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 6308332
@summary FileDialog.setDirectory() throws exception on Linux & Solaris
@key headful
@run main ExceptionAfterSetDirectory
*/

import java.awt.AWTException;
import java.awt.EventQueue;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.lang.reflect.InvocationTargetException;

public class ExceptionAfterSetDirectory {
FileDialog fd = null;
Frame frame;

public void start() throws InterruptedException,
InvocationTargetException {
EventQueue.invokeAndWait(() -> {
frame = new Frame("ExceptionAfterSetDirectory");
frame.setLayout(new FlowLayout());
frame.setBounds(100, 100, 100, 100);
frame.setVisible(true);
fd = new FileDialog(frame, "file dialog", FileDialog.LOAD);
});

try {
test();
} catch (Exception e) {
throw new RuntimeException("Test failed.", e);
} finally {
if (frame != null) {
EventQueue.invokeAndWait(frame::dispose);
}
if (fd != null) {
EventQueue.invokeAndWait(fd::dispose);;
}
}
}

private void test() throws InterruptedException, InvocationTargetException {
final Robot r;

try {
r = new Robot();
} catch (AWTException e) {
throw new RuntimeException("Can not initialize Robot.", e);
}

r.setAutoDelay(200);
r.delay(500);

EventQueue.invokeLater(() -> {
fd.setVisible(true);
});
r.delay(2000);
r.waitForIdle();

if (System.getProperty("os.name").contains("OS X")) {
// Workaround for JDK-7186009 - try to close file dialog pressing escape
r.keyPress(KeyEvent.VK_ESCAPE);
r.keyRelease(KeyEvent.VK_ESCAPE);
r.delay(2000);
r.waitForIdle();
}

if (fd.isVisible()) {
EventQueue.invokeAndWait(() -> {
fd.setVisible(false);
});
r.delay(2000);
r.waitForIdle();
}

// Changing directory on hidden file dialog should not cause an exception
EventQueue.invokeAndWait(() -> {
fd.setDirectory("/");
});
r.delay(2000);
r.waitForIdle();
}

public static void main(String[] args) throws InterruptedException,
InvocationTargetException {
ExceptionAfterSetDirectory test = new ExceptionAfterSetDirectory();
test.start();
}
}
98 changes: 98 additions & 0 deletions test/jdk/java/awt/FlowLayout/MinimumLayoutSize.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright (c) 2005, 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 6257219
@summary FlowLayout gives a wrong minimum size if the first component is hidden.
@key headful
@run main MinimumLayoutSize
*/


import java.awt.AWTException;
import java.awt.Button;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.LayoutManager;
import java.awt.Panel;
import java.awt.Robot;
import java.lang.reflect.InvocationTargetException;

public class MinimumLayoutSize {
Frame frame;
Button b1;
Button b2;
Panel panel;

public void start() throws AWTException,
InterruptedException, InvocationTargetException {
try {
Robot robot = new Robot();
LayoutManager layout = new FlowLayout(FlowLayout.LEFT, 100, 0);
final int[] widths = new int[2];
EventQueue.invokeAndWait(() -> {
frame = new Frame("MinimumLayoutSize");
b1 = new Button("B1");
b2 = new Button("B2");
panel = new Panel();
panel.add(b2);
frame.add(panel);
frame.pack();
frame.setVisible(true);
});
robot.waitForIdle();
robot.delay(1000);
//add hidden component b1
EventQueue.invokeAndWait(() -> {
widths[0] = layout.minimumLayoutSize(panel).width;
b1.setVisible(false);
panel.add(b1, 0);
});
robot.waitForIdle();
robot.delay(1000);
EventQueue.invokeAndWait(() -> {
widths[1] = layout.minimumLayoutSize(panel).width;
frame.setVisible(false);
});
System.out.println("TRACE: w1 = " + widths[0] + " w2 = " + widths[1]);

if (widths[0] != widths[1]) {
throw new RuntimeException("Test FAILED. Minimum sizes are not equal."
+ " w1 = " + widths[0] + " w2 = " + widths[1]);
}
} finally {
if (frame != null) {
frame.dispose();
}
}
System.out.println("Test passed");
}

public static void main(String[] args) throws InterruptedException,
InvocationTargetException, AWTException {
MinimumLayoutSize test = new MinimumLayoutSize();
test.start();
}
}

3 comments on commit 9beae21

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GoeLin
Copy link
Member

@GoeLin GoeLin commented on 9beae21 Jul 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 9beae21 Jul 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GoeLin the backport was successfully created on the branch GoeLin-backport-9beae218 in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit 9beae218 from the openjdk/jdk repository.

The commit being backported was authored by Alexander Zuev on 25 Apr 2023 and was reviewed by Phil Race.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev.git GoeLin-backport-9beae218:GoeLin-backport-9beae218
$ git checkout GoeLin-backport-9beae218
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev.git GoeLin-backport-9beae218

Please sign in to comment.