1/* 2 * Copyright (C) 2016 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 17package android.graphics.perftests; 18 19import android.content.Context; 20import android.content.res.AssetManager; 21import android.graphics.Typeface; 22import android.perftests.utils.BenchmarkState; 23import android.perftests.utils.PerfStatusReporter; 24import android.support.test.InstrumentationRegistry; 25import android.support.test.filters.LargeTest; 26import android.support.test.runner.AndroidJUnit4; 27 28import java.io.File; 29import java.io.FileOutputStream; 30import java.io.IOException; 31import java.io.InputStream; 32import java.io.OutputStream; 33 34import org.junit.Rule; 35import org.junit.Test; 36import org.junit.runner.RunWith; 37 38@LargeTest 39@RunWith(AndroidJUnit4.class) 40public class TypefaceCreatePerfTest { 41 // A font file name in asset directory. 42 private static final String TEST_FONT_NAME = "DancingScript-Regular.ttf"; 43 44 @Rule 45 public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); 46 47 @Test 48 public void testCreate_fromFamily() { 49 BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 50 51 while (state.keepRunning()) { 52 Typeface face = Typeface.create(Typeface.SANS_SERIF, Typeface.NORMAL); 53 } 54 } 55 56 @Test 57 public void testCreate_fromFamilyName() { 58 BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 59 60 while (state.keepRunning()) { 61 Typeface face = Typeface.create("monospace", Typeface.NORMAL); 62 } 63 } 64 65 @Test 66 public void testCreate_fromAsset() { 67 BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 68 final Context context = InstrumentationRegistry.getContext(); 69 final AssetManager am = context.getAssets(); 70 71 while (state.keepRunning()) { 72 Typeface face = Typeface.createFromAsset(am, TEST_FONT_NAME); 73 } 74 } 75 76 @Test 77 public void testCreate_fromFile() { 78 BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); 79 final Context context = InstrumentationRegistry.getContext(); 80 final AssetManager am = context.getAssets(); 81 82 File outFile = null; 83 try { 84 outFile = File.createTempFile("example", "ttf", context.getCacheDir()); 85 } catch (IOException e) { 86 throw new RuntimeException(e); 87 } 88 89 try (InputStream in = am.open(TEST_FONT_NAME); 90 OutputStream out = new FileOutputStream(outFile)) { 91 byte[] buf = new byte[1024]; 92 int n = 0; 93 while ((n = in.read(buf)) != -1) { 94 out.write(buf, 0, n); 95 } 96 } catch (IOException e) { 97 throw new RuntimeException(e); 98 } 99 100 while (state.keepRunning()) { 101 Typeface face = Typeface.createFromFile(outFile); 102 } 103 104 outFile.delete(); 105 } 106} 107