1 /*
   2  * Copyright (c) 2011, 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 static test.com.sun.javafx.test.TestHelper.assertImmutableList;
  29 import static junit.framework.Assert.assertEquals;
  30 import static junit.framework.Assert.assertNotNull;
  31 import static junit.framework.Assert.assertNotSame;
  32 import static junit.framework.Assert.assertTrue;
  33 
  34 import java.util.List;
  35 import javafx.scene.text.Font;
  36 import javafx.scene.text.FontPosture;
  37 import javafx.scene.text.FontWeight;
  38 
  39 import org.junit.Test;
  40 
  41 public class FontTest {
  42 
  43     @Test
  44     public void testGetFamilies() {
  45         List<String> families = Font.getFamilies();
  46         assertNotNull(families);
  47         assertImmutableList(families);
  48     }
  49 
  50     @Test
  51     public void testGetAllFontNames() {
  52         List<String> names = Font.getFontNames();
  53         assertNotNull(names);
  54         assertImmutableList(names);
  55     }
  56 
  57     @Test
  58     public void testGetFontNames() {
  59         String family = Font.getFamilies().get(0);
  60         List<String> names = Font.getFontNames(family);
  61         assertNotNull(names);
  62         assertImmutableList(names);
  63     }
  64 
  65     @Test
  66     public void testFontFactory1() {
  67         Font font = Font.font("Amble", FontWeight.NORMAL,
  68                               FontPosture.ITALIC, 30);
  69         assertEquals("Amble", font.getFamily());
  70         assertEquals("Amble Italic", font.getName());
  71         assertEquals(30f, (float) font.getSize());
  72 
  73         font = Font.font(null, null, null, -1);
  74         // The tests against System as default are all commented out
  75         // as it needs a re-working of StubFontLoader which I consider
  76         // pointless as its not testing the product, and may give a
  77         // fall belief that the product passes.
  78         //assertEquals("System", font.getFamily());
  79         assertTrue(0 < font.getSize());
  80     }
  81 
  82     @Test
  83     public void testFontFactory2() {
  84         Font font = Font.font("Amble", FontWeight.BOLD, 30);
  85         assertEquals("Amble", font.getFamily());
  86         assertEquals("Amble Bold", font.getName());
  87         assertEquals(30f, (float) font.getSize());
  88     }
  89 
  90     @Test
  91     public void testFontFactory3() {
  92         Font font = Font.font("Amble", FontPosture.ITALIC, 30);
  93         assertEquals("Amble", font.getFamily());
  94         assertEquals("Amble Italic", font.getName());
  95         assertEquals(30f, (float) font.getSize());
  96     }
  97 
  98     @Test
  99     public void testDefault() {
 100         Font font = Font.getDefault();
 101         //assertEquals("System", font.getFamily());
 102     }
 103 
 104     @Test
 105     public void testCtor2() {
 106         Font font = new Font(20);
 107         //assertEquals("System", font.getFamily());
 108         assertEquals(20f, (float) font.getSize());
 109     }
 110 
 111     @Test
 112     public void testCtor3() {
 113         Font font = new Font("Amble Bold", 32);
 114         assertEquals("Amble", font.getFamily());
 115         assertEquals(32f, (float) font.getSize());
 116         assertEquals("Amble Bold", font.getName());
 117 
 118         Font def = new Font(null, -1);
 119         //assertEquals("System Regular", def.getName());
 120         assertTrue(0 < def.getSize());
 121     }
 122 
 123     @Test
 124     public void testToString() {
 125         assertNotNull(new Font(12).toString());
 126     }
 127 
 128     @Test
 129     public void testEqualsHashCode() {
 130         Font f1 = new Font(12);
 131         Font f2 = new Font(12);
 132         assertEquals(f1, f2);
 133         assertEquals(f1.hashCode(), f2.hashCode());
 134         Font f3 = new Font(40);
 135         assertNotSame(f1, f3);
 136         assertNotSame(f1.hashCode(), f3.hashCode());
 137         Font f4 = new Font(40);
 138         assertEquals(f3, f4);
 139     }
 140 
 141     @Test
 142     public void testSetNative() {
 143         new Font(12).impl_setNativeFont(new Object(), "", "", "");
 144         // no assumptions
 145     }
 146 
 147 /*
 148  * Sad to say, all these tests are not useful since 'ant test' uses
 149  * a stub toolkit and font loader which cannot implement these, since
 150  * they require real code behind them.
 151  * Therefore this is one big comment block
 152  */
 153 //     @Test
 154 //     public void testLoadFontFromLocalFileURL() {
 155 //         // First make sure we are on Windows and can find the file
 156 //         String os = System.getProperty("os.name");
 157 //         if (!os.startsWith("Win")) {
 158 //             return;
 159 //         }
 160 //         String path = "c:\\windows\\fonts\\times.ttf";
 161 //         if (!((new File(path)).canRead())) {
 162 //              return;
 163 //         }
 164 //         float sz = 20f;
 165 //         Font font = Font.loadFont(path, sz);
 166 //         assertNotNull(font);
 167 //         assertEquals("Times New Roman", font.getFamily());
 168 //         assertEquals(sz, (float) font.getSize());
 169 //     }
 170 
 171 //     @Test
 172 //     public void testLoadFontFromInputStream() {
 173 //         // First make sure we are on Windows and can find the file
 174 //         String os = System.getProperty("os.name");
 175 //         if (!os.startsWith("Win")) {
 176 //             return;
 177 //         }
 178 //         String path = "c:\\windows\\fonts\\times.ttf";
 179 //         if (!((new File(path)).canRead())) {
 180 //              return;
 181 //         }
 182 //         try {
 183 //              InputStream is = new FileInputStream(path);
 184 //              float sz = 20f;
 185 //              Font font = Font.loadFont(is, sz);
 186 //              assertNotNull(font);
 187 //              assertEquals("Times New Roman", font.getFamily());
 188 //              assertEquals(sz, (float) font.getSize());
 189 //         } catch (IOException e) {
 190 //              assertNull("Unexpected Exception", e);
 191 //         }
 192 //     }
 193 
 194 //     @Test
 195 //     public void testLoadFontFromBadFileURL() {
 196 //         Font font = Font.loadFont("non-existent file", 10f);
 197 //         assertNull(font);
 198 //     }
 199 
 200 //     @Test
 201 //     public void testLoadFontFromBadStream() {
 202 //         InputStream is = new ByteArrayInputStream(new byte[100]);
 203 //         Font font = Font.loadFont(is, 10f);
 204 //         assertNull(font);
 205 //     }
 206 
 207 }