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.content.res; 18 19import static android.support.v4.content.res.FontResourcesParserCompat.FamilyResourceEntry; 20import static android.support.v4.content.res.FontResourcesParserCompat.FontFamilyFilesResourceEntry; 21import static android.support.v4.content.res.FontResourcesParserCompat.FontFileResourceEntry; 22import static android.support.v4.content.res.FontResourcesParserCompat.ProviderResourceEntry; 23 24import static org.junit.Assert.assertEquals; 25import static org.junit.Assert.assertNotNull; 26 27import android.annotation.SuppressLint; 28import android.app.Instrumentation; 29import android.content.res.Resources; 30import android.content.res.XmlResourceParser; 31import android.support.compat.test.R; 32import android.support.test.InstrumentationRegistry; 33import android.support.test.filters.SmallTest; 34import android.support.test.runner.AndroidJUnit4; 35import android.support.v4.provider.FontRequest; 36import android.util.Base64; 37 38import org.junit.Before; 39import org.junit.Test; 40import org.junit.runner.RunWith; 41import org.xmlpull.v1.XmlPullParserException; 42 43import java.io.IOException; 44import java.util.List; 45 46/** 47 * Tests for {@link FontResourcesParserCompat}. 48 */ 49@SmallTest 50@RunWith(AndroidJUnit4.class) 51public class FontResourcesParserCompatTest { 52 53 private Instrumentation mInstrumentation; 54 private Resources mResources; 55 56 @Before 57 public void setup() { 58 mInstrumentation = InstrumentationRegistry.getInstrumentation(); 59 mResources = mInstrumentation.getContext().getResources(); 60 } 61 62 @Test 63 public void testParse() throws XmlPullParserException, IOException { 64 @SuppressLint("ResourceType") 65 XmlResourceParser parser = mResources.getXml(R.font.samplexmlfont); 66 67 FamilyResourceEntry result = FontResourcesParserCompat.parse(parser, mResources); 68 69 assertNotNull(result); 70 FontFamilyFilesResourceEntry filesEntry = (FontFamilyFilesResourceEntry) result; 71 FontFileResourceEntry[] fileEntries = filesEntry.getEntries(); 72 assertEquals(4, fileEntries.length); 73 FontFileResourceEntry font1 = fileEntries[0]; 74 assertEquals(400, font1.getWeight()); 75 assertEquals(false, font1.isItalic()); 76 assertEquals(R.font.samplefont, font1.getResourceId()); 77 FontFileResourceEntry font2 = fileEntries[1]; 78 assertEquals(400, font2.getWeight()); 79 assertEquals(true, font2.isItalic()); 80 assertEquals(R.font.samplefont2, font2.getResourceId()); 81 FontFileResourceEntry font3 = fileEntries[2]; 82 assertEquals(700, font3.getWeight()); 83 assertEquals(false, font3.isItalic()); 84 assertEquals(R.font.samplefont3, font3.getResourceId()); 85 FontFileResourceEntry font4 = fileEntries[3]; 86 assertEquals(700, font4.getWeight()); 87 assertEquals(true, font4.isItalic()); 88 assertEquals(R.font.samplefont4, font4.getResourceId()); 89 } 90 91 @Test 92 public void testParseDownloadableFont() throws IOException, XmlPullParserException { 93 @SuppressLint("ResourceType") 94 XmlResourceParser parser = mResources.getXml(R.font.samplexmldownloadedfont); 95 96 FamilyResourceEntry result = FontResourcesParserCompat.parse(parser, mResources); 97 98 assertNotNull(result); 99 ProviderResourceEntry providerEntry = (ProviderResourceEntry) result; 100 FontRequest request = providerEntry.getRequest(); 101 assertEquals("com.example.test.fontprovider.authority", 102 request.getProviderAuthority()); 103 assertEquals("com.example.test.fontprovider.package", request.getProviderPackage()); 104 assertEquals("MyRequestedFont", request.getQuery()); 105 } 106 107 @Test 108 public void testReadCertsSingleArray() { 109 List<List<byte[]>> result = FontResourcesParserCompat.readCerts(mResources, R.array.certs1); 110 111 assertEquals(1, result.size()); 112 List<byte[]> firstSet = result.get(0); 113 assertEquals(2, firstSet.size()); 114 String firstValue = Base64.encodeToString(firstSet.get(0), Base64.DEFAULT).trim(); 115 assertEquals("MIIEqDCCA5CgAwIBAgIJANWFuGx9", firstValue); 116 String secondValue = Base64.encodeToString(firstSet.get(1), Base64.DEFAULT).trim(); 117 assertEquals("UEChMHQW5kcm9pZDEQMA4GA=", secondValue); 118 } 119 120 @Test 121 public void testReadCertsMultiArray() { 122 List<List<byte[]>> result = 123 FontResourcesParserCompat.readCerts(mResources, R.array.certarray); 124 125 assertEquals(2, result.size()); 126 List<byte[]> firstSet = result.get(0); 127 assertEquals(2, firstSet.size()); 128 String firstValue = Base64.encodeToString(firstSet.get(0), Base64.DEFAULT).trim(); 129 assertEquals("MIIEqDCCA5CgAwIBAgIJANWFuGx9", firstValue); 130 String secondValue = Base64.encodeToString(firstSet.get(1), Base64.DEFAULT).trim(); 131 assertEquals("UEChMHQW5kcm9pZDEQMA4GA=", secondValue); 132 List<byte[]> secondSet = result.get(1); 133 assertEquals(2, secondSet.size()); 134 String thirdValue = Base64.encodeToString(secondSet.get(0), Base64.DEFAULT).trim(); 135 assertEquals("MDEyMzM2NTZaMIGUMQswCQYD", thirdValue); 136 String fourthValue = Base64.encodeToString(secondSet.get(1), Base64.DEFAULT).trim(); 137 assertEquals("DHThvbbR24kT9ixcOd9W+EY=", fourthValue); 138 } 139} 140