001 /*
002 * Copyright 2007 - 2010 JEuclid, http://jeuclid.sf.net
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 /* $Id: FontFactoryTest.java,v 92b6a7c39d7f 2010/08/11 20:23:17 max $ */
018
019 package net.sourceforge.jeuclid.test;
020
021 import java.awt.Font;
022
023 import net.sourceforge.jeuclid.font.FontFactory;
024
025 import org.junit.Assert;
026 import org.junit.Test;
027
028 /**
029 * Test for {@link net.sourceforge.jeuclid.font.FontFactory} class and its
030 * concrete subclasses.
031 *
032 * @version $Revision: 92b6a7c39d7f $
033 */
034 public final class FontFactoryTest {
035
036 private static final String DIALOG = "Dialog";
037
038 private static final int TEST_FONT_SIZE = 14;
039
040 /** */
041 public FontFactoryTest() {
042 }
043
044 /**
045 * Tests basic FontFactory functionality (returning standard fonts,
046 * defaulting to standard fonts).
047 *
048 * @throws Exception
049 * if anything goes wrong
050 */
051 @Test
052 public void fontFactoryTest() throws Exception {
053 final String builtInFamily = "Monospaced";
054 final String defaultFamily = FontFactoryTest.DIALOG;
055 final String customFamily = "Bitstream Vera Sans Mono";
056 final String customAltname = "BitstreamVeraSansMono-Roman";
057 final FontFactory fontFactory = FontFactory.getInstance();
058
059 // get a non-default built-in font
060 final Font builtInFont = fontFactory.getFont(builtInFamily,
061 Font.PLAIN, FontFactoryTest.TEST_FONT_SIZE);
062 Assert.assertEquals(builtInFamily, builtInFont.getFamily());
063
064 // get a non-default, not cached font
065 final Font foo = fontFactory.getFont("foo456789", Font.PLAIN,
066 FontFactoryTest.TEST_FONT_SIZE);
067 Assert.assertEquals(foo.getFamily(), defaultFamily);
068
069 // register a custom font and then get it
070 fontFactory.registerFont(Font.TRUETYPE_FONT, this.getClass()
071 .getResourceAsStream("/VeraMono.ttf"));
072 Font customFont = fontFactory.getFont(customFamily, Font.PLAIN,
073 FontFactoryTest.TEST_FONT_SIZE);
074 if (customFont.getFamily().equals(FontFactoryTest.DIALOG)) {
075 customFont = fontFactory.getFont(customAltname, Font.PLAIN,
076 FontFactoryTest.TEST_FONT_SIZE);
077 Assert.assertEquals(customFont.getFamily(), customAltname);
078 } else {
079 Assert.assertEquals(customFont.getFamily(), customFamily);
080 }
081 Assert.assertEquals(customFont.getSize(),
082 FontFactoryTest.TEST_FONT_SIZE);
083 }
084
085 }