Skip to content

Commit ed698e6

Browse files
committed
8306718: Optimize and opensource some old AWT tests
Backport-of: 9beae21864d18054ca3762ec989d51ff0660db84
1 parent 5fd04f9 commit ed698e6

File tree

4 files changed

+410
-0
lines changed

4 files changed

+410
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
@test
26+
@bug 4848555
27+
@summary Popping an event queue could cause its thread to restart inadvertently
28+
@run main StoppingEdtOnPushPopTest
29+
*/
30+
31+
import java.awt.AWTEvent;
32+
import java.awt.ActiveEvent;
33+
import java.awt.EventQueue;
34+
import java.awt.Toolkit;
35+
36+
public class StoppingEdtOnPushPopTest implements Runnable {
37+
public void start() {
38+
int before = countEventQueues();
39+
try {
40+
for (int i = 0; i < 10; i++) {
41+
EventQueue.invokeAndWait(this);
42+
}
43+
} catch (InterruptedException e) {
44+
e.printStackTrace();
45+
throw new RuntimeException("Test was interrupted");
46+
} catch (java.lang.reflect.InvocationTargetException e) {
47+
e.printStackTrace();
48+
throw new RuntimeException("InvocationTargetException occurred");
49+
}
50+
pause(1000);
51+
int after = countEventQueues();
52+
if (before < after && after > 1) {
53+
throw new RuntimeException("Test failed (before=" + before
54+
+ "; after=" + after + ")");
55+
}
56+
System.out.println("Test passed");
57+
}
58+
59+
public void run() {
60+
System.out.println("push/pop");
61+
MyEventQueue queue = new MyEventQueue();
62+
Toolkit.getDefaultToolkit().getSystemEventQueue().push(queue);
63+
Toolkit.getDefaultToolkit().getSystemEventQueue()
64+
.postEvent(new EmptyEvent());
65+
queue.pop();
66+
}
67+
68+
public int countEventQueues() {
69+
int count = 0;
70+
System.out.println("All threads currently running in the system");
71+
Thread threads[] = new Thread[Thread.activeCount()];
72+
Thread.enumerate(threads);
73+
for (int i = 0; i < threads.length; ++i) {
74+
Thread thread = threads[i];
75+
if (thread != null) {
76+
System.out.println(thread.getName());
77+
if (thread.getName().startsWith("AWT-EventQueue")) {
78+
count++;
79+
}
80+
}
81+
}
82+
return count;
83+
}
84+
85+
public void pause(long aMillis) {
86+
try {
87+
Thread.sleep(aMillis);
88+
} catch (InterruptedException e) {
89+
e.printStackTrace();
90+
throw new RuntimeException("Test was interrupted");
91+
}
92+
}
93+
94+
public static void main(String[] args) {
95+
StoppingEdtOnPushPopTest test = new StoppingEdtOnPushPopTest();
96+
test.start();
97+
}
98+
}
99+
100+
class MyEventQueue extends EventQueue {
101+
public MyEventQueue() {
102+
super();
103+
}
104+
105+
public void pop() {
106+
super.pop();
107+
}
108+
}
109+
110+
class EmptyEvent extends AWTEvent implements ActiveEvent {
111+
public EmptyEvent() {
112+
super(new Object(), 0);
113+
}
114+
115+
public void dispatch() {
116+
System.out.println("one more EmptyEvent");
117+
}
118+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
@test
26+
@bug 6308332
27+
@summary FileDialog.setDirectory() throws exception on Linux & Solaris
28+
@key headful
29+
@run main ExceptionAfterSetDirectory
30+
*/
31+
32+
import java.awt.AWTException;
33+
import java.awt.EventQueue;
34+
import java.awt.FileDialog;
35+
import java.awt.FlowLayout;
36+
import java.awt.Frame;
37+
import java.awt.Robot;
38+
import java.awt.event.KeyEvent;
39+
import java.lang.reflect.InvocationTargetException;
40+
41+
public class ExceptionAfterSetDirectory {
42+
FileDialog fd = null;
43+
Frame frame;
44+
45+
public void start() throws InterruptedException,
46+
InvocationTargetException {
47+
EventQueue.invokeAndWait(() -> {
48+
frame = new Frame("ExceptionAfterSetDirectory");
49+
frame.setLayout(new FlowLayout());
50+
frame.setBounds(100, 100, 100, 100);
51+
frame.setVisible(true);
52+
fd = new FileDialog(frame, "file dialog", FileDialog.LOAD);
53+
});
54+
55+
try {
56+
test();
57+
} catch (Exception e) {
58+
throw new RuntimeException("Test failed.", e);
59+
} finally {
60+
if (frame != null) {
61+
EventQueue.invokeAndWait(frame::dispose);
62+
}
63+
if (fd != null) {
64+
EventQueue.invokeAndWait(fd::dispose);;
65+
}
66+
}
67+
}
68+
69+
private void test() throws InterruptedException, InvocationTargetException {
70+
final Robot r;
71+
72+
try {
73+
r = new Robot();
74+
} catch (AWTException e) {
75+
throw new RuntimeException("Can not initialize Robot.", e);
76+
}
77+
78+
r.setAutoDelay(200);
79+
r.delay(500);
80+
81+
EventQueue.invokeLater(() -> {
82+
fd.setVisible(true);
83+
});
84+
r.delay(2000);
85+
r.waitForIdle();
86+
87+
if (System.getProperty("os.name").contains("OS X")) {
88+
// Workaround for JDK-7186009 - try to close file dialog pressing escape
89+
r.keyPress(KeyEvent.VK_ESCAPE);
90+
r.keyRelease(KeyEvent.VK_ESCAPE);
91+
r.delay(2000);
92+
r.waitForIdle();
93+
}
94+
95+
if (fd.isVisible()) {
96+
EventQueue.invokeAndWait(() -> {
97+
fd.setVisible(false);
98+
});
99+
r.delay(2000);
100+
r.waitForIdle();
101+
}
102+
103+
// Changing directory on hidden file dialog should not cause an exception
104+
EventQueue.invokeAndWait(() -> {
105+
fd.setDirectory("/");
106+
});
107+
r.delay(2000);
108+
r.waitForIdle();
109+
}
110+
111+
public static void main(String[] args) throws InterruptedException,
112+
InvocationTargetException {
113+
ExceptionAfterSetDirectory test = new ExceptionAfterSetDirectory();
114+
test.start();
115+
}
116+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright (c) 2005, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
@test
26+
@bug 6257219
27+
@summary FlowLayout gives a wrong minimum size if the first component is hidden.
28+
@key headful
29+
@run main MinimumLayoutSize
30+
*/
31+
32+
33+
import java.awt.AWTException;
34+
import java.awt.Button;
35+
import java.awt.EventQueue;
36+
import java.awt.FlowLayout;
37+
import java.awt.Frame;
38+
import java.awt.LayoutManager;
39+
import java.awt.Panel;
40+
import java.awt.Robot;
41+
import java.lang.reflect.InvocationTargetException;
42+
43+
public class MinimumLayoutSize {
44+
Frame frame;
45+
Button b1;
46+
Button b2;
47+
Panel panel;
48+
49+
public void start() throws AWTException,
50+
InterruptedException, InvocationTargetException {
51+
try {
52+
Robot robot = new Robot();
53+
LayoutManager layout = new FlowLayout(FlowLayout.LEFT, 100, 0);
54+
final int[] widths = new int[2];
55+
EventQueue.invokeAndWait(() -> {
56+
frame = new Frame("MinimumLayoutSize");
57+
b1 = new Button("B1");
58+
b2 = new Button("B2");
59+
panel = new Panel();
60+
panel.add(b2);
61+
frame.add(panel);
62+
frame.pack();
63+
frame.setVisible(true);
64+
});
65+
robot.waitForIdle();
66+
robot.delay(1000);
67+
//add hidden component b1
68+
EventQueue.invokeAndWait(() -> {
69+
widths[0] = layout.minimumLayoutSize(panel).width;
70+
b1.setVisible(false);
71+
panel.add(b1, 0);
72+
});
73+
robot.waitForIdle();
74+
robot.delay(1000);
75+
EventQueue.invokeAndWait(() -> {
76+
widths[1] = layout.minimumLayoutSize(panel).width;
77+
frame.setVisible(false);
78+
});
79+
System.out.println("TRACE: w1 = " + widths[0] + " w2 = " + widths[1]);
80+
81+
if (widths[0] != widths[1]) {
82+
throw new RuntimeException("Test FAILED. Minimum sizes are not equal."
83+
+ " w1 = " + widths[0] + " w2 = " + widths[1]);
84+
}
85+
} finally {
86+
if (frame != null) {
87+
frame.dispose();
88+
}
89+
}
90+
System.out.println("Test passed");
91+
}
92+
93+
public static void main(String[] args) throws InterruptedException,
94+
InvocationTargetException, AWTException {
95+
MinimumLayoutSize test = new MinimumLayoutSize();
96+
test.start();
97+
}
98+
}

0 commit comments

Comments
 (0)