bug8058120.java revision 12677:a4299d47bd00
1/*
2 * Copyright (c) 2014, 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/* @test
25 * @bug 8058120
26 * @summary Rendering / caret errors with HTMLDocument
27 * @author Dmitry Markov
28 * @run main bug8058120
29 */
30
31import javax.swing.*;
32import javax.swing.text.Element;
33import javax.swing.text.html.HTML;
34import javax.swing.text.html.HTMLDocument;
35import javax.swing.text.html.HTMLEditorKit;
36import java.awt.*;
37
38public class bug8058120 {
39    private static HTMLDocument document = null;
40    private static final String text = "<p id = 'ab'>ab</p>";
41    private static final String textToInsert = "c";
42
43    public static void main(String[] args) {
44        Robot robot;
45        try {
46             robot = new Robot();
47        }catch(Exception ex) {
48            ex.printStackTrace();
49            throw new RuntimeException("Unexpected failure");
50        }
51        SwingUtilities.invokeLater(new Runnable() {
52            @Override
53            public void run() {
54                createAndShowGUI();
55            }
56        });
57
58        robot.waitForIdle();
59
60        SwingUtilities.invokeLater(new Runnable() {
61            @Override
62            public void run() {
63                try {
64                    document.insertAfterEnd(document.getElement("ab"), textToInsert);
65                } catch (Exception ex) {
66                    throw new RuntimeException(ex);
67                }
68            }
69        });
70
71        robot.waitForIdle();
72
73        SwingUtilities.invokeLater(new Runnable() {
74            @Override
75            public void run() {
76                Element parent = document.getElement("ab").getParentElement();
77                int count = parent.getElementCount();
78                if (count != 2) {
79                    throw new RuntimeException("Test Failed! Unexpected Element count = "+count);
80                }
81                Element insertedElement = parent.getElement(count - 1);
82                if (!HTML.Tag.IMPLIED.toString().equals(insertedElement.getName())) {
83                    throw new RuntimeException("Test Failed! Inserted text is not wrapped by " + HTML.Tag.IMPLIED + " tag");
84                }
85            }
86        });
87    }
88
89    private static void createAndShowGUI() {
90        try {
91            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
92        } catch (Exception ex) {
93            throw new RuntimeException(ex);
94        }
95
96        JFrame frame = new JFrame("bug8058120");
97        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
98
99        JEditorPane editorPane = new JEditorPane();
100        editorPane.setContentType("text/html");
101        editorPane.setEditorKit(new HTMLEditorKit());
102
103        document = (HTMLDocument) editorPane.getDocument();
104
105        editorPane.setText(text);
106
107        frame.add(editorPane);
108        frame.setSize(200, 200);
109        frame.setVisible(true);
110    }
111}
112