• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         assertEquals("my-request-font", providerEntry.getSystemFontFamilyName());
109     }
110 
111     @Test
testParseDownloadableFont_singleCerts()112     public void testParseDownloadableFont_singleCerts() throws IOException, XmlPullParserException {
113         XmlResourceParser parser = mResources.getXml(R.font.samplexmldownloadedfontsinglecerts);
114 
115         FamilyResourceEntry result = FontResourcesParser.parse(parser, mResources);
116 
117         assertNotNull(result);
118         assertTrue(result instanceof ProviderResourceEntry);
119         ProviderResourceEntry providerResourceEntry = (ProviderResourceEntry) result;
120         assertEquals("com.example.test.fontprovider", providerResourceEntry.getAuthority());
121         assertEquals("MyRequestedFont", providerResourceEntry.getQuery());
122         assertEquals("com.example.test.fontprovider.package", providerResourceEntry.getPackage());
123         List<List<String>> certList = providerResourceEntry.getCerts();
124         assertNotNull(certList);
125         assertEquals(1, certList.size());
126         List<String> certs = certList.get(0);
127         assertEquals(2, certs.size());
128         assertEquals("123456789", certs.get(0));
129         assertEquals("987654321", certs.get(1));
130     }
131 
132     @Test
testParseDownloadableFont_multipleCerts()133     public void testParseDownloadableFont_multipleCerts() throws IOException, XmlPullParserException {
134         XmlResourceParser parser = mResources.getXml(R.font.samplexmldownloadedfontmulticerts);
135 
136         FamilyResourceEntry result = FontResourcesParser.parse(parser, mResources);
137 
138         assertNotNull(result);
139         assertTrue(result instanceof ProviderResourceEntry);
140         ProviderResourceEntry providerResourceEntry = (ProviderResourceEntry) result;
141         assertEquals("com.example.test.fontprovider", providerResourceEntry.getAuthority());
142         assertEquals("MyRequestedFont", providerResourceEntry.getQuery());
143         assertEquals("com.example.test.fontprovider.package", providerResourceEntry.getPackage());
144         List<List<String>> certList = providerResourceEntry.getCerts();
145         assertNotNull(certList);
146         assertEquals(2, certList.size());
147         List<String> certs1 = certList.get(0);
148         assertEquals(2, certs1.size());
149         assertEquals("123456789", certs1.get(0));
150         assertEquals("987654321", certs1.get(1));
151         List<String> certs2 = certList.get(1);
152         assertEquals(2, certs2.size());
153         assertEquals("abcdefg", certs2.get(0));
154         assertEquals("gfedcba", certs2.get(1));
155     }
156 }
157