1/* 2 * Copyright (C) 2014 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 17package android.uirendering.cts.testclasses; 18 19import android.graphics.Bitmap; 20import android.graphics.BitmapFactory; 21import android.graphics.Color; 22import android.graphics.Paint; 23import android.graphics.Typeface; 24import android.support.test.filters.MediumTest; 25import android.support.test.runner.AndroidJUnit4; 26import android.uirendering.cts.R; 27import android.uirendering.cts.bitmapcomparers.BitmapComparer; 28import android.uirendering.cts.bitmapcomparers.MSSIMComparer; 29import android.uirendering.cts.bitmapverifiers.GoldenImageVerifier; 30import android.uirendering.cts.testinfrastructure.ActivityTestBase; 31 32import org.junit.Test; 33import org.junit.runner.RunWith; 34 35@MediumTest 36@RunWith(AndroidJUnit4.class) 37public class FontRenderingTests extends ActivityTestBase { 38 // Thresholds are barely loose enough for differences between sw and hw renderers. 39 private static final double REGULAR_THRESHOLD = 0.92; 40 private static final double THIN_THRESHOLD = 0.87; 41 42 // Representative characters including some from Unicode 7 43 private static final String sTestString1 = "Hambu"; 44 private static final String sTestString2 = "rg \u20bd"; 45 private static final String sTestString3 = "\u20b9\u0186\u0254\u1e24\u1e43"; 46 47 private void fontTestBody(String family, int style, int id) { 48 Bitmap goldenBitmap = BitmapFactory.decodeResource(getActivity().getResources(), id); 49 50 // adjust threshold based on thinness - more variance is expected in thin cases 51 boolean thinTestCase = family.endsWith("-thin") && ((style & Typeface.BOLD) == 0); 52 BitmapComparer comparer = new MSSIMComparer( 53 thinTestCase ? THIN_THRESHOLD : REGULAR_THRESHOLD); 54 55 final Typeface typeface = Typeface.create(family, style); 56 createTest() 57 .addCanvasClient((canvas, width, height) -> { 58 Paint p = new Paint(); 59 p.setAntiAlias(true); 60 p.setColor(Color.BLACK); 61 p.setTextSize(26); 62 p.setTypeface(typeface); 63 canvas.drawText(sTestString1, 1, 20, p); 64 canvas.drawText(sTestString2, 1, 50, p); 65 canvas.drawText(sTestString3, 1, 80, p); 66 }) 67 .runWithVerifier(new GoldenImageVerifier(goldenBitmap, comparer)); 68 } 69 70 @Test 71 public void testDefaultFont() { 72 fontTestBody("sans-serif", 73 Typeface.NORMAL, 74 R.drawable.hello1); 75 } 76 77 @Test 78 public void testBoldFont() { 79 fontTestBody("sans-serif", 80 Typeface.BOLD, 81 R.drawable.bold1); 82 } 83 84 @Test 85 public void testItalicFont() { 86 fontTestBody("sans-serif", 87 Typeface.ITALIC, 88 R.drawable.italic1); 89 } 90 91 @Test 92 public void testBoldItalicFont() { 93 fontTestBody("sans-serif", 94 Typeface.BOLD | Typeface.ITALIC, 95 R.drawable.bolditalic1); 96 } 97 98 @Test 99 public void testMediumFont() { 100 fontTestBody("sans-serif-medium", 101 Typeface.NORMAL, 102 R.drawable.medium1); 103 } 104 105 @Test 106 public void testMediumBoldFont() { 107 // bold attribute on medium base font = black 108 fontTestBody("sans-serif-medium", 109 Typeface.BOLD, 110 R.drawable.black1); 111 } 112 113 @Test 114 public void testMediumItalicFont() { 115 fontTestBody("sans-serif-medium", 116 Typeface.ITALIC, 117 R.drawable.mediumitalic1); 118 } 119 120 @Test 121 public void testMediumBoldItalicFont() { 122 fontTestBody("sans-serif-medium", 123 Typeface.BOLD | Typeface.ITALIC, 124 R.drawable.blackitalic1); 125 } 126 127 @Test 128 public void testLightFont() { 129 fontTestBody("sans-serif-light", 130 Typeface.NORMAL, 131 R.drawable.light1); 132 } 133 134 @Test 135 public void testLightBoldFont() { 136 // bold attribute on light base font = medium 137 fontTestBody("sans-serif-light", 138 Typeface.BOLD, 139 R.drawable.medium1); 140 } 141 142 @Test 143 public void testLightItalicFont() { 144 fontTestBody("sans-serif-light", 145 Typeface.ITALIC, 146 R.drawable.lightitalic1); 147 } 148 149 @Test 150 public void testLightBoldItalicFont() { 151 fontTestBody("sans-serif-light", 152 Typeface.BOLD | Typeface.ITALIC, 153 R.drawable.mediumitalic1); 154 } 155 156 @Test 157 public void testThinFont() { 158 fontTestBody("sans-serif-thin", 159 Typeface.NORMAL, 160 R.drawable.thin1); 161 } 162 163 @Test 164 public void testThinBoldFont() { 165 // bold attribute on thin base font = normal 166 fontTestBody("sans-serif-thin", 167 Typeface.BOLD, 168 R.drawable.hello1); 169 } 170 171 @Test 172 public void testThinItalicFont() { 173 fontTestBody("sans-serif-thin", 174 Typeface.ITALIC, 175 R.drawable.thinitalic1); 176 } 177 178 @Test 179 public void testThinBoldItalicFont() { 180 fontTestBody("sans-serif-thin", 181 Typeface.BOLD | Typeface.ITALIC, 182 R.drawable.italic1); 183 } 184 185 @Test 186 public void testBlackFont() { 187 fontTestBody("sans-serif-black", 188 Typeface.NORMAL, 189 R.drawable.black1); 190 } 191 192 @Test 193 public void testBlackBoldFont() { 194 // bold attribute on black base font = black 195 fontTestBody("sans-serif-black", 196 Typeface.BOLD, 197 R.drawable.black1); 198 } 199 200 @Test 201 public void testBlackItalicFont() { 202 fontTestBody("sans-serif-black", 203 Typeface.ITALIC, 204 R.drawable.blackitalic1); 205 } 206 207 @Test 208 public void testBlackBoldItalicFont() { 209 fontTestBody("sans-serif-black", 210 Typeface.BOLD | Typeface.ITALIC, 211 R.drawable.blackitalic1); 212 } 213 214 /* condensed fonts */ 215 216 @Test 217 public void testCondensedFont() { 218 fontTestBody("sans-serif-condensed", 219 Typeface.NORMAL, 220 R.drawable.condensed1); 221 } 222 223 @Test 224 public void testCondensedBoldFont() { 225 fontTestBody("sans-serif-condensed", 226 Typeface.BOLD, 227 R.drawable.condensedbold1); 228 } 229 230 @Test 231 public void testCondensedItalicFont() { 232 fontTestBody("sans-serif-condensed", 233 Typeface.ITALIC, 234 R.drawable.condenseditalic1); 235 } 236 237 @Test 238 public void testCondensedBoldItalicFont() { 239 fontTestBody("sans-serif-condensed", 240 Typeface.BOLD | Typeface.ITALIC, 241 R.drawable.condensedbolditalic1); 242 } 243 244 @Test 245 public void testCondensedLightFont() { 246 fontTestBody("sans-serif-condensed-light", 247 Typeface.NORMAL, 248 R.drawable.condensedlight1); 249 } 250 251 @Test 252 public void testCondensedLightItalicFont() { 253 fontTestBody("sans-serif-condensed-light", 254 Typeface.ITALIC, 255 R.drawable.condensedlightitalic1); 256 } 257} 258