summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Clara Bayarri <clarabayarri@google.com> 2017-02-02 13:57:11 +0000
committer Clara Bayarri <clarabayarri@google.com> 2017-02-02 18:45:54 +0000
commit9b161fbdbc198d0162c02e28c1c5573cc1d66215 (patch)
treeceba80b79a71de199fd76a17eb872630e3d1b513
parent3084ec2df4f14684d11ca1006f8f243243071730 (diff)
Fix bugs in FontResourcesParser and add coretests
Bug: 34920360 Test: runtest --path frameworks/base/core/tests/coretests/src/android/content/res/FontResourcesParserTest.java Change-Id: I41da558957d25a6f7ee62a4ed9fecf470b74f1d0
-rw-r--r--core/java/android/content/res/FontResourcesParser.java7
-rw-r--r--core/tests/coretests/res/font/samplefont.ttfbin0 -> 133372 bytes
-rw-r--r--core/tests/coretests/res/font/samplefont2.ttfbin0 -> 133372 bytes
-rw-r--r--core/tests/coretests/res/font/samplefont3.ttfbin0 -> 133372 bytes
-rw-r--r--core/tests/coretests/res/font/samplefont4.ttfbin0 -> 133372 bytes
-rw-r--r--core/tests/coretests/res/font/samplexmlfont.xml7
-rw-r--r--core/tests/coretests/src/android/content/res/FontResourcesParserTest.java81
7 files changed, 93 insertions, 2 deletions
diff --git a/core/java/android/content/res/FontResourcesParser.java b/core/java/android/content/res/FontResourcesParser.java
index 15f3a099c163..7ea62db3b1f0 100644
--- a/core/java/android/content/res/FontResourcesParser.java
+++ b/core/java/android/content/res/FontResourcesParser.java
@@ -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);
}
diff --git a/core/tests/coretests/res/font/samplefont.ttf b/core/tests/coretests/res/font/samplefont.ttf
new file mode 100644
index 000000000000..285230293580
--- /dev/null
+++ b/core/tests/coretests/res/font/samplefont.ttf
Binary files differ
diff --git a/core/tests/coretests/res/font/samplefont2.ttf b/core/tests/coretests/res/font/samplefont2.ttf
new file mode 100644
index 000000000000..285230293580
--- /dev/null
+++ b/core/tests/coretests/res/font/samplefont2.ttf
Binary files differ
diff --git a/core/tests/coretests/res/font/samplefont3.ttf b/core/tests/coretests/res/font/samplefont3.ttf
new file mode 100644
index 000000000000..285230293580
--- /dev/null
+++ b/core/tests/coretests/res/font/samplefont3.ttf
Binary files differ
diff --git a/core/tests/coretests/res/font/samplefont4.ttf b/core/tests/coretests/res/font/samplefont4.ttf
new file mode 100644
index 000000000000..285230293580
--- /dev/null
+++ b/core/tests/coretests/res/font/samplefont4.ttf
Binary files differ
diff --git a/core/tests/coretests/res/font/samplexmlfont.xml b/core/tests/coretests/res/font/samplexmlfont.xml
new file mode 100644
index 000000000000..bb813e1f9c1b
--- /dev/null
+++ b/core/tests/coretests/res/font/samplexmlfont.xml
@@ -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> \ No newline at end of file
diff --git a/core/tests/coretests/src/android/content/res/FontResourcesParserTest.java b/core/tests/coretests/src/android/content/res/FontResourcesParserTest.java
new file mode 100644
index 000000000000..380a28774108
--- /dev/null
+++ b/core/tests/coretests/src/android/content/res/FontResourcesParserTest.java
@@ -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());
+ }
+}