1 /* 2 * Copyright (c) 2010, 2014, 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 com.sun.javafx.css; 27 28 import com.sun.javafx.css.converters.FontConverter; 29 import com.sun.javafx.css.converters.SizeConverter; 30 import com.sun.javafx.css.parser.CSSParser; 31 import javafx.css.ParsedValue; 32 import javafx.scene.Group; 33 import javafx.scene.Scene; 34 import javafx.scene.text.Font; 35 import javafx.scene.text.FontPosture; 36 import javafx.scene.text.FontWeight; 37 import javafx.scene.text.Text; 38 39 import static org.junit.Assert.*; 40 import org.junit.Test; 41 42 43 public class FontTypeTest { 44 45 public FontTypeTest() { 46 } 47 48 void checkFont(Font expResult, Font result) { 49 assertEquals("family", expResult.getFamily(), result.getFamily()); 50 assertEquals("size", expResult.getSize(), result.getSize(), 0.001); 51 // TODO: how to check for weight and posture? 52 } 53 /** 54 * Test of convert method, of class FontType. 55 */ 56 @Test 57 public void testConvert_Value_Font() { 58 //System.out.println("convert"); 59 Font font = Font.getDefault(); 60 61 ParsedValue<String,String> family = new ParsedValueImpl<String,String>(font.getFamily(), null); 62 63 ParsedValue<ParsedValue<?,Size>,Number> size = 64 new ParsedValueImpl<ParsedValue<?,Size>,Number>( 65 new ParsedValueImpl<Size,Size>(new Size(2.0f, SizeUnits.EM), null), 66 SizeConverter.getInstance() 67 ); 68 69 ParsedValue<String,FontPosture> style = 70 new ParsedValueImpl<String,FontPosture>(FontPosture.REGULAR.name(), FontConverter.FontStyleConverter.getInstance()); 71 72 ParsedValue<String,FontWeight> weight = 73 new ParsedValueImpl<String,FontWeight>(FontWeight.NORMAL.name(), FontConverter.FontWeightConverter.getInstance()); 74 ParsedValue<ParsedValue[],Font> value = new ParsedValueImpl<ParsedValue[],Font>( 75 new ParsedValue[] {family, size, weight, style}, 76 FontConverter.getInstance() 77 ); 78 79 Font expResult = Font.font(font.getFamily(), font.getSize() * 2); 80 Font result = value.convert(font); 81 checkFont(expResult, result); 82 83 size = 84 new ParsedValueImpl<ParsedValue<?,Size>,Number>( 85 new ParsedValueImpl<Size,Size>(new Size(120, SizeUnits.PERCENT), null), 86 SizeConverter.getInstance() 87 ); 88 89 value = new ParsedValueImpl<ParsedValue[],Font>( 90 new ParsedValue[] {family, size, weight, style}, 91 FontConverter.getInstance() 92 ); 93 94 expResult = Font.font(font.getFamily(), font.getSize() * 1.2); 95 result = value.convert(font); 96 checkFont(expResult, result); 97 98 size = 99 new ParsedValueImpl<ParsedValue<?,Size>,Number>( 100 new ParsedValueImpl<Size,Size>(new Size(font.getSize(), SizeUnits.PT), null), 101 SizeConverter.getInstance() 102 ); 103 104 value = new ParsedValueImpl<ParsedValue[],Font>( 105 new ParsedValue[] {family, size, weight, style}, 106 FontConverter.getInstance() 107 ); 108 109 expResult = Font.font(font.getFamily(), font.getSize() * (96.0/72.0)); 110 result = value.convert(font); 111 checkFont(expResult, result); 112 113 } 114 115 @Test public void test_RT_21960_Bold_Italic() { 116 117 ParsedValue pv = CSSParser.getInstance().parseExpr("-fx-font", "italic bold 24 Amble"); 118 Font f = (Font)pv.convert(null); 119 assertEquals("Bold Italic", f.getStyle()); 120 assertEquals("Amble", f.getFamily()); 121 assertEquals(24, f.getSize(),0); 122 } 123 124 @Test public void test_RT_21960_Bold() { 125 126 ParsedValue pv = CSSParser.getInstance().parseExpr("-fx-font", "bold 24 Amble"); 127 Font f = (Font)pv.convert(null); 128 assertEquals("Bold", f.getStyle()); 129 assertEquals("Amble", f.getFamily()); 130 assertEquals(24, f.getSize(),0); 131 } 132 133 @Test public void test_RT_21960_Italic() { 134 135 ParsedValue pv = CSSParser.getInstance().parseExpr("-fx-font", "italic 24 Amble"); 136 Font f = (Font)pv.convert(null); 137 assertEquals("Italic", f.getStyle()); 138 assertEquals("Amble", f.getFamily()); 139 assertEquals(24, f.getSize(),0); 140 } 141 142 @Test public void test_RT_21960_Neither_Bold_Nor_Italic() { 143 144 ParsedValue pv = CSSParser.getInstance().parseExpr("-fx-font", "24 Amble"); 145 Font f = (Font)pv.convert(null); 146 assertEquals("Regular", f.getStyle()); 147 assertEquals("Amble", f.getFamily()); 148 assertEquals(24, f.getSize(),0); 149 } 150 151 @Test public void test_RT_25355_shorthandLast() { 152 153 Text txt = new Text("test_RT_25355"); 154 txt.setStyle("-fx-font-weight: bold; -fx-font: 16 Amble;"); 155 156 Scene scene = new Scene(new Group()); 157 ((Group)scene.getRoot()).getChildren().add(txt); 158 159 txt.applyCss(); 160 161 Font f = txt.getFont(); 162 assertEquals("Regular", f.getStyle()); 163 assertEquals("Amble", f.getFamily()); 164 assertEquals(16, f.getSize(),0); 165 } 166 167 @Test public void test_RT_25355_shorthandFirst() { 168 169 Text txt = new Text("test_RT_25355"); 170 txt.setStyle("-fx-font: 16 Amble; -fx-font-weight: bold;"); 171 172 Scene scene = new Scene(new Group()); 173 ((Group)scene.getRoot()).getChildren().add(txt); 174 175 txt.applyCss(); 176 177 Font f = txt.getFont(); 178 assertEquals("Bold", f.getStyle()); 179 assertEquals("Amble", f.getFamily()); 180 assertEquals(16, f.getSize(),0); 181 182 } 183 184 @Test public void test_RT_25355_shorthandFirstInheritedWeight() { 185 186 Text txt = new Text("test_RT_25355"); 187 txt.setStyle("-fx-font: 16 Amble;"); 188 189 Group g = new Group(); 190 g.setStyle("-fx-font-weight: bold"); 191 192 Scene scene = new Scene(g); 193 g.getChildren().add(txt); 194 195 g.applyCss(); 196 197 Font f = txt.getFont(); 198 assertEquals("Regular", f.getStyle()); 199 assertEquals("Amble", f.getFamily()); 200 assertEquals(16, f.getSize(),0); 201 202 } 203 204 @Test public void test_RT_25355_weightFirstInheritedShorthand() { 205 206 Text txt = new Text("test_RT_25355"); 207 txt.setStyle("-fx-font-weight: bold;"); 208 209 Group g = new Group(); 210 g.setStyle("-fx-font: 16 Amble;"); 211 212 Scene scene = new Scene(g); 213 g.getChildren().add(txt); 214 215 g.applyCss(); 216 217 Font f = txt.getFont(); 218 assertEquals("Bold", f.getStyle()); 219 assertEquals("Amble", f.getFamily()); 220 assertEquals(16, f.getSize(),0); 221 222 } 223 224 @Test public void testInheritedFontDoesNotOverrideUserSetFont() { 225 226 Text txt = new Text("testInheritedFontDoesNotOverrideUserSetFont"); 227 txt.setFont(Font.font("Amble", 32)); 228 229 Group g = new Group(); 230 g.setStyle("-fx-font: 16 Amble;"); 231 232 Scene scene = new Scene(g); 233 g.getChildren().add(txt); 234 235 g.applyCss(); 236 237 Font f = txt.getFont(); 238 assertEquals("Amble", f.getFamily()); 239 assertEquals(32, f.getSize(),0); 240 241 } 242 243 @Test public void testRT_32551() { 244 245 Text txt = new Text("testRT_32551"); 246 txt.setId("test-rt-32551"); 247 txt.setStyle("-fx-font-weight:bold;"); 248 249 Group g = new Group(); 250 251 Scene scene = new Scene(g); 252 scene.getStylesheets().add(FontTypeTest.class.getResource("HonorDeveloperSettingsTest_AUTHOR.css").toExternalForm()); 253 g.getChildren().add(txt); 254 255 g.applyCss(); 256 257 Font f = txt.getFont(); 258 // should get size and amble from .root, 'italic' from #test-rt-32551, bold from inline. 259 assertEquals("Amble Bold Italic", f.getName()); 260 assertEquals(20, f.getSize(),0); 261 262 } 263 264 @Test public void testRT_29773() { 265 266 Text txt = new Text("testRT_29773"); 267 txt.setId("test-rt-29773"); 268 269 Group g = new Group(); 270 271 Scene scene = new Scene(g); 272 scene.getStylesheets().add(FontTypeTest.class.getResource("HonorDeveloperSettingsTest_AUTHOR.css").toExternalForm()); 273 g.getChildren().add(txt); 274 275 g.applyCss(); 276 277 Font f = txt.getFont(); 278 // should get size and amble from .root, 'italic' from #test-rt-32551, bold from inline. 279 assertEquals("Amble Condensed", f.getName()); 280 assertEquals(20, f.getSize(),0); 281 282 } 283 }