• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.widget.cts;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 
22 import android.content.Context;
23 import android.graphics.Paint;
24 import android.graphics.Typeface;
25 import android.view.LayoutInflater;
26 import android.view.ViewGroup;
27 import android.widget.TextView;
28 
29 import androidx.test.InstrumentationRegistry;
30 import androidx.test.filters.SmallTest;
31 import androidx.test.runner.AndroidJUnit4;
32 
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.Map;
39 
40 /**
41  * Test for font weight in TextView
42  */
43 @SmallTest
44 @RunWith(AndroidJUnit4.class)
45 public class TextViewFontWeightTest {
getTextView(int id)46     private TextView getTextView(int id) {
47         final Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
48         final LayoutInflater inflater = LayoutInflater.from(context);
49         final ViewGroup container =
50                 (ViewGroup) inflater.inflate(R.layout.textview_weight_test_layout, null);
51         return (TextView) container.findViewById(id);
52     }
53 
54     private static class FontStyle {
FontStyle(int weight, boolean italic)55         FontStyle(int weight, boolean italic) {
56             mWeight = weight;
57             mItalic = italic;
58         }
59 
getWeight()60         public int getWeight() {
61             return mWeight;
62         }
63 
isItalic()64         public boolean isItalic() {
65             return mItalic;
66         }
67 
68         private int mWeight;
69         private boolean mItalic;
70     };
71 
72     private static final Map<Character, FontStyle> CHAR_FONT_MAP;
73     static {
74         // This mapping needs to be synced with res/font/fullfamily.xml
75         final HashMap<Character, FontStyle> map = new HashMap<>();
76         map.put('a', new FontStyle(100, false));
77         map.put('b', new FontStyle(100, true));
78         map.put('c', new FontStyle(200, false));
79         map.put('d', new FontStyle(200, true));
80         map.put('e', new FontStyle(300, false));
81         map.put('f', new FontStyle(300, true));
82         map.put('g', new FontStyle(400, false));
83         map.put('h', new FontStyle(400, true));
84         map.put('i', new FontStyle(500, false));
85         map.put('j', new FontStyle(500, true));
86         map.put('k', new FontStyle(600, false));
87         map.put('l', new FontStyle(600, true));
88         map.put('m', new FontStyle(700, false));
89         map.put('n', new FontStyle(700, true));
90         map.put('o', new FontStyle(800, false));
91         map.put('p', new FontStyle(800, true));
92         map.put('q', new FontStyle(900, false));
93         map.put('r', new FontStyle(900, true));
94         CHAR_FONT_MAP = Collections.unmodifiableMap(map);
95     }
96 
assertFontSelected(TextView tv, FontStyle style)97     private static void assertFontSelected(TextView tv, FontStyle style) {
98         // In this tests, the following special font is used for testing typeface.
99         // All fonts support 'a' to 'z' characters and all character has 1em advance except for one
100         // character. For example, 'ascii_a3em_weight100_upright.ttf' supports 'a' to 'z' characters
101         // and 'a' has 3em advance and others has 1em advance. Also, the metadata has width=100 and
102         // slant=upright information.
103         Typeface typeface = tv.getTypeface();
104         assertNotNull(typeface);
105         assertEquals(style.getWeight(), typeface.getWeight());
106         assertEquals(style.isItalic(), typeface.isItalic());
107 
108         // Check if the correct underlying font is selected.
109         char threeEmChar = 0;
110         for (char c = 'a'; c <= 'z'; c++) {
111             Paint paint = new Paint();
112             paint.setTextSize(10.0f);  // Make 1em=10px
113             paint.setTypeface(typeface);
114 
115             final float width = paint.measureText(new char[] { c }, 0 /* index */, 1 /* count */);
116             if (width == 30.0f) {
117                 if (threeEmChar != 0) {
118                     throw new IllegalStateException(
119                         "There are multiple 3em characters. Incorrect test set up?");
120                 }
121                 threeEmChar = c;
122             }
123         }
124 
125         FontStyle fontStyle = CHAR_FONT_MAP.get(threeEmChar);
126         assertNotNull(fontStyle);
127         assertEquals(style.getWeight(), fontStyle.getWeight());
128         assertEquals(style.isItalic(), fontStyle.isItalic());
129     }
130 
131     @Test
testWeight()132     public void testWeight() {
133         assertFontSelected(getTextView(R.id.textView_weight100_upright), new FontStyle(100, false));
134         assertFontSelected(getTextView(R.id.textView_weight100_italic), new FontStyle(100, true));
135         assertFontSelected(getTextView(R.id.textView_weight200_upright), new FontStyle(200, false));
136         assertFontSelected(getTextView(R.id.textView_weight200_italic), new FontStyle(200, true));
137         assertFontSelected(getTextView(R.id.textView_weight300_upright), new FontStyle(300, false));
138         assertFontSelected(getTextView(R.id.textView_weight300_italic), new FontStyle(300, true));
139         assertFontSelected(getTextView(R.id.textView_weight400_upright), new FontStyle(400, false));
140         assertFontSelected(getTextView(R.id.textView_weight400_italic), new FontStyle(400, true));
141         assertFontSelected(getTextView(R.id.textView_weight500_upright), new FontStyle(500, false));
142         assertFontSelected(getTextView(R.id.textView_weight500_italic), new FontStyle(500, true));
143         assertFontSelected(getTextView(R.id.textView_weight600_upright), new FontStyle(600, false));
144         assertFontSelected(getTextView(R.id.textView_weight600_italic), new FontStyle(600, true));
145         assertFontSelected(getTextView(R.id.textView_weight700_upright), new FontStyle(700, false));
146         assertFontSelected(getTextView(R.id.textView_weight700_italic), new FontStyle(700, true));
147         assertFontSelected(getTextView(R.id.textView_weight800_upright), new FontStyle(800, false));
148         assertFontSelected(getTextView(R.id.textView_weight800_italic), new FontStyle(800, true));
149         assertFontSelected(getTextView(R.id.textView_weight900_upright), new FontStyle(900, false));
150         assertFontSelected(getTextView(R.id.textView_weight900_italic), new FontStyle(900, true));
151     }
152 
153     @Test
testTextAppearance()154     public void testTextAppearance() {
155         assertFontSelected(getTextView(R.id.textAppearance_weight100_upright),
156                 new FontStyle(100, false));
157         assertFontSelected(getTextView(R.id.textAppearance_weight100_italic),
158                 new FontStyle(100, true));
159         assertFontSelected(getTextView(R.id.textAppearance_weight200_upright),
160                 new FontStyle(200, false));
161         assertFontSelected(getTextView(R.id.textAppearance_weight200_italic),
162                 new FontStyle(200, true));
163         assertFontSelected(getTextView(R.id.textAppearance_weight300_upright),
164                 new FontStyle(300, false));
165         assertFontSelected(getTextView(R.id.textAppearance_weight300_italic),
166                 new FontStyle(300, true));
167         assertFontSelected(getTextView(R.id.textAppearance_weight400_upright),
168                 new FontStyle(400, false));
169         assertFontSelected(getTextView(R.id.textAppearance_weight400_italic),
170                 new FontStyle(400, true));
171         assertFontSelected(getTextView(R.id.textAppearance_weight500_upright),
172                 new FontStyle(500, false));
173         assertFontSelected(getTextView(R.id.textAppearance_weight500_italic),
174                 new FontStyle(500, true));
175         assertFontSelected(getTextView(R.id.textAppearance_weight600_upright),
176                 new FontStyle(600, false));
177         assertFontSelected(getTextView(R.id.textAppearance_weight600_italic),
178                 new FontStyle(600, true));
179         assertFontSelected(getTextView(R.id.textAppearance_weight700_upright),
180                 new FontStyle(700, false));
181         assertFontSelected(getTextView(R.id.textAppearance_weight700_italic),
182                 new FontStyle(700, true));
183         assertFontSelected(getTextView(R.id.textAppearance_weight800_upright),
184                 new FontStyle(800, false));
185         assertFontSelected(getTextView(R.id.textAppearance_weight800_italic),
186                 new FontStyle(800, true));
187         assertFontSelected(getTextView(R.id.textAppearance_weight900_upright),
188                 new FontStyle(900, false));
189         assertFontSelected(getTextView(R.id.textAppearance_weight900_italic),
190                 new FontStyle(900, true));
191     }
192 
193     @Test
testStyle()194     public void testStyle() {
195         assertFontSelected(getTextView(R.id.textView_normal), new FontStyle(400, false));
196         assertFontSelected(getTextView(R.id.textView_bold), new FontStyle(700, false));
197         assertFontSelected(getTextView(R.id.textView_italic), new FontStyle(400, true));
198         assertFontSelected(getTextView(R.id.textView_bold_italic), new FontStyle(700, true));
199         assertFontSelected(getTextView(R.id.textAppearance_normal), new FontStyle(400, false));
200         assertFontSelected(getTextView(R.id.textAppearance_bold), new FontStyle(700, false));
201         assertFontSelected(getTextView(R.id.textAppearance_italic), new FontStyle(400, true));
202         assertFontSelected(getTextView(R.id.textAppearance_bold_italic), new FontStyle(700, true));
203     }
204 
205     @Test
testWeightStyleResolve()206     public void testWeightStyleResolve() {
207         // If both weight and style=bold is specified, ignore the boldness and use weight.
208         assertFontSelected(getTextView(R.id.textView_weight100_bold), new FontStyle(100, false));
209         assertFontSelected(getTextView(R.id.textAppearance_weight100_bold),
210                 new FontStyle(100, false));
211     }
212 }
213