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.assertNotNull; 21 import static org.junit.Assert.assertNotSame; 22 import static org.junit.Assert.assertSame; 23 import static org.junit.Assert.assertTrue; 24 25 import android.content.res.AssetManager; 26 27 import androidx.test.InstrumentationRegistry; 28 import androidx.test.filters.SmallTest; 29 import androidx.test.runner.AndroidJUnit4; 30 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 34 import java.io.IOException; 35 36 @SmallTest 37 @RunWith(AndroidJUnit4.class) 38 public class FontFamilyTest { 39 private static final String TAG = "FontFamilyTest"; 40 private static final String FONT_DIR = "fonts/family_selection/ttf/"; 41 42 @Test 43 public void testBuilder_SingleFont() throws IOException { 44 AssetManager am = InstrumentationRegistry.getTargetContext().getAssets(); 45 Font font = new Font.Builder(am, FONT_DIR + "ascii_g3em_weight400_upright.ttf").build(); 46 FontFamily family = new FontFamily.Builder(font).build(); 47 assertNotNull(family); 48 assertEquals(1, family.getSize()); 49 assertSame(font, family.getFont(0)); 50 } 51 52 @Test 53 public void testBuilder_MultipleFont() throws IOException { 54 AssetManager am = InstrumentationRegistry.getTargetContext().getAssets(); 55 Font regularFont = new Font.Builder( 56 am, FONT_DIR + "ascii_g3em_weight400_upright.ttf").build(); 57 Font boldFont = new Font.Builder( 58 am, FONT_DIR + "ascii_m3em_weight700_upright.ttf").build(); 59 FontFamily family = new FontFamily.Builder(regularFont).addFont(boldFont).build(); 60 assertNotNull(family); 61 assertEquals(2, family.getSize()); 62 assertNotSame(family.getFont(0), family.getFont(1)); 63 assertTrue(family.getFont(0) == regularFont || family.getFont(0) == boldFont); 64 assertTrue(family.getFont(1) == regularFont || family.getFont(1) == boldFont); 65 } 66 67 @Test 68 public void testBuilder_MultipleFont_overrideWeight() throws IOException { 69 AssetManager am = InstrumentationRegistry.getTargetContext().getAssets(); 70 Font regularFont = new Font.Builder( 71 am, FONT_DIR + "ascii_g3em_weight400_upright.ttf").build(); 72 Font boldFont = new Font.Builder(am, FONT_DIR + "ascii_g3em_weight400_upright.ttf") 73 .setWeight(700).build(); 74 FontFamily family = new FontFamily.Builder(regularFont).addFont(boldFont).build(); 75 assertNotNull(family); 76 assertEquals(2, family.getSize()); 77 assertNotSame(family.getFont(0), family.getFont(1)); 78 assertTrue(family.getFont(0) == regularFont || family.getFont(0) == boldFont); 79 assertTrue(family.getFont(1) == regularFont || family.getFont(1) == boldFont); 80 } 81 82 @Test 83 public void testBuilder_MultipleFont_overrideItalic() throws IOException { 84 AssetManager am = InstrumentationRegistry.getTargetContext().getAssets(); 85 Font regularFont = new Font.Builder( 86 am, FONT_DIR + "ascii_g3em_weight400_upright.ttf").build(); 87 Font italicFont = new Font.Builder(am, FONT_DIR + "ascii_g3em_weight400_upright.ttf") 88 .setSlant(FontStyle.FONT_SLANT_ITALIC).build(); 89 FontFamily family = new FontFamily.Builder(regularFont).addFont(italicFont).build(); 90 assertNotNull(family); 91 assertEquals(2, family.getSize()); 92 assertNotSame(family.getFont(0), family.getFont(1)); 93 assertTrue(family.getFont(0) == regularFont || family.getFont(0) == italicFont); 94 assertTrue(family.getFont(1) == regularFont || family.getFont(1) == italicFont); 95 } 96 97 @Test(expected = IllegalArgumentException.class) 98 public void testBuilder_MultipleFont_SameStyle() throws IOException { 99 AssetManager am = InstrumentationRegistry.getTargetContext().getAssets(); 100 Font regularFont = new Font.Builder( 101 am, FONT_DIR + "ascii_g3em_weight400_upright.ttf").build(); 102 Font regularFont2 = new Font.Builder( 103 am, FONT_DIR + "ascii_g3em_weight400_upright.ttf").build(); 104 new FontFamily.Builder(regularFont).addFont(regularFont2).build(); 105 } 106 107 @Test(expected = IllegalArgumentException.class) 108 public void testBuilder_MultipleFont_SameStyle_overrideWeight() throws IOException { 109 AssetManager am = InstrumentationRegistry.getTargetContext().getAssets(); 110 Font regularFont = new Font.Builder( 111 am, FONT_DIR + "ascii_g3em_weight400_upright.ttf").build(); 112 Font regularFont2 = new Font.Builder(am, FONT_DIR + "ascii_m3em_weight700_upright.ttf") 113 .setWeight(400).build(); 114 new FontFamily.Builder(regularFont).addFont(regularFont2).build(); 115 } 116 117 @Test(expected = IllegalArgumentException.class) 118 public void testBuilder_MultipleFont_SameStyle_overrideItalic() throws IOException { 119 AssetManager am = InstrumentationRegistry.getTargetContext().getAssets(); 120 Font regularFont = new Font.Builder( 121 am, FONT_DIR + "ascii_g3em_weight400_upright.ttf").build(); 122 Font regularFont2 = new Font.Builder(am, FONT_DIR + "ascii_h3em_weight400_italic.ttf") 123 .setSlant(FontStyle.FONT_SLANT_UPRIGHT).build(); 124 new FontFamily.Builder(regularFont).addFont(regularFont2).build(); 125 } 126 } 127