Skip to content

Commit 788d658

Browse files
committed
8306135: Clean up and open source some AWT tests
Backport-of: 4ad3ac6317f6fc95fdf0340885d4099e785132ad
1 parent 2f31b3f commit 788d658

File tree

3 files changed

+360
-0
lines changed

3 files changed

+360
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (c) 2002, 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 4653170
27+
@summary Make sure setCursor does not produce Arithmetic Exception.
28+
@key headful
29+
@run main SingleColorCursorTest
30+
*/
31+
32+
import java.awt.BorderLayout;
33+
import java.awt.Button;
34+
import java.awt.Cursor;
35+
import java.awt.EventQueue;
36+
import java.awt.Frame;
37+
import java.awt.Panel;
38+
import java.awt.Point;
39+
import java.awt.Toolkit;
40+
import java.awt.image.BufferedImage;
41+
import java.lang.reflect.InvocationTargetException;
42+
43+
public class SingleColorCursorTest extends Panel {
44+
public void init() {
45+
setLayout (new BorderLayout());
46+
setSize (200,200);
47+
add(new Button("JButton"));
48+
}
49+
50+
public void start () {
51+
Cursor singleColorCursor = Toolkit.getDefaultToolkit()
52+
.createCustomCursor(new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_BINARY),
53+
new Point(0,0), "Single Color Cursor");
54+
try {
55+
setCursor(singleColorCursor);
56+
} catch (ArithmeticException ae) {
57+
throw new RuntimeException("Setting a 1x1 custom cursor causes arithmetic exception");
58+
}
59+
}
60+
61+
public static void main(String[] args) throws InterruptedException, InvocationTargetException {
62+
EventQueue.invokeAndWait(() -> {
63+
Frame frame = new Frame("Test window");
64+
try {
65+
SingleColorCursorTest test = new SingleColorCursorTest();
66+
test.init();
67+
frame.add(test);
68+
frame.pack();
69+
frame.setLocationRelativeTo(null);
70+
frame.setVisible(true);
71+
test.start();
72+
frame.setVisible(false);
73+
} finally {
74+
frame.dispose();
75+
}
76+
});
77+
}
78+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 4274360
27+
@summary Ensures that Dialogs receive COMPONENT_SHOWN events
28+
@key headful
29+
@run main ComponentShownEvent
30+
*/
31+
32+
import java.awt.AWTException;
33+
import java.awt.Dialog;
34+
import java.awt.EventQueue;
35+
import java.awt.Frame;
36+
import java.awt.Robot;
37+
import java.awt.event.ComponentAdapter;
38+
import java.awt.event.ComponentEvent;
39+
import java.lang.reflect.InvocationTargetException;
40+
41+
public class ComponentShownEvent {
42+
43+
volatile boolean componentShown = false;
44+
Frame f;
45+
Dialog d;
46+
47+
public void start() throws InterruptedException,
48+
InvocationTargetException, AWTException {
49+
Robot robot = new Robot();
50+
try {
51+
EventQueue.invokeAndWait(() -> {
52+
f = new Frame();
53+
d = new Dialog(f);
54+
55+
d.addComponentListener(new ComponentAdapter() {
56+
public void componentShown(ComponentEvent e) {
57+
componentShown = true;
58+
}
59+
});
60+
61+
f.setSize(100, 100);
62+
f.setLocationRelativeTo(null);
63+
f.setVisible(true);
64+
d.setVisible(true);
65+
});
66+
67+
robot.waitForIdle();
68+
robot.delay(1000);
69+
70+
if (!componentShown) {
71+
throw new RuntimeException("test failed");
72+
}
73+
} finally {
74+
EventQueue.invokeAndWait(() -> {
75+
if (d != null) {
76+
d.setVisible(false);
77+
d.dispose();
78+
}
79+
if (f != null) {
80+
f.setVisible(false);
81+
f.dispose();
82+
}
83+
});
84+
}
85+
}
86+
87+
public static void main(String[] args) throws InterruptedException,
88+
InvocationTargetException, AWTException {
89+
ComponentShownEvent test = new ComponentShownEvent();
90+
test.start();
91+
System.out.println("test passed");
92+
}
93+
}
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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 4221123
27+
@summary Why Dialog can't be an owner of FileDialog?
28+
@key headful
29+
@run main DialogAsParentOfFileDialog
30+
*/
31+
32+
import java.awt.Dialog;
33+
import java.awt.EventQueue;
34+
import java.awt.FileDialog;
35+
import java.awt.Frame;
36+
import java.lang.reflect.InvocationTargetException;
37+
38+
public class DialogAsParentOfFileDialog {
39+
FileDialog fdialog;
40+
41+
public void start () {
42+
StringBuilder errors = new StringBuilder();
43+
String nl = System.lineSeparator();
44+
Dialog dlg;
45+
String title;
46+
int mode;
47+
boolean passed;
48+
49+
System.out.println("DialogAsParentOfFileDialog");
50+
51+
/*
52+
* public FileDialog(Dialog parent),
53+
* checks owner and default settings.
54+
*/
55+
System.out.print("\ttest 01: ");
56+
dlg = new Dialog(new Frame());
57+
fdialog = new FileDialog(dlg);
58+
passed =
59+
fdialog.getOwner() == dlg
60+
&& fdialog.isModal()
61+
&& fdialog.getTitle().equals("")
62+
&& fdialog.getMode() == FileDialog.LOAD
63+
&& fdialog.getFile() == null
64+
&& fdialog.getDirectory() == null
65+
&& fdialog.getFilenameFilter() == null;
66+
System.out.println(passed ? "passed" : "FAILED");
67+
if (!passed) {
68+
errors.append(nl);
69+
errors.append("DialogAsParentOfFileDialog FAILED");
70+
}
71+
72+
/*
73+
* public FileDialog(Dialog parent, String title),
74+
* checks owner, title and default settings.
75+
*/
76+
System.out.print("\ttest 02: ");
77+
dlg = new Dialog(new Frame());
78+
title = "Title";
79+
fdialog = new FileDialog(dlg, title);
80+
passed =
81+
fdialog.getOwner() == dlg
82+
&& fdialog.isModal()
83+
&& fdialog.getTitle().equals(title)
84+
&& fdialog.getMode() == FileDialog.LOAD
85+
&& fdialog.getFile() == null
86+
&& fdialog.getDirectory() == null
87+
&& fdialog.getFilenameFilter() == null;
88+
System.out.println(passed ? "passed" : "FAILED");
89+
if (!passed) {
90+
errors.append(nl);
91+
errors.append("DialogAsParentOfFileDialog FAILED");
92+
}
93+
94+
/*
95+
* public FileDialog(Dialog parent, String title),
96+
* title: null.
97+
* expected results: FileDialog object with a null title
98+
*/
99+
System.out.print("\ttest 03: ");
100+
dlg = new Dialog(new Frame());
101+
title = null;
102+
fdialog = new FileDialog(dlg, title);
103+
passed =
104+
fdialog.getOwner() == dlg
105+
&& (fdialog.getTitle() == null
106+
|| fdialog.getTitle().equals(""));
107+
System.out.println(passed ? "passed" : "FAILED");
108+
if (!passed) {
109+
errors.append(nl);
110+
errors.append("DialogAsParentOfFileDialog FAILED");
111+
}
112+
113+
/*
114+
* public FileDialog(Dialog parent, String title, int mode),
115+
* checks owner, title and mode.
116+
*/
117+
dlg = new Dialog(new Frame());
118+
title = "Title";
119+
120+
System.out.print("\ttest 04: ");
121+
mode = FileDialog.SAVE;
122+
fdialog = new FileDialog(dlg, title, mode);
123+
passed =
124+
fdialog.getOwner() == dlg
125+
&& fdialog.isModal()
126+
&& fdialog.getTitle().equals(title)
127+
&& fdialog.getMode() == mode
128+
&& fdialog.getFile() == null
129+
&& fdialog.getDirectory() == null
130+
&& fdialog.getFilenameFilter() == null;
131+
System.out.println(passed ? "passed" : "FAILED");
132+
if (!passed) {
133+
errors.append(nl);
134+
errors.append("DialogAsParentOfFileDialog FAILED");
135+
}
136+
137+
System.out.print("\ttest 05: ");
138+
mode = FileDialog.LOAD;
139+
fdialog = new FileDialog(dlg, title, mode);
140+
passed =
141+
fdialog.getOwner() == dlg
142+
&& fdialog.isModal()
143+
&& fdialog.getTitle().equals(title)
144+
&& fdialog.getMode() == mode
145+
&& fdialog.getFile() == null
146+
&& fdialog.getDirectory() == null
147+
&& fdialog.getFilenameFilter() == null;
148+
System.out.println(passed ? "passed" : "FAILED");
149+
if (!passed) {
150+
errors.append(nl);
151+
errors.append("DialogAsParentOfFileDialog FAILED");
152+
}
153+
154+
/*
155+
* public FileDialog(Dialog parent, String title, int mode),
156+
* mode: Integer.MIN_VALUE, Integer.MIN_VALUE+1,
157+
* Integer.MAX_VALUE-1, Integer.MAX_VALUE
158+
* expected results: IllegalArgumentException should be thrown
159+
*/
160+
System.out.print("\ttest 06: ");
161+
dlg = new Dialog(new Frame());
162+
title = "Title";
163+
int[] modes = {Integer.MIN_VALUE, Integer.MIN_VALUE+1,
164+
Integer.MAX_VALUE-1, Integer.MAX_VALUE};
165+
passed = true;
166+
for (int i = 0; i < modes.length; i++) {
167+
try {
168+
fdialog = new FileDialog(dlg, title, modes[i]);
169+
passed = false;
170+
} catch (IllegalArgumentException e) {}
171+
}
172+
System.out.println(passed ? "passed" : "FAILED");
173+
if (!passed) {
174+
errors.append(nl);
175+
errors.append("DialogAsParentOfFileDialog FAILED");
176+
}
177+
178+
if (!errors.isEmpty()) {
179+
throw new RuntimeException("Following tests failed:" + errors);
180+
}
181+
}
182+
183+
public static void main(String[] args) throws InterruptedException,
184+
InvocationTargetException {
185+
EventQueue.invokeAndWait(() -> {
186+
new DialogAsParentOfFileDialog().start();
187+
});
188+
}
189+
}

0 commit comments

Comments
 (0)