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