1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.graphics.cts; 18 19 20 import android.graphics.Typeface; 21 import android.content.res.AssetFileDescriptor; 22 import android.content.res.AssetManager; 23 import android.content.res.Resources; 24 import android.graphics.Typeface; 25 import android.os.ParcelFileDescriptor; 26 import android.test.AndroidTestCase; 27 28 import java.io.File; 29 import java.io.FileDescriptor; 30 import java.io.FileOutputStream; 31 import java.io.IOException; 32 import java.io.InputStream; 33 34 public class TypefaceTest extends AndroidTestCase { 35 36 // generic family name for monospaced fonts 37 private static final String MONO = "monospace"; 38 private static final String DEFAULT = (String)null; 39 private static final String INVALID = "invalid-family-name"; 40 41 // list of family names to try when attempting to find a typeface with a given style 42 private static final String[] FAMILIES = 43 { (String) null, "monospace", "serif", "sans-serif", "cursive", "arial", "times" }; 44 45 /** 46 * Create a typeface of the given style. If the default font does not support the style, 47 * a number of generic families are tried. 48 * @return The typeface or null, if no typeface with the given style can be found. 49 */ 50 private Typeface createTypeface(int style) { 51 for (String family : FAMILIES) { 52 Typeface tf = Typeface.create(family, style); 53 if (tf.getStyle() == style) { 54 return tf; 55 } 56 } 57 return null; 58 } 59 60 61 public void testIsBold() { 62 Typeface typeface = createTypeface(Typeface.BOLD); 63 if (typeface != null) { 64 assertEquals(Typeface.BOLD, typeface.getStyle()); 65 assertTrue(typeface.isBold()); 66 assertFalse(typeface.isItalic()); 67 } 68 69 typeface = createTypeface(Typeface.ITALIC); 70 if (typeface != null) { 71 assertEquals(Typeface.ITALIC, typeface.getStyle()); 72 assertFalse(typeface.isBold()); 73 assertTrue(typeface.isItalic()); 74 } 75 76 typeface = createTypeface(Typeface.BOLD_ITALIC); 77 if (typeface != null) { 78 assertEquals(Typeface.BOLD_ITALIC, typeface.getStyle()); 79 assertTrue(typeface.isBold()); 80 assertTrue(typeface.isItalic()); 81 } 82 83 typeface = createTypeface(Typeface.NORMAL); 84 if (typeface != null) { 85 assertEquals(Typeface.NORMAL, typeface.getStyle()); 86 assertFalse(typeface.isBold()); 87 assertFalse(typeface.isItalic()); 88 } 89 } 90 91 public void testCreate() { 92 Typeface typeface = Typeface.create(DEFAULT, Typeface.NORMAL); 93 assertNotNull(typeface); 94 typeface = Typeface.create(MONO, Typeface.BOLD); 95 assertNotNull(typeface); 96 typeface = Typeface.create(INVALID, Typeface.ITALIC); 97 assertNotNull(typeface); 98 99 typeface = Typeface.create(typeface, Typeface.NORMAL); 100 assertNotNull(typeface); 101 typeface = Typeface.create(typeface, Typeface.BOLD); 102 assertNotNull(typeface); 103 } 104 105 public void testDefaultFromStyle() { 106 Typeface typeface = Typeface.defaultFromStyle(Typeface.NORMAL); 107 assertNotNull(typeface); 108 typeface = Typeface.defaultFromStyle(Typeface.BOLD); 109 assertNotNull(typeface); 110 typeface = Typeface.defaultFromStyle(Typeface.ITALIC); 111 assertNotNull(typeface); 112 typeface = Typeface.defaultFromStyle(Typeface.BOLD_ITALIC); 113 assertNotNull(typeface); 114 } 115 116 public void testConstants() { 117 assertNotNull(Typeface.DEFAULT); 118 assertNotNull(Typeface.DEFAULT_BOLD); 119 assertNotNull(Typeface.MONOSPACE); 120 assertNotNull(Typeface.SANS_SERIF); 121 assertNotNull(Typeface.SERIF); 122 } 123 124 public void testCreateFromAsset() { 125 // input abnormal params. 126 try { 127 Typeface.createFromAsset(null, null); 128 fail("Should throw a NullPointerException."); 129 } catch (NullPointerException e) { 130 // except here 131 } 132 133 Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "samplefont.ttf"); 134 assertNotNull(typeface); 135 } 136 137 public void testCreateFromFile1() throws IOException { 138 // input abnormal params. 139 try { 140 Typeface.createFromFile((File)null); 141 fail("Should throw a NullPointerException."); 142 } catch (NullPointerException e) { 143 // except here 144 } 145 File file = new File(obtainPath()); 146 Typeface typeface = Typeface.createFromFile(file); 147 assertNotNull(typeface); 148 } 149 150 public void testCreateFromFile2() throws IOException { 151 // input abnormal params. 152 try { 153 Typeface.createFromFile((String)null); 154 fail("Should throw a NullPointerException."); 155 } catch (NullPointerException e) { 156 // except here 157 } 158 159 Typeface typeface = Typeface.createFromFile(obtainPath()); 160 assertNotNull(typeface); 161 } 162 163 private String obtainPath() throws IOException { 164 File dir = getContext().getFilesDir(); 165 dir.mkdirs(); 166 File file = new File(dir, "test.jpg"); 167 if (!file.createNewFile()) { 168 if (!file.exists()) { 169 fail("Failed to create new File!"); 170 } 171 } 172 InputStream is = getContext().getAssets().open("samplefont.ttf"); 173 FileOutputStream fOutput = new FileOutputStream(file); 174 byte[] dataBuffer = new byte[1024]; 175 int readLength = 0; 176 while ((readLength = is.read(dataBuffer)) != -1) { 177 fOutput.write(dataBuffer, 0, readLength); 178 } 179 is.close(); 180 fOutput.close(); 181 return (file.getPath()); 182 } 183 } 184