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