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