1 /* 2 * Copyright (C) 2018 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.graphics.fonts; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertTrue; 22 23 import android.util.Pair; 24 25 import androidx.test.filters.SmallTest; 26 import androidx.test.runner.AndroidJUnit4; 27 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 31 import java.io.File; 32 import java.util.Set; 33 34 @SmallTest 35 @RunWith(AndroidJUnit4.class) 36 public class NativeSystemFontTest { 37 38 @Test 39 public void testSameResultAsJava() { 40 Set<Font> javaFonts = SystemFonts.getAvailableFonts(); 41 Set<Font> nativeFonts = NativeSystemFontHelper.getAvailableFonts(); 42 43 assertEquals(javaFonts.size(), nativeFonts.size()); 44 45 for (Font f : nativeFonts) { 46 assertTrue(javaFonts.contains(f)); 47 } 48 49 for (Font f : javaFonts) { 50 assertTrue(nativeFonts.contains(f)); 51 } 52 } 53 54 @Test 55 public void testMatchFamilyStyleCharacter() { 56 Pair<File, Integer> fontForA = NativeSystemFontHelper.matchFamilyStyleCharacter( 57 "sans", 400, false, "en-US", 0 /* default family variant */, "A"); 58 Pair<File, Integer> fontForB = NativeSystemFontHelper.matchFamilyStyleCharacter( 59 "sans", 400, false, "en-US", 0 /* default family variant */, "B"); 60 assertEquals(fontForA, fontForB); 61 } 62 63 @Test 64 public void testMatchFamilyStyleCharacter_fallback() { 65 Pair<File, Integer> fontForA = NativeSystemFontHelper.matchFamilyStyleCharacter( 66 "Unknown-Generic-Family", 400, false, "en-US", 0 /* default family variant */, "A"); 67 Pair<File, Integer> fontForB = NativeSystemFontHelper.matchFamilyStyleCharacter( 68 "Another-Unknown-Generic-Family", 400, false, "en-US", 69 0 /* default family variant */, "B"); 70 assertEquals(fontForA, fontForB); 71 } 72 73 @Test 74 public void testMatchFamilyStyleCharacter_notCrash() { 75 String[] genericFamilies = { 76 "sans", "sans-serif", "monospace", "cursive", "fantasy", // generic families 77 "Helvetica", "Roboto", "Times", // known family names but not supported by Android 78 "Unknown Families", // Random string 79 }; 80 81 int[] weights = { 82 0, 150, 400, 700, 1000, // valid weights 83 -100, 1100 // out-of-range 84 }; 85 86 boolean[] italics = { false, true }; 87 88 String[] languageTags = { 89 // Valid language tags 90 "", "en-US", "und", "ja-JP,zh-CN", "en-Latn", "en-Zsye-US", "en-GB", "en-GB,en-AU", 91 // Invalid language tags 92 "aaa", "100", "\u3042", "-" 93 }; 94 95 int[] familyVariants = { 0, 1, 2 }; // Family variants, DEFAULT, COMPACT and ELEGANT. 96 97 String[] inputTexts = { 98 "A", "B", "abc", // Alphabet input 99 "\u3042", "\u3042\u3046\u3048", "\u4F60\u597D", // CJK characters 100 "\u0627\u0644\u0639\u064E\u0631\u064E\u0628\u0650\u064A\u064E\u0651\u0629", // Arabic 101 // Emoji, emoji sequence and surrogate pairs 102 "\uD83D\uDE00", "\uD83C\uDDFA\uD83C\uDDF8", "\uD83D\uDC68\u200D\uD83C\uDFA4", 103 // Unpaired surrogate pairs 104 "\uD83D", "\uDE00", "\uDE00\uD83D", 105 106 }; 107 108 for (String familyName : genericFamilies) { 109 for (int weight : weights) { 110 for (boolean italic : italics) { 111 for (String languageTag : languageTags) { 112 for (int familyVariant : familyVariants) { 113 for (String inputText : inputTexts) { 114 Pair<File, Integer> result = 115 NativeSystemFontHelper.matchFamilyStyleCharacter( 116 familyName, weight, italic, languageTag, 117 familyVariant, inputText); 118 // We cannot expcet much here since OEM can change font 119 // configurations. 120 // At least, a font must be assigned for the first character. 121 assertTrue(result.second >= 1); 122 123 final File fontFile = result.first; 124 assertTrue(fontFile.exists()); 125 assertTrue(fontFile.isAbsolute()); 126 assertTrue(fontFile.isFile()); 127 assertTrue(fontFile.canRead()); 128 assertFalse(fontFile.canExecute()); 129 assertFalse(fontFile.canWrite()); 130 assertTrue(fontFile.length() > 0); 131 } 132 } 133 } 134 } 135 } 136 } 137 } 138 } 139