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