Skip to content

Commit 8d696ae

Browse files
author
Alexander Zuev
committed
8306575: Clean up and open source four Dialog related tests
Reviewed-by: prr
1 parent 9ed456f commit 8d696ae

File tree

4 files changed

+587
-0
lines changed

4 files changed

+587
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright (c) 2004, 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 5006427
27+
@summary Shows many modal dialog and checks if there is a deadlock or thread race.
28+
@key headful
29+
@run main DialogDeadlockTest
30+
*/
31+
32+
import java.awt.BorderLayout;
33+
import java.awt.Button;
34+
import java.awt.Dialog;
35+
import java.awt.EventQueue;
36+
import java.awt.Frame;
37+
import java.awt.Window;
38+
import java.lang.reflect.InvocationTargetException;
39+
import java.util.LinkedList;
40+
import java.util.List;
41+
42+
public class DialogDeadlockTest {
43+
public static final int MAX_COUNT = 200;
44+
private static Dialog lastDialog;
45+
private static Runnable r;
46+
private static volatile int count;
47+
private static volatile int cumul;
48+
49+
public static void main(String[] args) throws InterruptedException,
50+
InvocationTargetException {
51+
DialogDeadlockTest ddt = new DialogDeadlockTest();
52+
ddt.start();
53+
}
54+
55+
public void start() {
56+
final Frame frame = new Frame("abc");
57+
final List<Window> toDispose = new LinkedList<>();
58+
59+
try {
60+
frame.setLocation(300, 0);
61+
frame.add(new Button("def"));
62+
frame.pack();
63+
frame.setVisible(true);
64+
cumul = 0;
65+
66+
r = new Runnable() {
67+
public void run() {
68+
count++;
69+
if (count < 10) {
70+
Dialog xlastDialog = lastDialog;
71+
cumul += count;
72+
Dialog d = new Dialog(frame, "Dialog "
73+
+ cumul, true);
74+
d.setLayout(new BorderLayout());
75+
d.add(new Button("button " + count), BorderLayout.CENTER);
76+
d.pack();
77+
toDispose.add(d);
78+
lastDialog = d;
79+
EventQueue.invokeLater(r);
80+
d.setVisible(true);
81+
if (xlastDialog != null) {
82+
xlastDialog.setVisible(false);
83+
} else {
84+
if (cumul < MAX_COUNT) {
85+
count = 0;
86+
lastDialog = null;
87+
EventQueue.invokeLater(r);
88+
}
89+
}
90+
} else {
91+
try {
92+
Thread.sleep(1000);
93+
} catch (InterruptedException ignore) {
94+
}
95+
lastDialog.setVisible(false);
96+
lastDialog = null;
97+
}
98+
}
99+
};
100+
try {
101+
EventQueue.invokeAndWait(r);
102+
} catch (InterruptedException ignore) {
103+
} catch (Exception e) {
104+
throw new RuntimeException("Unexpected exception: "
105+
+ e.getLocalizedMessage());
106+
}
107+
while (cumul < MAX_COUNT - 1) {
108+
try {
109+
Thread.sleep(1000);
110+
} catch (InterruptedException ignore) {}
111+
}
112+
System.out.println("Test PASSED");
113+
} finally {
114+
try {
115+
EventQueue.invokeAndWait(() -> {
116+
frame.setVisible(false);
117+
frame.dispose();
118+
for (Window w: toDispose) {
119+
w.dispose();
120+
}
121+
});
122+
} catch (InterruptedException e) {
123+
throw new RuntimeException(e);
124+
} catch (InvocationTargetException e) {
125+
throw new RuntimeException(e);
126+
}
127+
}
128+
}
129+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/*
2+
* Copyright (c) 1999, 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 4101437
27+
@summary Dialog.setLocation(int,int) works unstable when the dialog is visible
28+
@key headful
29+
@run main DialogLocationTest
30+
*/
31+
32+
import java.awt.AWTException;
33+
import java.awt.Container;
34+
import java.awt.Dialog;
35+
import java.awt.EventQueue;
36+
import java.awt.Frame;
37+
import java.awt.GraphicsEnvironment;
38+
import java.awt.Panel;
39+
import java.awt.Point;
40+
import java.awt.Rectangle;
41+
import java.awt.Robot;
42+
import java.awt.event.ComponentAdapter;
43+
import java.awt.event.ComponentEvent;
44+
import java.lang.reflect.InvocationTargetException;
45+
import java.util.Random;
46+
47+
public class DialogLocationTest extends Panel {
48+
private volatile int count = 0;
49+
private Dialog my_dialog;
50+
private volatile boolean waitingForEvent = false;
51+
private volatile int newX, newY;
52+
Random random = new Random();
53+
54+
public void init() {
55+
Container f = getParent();
56+
57+
while (!(f instanceof Frame)) {
58+
f = f.getParent();
59+
}
60+
61+
my_dialog = new Dialog((Frame) f, "TestDialog");
62+
my_dialog.setSize(150, 100);
63+
64+
setSize(200, 200);
65+
}
66+
67+
public void start() throws InterruptedException,
68+
InvocationTargetException {
69+
Robot robot;
70+
try {
71+
robot = new Robot();
72+
EventQueue.invokeAndWait(() -> {
73+
my_dialog.setLocationRelativeTo(null);
74+
my_dialog.setVisible(true);
75+
});
76+
robot.waitForIdle();
77+
robot.delay(1000);
78+
my_dialog.addComponentListener(new CL());
79+
setDialogLocation(my_dialog);
80+
} catch (AWTException e) {
81+
throw new RuntimeException(e);
82+
} finally {
83+
EventQueue.invokeAndWait(() -> {
84+
my_dialog.setVisible(false);
85+
my_dialog.dispose();
86+
});
87+
}
88+
}
89+
90+
public void setDialogLocation(Dialog dialog) {
91+
int height, width, insetX, insetY;
92+
Point curLoc;
93+
int i;
94+
95+
Rectangle screen = GraphicsEnvironment
96+
.getLocalGraphicsEnvironment()
97+
.getMaximumWindowBounds();
98+
height = screen.height;
99+
width = screen.width;
100+
insetX = screen.x;
101+
insetY = screen.y;
102+
103+
String message = "Failed on iteration %d expect:[%d,%d] "
104+
+ "reported:[%d,%d] diff:[%d,%d]";
105+
106+
for (i = 0; i < 100; i++) {
107+
newX = random.nextInt(insetX, width - 300);
108+
newY = random.nextInt(insetY, height - 400);
109+
110+
if (newX == 0 && newY == 0) {
111+
i--;
112+
continue;
113+
}
114+
115+
waitingForEvent = true;
116+
117+
EventQueue.invokeLater(() -> {
118+
dialog.setLocation(newX, newY);
119+
});
120+
121+
while (waitingForEvent) {
122+
Thread.yield();
123+
}
124+
125+
curLoc = dialog.getLocation();
126+
if (curLoc.x != newX || curLoc.y != newY) {
127+
count++;
128+
System.out.println(message.formatted(i, newX, newY,
129+
curLoc.x, curLoc.y, curLoc.x - newX, curLoc.y - newY));
130+
System.out.flush();
131+
}
132+
}
133+
134+
if (count > 0) {
135+
throw new RuntimeException("Dialog Location was set incorrectly");
136+
}
137+
}
138+
139+
public class CL extends ComponentAdapter {
140+
int lastX, lastY;
141+
String message = "Failed in componentMoved() expect:[%d,%d]"
142+
+ " reported: [%d,%d] diff [%d,%d]";
143+
144+
public void componentMoved(ComponentEvent e) {
145+
if (e.getComponent() == my_dialog) {
146+
Point eventLoc = e.getComponent().getLocation();
147+
if (lastX != eventLoc.x || lastY != eventLoc.y) {
148+
lastX = eventLoc.x;
149+
lastY = eventLoc.y;
150+
if (newX != 0 && newY != 0 && (eventLoc.x != newX || eventLoc.y != newY)) {
151+
count++;
152+
System.out.println(message.formatted(newX, newY,
153+
eventLoc.x, eventLoc.y,
154+
eventLoc.x - newX, eventLoc.y - newY));
155+
System.out.flush();
156+
}
157+
waitingForEvent = false;
158+
}
159+
}
160+
}
161+
}
162+
163+
public static void main(String[] args) throws InterruptedException,
164+
InvocationTargetException {
165+
Frame frame = new Frame("DialogLocationTest");
166+
try {
167+
DialogLocationTest test = new DialogLocationTest();
168+
EventQueue.invokeAndWait(() -> {
169+
frame.add(test);
170+
test.init();
171+
frame.setVisible(true);
172+
});
173+
test.start();
174+
} finally {
175+
EventQueue.invokeLater(() -> {
176+
frame.setVisible(false);
177+
frame.dispose();
178+
});
179+
}
180+
}
181+
}
182+

0 commit comments

Comments
 (0)