1 /* 2 * Copyright (c) 2013, 2015, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package test.javafx.scene.text; 27 28 import com.sun.javafx.application.PlatformImpl; 29 import java.util.concurrent.CountDownLatch; 30 import java.util.concurrent.TimeUnit; 31 import javafx.geometry.Bounds; 32 import javafx.geometry.VPos; 33 import javafx.scene.shape.LineTo; 34 import javafx.scene.shape.MoveTo; 35 import javafx.scene.shape.PathElement; 36 import javafx.scene.text.Font; 37 import javafx.scene.text.Text; 38 39 import org.junit.BeforeClass; 40 import org.junit.Test; 41 import static org.junit.Assert.*; 42 43 public class TextNodeTest { 44 45 @BeforeClass 46 public static void initFX() { 47 final CountDownLatch startupLatch = new CountDownLatch(1); 48 PlatformImpl.startup(() -> { 49 startupLatch.countDown(); 50 }); 51 try { 52 if (!startupLatch.await(5, TimeUnit.SECONDS)) { 53 fail("Timeout waiting for FX runtime to start"); 54 } 55 } catch (InterruptedException ex) { 56 fail("Unexpected exception: " + ex); 57 } 58 } 59 60 @Test public void testBounds() { 61 Text text = new Text("a"); 62 Bounds bounds = text.getLayoutBounds(); 63 text.setText(""); 64 Bounds bounds2 = text.getLayoutBounds(); 65 66 //empty text should still have the same height 67 assertEquals(bounds.getHeight(), bounds2.getHeight(), 0.00001); 68 } 69 70 public void assertBoundsEquals(PathElement[] boundsShape, 71 double x, double y, 72 double w, double h) { 73 assertNotNull(boundsShape); 74 assertEquals(5, boundsShape.length); 75 assertNotNull(boundsShape[0]); 76 assertNotNull(boundsShape[1]); 77 assertNotNull(boundsShape[2]); 78 assertNotNull(boundsShape[3]); 79 assertNotNull(boundsShape[4]); 80 assertEquals(boundsShape[0].getClass(), MoveTo.class); 81 assertEquals(boundsShape[1].getClass(), LineTo.class); 82 assertEquals(boundsShape[2].getClass(), LineTo.class); 83 assertEquals(boundsShape[3].getClass(), LineTo.class); 84 assertEquals(boundsShape[4].getClass(), LineTo.class); 85 MoveTo m = (MoveTo)boundsShape[0]; 86 LineTo l0 = (LineTo)boundsShape[1]; 87 LineTo l1 = (LineTo)boundsShape[2]; 88 LineTo l2 = (LineTo)boundsShape[3]; 89 LineTo l3 = (LineTo)boundsShape[4]; 90 double e = 0.00001; 91 assertEquals(m.getX(), x, e); 92 assertEquals(m.getY(), y, e); 93 assertEquals(l0.getX(), x+w, e); 94 assertEquals(l0.getY(), y, e); 95 assertEquals(l1.getX(), x+w, e); 96 assertEquals(l1.getY(), y+h, e); 97 assertEquals(l2.getX(), x, e); 98 assertEquals(l2.getY(), y+h, e); 99 assertEquals(l3.getX(), x, e); 100 assertEquals(l3.getY(), y, e); 101 } 102 103 public void assertCaretEquals(PathElement[] caretShape, 104 double x0, double y0, 105 double x1, double y1) { 106 assertNotNull(caretShape); 107 assertEquals(2, caretShape.length); 108 assertNotNull(caretShape[0]); 109 assertNotNull(caretShape[1]); 110 assertEquals(caretShape[0].getClass(), MoveTo.class); 111 assertEquals(caretShape[1].getClass(), LineTo.class); 112 MoveTo m = (MoveTo)caretShape[0]; 113 LineTo l = (LineTo)caretShape[1]; 114 double e = 0.00001; 115 assertEquals(m.getX(), x0, e); 116 assertEquals(m.getY(), y0, e); 117 assertEquals(l.getX(), x1, e); 118 assertEquals(l.getY(), y1, e); 119 } 120 121 @SuppressWarnings("deprecation") 122 @Test public void testCaretShape() { 123 Font font = new Font("Monospaced Regular", 16); 124 Text text = new Text("a"); 125 text.setFont(font); 126 text.setTextOrigin(VPos.TOP); 127 //based on current implementation, caret just vertical line 128 Bounds bounds = text.getLayoutBounds(); 129 float lineHeight = (float)bounds.getHeight(); 130 float avgChar = (float)bounds.getWidth(); 131 PathElement[] empty = {}; 132 133 assertEquals(empty, text.getImpl_caretShape()); //initially empty 134 135 text.setImpl_caretPosition(0); 136 assertCaretEquals(text.getImpl_caretShape(), 0, 0, 0, lineHeight); 137 138 text.setImpl_caretPosition(-1); 139 assertEquals(empty, text.getImpl_caretShape()); //empty after -1 140 141 //set back 142 text.setImpl_caretPosition(0); 143 assertCaretEquals(text.getImpl_caretShape(), 0, 0, 0, lineHeight); 144 145 text.setImpl_caretBias(false); 146 text.setText("abc"); 147 assertEquals(empty, text.getImpl_caretShape()); //empty after setText 148 assertEquals(-1, text.getImpl_caretPosition()); 149 assertEquals(true, text.impl_caretBiasProperty().get()); 150 151 152 // trailing edges 153 text.setImpl_caretPosition(0); 154 text.setImpl_caretBias(true); 155 assertCaretEquals(text.getImpl_caretShape(), 0, 0, 0, lineHeight); 156 text.setImpl_caretPosition(0); 157 text.setImpl_caretBias(false); 158 assertCaretEquals(text.getImpl_caretShape(), avgChar, 0, avgChar, lineHeight); 159 text.setImpl_caretPosition(1); 160 text.setImpl_caretBias(true); 161 assertCaretEquals(text.getImpl_caretShape(), avgChar, 0, avgChar, lineHeight); 162 text.setImpl_caretPosition(1); 163 text.setImpl_caretBias(false); 164 assertCaretEquals(text.getImpl_caretShape(), avgChar*2, 0, avgChar*2, lineHeight); 165 text.setImpl_caretPosition(2); 166 text.setImpl_caretBias(true); 167 assertCaretEquals(text.getImpl_caretShape(), avgChar*2, 0, avgChar*2, lineHeight); 168 text.setImpl_caretPosition(2); 169 text.setImpl_caretBias(false); 170 assertCaretEquals(text.getImpl_caretShape(), avgChar*3, 0, avgChar*3, lineHeight); 171 172 //test length 173 text.setImpl_caretPosition(3); 174 text.setImpl_caretBias(true); 175 assertCaretEquals(text.getImpl_caretShape(), avgChar*3, 0, avgChar*3, lineHeight); 176 text.setImpl_caretPosition(3); 177 text.setImpl_caretBias(false); 178 assertCaretEquals(text.getImpl_caretShape(), avgChar*3, 0, avgChar*3, lineHeight); 179 180 //test out of bounds 181 text.setImpl_caretPosition(4); 182 text.setImpl_caretBias(true); 183 assertEquals(empty, text.getImpl_caretShape()); 184 text.setImpl_caretPosition(4); 185 text.setImpl_caretBias(false); 186 assertEquals(empty, text.getImpl_caretShape()); 187 188 //test empty text 189 text.setText(""); 190 text.setImpl_caretPosition(0); 191 text.setImpl_caretBias(true); 192 assertCaretEquals(text.getImpl_caretShape(), 0, 0, 0, lineHeight); 193 } 194 195 196 @SuppressWarnings("deprecation") 197 @Test public void testSelectionShape() { 198 Font font = new Font("Monospaced Regular", 16); 199 Text text = new Text("a"); 200 text.setFont(font); 201 text.setTextOrigin(VPos.TOP); 202 //based on current implementation, caret just vertical line 203 Bounds bounds = text.getLayoutBounds(); 204 float lineHeight = (float)bounds.getHeight(); 205 float avgChar = (float)bounds.getWidth(); 206 PathElement[] empty = {}; 207 208 assertEquals(empty, text.getImpl_selectionShape()); //initially null 209 210 text.setImpl_selectionStart(0); 211 assertEquals(empty, text.getImpl_selectionShape()); //set start, but not end 212 213 text.setImpl_selectionStart(0); 214 text.setImpl_selectionEnd(1); 215 assertBoundsEquals(text.getImpl_selectionShape(), 0, 0, avgChar, lineHeight); 216 217 text.setImpl_selectionStart(-1); 218 assertEquals(empty, text.getImpl_selectionShape());; //no start 219 220 text.setImpl_selectionStart(0); 221 text.setImpl_selectionEnd(1); 222 assertBoundsEquals(text.getImpl_selectionShape(), 0, 0, avgChar, lineHeight); 223 224 text.setImpl_selectionEnd(-1); 225 assertEquals(empty, text.getImpl_selectionShape()); //no end 226 227 text.setImpl_selectionStart(0); 228 text.setImpl_selectionEnd(1); 229 assertBoundsEquals(text.getImpl_selectionShape(), 0, 0, avgChar, lineHeight); 230 231 text.setImpl_selectionStart(1); 232 text.setImpl_selectionEnd(0); 233 assertEquals(empty, text.getImpl_selectionShape()); //end > start 234 235 text.setImpl_selectionStart(0); 236 text.setImpl_selectionEnd(1); 237 assertBoundsEquals(text.getImpl_selectionShape(), 0, 0, avgChar, lineHeight); 238 239 text.setImpl_selectionStart(0); 240 text.setImpl_selectionEnd(0); 241 assertEquals(empty, text.getImpl_selectionShape()); //end == start 242 243 text.setImpl_selectionStart(0); 244 text.setImpl_selectionEnd(1); 245 assertBoundsEquals(text.getImpl_selectionShape(), 0, 0, avgChar, lineHeight); 246 247 text.setImpl_selectionStart(0); 248 text.setImpl_selectionEnd(3); 249 assertEquals(empty, text.getImpl_selectionShape()); //end > length 250 251 text.setImpl_selectionStart(0); 252 text.setImpl_selectionEnd(1); 253 assertBoundsEquals(text.getImpl_selectionShape(), 0, 0, avgChar, lineHeight); 254 255 text.setImpl_selectionStart(3); 256 text.setImpl_selectionEnd(5); 257 assertEquals(empty, text.getImpl_selectionShape()); //start > length 258 259 text.setText("abc"); 260 assertEquals(empty, text.getImpl_selectionShape()); //setText resets 261 assertEquals(-1, text.getImpl_selectionStart()); 262 assertEquals(-1, text.getImpl_selectionEnd()); 263 264 text.setImpl_selectionStart(0); 265 text.setImpl_selectionEnd(0); 266 assertEquals(empty, text.getImpl_selectionShape()); 267 text.setImpl_selectionStart(0); 268 text.setImpl_selectionEnd(1); 269 assertBoundsEquals(text.getImpl_selectionShape(), 0, 0, avgChar, lineHeight); 270 text.setImpl_selectionStart(0); 271 text.setImpl_selectionEnd(2); 272 assertBoundsEquals(text.getImpl_selectionShape(), 0, 0, 2*avgChar, lineHeight); 273 text.setImpl_selectionStart(0); 274 text.setImpl_selectionEnd(3); 275 assertBoundsEquals(text.getImpl_selectionShape(), 0, 0, 3*avgChar, lineHeight); 276 text.setImpl_selectionStart(0); 277 text.setImpl_selectionEnd(4); 278 assertEquals(empty, text.getImpl_selectionShape()); 279 text.setImpl_selectionStart(1); 280 text.setImpl_selectionEnd(2); 281 assertBoundsEquals(text.getImpl_selectionShape(), avgChar, 0, avgChar, lineHeight); 282 text.setImpl_selectionStart(1); 283 text.setImpl_selectionEnd(3); 284 assertBoundsEquals(text.getImpl_selectionShape(), avgChar, 0, 2*avgChar, lineHeight); 285 text.setImpl_selectionStart(2); 286 text.setImpl_selectionEnd(3); 287 assertBoundsEquals(text.getImpl_selectionShape(), 2*avgChar, 0, avgChar, lineHeight); 288 text.setImpl_selectionStart(3); 289 text.setImpl_selectionEnd(3); 290 assertEquals(empty, text.getImpl_selectionShape()); 291 } 292 293 }