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