Merge "Fix bugs in FontResourcesParser and add coretests"

This commit is contained in:
TreeHugger Robot
2017-02-03 10:15:57 +00:00
committed by Android (Google) Code Review
7 changed files with 93 additions and 2 deletions

View File

@@ -33,7 +33,7 @@ import java.util.List;
*/
public class FontResourcesParser {
private static final int NORMAL_WEIGHT = 400;
private static final String ITALIC = "italic";
private static final int ITALIC = 1;
public static FontConfig parse(XmlPullParser parser, Resources resources)
throws XmlPullParserException, IOException {
@@ -82,9 +82,12 @@ public class FontResourcesParser {
AttributeSet attrs = Xml.asAttributeSet(parser);
TypedArray array = resources.obtainAttributes(attrs, R.styleable.FontFamilyFont);
int weight = array.getInt(R.styleable.FontFamilyFont_fontWeight, NORMAL_WEIGHT);
boolean isItalic = ITALIC.equals(array.getString(R.styleable.FontFamilyFont_fontStyle));
boolean isItalic = ITALIC == array.getInt(R.styleable.FontFamilyFont_fontStyle, 0);
String filename = array.getString(R.styleable.FontFamilyFont_font);
array.recycle();
while (parser.next() != XmlPullParser.END_TAG) {
skip(parser);
}
return new FontConfig.Font(filename, 0, null, weight, isItalic);
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font android:fontStyle="normal" android:fontWeight="400" android:font="@font/samplefont" />
<font android:fontStyle="italic" android:fontWeight="400" android:font="@font/samplefont2" />
<font android:fontStyle="normal" android:fontWeight="800" android:font="@font/samplefont3" />
<font android:fontStyle="italic" android:fontWeight="800" android:font="@font/samplefont4" />
</font-family>

View File

@@ -0,0 +1,81 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.content.res;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import android.app.Instrumentation;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import android.text.FontConfig;
import com.android.frameworks.coretests.R;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.xmlpull.v1.XmlPullParserException;
import java.io.IOException;
import java.util.List;
/**
* Tests for {@link FontResourcesParser}.
*/
@SmallTest
@RunWith(AndroidJUnit4.class)
public class FontResourcesParserTest {
private Instrumentation mInstrumentation;
private Resources mResources;
@Before
public void setup() {
mInstrumentation = InstrumentationRegistry.getInstrumentation();
mResources = mInstrumentation.getContext().getResources();
}
@Test
public void testParse() throws XmlPullParserException, IOException {
XmlResourceParser parser = mResources.getXml(R.font.samplexmlfont);
FontConfig result = FontResourcesParser.parse(parser, mResources);
assertNotNull(result);
List<FontConfig.Family> families = result.getFamilies();
assertEquals(1, families.size());
List<FontConfig.Font> fonts = families.get(0).getFonts();
assertEquals(4, fonts.size());
FontConfig.Font font1 = fonts.get(0);
assertEquals(400, font1.getWeight());
assertEquals(false, font1.isItalic());
assertEquals("res/font/samplefont.ttf", font1.getFontName());
FontConfig.Font font2 = fonts.get(1);
assertEquals(400, font2.getWeight());
assertEquals(true, font2.isItalic());
assertEquals("res/font/samplefont2.ttf", font2.getFontName());
FontConfig.Font font3 = fonts.get(2);
assertEquals(800, font3.getWeight());
assertEquals(false, font3.isItalic());
assertEquals("res/font/samplefont3.ttf", font3.getFontName());
FontConfig.Font font4 = fonts.get(3);
assertEquals(800, font4.getWeight());
assertEquals(true, font4.isItalic());
assertEquals("res/font/samplefont4.ttf", font4.getFontName());
}
}