1 /* 2 * Copyright (c) 2010, 2019, 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 static org.junit.Assert.assertEquals; 29 import static org.junit.Assert.assertFalse; 30 import static org.junit.Assert.assertTrue; 31 import static org.junit.Assert.assertNotNull; 32 import javafx.geometry.VPos; 33 import test.javafx.scene.NodeTest; 34 import javafx.scene.text.Font; 35 import javafx.scene.text.Text; 36 import javafx.scene.layout.HBox; 37 import javafx.scene.Scene; 38 import javafx.stage.Stage; 39 40 import test.com.sun.javafx.pgstub.StubToolkit; 41 import com.sun.javafx.tk.Toolkit; 42 43 import org.junit.Test; 44 45 public class TextTest { 46 47 @Test public void testCtors() { 48 Text t1 = new Text(); 49 assertEquals("", t1.getText()); 50 Text t2 = new Text("test content"); 51 assertEquals("test content", t2.getText()); 52 Text t3 = new Text(10, 20, "2"); 53 assertEquals(10f, t3.getX(), 0); 54 assertEquals(20f, t3.getY(), 0); 55 assertEquals("2", t3.getText()); 56 } 57 58 @Test public void testSettingNullText() { 59 Text t = new Text(); 60 t.setText(null); 61 assertEquals("", t.getText()); 62 t.textProperty().set(null); 63 assertEquals("", t.getText()); 64 t.setText("1"); 65 assertEquals("1", t.getText()); 66 assertEquals("1", t.textProperty().get()); 67 t.setText(null); 68 assertEquals("", t.getText()); 69 t.textProperty().set(null); 70 assertEquals("", t.getText()); 71 } 72 73 @Test public void testDefaultTextNotNull() { 74 Text t = new Text(); 75 assertEquals("", t.getText()); 76 assertEquals("", t.textProperty().get()); 77 } 78 79 @Test public void testStoreFont() { 80 Text t = new Text(); 81 Font f = new Font(44); 82 assertEquals(Font.getDefault(), t.getFont()); 83 t.setFont(f); 84 assertEquals(44f, t.getBaselineOffset(), 0); 85 } 86 87 // Commented out as StubFontLoader only knows about Amble and its 88 // also not a given that the Font.getDefault() matches the default font 89 // on a Text node anyway, as CSS defaults are applied to the Text node. 90 /* 91 @Test public void testPropertyPropagation_font() throws Exception { 92 final Text node = new Text(); 93 NodeTest.testObjectPropertyPropagation(node, "font", Font.getDefault(), new Font(44)); 94 } 95 */ 96 97 // @Test public void testPropertyPropagation_textOrigin() throws Exception { 98 // final Text node = new Text(); 99 // NodeTest.testObjectPropertyPropagation(node, "textOrigin", "textOrigin", 100 // VPos.BASELINE, VPos.TOP, new NodeTest.ObjectValueConvertor() { 101 // @Override 102 // public Object toSg(Object pgValue) { 103 // return VPos.values()[((Number)pgValue).intValue()]; 104 // } 105 // }); 106 // } 107 108 // @Test public void testPropertyPropagation_boundsType() throws Exception { 109 // final Text node = new Text(); 110 // NodeTest.testObjectPropertyPropagation(node, "boundsType", "textBoundsType", 111 // TextBoundsType.LOGICAL, TextBoundsType.VISUAL, new NodeTest.ObjectValueConvertor() { 112 // @Override 113 // public Object toSg(Object pgValue) { 114 // return TextBoundsType.values()[((Number)pgValue).intValue()]; 115 // } 116 // }); 117 // } 118 119 // @Test public void testPropertyPropagation_textAlignment() throws Exception { 120 // final Text node = new Text(); 121 // NodeTest.testObjectPropertyPropagation(node, "textAlignment", "textAlignment", 122 // TextAlignment.LEFT, TextAlignment.CENTER, new NodeTest.ObjectValueConvertor() { 123 // @Override 124 // public Object toSg(Object pgValue) { 125 // return TextAlignment.values()[(((Number)pgValue).intValue())]; 126 // } 127 // }); 128 // } 129 130 // @Test public void testPropertyPropagation_visible() throws Exception { 131 // final Text node = new Text(); 132 // NodeTest.testBooleanPropertyPropagation(node, "visible", false, true); 133 // } 134 135 // @Test public void testPropertyPropagation_text() throws Exception { 136 // final Text node = new Text(); 137 // NodeTest.testObjectPropertyPropagation(node, "text", "text", "Hello", "World"); 138 // } 139 140 // @Test public void testPropertyPropagation_strikethrough() throws Exception { 141 // final Text node = new Text(); 142 // NodeTest.testBooleanPropertyPropagation(node, "strikethrough", false, true); 143 // } 144 145 // @Test public void testPropertyPropagation_underline() throws Exception { 146 // final Text node = new Text(); 147 // NodeTest.testBooleanPropertyPropagation(node, "underline", false, true); 148 // } 149 150 // @Test public void testPropertyPropagation_x() throws Exception { 151 // final Text node = new Text(); 152 // NodeTest.testDoublePropertyPropagation(node, "x", 100, 200); 153 // } 154 155 // @Test public void testPropertyPropagation_y() throws Exception { 156 // final Text node = new Text(); 157 // NodeTest.testDoublePropertyPropagation(node, "y", 100, 200); 158 // } 159 160 // @Test public void testPropertyPropagation_wrappingWidth() throws Exception { 161 // final Text node = new Text(); 162 // NodeTest.testDoublePropertyPropagation(node, "wrappingWidth", 100, 200); 163 // } 164 165 // @Test public void testBoundPropertySync_X() throws Exception { 166 // NodeTest.assertDoublePropertySynced( 167 // new Text(1.0, 2.0, "The Text"), 168 // "x", "x", 10.0); 169 // } 170 171 // @Test public void testBoundPropertySync_Y() throws Exception { 172 // NodeTest.assertDoublePropertySynced( 173 // new Text(1.0, 2.0, "The Text"), 174 // "y", "y", 20.0); 175 // } 176 177 // @Test public void testBoundPropertySync_Text() throws Exception { 178 // NodeTest.assertStringPropertySynced( 179 // new Text(1.0, 2.0, "The Text"), 180 // "text", "text", "The Changed Text"); 181 // } 182 183 // // The StubFontLoader is not adequate. SansSerif is the default font 184 // // family. But StubFontLoader is hard coded with some knowledge of 185 // // Amble so we end up with a null reference for its the PGFont 186 // // and it sets null on the PGText node. StubFontLoader needs to be 187 // // replaced with the real font loader. 188 // /* 189 // @Test public void testBoundPropertySync_Font() throws Exception { 190 // List<String> fontNames = Font.getFontNames(); 191 // String fontName = fontNames.get(fontNames.size() - 1); 192 // NodeTest.assertObjectPropertySynced( 193 // new Text(1.0, 2.0, "The Text"), 194 // "font", "font", new Font(fontName, 22)); 195 // } 196 // */ 197 198 // @Test public void testBoundPropertySync_BoundsType() throws Exception { 199 // NodeTest.assertObjectPropertySynced( 200 // new Text(1.0, 2.0, "The Text"), 201 // "boundsType", "textBoundsType", TextBoundsType.VISUAL); 202 // } 203 204 205 // @Test public void testBoundPropertySync_WrappingWidth() throws Exception { 206 // NodeTest.assertDoublePropertySynced( 207 // new Text(1.0, 2.0, "The Text"), 208 // "wrappingWidth", "wrappingWidth", 50); 209 // } 210 211 212 // @Test public void testBoundPropertySync_Underline() throws Exception { 213 // NodeTest.assertBooleanPropertySynced( 214 // new Text(1.0, 2.0, "The Text"), 215 // "underline", "underline", true); 216 // } 217 218 // @Test public void testBoundPropertySync_Strikethrough() throws Exception { 219 // NodeTest.assertBooleanPropertySynced( 220 // new Text(1.0, 2.0, "The Text"), 221 // "strikethrough", "strikethrough", true); 222 // } 223 224 // @Test public void testBoundPropertySync_TextAlignment() throws Exception { 225 // NodeTest.assertObjectPropertySynced( 226 // new Text(1.0, 2.0, "The Text"), 227 // "textAlignment", "textAlignment", TextAlignment.RIGHT); 228 // } 229 230 @Test public void toStringShouldReturnNonEmptyString() { 231 String s = new Text().toString(); 232 assertNotNull(s); 233 assertFalse(s.isEmpty()); 234 } 235 236 // Test for JDK-8130738 237 @Test public void testTabSize() { 238 Toolkit tk = (StubToolkit)Toolkit.getToolkit(); 239 HBox root = new HBox(); 240 Scene scene = new Scene(root); 241 Stage stage = new Stage(); 242 stage.setScene(scene); 243 stage.setWidth(300); 244 stage.setHeight(200); 245 246 try { 247 Text text = new Text("\tHello"); 248 root.getChildren().addAll(text); 249 stage.show(); 250 tk.firePulse(); 251 assertEquals(text.getTabSize(),8); 252 // initial width with default 8-space tab 253 double widthT8 = text.getBoundsInLocal().getWidth(); 254 text.setTabSize(1); 255 tk.firePulse(); 256 // width with tab at 1 spaces 257 double widthT1 = text.getBoundsInLocal().getWidth(); 258 // approximate width of a single space 259 double widthSpace = (widthT8 - widthT1) / 7; 260 assertTrue(widthSpace > 0); 261 text.setTabSize(4); 262 tk.firePulse(); 263 // width with tab at 4 spaces 264 double widthT4 = text.getBoundsInLocal().getWidth(); 265 double expected = widthT8 - 4 * widthSpace; 266 // should be approximately 4 space-widths shorter 267 assertEquals(expected, widthT4, 0.5); 268 } finally { 269 stage.hide(); 270 } 271 } 272 }