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 17 package android.content.res; 18 19 import static android.content.res.FontResourcesParser.FamilyResourceEntry; 20 import static android.content.res.FontResourcesParser.FontFamilyFilesResourceEntry; 21 import static android.content.res.FontResourcesParser.FontFileResourceEntry; 22 import static android.content.res.FontResourcesParser.ProviderResourceEntry; 23 24 import static junit.framework.Assert.assertTrue; 25 26 import static org.junit.Assert.assertEquals; 27 import static org.junit.Assert.assertNotNull; 28 29 import android.app.Instrumentation; 30 31 import androidx.test.InstrumentationRegistry; 32 import androidx.test.filters.SmallTest; 33 import androidx.test.runner.AndroidJUnit4; 34 35 import com.android.frameworks.coretests.R; 36 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.xmlpull.v1.XmlPullParserException; 41 42 import java.io.IOException; 43 import java.util.List; 44 45 /** 46 * Tests for {@link FontResourcesParser}. 47 */ 48 @SmallTest 49 @RunWith(AndroidJUnit4.class) 50 public class FontResourcesParserTest { 51 52 private Instrumentation mInstrumentation; 53 private Resources mResources; 54 55 @Before setup()56 public void setup() { 57 mInstrumentation = InstrumentationRegistry.getInstrumentation(); 58 mResources = mInstrumentation.getContext().getResources(); 59 } 60 61 @Test testParse()62 public void testParse() throws XmlPullParserException, IOException { 63 XmlResourceParser parser = mResources.getXml(R.font.samplexmlfontforparsing); 64 65 FamilyResourceEntry result = FontResourcesParser.parse(parser, mResources); 66 67 assertNotNull(result); 68 FontFamilyFilesResourceEntry filesEntry = (FontFamilyFilesResourceEntry) result; 69 FontFileResourceEntry[] fileEntries = filesEntry.getEntries(); 70 assertEquals(4, fileEntries.length); 71 FontFileResourceEntry font1 = fileEntries[0]; 72 assertEquals(400, font1.getWeight()); 73 assertEquals(0, font1.getItalic()); 74 assertEquals("'wdth' 0.8", font1.getVariationSettings()); 75 assertEquals(0, font1.getTtcIndex()); 76 assertEquals("res/font/samplefont.ttf", font1.getFileName()); 77 FontFileResourceEntry font2 = fileEntries[1]; 78 assertEquals(400, font2.getWeight()); 79 assertEquals(1, font2.getItalic()); 80 assertEquals(1, font2.getTtcIndex()); 81 assertEquals("'cntr' 0.5", font2.getVariationSettings()); 82 assertEquals("res/font/samplefont2.ttf", font2.getFileName()); 83 FontFileResourceEntry font3 = fileEntries[2]; 84 assertEquals(800, font3.getWeight()); 85 assertEquals(0, font3.getItalic()); 86 assertEquals(2, font3.getTtcIndex()); 87 assertEquals("'wdth' 500.0, 'wght' 300.0", font3.getVariationSettings()); 88 assertEquals("res/font/samplefont3.ttf", font3.getFileName()); 89 FontFileResourceEntry font4 = fileEntries[3]; 90 assertEquals(800, font4.getWeight()); 91 assertEquals(1, font4.getItalic()); 92 assertEquals(0, font4.getTtcIndex()); 93 assertEquals(null, font4.getVariationSettings()); 94 assertEquals("res/font/samplefont4.ttf", font4.getFileName()); 95 } 96 97 @Test testParseDownloadableFont()98 public void testParseDownloadableFont() throws IOException, XmlPullParserException { 99 XmlResourceParser parser = mResources.getXml(R.font.samplexmldownloadedfont); 100 101 FamilyResourceEntry result = FontResourcesParser.parse(parser, mResources); 102 103 assertNotNull(result); 104 ProviderResourceEntry providerEntry = (ProviderResourceEntry) result; 105 assertEquals("com.example.test.fontprovider.authority", providerEntry.getAuthority()); 106 assertEquals("com.example.test.fontprovider.package", providerEntry.getPackage()); 107 assertEquals("MyRequestedFont", providerEntry.getQuery()); 108 } 109 110 @Test testParseDownloadableFont_singleCerts()111 public void testParseDownloadableFont_singleCerts() throws IOException, XmlPullParserException { 112 XmlResourceParser parser = mResources.getXml(R.font.samplexmldownloadedfontsinglecerts); 113 114 FamilyResourceEntry result = FontResourcesParser.parse(parser, mResources); 115 116 assertNotNull(result); 117 assertTrue(result instanceof ProviderResourceEntry); 118 ProviderResourceEntry providerResourceEntry = (ProviderResourceEntry) result; 119 assertEquals("com.example.test.fontprovider", providerResourceEntry.getAuthority()); 120 assertEquals("MyRequestedFont", providerResourceEntry.getQuery()); 121 assertEquals("com.example.test.fontprovider.package", providerResourceEntry.getPackage()); 122 List<List<String>> certList = providerResourceEntry.getCerts(); 123 assertNotNull(certList); 124 assertEquals(1, certList.size()); 125 List<String> certs = certList.get(0); 126 assertEquals(2, certs.size()); 127 assertEquals("123456789", certs.get(0)); 128 assertEquals("987654321", certs.get(1)); 129 } 130 131 @Test testParseDownloadableFont_multipleCerts()132 public void testParseDownloadableFont_multipleCerts() throws IOException, XmlPullParserException { 133 XmlResourceParser parser = mResources.getXml(R.font.samplexmldownloadedfontmulticerts); 134 135 FamilyResourceEntry result = FontResourcesParser.parse(parser, mResources); 136 137 assertNotNull(result); 138 assertTrue(result instanceof ProviderResourceEntry); 139 ProviderResourceEntry providerResourceEntry = (ProviderResourceEntry) result; 140 assertEquals("com.example.test.fontprovider", providerResourceEntry.getAuthority()); 141 assertEquals("MyRequestedFont", providerResourceEntry.getQuery()); 142 assertEquals("com.example.test.fontprovider.package", providerResourceEntry.getPackage()); 143 List<List<String>> certList = providerResourceEntry.getCerts(); 144 assertNotNull(certList); 145 assertEquals(2, certList.size()); 146 List<String> certs1 = certList.get(0); 147 assertEquals(2, certs1.size()); 148 assertEquals("123456789", certs1.get(0)); 149 assertEquals("987654321", certs1.get(1)); 150 List<String> certs2 = certList.get(1); 151 assertEquals(2, certs2.size()); 152 assertEquals("abcdefg", certs2.get(0)); 153 assertEquals("gfedcba", certs2.get(1)); 154 } 155 } 156