Skip to content

Commit 3b397c8

Browse files
author
Alexander Zvegintsev
committedSep 21, 2023
8315965: Open source various AWT applet tests
Reviewed-by: honkar, psadhukhan
1 parent c698b45 commit 3b397c8

File tree

4 files changed

+546
-0
lines changed

4 files changed

+546
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
/*
2+
* Copyright (c) 1998, 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+
import java.awt.BorderLayout;
25+
import java.awt.Button;
26+
import java.awt.Component;
27+
import java.awt.Dimension;
28+
import java.awt.EventQueue;
29+
import java.awt.Frame;
30+
import java.awt.GridLayout;
31+
import java.awt.Panel;
32+
import java.awt.Point;
33+
import java.awt.Robot;
34+
import java.awt.ScrollPane;
35+
import java.awt.TextField;
36+
import java.awt.event.ActionEvent;
37+
import java.awt.event.ActionListener;
38+
import java.awt.event.InputEvent;
39+
40+
/*
41+
* @test
42+
* @bug 4124460
43+
* @key headful
44+
* @summary Test for initializing a Motif peer component causes a crash.
45+
*/
46+
47+
public class ScrollPaneTest {
48+
private static volatile Point p1 = null;
49+
private static volatile Point p2 = null;
50+
private static Robot robot = null;
51+
52+
private static Point getClickPoint(Component component) {
53+
Point locationOnScreen = component.getLocationOnScreen();
54+
Dimension size = component.getSize();
55+
locationOnScreen.x += size.width / 2;
56+
locationOnScreen.y += size.height / 2;
57+
return locationOnScreen;
58+
}
59+
public static void main(String[] args) throws Exception {
60+
robot = new Robot();
61+
robot.setAutoWaitForIdle(true);
62+
robot.setAutoDelay(100);
63+
64+
try {
65+
doTest();
66+
} finally {
67+
ScrollPaneTester.disposeAll();
68+
}
69+
}
70+
71+
private static void doTest() throws Exception {
72+
EventQueue.invokeAndWait(ScrollPaneTester::initAndShowGui);
73+
74+
robot.waitForIdle();
75+
robot.delay(1000);
76+
77+
EventQueue.invokeAndWait(() -> {
78+
p1 = getClickPoint(ScrollPaneTester.st1.buttonRight);
79+
p2 = getClickPoint(ScrollPaneTester.st1.buttonSwap);
80+
});
81+
82+
robot.mouseMove(p1.x, p1.y);
83+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
84+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
85+
86+
robot.mouseMove(p2.x, p2.y);
87+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
88+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
89+
90+
robot.delay(1000);
91+
92+
EventQueue.invokeAndWait(() -> {
93+
p1 = getClickPoint(ScrollPaneTester.st2.buttonRight);
94+
p2 = getClickPoint(ScrollPaneTester.st2.buttonSwap);
95+
});
96+
97+
robot.mouseMove(p1.x, p1.y);
98+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
99+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
100+
101+
robot.mouseMove(p2.x, p2.y);
102+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
103+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
104+
}
105+
}
106+
107+
class ScrollPaneTester implements ActionListener {
108+
static ScrollPaneTester st1, st2;
109+
final Button buttonLeft, buttonRight, buttonQuit, buttonSwap;
110+
protected ScrollPane sp;
111+
protected Frame f;
112+
113+
public static void initAndShowGui() {
114+
ScrollPaneTester.st1 = new ScrollPaneTester(true);
115+
ScrollPaneTester.st2 = new ScrollPaneTester(false);
116+
}
117+
118+
public ScrollPaneTester(boolean large) {
119+
sp = new ScrollPane(ScrollPane.SCROLLBARS_NEVER);
120+
121+
Panel p = new Panel();
122+
123+
if (large) {
124+
p.setLayout(new GridLayout(10, 10));
125+
for (int i = 0; i < 10; i++)
126+
for (int j = 0; j < 10; j++) {
127+
TextField tf = new TextField("I am " + i + j);
128+
tf.setSize(100, 20);
129+
p.add(tf);
130+
}
131+
} else {
132+
TextField tf = new TextField("Smallness:");
133+
tf.setSize(150, 50);
134+
p.add(tf);
135+
}
136+
137+
sp.add(p);
138+
139+
// Button panel
140+
buttonLeft = new Button("Left");
141+
buttonLeft.addActionListener(this);
142+
buttonQuit = new Button("Quit");
143+
buttonQuit.addActionListener(this);
144+
buttonSwap = new Button("Swap");
145+
buttonSwap.addActionListener(this);
146+
buttonRight = new Button("Right");
147+
buttonRight.addActionListener(this);
148+
149+
Panel bp = new Panel();
150+
bp.add(buttonLeft);
151+
bp.add(buttonSwap);
152+
bp.add(buttonQuit);
153+
bp.add(buttonRight);
154+
155+
// Window w/ button panel and scrollpane
156+
f = new Frame("ScrollPane Test " + (large ? "large" : "small"));
157+
f.setLayout(new BorderLayout());
158+
f.add("South", bp);
159+
f.add("Center", sp);
160+
161+
if (large) {
162+
f.setSize(300, 200);
163+
f.setLocation(100, 100);
164+
} else {
165+
f.setSize(200, 100);
166+
f.setLocation(500, 100);
167+
}
168+
169+
f.setVisible(true);
170+
}
171+
172+
public static void disposeAll() {
173+
ScrollPaneTester.st1.f.dispose();
174+
ScrollPaneTester.st2.f.dispose();
175+
}
176+
177+
public static void
178+
swapPanels() {
179+
ScrollPane sss = st1.sp;
180+
181+
st1.f.add("Center", st2.sp);
182+
st1.sp = st2.sp;
183+
184+
st2.f.add("Center", sss);
185+
st2.sp = sss;
186+
}
187+
188+
public void
189+
actionPerformed(ActionEvent ev) {
190+
Object s = ev.getSource();
191+
192+
if (s == buttonLeft) {
193+
scroll(true);
194+
} else if (s == buttonRight) {
195+
scroll(false);
196+
} else if (s == buttonSwap) {
197+
swapPanels();
198+
} else if (s == buttonQuit) {
199+
disposeAll();
200+
}
201+
}
202+
203+
private void
204+
scroll(boolean scroll_left) {
205+
Point p = sp.getScrollPosition();
206+
207+
if (scroll_left)
208+
p.x = Math.max(0, p.x - 20);
209+
else {
210+
int cwidth = sp.getComponent(0).getSize().width;
211+
p.x = Math.min(p.x + 20, cwidth);
212+
}
213+
214+
sp.setScrollPosition(p);
215+
}
216+
}
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
import java.awt.EventQueue;
25+
import java.awt.TextArea;
26+
27+
/*
28+
* @test
29+
* @bug 4120876
30+
* @key headful
31+
* @summary Ensure that getText can handle strings of various lengths,
32+
* in particular strings longer than 255 characters
33+
*/
34+
35+
public class Length {
36+
37+
public static void main(String[] args) throws Exception {
38+
EventQueue.invokeAndWait(() -> {
39+
TextArea ta = new TextArea();
40+
StringBuffer sb = new StringBuffer("x");
41+
42+
for (int i = 0; i < 14; i++) {
43+
String s = sb.toString();
44+
check(ta, s.substring(1));
45+
check(ta, s);
46+
check(ta, s + "y");
47+
sb.append(s);
48+
}
49+
});
50+
}
51+
52+
static void check(TextArea ta, String s) {
53+
ta.setText(s);
54+
String s2 = ta.getText();
55+
System.err.println(s.length() + " " + s2.length());
56+
if (s.length() != s2.length()) {
57+
throw new RuntimeException("Expected " + s.length() +
58+
"chars, got " + s2.length());
59+
}
60+
}
61+
}
+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* Copyright (c) 1998, 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+
import java.awt.Dialog;
25+
import java.awt.EventQueue;
26+
import java.awt.Frame;
27+
import java.awt.Label;
28+
import java.awt.Panel;
29+
import java.awt.Window;
30+
import java.util.ArrayList;
31+
import java.util.List;
32+
33+
/*
34+
* @test
35+
* @key headful
36+
* @summary automated test for window-ownership on Windows, Frames, and Dialogs
37+
*/
38+
39+
public class WindowOwner extends Panel {
40+
41+
Label status = null;
42+
static List<Window> windowsToDispose = new ArrayList<>();
43+
44+
public static void main(String[] args) throws Exception {
45+
WindowOwner windowOwner = new WindowOwner();
46+
try {
47+
EventQueue.invokeAndWait(windowOwner::init);
48+
Thread.sleep(2000);
49+
} finally {
50+
EventQueue.invokeAndWait(
51+
() -> windowsToDispose.forEach(Window::dispose)
52+
);
53+
}
54+
}
55+
56+
public void init() {
57+
status = new Label();
58+
add(status);
59+
60+
statusMessage("Testing Window Ownership...");
61+
62+
// Test Frame as owner
63+
Frame frame0 = new Frame("WindowOwner Test");
64+
windowsToDispose.add(frame0);
65+
frame0.add("Center", new Label("Frame Level0"));
66+
67+
Dialog dialog1 = new Dialog(frame0, "WindowOwner Test");
68+
windowsToDispose.add(dialog1);
69+
dialog1.add("Center", new Label("Dialog Level1"));
70+
verifyOwner(dialog1, frame0);
71+
72+
Window window1 = new Window(frame0);
73+
windowsToDispose.add(window1);
74+
window1.add("Center", new Label("Window Level1"));
75+
window1.setBounds(10, 10, 140, 70);
76+
verifyOwner(window1, frame0);
77+
78+
verifyOwnee(frame0, dialog1);
79+
verifyOwnee(frame0, window1);
80+
81+
// Test Dialog as owner
82+
Dialog dialog2 = new Dialog(dialog1, "WindowOwner Test");
83+
windowsToDispose.add(dialog2);
84+
dialog2.add("Center", new Label("Dialog Level2"));
85+
verifyOwner(dialog2, dialog1);
86+
87+
Window window2 = new Window(dialog1);
88+
windowsToDispose.add(window2);
89+
window2.add("Center", new Label("Window Level2"));
90+
window2.setBounds(110, 110, 140, 70);
91+
verifyOwner(window2, dialog1);
92+
93+
verifyOwnee(dialog1, window2);
94+
verifyOwnee(dialog1, dialog2);
95+
96+
// Test Window as owner
97+
Window window3 = new Window(window2);
98+
windowsToDispose.add(window3);
99+
window3.add("Center", new Label("Window Level3"));
100+
window3.setBounds(210, 210, 140, 70);
101+
verifyOwner(window3, window2);
102+
verifyOwnee(window2, window3);
103+
104+
// Ensure native peers handle ownership without errors
105+
frame0.pack();
106+
frame0.setVisible(true);
107+
108+
dialog1.pack();
109+
dialog1.setVisible(true);
110+
111+
window1.setLocation(50, 50);
112+
window1.setVisible(true);
113+
114+
dialog2.pack();
115+
dialog2.setVisible(true);
116+
117+
window2.setLocation(100, 100);
118+
window2.setVisible(true);
119+
120+
window3.setLocation(150, 150);
121+
window3.setVisible(true);
122+
123+
statusMessage("Window Ownership test completed successfully.");
124+
}
125+
126+
public void statusMessage(String msg) {
127+
status.setText(msg);
128+
status.invalidate();
129+
validate();
130+
}
131+
132+
public static void verifyOwner(Window ownee, Window owner) {
133+
if (ownee.getOwner() != owner) {
134+
throw new RuntimeException("Window owner not valid for "
135+
+ ownee.getName());
136+
}
137+
}
138+
139+
public static void verifyOwnee(Window owner, Window ownee) {
140+
Window[] ownedWins = owner.getOwnedWindows();
141+
if (!windowInList(ownedWins, ownee)) {
142+
throw new RuntimeException("Ownee " + ownee.getName()
143+
+ " not found in owner list for " + owner.getName());
144+
}
145+
}
146+
147+
public static boolean windowInList(Window[] windows, Window target) {
148+
for (Window window : windows) {
149+
if (window == target) {
150+
return true;
151+
}
152+
}
153+
return false;
154+
}
155+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
import java.awt.Color;
25+
import java.awt.Component;
26+
import java.awt.Dimension;
27+
import java.awt.Graphics;
28+
import java.awt.Graphics2D;
29+
import java.awt.GridLayout;
30+
import java.awt.Panel;
31+
import java.awt.event.WindowAdapter;
32+
import java.awt.event.WindowEvent;
33+
34+
import javax.swing.JFrame;
35+
import javax.swing.SwingUtilities;
36+
37+
/*
38+
* @test
39+
* @key headful
40+
* @bug 4240228
41+
* @summary This test is designed to test for a crashing bug in the zh
42+
* locale on Solaris. Rotated text should be displayed, but
43+
* anything other than a crash passes the specific test.
44+
* For example, the missing glyph empty box may be displayed
45+
* in some locales, or no text at all.
46+
*/
47+
48+
public class RotateTest3 extends Panel {
49+
static JFrame frame;
50+
51+
protected Java2DView java2DView;
52+
53+
public RotateTest3(){
54+
this.setLayout(new GridLayout(1, 1));
55+
this.setSize(300, 300);
56+
this.java2DView = new Java2DView();
57+
this.add(this.java2DView);
58+
}
59+
60+
public static void main(String[] s) throws Exception {
61+
try {
62+
SwingUtilities.invokeAndWait(RotateTest3::initAndShowGui);
63+
Thread.sleep(1000);
64+
} finally {
65+
SwingUtilities.invokeAndWait(() -> {
66+
if (frame != null) {
67+
frame.dispose();
68+
}
69+
});
70+
}
71+
}
72+
73+
private static void initAndShowGui() {
74+
RotateTest3 panel = new RotateTest3();
75+
76+
frame = new JFrame("RotateTest3");
77+
frame.addWindowListener(new WindowAdapter() {
78+
public void windowClosing(WindowEvent e) {
79+
frame.dispose();
80+
}
81+
});
82+
frame.getContentPane().setLayout(new GridLayout(1, 1));
83+
frame.getContentPane().add("Center", panel);
84+
frame.pack();
85+
frame.setLocationRelativeTo(null);
86+
frame.setVisible(true);
87+
}
88+
89+
static public class Java2DView extends Component {
90+
91+
public void paint(Graphics g){
92+
Graphics2D g2d = (Graphics2D) g;
93+
Dimension d = this.getSize();
94+
g.setColor(this.getBackground());
95+
g.fillRect(0, 0, d.width, d.height);
96+
g2d.setPaint(Color.black);
97+
98+
g2d.translate(150,150);
99+
g2d.rotate(-Math.PI / 3);
100+
101+
String testString =
102+
"\u4e00\u4e8c\u4e09\u56db\u4e94\u516d\u4e03\u516b\u4e5d\u5341";
103+
g2d.drawString(testString, 0, 0);
104+
}
105+
106+
public Dimension getMinimumSize(){
107+
return new Dimension(300, 300);
108+
}
109+
110+
public Dimension getPreferredSize(){
111+
return new Dimension(300, 300);
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)
Please sign in to comment.