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