1 /*
   2  * Copyright (c) 2007, 2013, 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  * @test
  26  * @bug 8014863
  27  * @bug 8024395
  28  * @summary  Tests the calculation of the line breaks when a text is inserted
  29  * @author Dmitry Markov
  30  * @library ../../../regtesthelpers
  31  * @build Util
  32  * @run main bug8014863
  33  */
  34 
  35 import sun.awt.SunToolkit;
  36 
  37 import javax.swing.*;
  38 import javax.swing.text.GlyphView;
  39 import javax.swing.text.View;
  40 import javax.swing.text.html.HTMLEditorKit;
  41 import java.awt.*;
  42 import java.awt.event.KeyEvent;
  43 import java.lang.reflect.Field;
  44 import java.util.ArrayList;
  45 import java.util.Arrays;
  46 
  47 public class bug8014863 {
  48 
  49     private static JEditorPane editorPane;
  50     private static JFrame frame;
  51     private static Robot robot;
  52     private static SunToolkit toolkit;
  53 
  54     private static String text1 = "<p>one two qqqq <em>this is a test sentence</em> qqqq <em>pp</em> qqqq <em>pp</em> " +
  55             "qqqq <em>pp</em> qqqq <em>pp</em> qqqq <em>pp</em> qqqq <em>pp</em> qqqq <em>pp</em> qqqq <em>pp</em> qqqq</p>";
  56     private static String text2 = "<p>qqqq <em>this is a test sentence</em> qqqq <em>pp</em> qqqq <em>pp</em> " +
  57             "qqqq <em>pp</em> qqqq <em>pp</em> qqqq <em>pp</em> qqqq <em>pp</em> qqqq <em>pp</em> qqqq <em>pp</em> qqqq</p>";
  58 
  59     private static ArrayList<GlyphView> glyphViews;
  60 
  61     public static void main(String[] args) throws Exception {
  62         toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  63         robot = new Robot();
  64         robot.setAutoDelay(50);
  65         glyphViews = new ArrayList<GlyphView>();
  66 
  67         createAndShowGUI(text1);
  68 
  69         toolkit.realSync();
  70 
  71         SwingUtilities.invokeAndWait(new Runnable() {
  72             public void run() {
  73                 retrieveGlyphViews(editorPane.getUI().getRootView(editorPane));
  74             }
  75         });
  76         GlyphView [] arr1 = glyphViews.toArray(new GlyphView[glyphViews.size()]);
  77 
  78         frame.dispose();
  79         glyphViews.clear();
  80 
  81         createAndShowGUI(text2);
  82 
  83         toolkit.realSync();
  84 
  85         Util.hitKeys(robot, KeyEvent.VK_HOME);
  86         toolkit.realSync();
  87 
  88         Util.hitKeys(robot, KeyEvent.VK_O);
  89         Util.hitKeys(robot, KeyEvent.VK_N);
  90         Util.hitKeys(robot, KeyEvent.VK_E);
  91         Util.hitKeys(robot, KeyEvent.VK_SPACE);
  92         Util.hitKeys(robot, KeyEvent.VK_T);
  93         Util.hitKeys(robot, KeyEvent.VK_W);
  94         Util.hitKeys(robot, KeyEvent.VK_O);
  95         Util.hitKeys(robot, KeyEvent.VK_SPACE);
  96 
  97         toolkit.realSync();
  98 
  99         SwingUtilities.invokeAndWait(new Runnable() {
 100             public void run() {
 101                 retrieveGlyphViews(editorPane.getUI().getRootView(editorPane));
 102             }
 103         });
 104         GlyphView [] arr2 = glyphViews.toArray(new GlyphView[glyphViews.size()]);
 105 
 106         if (arr1.length != arr2.length) {
 107             throw new RuntimeException("Test Failed!");
 108         }
 109 
 110         for (int i=0; i<arr1.length; i++) {
 111             GlyphView v1 = arr1[i];
 112             GlyphView v2 = arr2[i];
 113             Field field = GlyphView.class.getDeclaredField("breakSpots");
 114             field.setAccessible(true);
 115             int[] breakSpots1 = (int[])field.get(v1);
 116             int[] breakSpots2 = (int[])field.get(v2);
 117             if (!Arrays.equals(breakSpots1,breakSpots2)) {
 118                 throw new RuntimeException("Test Failed!");
 119             }
 120         }
 121 
 122         frame.dispose();
 123     }
 124 
 125     private static void retrieveGlyphViews(View root) {
 126         for (int i=0; i<= root.getViewCount()-1; i++) {
 127             View view = root.getView(i);
 128             if (view instanceof GlyphView && view.isVisible()) {
 129                 if (!glyphViews.contains(view)) {
 130                     glyphViews.add((GlyphView)view);
 131                 }
 132             } else {
 133                 retrieveGlyphViews(view);
 134             }
 135         }
 136     }
 137 
 138     private static void createAndShowGUI(String text) throws Exception {
 139         SwingUtilities.invokeAndWait(new Runnable() {
 140             public void run() {
 141                 try {
 142                     UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
 143                 } catch (Exception ex) {
 144                     throw new RuntimeException(ex);
 145                 }
 146                 frame = new JFrame();
 147                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 148 
 149                 editorPane = new JEditorPane();
 150                 HTMLEditorKit editorKit = new HTMLEditorKit();
 151                 editorPane.setEditorKit(editorKit);
 152                 editorPane.setText(text);
 153                 editorPane.setCaretPosition(1);
 154 
 155                 frame.add(editorPane);
 156                 frame.setSize(200, 200);
 157                 frame.setVisible(true);
 158             }
 159         });
 160     }
 161 }