1 /*
   2  * Copyright (c) 2010, 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.  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.assertNotNull;
  31 import javafx.geometry.VPos;
  32 import test.javafx.scene.NodeTest;
  33 import javafx.scene.text.Font;
  34 import javafx.scene.text.Text;
  35 
  36 import org.junit.Test;
  37 
  38 public class TextTest {
  39     
  40     @Test public void testCtors() {
  41         Text t1 = new Text();
  42         assertEquals("", t1.getText());
  43         Text t2 = new Text("test content");
  44         assertEquals("test content", t2.getText());
  45         Text t3 = new Text(10, 20, "2");
  46         assertEquals(10f, t3.getX(), 0);
  47         assertEquals(20f, t3.getY(), 0);
  48         assertEquals("2", t3.getText());
  49     }
  50     
  51     @Test public void testSettingNullText() {
  52         Text t = new Text();
  53         t.setText(null);
  54         assertEquals("", t.getText());
  55         t.textProperty().set(null);
  56         assertEquals("", t.getText());
  57         t.setText("1");
  58         assertEquals("1", t.getText());
  59         assertEquals("1", t.textProperty().get());
  60         t.setText(null);
  61         assertEquals("", t.getText());
  62         t.textProperty().set(null);
  63         assertEquals("", t.getText());
  64     }
  65 
  66     @Test public void testDefaultTextNotNull() {
  67         Text t = new Text();
  68         assertEquals("", t.getText());
  69         assertEquals("", t.textProperty().get());
  70     }
  71     
  72     @Test public void testStoreFont() {
  73         Text t = new Text();
  74         Font f = new Font(44);
  75         assertEquals(Font.getDefault(), t.getFont());
  76         t.setFont(f);
  77         assertEquals(44f, t.getBaselineOffset(), 0);
  78     }
  79     
  80  // Commented out as StubFontLoader only knows about Amble and its
  81  // also not a given that the Font.getDefault() matches the default font
  82  // on a Text node anyway, as CSS defaults are applied to the Text node.
  83 /*
  84     @Test public void testPropertyPropagation_font() throws Exception {
  85         final Text node = new Text();
  86         NodeTest.testObjectPropertyPropagation(node, "font", Font.getDefault(), new Font(44));
  87     }
  88 */
  89 
  90 //     @Test public void testPropertyPropagation_textOrigin() throws Exception {
  91 //         final Text node = new Text();
  92 //         NodeTest.testObjectPropertyPropagation(node, "textOrigin", "textOrigin",
  93 //                 VPos.BASELINE, VPos.TOP, new NodeTest.ObjectValueConvertor() {
  94 //                     @Override
  95 //                     public Object toSg(Object pgValue) {
  96 //                         return VPos.values()[((Number)pgValue).intValue()];
  97 //                     }
  98 //                 });
  99 //     }
 100     
 101 //     @Test public void testPropertyPropagation_boundsType() throws Exception {
 102 //         final Text node = new Text();
 103 //         NodeTest.testObjectPropertyPropagation(node, "boundsType", "textBoundsType",
 104 //                 TextBoundsType.LOGICAL, TextBoundsType.VISUAL, new NodeTest.ObjectValueConvertor() {
 105 //                     @Override
 106 //                     public Object toSg(Object pgValue) {
 107 //                         return TextBoundsType.values()[((Number)pgValue).intValue()];
 108 //                     }
 109 //                 });
 110 //     }
 111     
 112 //     @Test public void testPropertyPropagation_textAlignment() throws Exception {
 113 //         final Text node = new Text();
 114 //         NodeTest.testObjectPropertyPropagation(node, "textAlignment", "textAlignment", 
 115 //                 TextAlignment.LEFT, TextAlignment.CENTER, new NodeTest.ObjectValueConvertor() {
 116 //                     @Override
 117 //                     public Object toSg(Object pgValue) {
 118 //                         return TextAlignment.values()[(((Number)pgValue).intValue())];
 119 //                     }
 120 //                 });
 121 //     }
 122     
 123 //     @Test public void testPropertyPropagation_visible() throws Exception {
 124 //         final Text node = new Text();
 125 //         NodeTest.testBooleanPropertyPropagation(node, "visible", false, true);
 126 //     }
 127 
 128 //     @Test public void testPropertyPropagation_text() throws Exception {
 129 //         final Text node = new Text();
 130 //         NodeTest.testObjectPropertyPropagation(node, "text", "text", "Hello", "World");
 131 //     }
 132 
 133 //     @Test public void testPropertyPropagation_strikethrough() throws Exception {
 134 //         final Text node = new Text();
 135 //         NodeTest.testBooleanPropertyPropagation(node, "strikethrough", false, true);
 136 //     }
 137 
 138 //     @Test public void testPropertyPropagation_underline() throws Exception {
 139 //         final Text node = new Text();
 140 //         NodeTest.testBooleanPropertyPropagation(node, "underline", false, true);
 141 //     }
 142 
 143 //     @Test public void testPropertyPropagation_x() throws Exception {
 144 //         final Text node = new Text();
 145 //         NodeTest.testDoublePropertyPropagation(node, "x", 100, 200);
 146 //     }
 147 
 148 //     @Test public void testPropertyPropagation_y() throws Exception {
 149 //         final Text node = new Text();
 150 //         NodeTest.testDoublePropertyPropagation(node, "y", 100, 200);
 151 //     }
 152 
 153 //     @Test public void testPropertyPropagation_wrappingWidth() throws Exception {
 154 //         final Text node = new Text();
 155 //         NodeTest.testDoublePropertyPropagation(node, "wrappingWidth", 100, 200);
 156 //     }
 157 
 158 //     @Test public void testBoundPropertySync_X() throws Exception {
 159 //         NodeTest.assertDoublePropertySynced(
 160 //                 new Text(1.0, 2.0, "The Text"),
 161 //                 "x", "x", 10.0);
 162 //     }
 163 
 164 //     @Test public void testBoundPropertySync_Y() throws Exception {
 165 //         NodeTest.assertDoublePropertySynced(
 166 //                 new Text(1.0, 2.0, "The Text"),
 167 //                 "y", "y", 20.0);
 168 //     }
 169 
 170 //     @Test public void testBoundPropertySync_Text() throws Exception {
 171 //         NodeTest.assertStringPropertySynced(
 172 //                 new Text(1.0, 2.0, "The Text"),
 173 //                 "text", "text", "The Changed Text");
 174 //     }
 175 
 176 //     // The StubFontLoader is not adequate. SansSerif is the default font
 177 //     // family. But StubFontLoader is hard coded with some knowledge of
 178 //     // Amble so we end up with a null reference for its the PGFont
 179 //     // and it sets null on the PGText node. StubFontLoader needs to be
 180 //     // replaced with the real font loader.
 181 // /*
 182 //     @Test public void testBoundPropertySync_Font() throws Exception {
 183 //         List<String> fontNames = Font.getFontNames();
 184 //         String fontName = fontNames.get(fontNames.size() - 1);
 185 //         NodeTest.assertObjectPropertySynced(
 186 //                 new Text(1.0, 2.0, "The Text"),
 187 //                 "font", "font", new Font(fontName, 22));
 188 //     }
 189 // */
 190 
 191 //     @Test public void testBoundPropertySync_BoundsType() throws Exception {
 192 //         NodeTest.assertObjectPropertySynced(
 193 //                 new Text(1.0, 2.0, "The Text"),
 194 //                 "boundsType", "textBoundsType", TextBoundsType.VISUAL);
 195 //     }
 196 
 197     
 198 //     @Test public void testBoundPropertySync_WrappingWidth() throws Exception {
 199 //         NodeTest.assertDoublePropertySynced(
 200 //                 new Text(1.0, 2.0, "The Text"),
 201 //                 "wrappingWidth", "wrappingWidth", 50);
 202 //     }
 203     
 204 
 205 //     @Test public void testBoundPropertySync_Underline() throws Exception {
 206 //         NodeTest.assertBooleanPropertySynced(
 207 //                 new Text(1.0, 2.0, "The Text"),
 208 //                 "underline", "underline", true);
 209 //     }
 210 
 211 //     @Test public void testBoundPropertySync_Strikethrough() throws Exception {
 212 //         NodeTest.assertBooleanPropertySynced(
 213 //                 new Text(1.0, 2.0, "The Text"),
 214 //                 "strikethrough", "strikethrough", true);
 215 //     }
 216 
 217 //     @Test public void testBoundPropertySync_TextAlignment() throws Exception {
 218 //         NodeTest.assertObjectPropertySynced(
 219 //                 new Text(1.0, 2.0, "The Text"),
 220 //                 "textAlignment", "textAlignment", TextAlignment.RIGHT);
 221 //     }
 222 
 223     @Test public void toStringShouldReturnNonEmptyString() {
 224         String s = new Text().toString();
 225         assertNotNull(s);
 226         assertFalse(s.isEmpty());
 227     }
 228 }