TypefaceCompatTest.java revision fc857452283b38dfbac9ca023a77a8cf8b8c5599
1/* 2 * Copyright (C) 2017 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.support.v4.graphics; 18 19import static android.content.res.AssetManager.ACCESS_BUFFER; 20 21import static org.junit.Assert.assertNotNull; 22 23import android.graphics.Typeface; 24import android.os.ParcelFileDescriptor; 25import android.support.test.filters.SmallTest; 26import android.support.test.runner.AndroidJUnit4; 27import android.support.v4.BaseInstrumentationTestCase; 28import android.support.v4.app.TestSupportActivity; 29import android.support.v4.graphics.fonts.FontResult; 30import android.util.Base64; 31 32import org.junit.Before; 33import org.junit.Test; 34import org.junit.runner.RunWith; 35 36import java.io.File; 37import java.io.FileOutputStream; 38import java.io.IOException; 39import java.io.InputStream; 40import java.util.Arrays; 41import java.util.List; 42 43/** 44 * Tests for {@link TypefaceCompatBaseImpl}. 45 */ 46@SmallTest 47@RunWith(AndroidJUnit4.class) 48public class TypefaceCompatTest extends BaseInstrumentationTestCase<TestSupportActivity> { 49 private static final String TEST_FONT_FILE = "fonts/samplefont1.ttf"; 50 private static final String CACHE_FILE = "cachedfont.ttf"; 51 private static final String PROVIDER = "com.test.fontprovider.authority"; 52 private static final String QUERY_CACHED = "query_cached"; 53 private static final String QUERY = "query"; 54 private static final String PACKAGE = "com.test.fontprovider.package"; 55 private static final byte[] BYTE_ARRAY = 56 Base64.decode("e04fd020ea3a6910a2d808002b30", Base64.DEFAULT); 57 private static final List<List<byte[]>> CERTS = Arrays.asList(Arrays.asList(BYTE_ARRAY)); 58 59 private TypefaceCompatBaseImpl mCompat; 60 61 public TypefaceCompatTest() { 62 super(TestSupportActivity.class); 63 } 64 65 @Before 66 public void setup() { 67 mCompat = new TypefaceCompatBaseImpl(mActivityTestRule.getActivity()); 68 TypefaceCompatBaseImpl.putInCache(PROVIDER, QUERY_CACHED, Typeface.MONOSPACE); 69 } 70 71 private File loadFont() { 72 File cacheFile = new File(mActivityTestRule.getActivity().getCacheDir(), CACHE_FILE); 73 try { 74 copyToCacheFile(TEST_FONT_FILE, cacheFile); 75 return cacheFile; 76 } catch (IOException e) { 77 e.printStackTrace(); 78 } 79 return null; 80 } 81 82 private void copyToCacheFile(final String assetPath, final File cacheFile) 83 throws IOException { 84 InputStream is = null; 85 FileOutputStream fos = null; 86 try { 87 is = mActivityTestRule.getActivity().getAssets().open(assetPath, ACCESS_BUFFER); 88 fos = new FileOutputStream(cacheFile, false); 89 byte[] buffer = new byte[1024]; 90 int readLen; 91 while ((readLen = is.read(buffer)) != -1) { 92 fos.write(buffer, 0, readLen); 93 } 94 } finally { 95 if (is != null) { 96 is.close(); 97 } 98 if (fos != null) { 99 fos.close(); 100 } 101 } 102 } 103 104 // TODO(nona): Remove once EmojiCompat stop using Typeface.createTypeface. 105 @Test 106 public void testCreateTypeface() throws IOException, InterruptedException { 107 File file = loadFont(); 108 ParcelFileDescriptor pfd = 109 ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY); 110 try { 111 FontResult result = new FontResult(pfd, 0, null, 400, false /* italic */); 112 Typeface typeface = mCompat.createTypeface(Arrays.asList(result)); 113 114 assertNotNull(typeface); 115 } finally { 116 if (file != null) { 117 file.delete(); 118 } 119 if (pfd != null) { 120 pfd.close(); 121 } 122 } 123 } 124} 125