Skip to content

Commit 4cec951

Browse files
author
Amos Shi
committed
8315889: Open source several Swing HTMLDocument related tests
Backport-of: 8f46abc938ffe338e25d5fdbdcfa0aaa12edfa58
1 parent 52d1f0c commit 4cec951

File tree

4 files changed

+250
-0
lines changed

4 files changed

+250
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
import javax.swing.JEditorPane;
26+
import java.io.ByteArrayOutputStream;
27+
import java.io.ObjectOutputStream;
28+
29+
/*
30+
* @test
31+
* @bug 4226914
32+
* @summary Tests if HTMLDocument streaming is broken
33+
*/
34+
35+
public class bug4226914 {
36+
37+
public static void main(String[] args) throws Exception {
38+
ObjectOutputStream oos = null;
39+
try {
40+
JEditorPane jtp = new JEditorPane("text/html", "<html></html>");
41+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
42+
oos = new ObjectOutputStream(baos);
43+
oos.writeObject(jtp.getDocument());
44+
oos.flush();
45+
baos.toByteArray();
46+
} finally {
47+
if (oos != null) {
48+
oos.close();
49+
}
50+
}
51+
}
52+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.event.ActionEvent;
25+
import javax.swing.Action;
26+
import javax.swing.JTextPane;
27+
import javax.swing.SwingUtilities;
28+
import javax.swing.text.html.HTML;
29+
import javax.swing.text.html.HTMLEditorKit;
30+
31+
/*
32+
* @test
33+
* @bug 4251593
34+
* @summary Tests that hyperlinks can be inserted into JTextPane
35+
* via InsertHTMLTextAction.
36+
*/
37+
38+
public class bug4251593 {
39+
private static JTextPane editor;
40+
41+
public static void main(String[] args) throws Exception {
42+
SwingUtilities.invokeAndWait(() -> {
43+
editor = new JTextPane();
44+
editor.setContentType("text/html");
45+
editor.setEditable(true);
46+
47+
int beforeLen = editor.getDocument().getLength();
48+
49+
String href = "<a HREF=\"https://java.sun.com\">javasoft </a>";
50+
Action a = new HTMLEditorKit.InsertHTMLTextAction("Tester", href, HTML.Tag.BODY, HTML.Tag.A);
51+
a.actionPerformed(new ActionEvent(editor, 0, null));
52+
53+
int afterLen = editor.getDocument().getLength();
54+
try {
55+
Thread.sleep(300);
56+
} catch (InterruptedException e) {
57+
e.printStackTrace();
58+
}
59+
if ((afterLen - beforeLen) < 8) {
60+
throw new RuntimeException("Test Failed: link not inserted!!");
61+
}
62+
});
63+
}
64+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
import javax.swing.JEditorPane;
25+
import javax.swing.SwingUtilities;
26+
import javax.swing.text.AttributeSet;
27+
import javax.swing.text.View;
28+
import javax.swing.text.html.CSS;
29+
import javax.swing.text.html.HTMLEditorKit;
30+
31+
/*
32+
* @test
33+
* @bug 4687405
34+
* @summary Tests if HTMLDocument very first paragraph doesn't have top margin.
35+
*/
36+
37+
public class bug4687405 {
38+
private static JEditorPane jep;
39+
private static volatile boolean passed = false;
40+
41+
public static void main(String[] args) throws Exception {
42+
SwingUtilities.invokeAndWait(bug4687405::createHTMLEditor);
43+
Thread.sleep(200);
44+
45+
SwingUtilities.invokeAndWait(bug4687405::testEditorPane);
46+
Thread.sleep(500);
47+
48+
if (!passed) {
49+
throw new RuntimeException("Test failed!!" +
50+
" Top margin present in HTMLDocument");
51+
}
52+
}
53+
54+
public static void createHTMLEditor() {
55+
jep = new JEditorPane();
56+
jep.setEditorKit(new HTMLEditorKit());
57+
jep.setEditable(false);
58+
}
59+
60+
private static void testEditorPane() {
61+
View v = jep.getUI().getRootView(jep);
62+
while (!(v instanceof javax.swing.text.html.ParagraphView)) {
63+
int n = v.getViewCount();
64+
v = v.getView(n - 1);
65+
}
66+
AttributeSet attrs = v.getAttributes();
67+
String marginTop = attrs.getAttribute(CSS.Attribute.MARGIN_TOP).toString();
68+
// MARGIN_TOP of the very first paragraph of the default html
69+
// document should be 0.
70+
passed = "0".equals(marginTop);
71+
}
72+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 javax.swing.text.html.HTMLEditorKit;
25+
import java.io.ByteArrayInputStream;
26+
import java.io.ByteArrayOutputStream;
27+
import java.io.ObjectInputStream;
28+
import java.io.ObjectOutputStream;
29+
30+
/*
31+
* @test
32+
* @bug 4213373
33+
* @summary Serialization bug on HTMLEditorKit.
34+
*/
35+
36+
public class bug4213373 {
37+
38+
public static void main(String[] args) throws Exception {
39+
HTMLEditorKit ekr = null;
40+
ObjectOutputStream oos = null;
41+
ObjectInputStream ois = null;
42+
43+
try {
44+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
45+
HTMLEditorKit ekw = new HTMLEditorKit();
46+
oos = new ObjectOutputStream(baos);
47+
oos.writeObject(ekw);
48+
byte[] buf = baos.toByteArray();
49+
50+
ByteArrayInputStream bais = new ByteArrayInputStream(buf);
51+
ois = new ObjectInputStream(bais);
52+
ekr = (HTMLEditorKit) ois.readObject();
53+
} finally {
54+
if (oos != null) {
55+
oos.close();
56+
}
57+
if (ois != null) {
58+
ois.close();
59+
}
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)