FontResourcesParserCompatTest.java revision 617be7af1f752cfaaf566e627ff6ea797623f2c3
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;
35
36import org.junit.Before;
37import org.junit.Test;
38import org.junit.runner.RunWith;
39import org.xmlpull.v1.XmlPullParserException;
40
41import java.io.IOException;
42
43/**
44 * Tests for {@link FontResourcesParserCompat}.
45 */
46@SmallTest
47@RunWith(AndroidJUnit4.class)
48public class FontResourcesParserCompatTest {
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        @SuppressLint("ResourceType")
62        XmlResourceParser parser = mResources.getXml(R.font.samplexmlfont);
63
64        FamilyResourceEntry result = FontResourcesParserCompat.parse(parser, mResources);
65
66        assertNotNull(result);
67        FontFamilyFilesResourceEntry filesEntry = (FontFamilyFilesResourceEntry) result;
68        FontFileResourceEntry[] fileEntries = filesEntry.getEntries();
69        assertEquals(4, fileEntries.length);
70        FontFileResourceEntry font1 = fileEntries[0];
71        assertEquals(400, font1.getWeight());
72        assertEquals(false, font1.isItalic());
73        assertEquals("res/font/samplefont.ttf", font1.getFileName());
74        FontFileResourceEntry font2 = fileEntries[1];
75        assertEquals(400, font2.getWeight());
76        assertEquals(true, font2.isItalic());
77        assertEquals("res/font/samplefont2.ttf", font2.getFileName());
78        FontFileResourceEntry font3 = fileEntries[2];
79        assertEquals(800, font3.getWeight());
80        assertEquals(false, font3.isItalic());
81        assertEquals("res/font/samplefont3.ttf", font3.getFileName());
82        FontFileResourceEntry font4 = fileEntries[3];
83        assertEquals(800, font4.getWeight());
84        assertEquals(true, font4.isItalic());
85        assertEquals("res/font/samplefont4.ttf", font4.getFileName());
86    }
87
88    @Test
89    public void testParseDownloadableFont() throws IOException, XmlPullParserException {
90        @SuppressLint("ResourceType")
91        XmlResourceParser parser = mResources.getXml(R.font.samplexmldownloadedfont);
92
93        FamilyResourceEntry result = FontResourcesParserCompat.parse(parser, mResources);
94
95        assertNotNull(result);
96        ProviderResourceEntry providerEntry = (ProviderResourceEntry) result;
97        assertEquals("com.example.test.fontprovider.authority", providerEntry.getAuthority());
98        assertEquals("com.example.test.fontprovider.package", providerEntry.getPackage());
99        assertEquals("MyRequestedFont", providerEntry.getQuery());
100    }
101}
102