1 /* 2 * Copyright (C) 2011 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.renderscriptgraphics.cts; 18 19 import java.io.File; 20 import com.android.cts.stub.R; 21 22 import android.os.Environment; 23 import android.renderscript.Font; 24 import android.renderscript.Font.Style; 25 import android.renderscript.RSIllegalArgumentException; 26 27 public class FontTest extends RSBaseGraphics { 28 29 public void testCreate() { 30 for (int fontSize = 8; fontSize <= 12; fontSize += 2) { 31 for (Font.Style style : Font.Style.values()) { 32 Font F = null; 33 F = Font.create(mRS, mRes, "sans-serif", style, fontSize); 34 assertTrue(F != null); 35 F.setName("sans-serif"); 36 try { 37 F.setName("sans-serif"); 38 fail("set name twice for a font"); 39 } catch (RSIllegalArgumentException e) { 40 } 41 F.destroy(); 42 43 F = Font.create(mRS, mRes, "serif", style, fontSize); 44 assertTrue(F != null); 45 try { 46 F.setName(""); 47 fail("set empty name for a font"); 48 } catch (RSIllegalArgumentException e) { 49 } 50 F.setName("serif"); 51 F.destroy(); 52 53 F = Font.create(mRS, mRes, "mono", style, fontSize); 54 assertTrue(F != null); 55 try { 56 F.setName(null); 57 fail("set name as null string reference for a font"); 58 } catch (RSIllegalArgumentException e) { 59 } 60 F.setName("mono"); 61 F.destroy(); 62 } 63 } 64 } 65 66 public void testCreateFromFile() { 67 String fontFile = "Roboto-Regular.ttf"; 68 String fontPath = Environment.getRootDirectory().getAbsolutePath(); 69 fontPath += "/fonts/" + fontFile; 70 File fileDesc = new File(fontPath); 71 assertTrue(Font.createFromFile(mRS, mRes, fontPath, 8) != null); 72 assertTrue(Font.createFromFile(mRS, mRes, fileDesc, 8) != null); 73 } 74 75 public void testCreateFromAsset() { 76 assertTrue(Font.createFromAsset(mRS, mRes, "samplefont.ttf", 8) != null); 77 } 78 79 public void testFontStyle() { 80 assertEquals(Font.Style.NORMAL, Font.Style.valueOf("NORMAL")); 81 assertEquals(Font.Style.BOLD, Font.Style.valueOf("BOLD")); 82 assertEquals(Font.Style.ITALIC, Font.Style.valueOf("ITALIC")); 83 assertEquals(Font.Style.BOLD_ITALIC, Font.Style.valueOf("BOLD_ITALIC")); 84 // Make sure no new enums are added 85 assertEquals(4, Font.Style.values().length); 86 } 87 88 public void testCreateFromResource() { 89 assertTrue(Font.createFromResource(mRS, mRes, R.raw.samplefont, 8) != null); 90 } 91 } 92 93