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 
17 package android.uirendering.cts.testclasses;
18 
19 import android.graphics.Bitmap;
20 import android.graphics.BitmapFactory;
21 import android.graphics.Canvas;
22 import android.graphics.Color;
23 import android.graphics.Paint;
24 import android.graphics.Typeface;
25 import android.test.suitebuilder.annotation.SmallTest;
26 import android.uirendering.cts.bitmapcomparers.BitmapComparer;
27 import android.uirendering.cts.bitmapcomparers.MSSIMComparer;
28 import android.uirendering.cts.bitmapverifiers.GoldenImageVerifier;
29 import android.uirendering.cts.testinfrastructure.ActivityTestBase;
30 import android.uirendering.cts.testinfrastructure.CanvasClient;
31 
32 import com.android.cts.uirendering.R;
33 
34 public class FontRenderingTests extends ActivityTestBase {
35     // Threshold is barely loose enough for differences between sw and hw renderers
36     static double MSSIM_THRESHOLD = 0.91;
37 
38     private final BitmapComparer mFuzzyComparer = new MSSIMComparer(MSSIM_THRESHOLD);
39 
40     // Representative characters including some from Unicode 7
41     private final String mTestString1 = "Hamburg \u20bd";
42     private final String mTestString2 = "\u20b9\u0186\u0254\u1e24\u1e43";
43 
fontTestBody(final Typeface typeface, int id)44     private void fontTestBody(final Typeface typeface, int id) {
45         Bitmap goldenBitmap = BitmapFactory.decodeResource(getActivity().getResources(), id);
46         createTest()
47                 .addCanvasClient(new CanvasClient() {
48                     @Override
49                     public void draw(Canvas canvas, int width, int height) {
50                         Paint p = new Paint();
51                         p.setAntiAlias(true);
52                         p.setColor(Color.BLACK);
53                         p.setTextSize(30);
54                         p.setTypeface(typeface);
55                         canvas.drawText(mTestString1, 10, 60, p);
56                         canvas.drawText(mTestString2, 10, 100, p);
57                     }
58                 })
59                 .runWithVerifier(new GoldenImageVerifier(goldenBitmap, mFuzzyComparer));
60     }
61 
62     @SmallTest
testDefaultFont()63     public void testDefaultFont() {
64         Typeface tf = Typeface.create("sans-serif", Typeface.NORMAL);
65         fontTestBody(tf, R.drawable.hello1);
66     }
67 
68     @SmallTest
testBoldFont()69     public void testBoldFont() {
70         Typeface tf = Typeface.create("sans-serif", Typeface.BOLD);
71         fontTestBody(tf, R.drawable.bold1);
72     }
73 
74     @SmallTest
testItalicFont()75     public void testItalicFont() {
76         Typeface tf = Typeface.create("sans-serif", Typeface.ITALIC);
77         fontTestBody(tf, R.drawable.italic1);
78     }
79 
80     @SmallTest
testBoldItalicFont()81     public void testBoldItalicFont() {
82         Typeface tf = Typeface.create("sans-serif", Typeface.BOLD | Typeface.ITALIC);
83         fontTestBody(tf, R.drawable.bolditalic1);
84     }
85 
86     @SmallTest
testMediumFont()87     public void testMediumFont() {
88         Typeface tf = Typeface.create("sans-serif-medium", Typeface.NORMAL);
89         fontTestBody(tf, R.drawable.medium1);
90     }
91 
92     @SmallTest
testMediumBoldFont()93     public void testMediumBoldFont() {
94         // bold attribute on medium base font = black
95         Typeface tf = Typeface.create("sans-serif-medium", Typeface.BOLD);
96         fontTestBody(tf, R.drawable.black1);
97     }
98 
99     @SmallTest
testMediumItalicFont()100     public void testMediumItalicFont() {
101         Typeface tf = Typeface.create("sans-serif-medium", Typeface.ITALIC);
102         fontTestBody(tf, R.drawable.mediumitalic1);
103     }
104 
105     @SmallTest
testMediumBoldItalicFont()106     public void testMediumBoldItalicFont() {
107         Typeface tf = Typeface.create("sans-serif-medium", Typeface.BOLD | Typeface.ITALIC);
108         fontTestBody(tf, R.drawable.blackitalic1);
109     }
110 
111     @SmallTest
testLightFont()112     public void testLightFont() {
113         Typeface tf = Typeface.create("sans-serif-light", Typeface.NORMAL);
114         fontTestBody(tf, R.drawable.light1);
115     }
116 
117     @SmallTest
testLightBoldFont()118     public void testLightBoldFont() {
119         // bold attribute on light base font = medium
120         Typeface tf = Typeface.create("sans-serif-light", Typeface.BOLD);
121         fontTestBody(tf, R.drawable.medium1);
122     }
123 
124     @SmallTest
testLightItalicFont()125     public void testLightItalicFont() {
126         Typeface tf = Typeface.create("sans-serif-light", Typeface.ITALIC);
127         fontTestBody(tf, R.drawable.lightitalic1);
128     }
129 
130     @SmallTest
testLightBoldItalicFont()131     public void testLightBoldItalicFont() {
132         Typeface tf = Typeface.create("sans-serif-light", Typeface.BOLD | Typeface.ITALIC);
133         fontTestBody(tf, R.drawable.mediumitalic1);
134     }
135 
136     @SmallTest
testThinFont()137     public void testThinFont() {
138         Typeface tf = Typeface.create("sans-serif-thin", Typeface.NORMAL);
139         fontTestBody(tf, R.drawable.thin1);
140     }
141 
142     @SmallTest
testThinBoldFont()143     public void testThinBoldFont() {
144         // bold attribute on thin base font = normal
145         Typeface tf = Typeface.create("sans-serif-thin", Typeface.BOLD);
146         fontTestBody(tf, R.drawable.hello1);
147     }
148 
149     @SmallTest
testThinItalicFont()150     public void testThinItalicFont() {
151         Typeface tf = Typeface.create("sans-serif-thin", Typeface.ITALIC);
152         fontTestBody(tf, R.drawable.thinitalic1);
153     }
154 
155     @SmallTest
testThinBoldItalicFont()156     public void testThinBoldItalicFont() {
157         Typeface tf = Typeface.create("sans-serif-thin", Typeface.BOLD | Typeface.ITALIC);
158         fontTestBody(tf, R.drawable.italic1);
159     }
160 
161     @SmallTest
testBlackFont()162     public void testBlackFont() {
163         Typeface tf = Typeface.create("sans-serif-black", Typeface.NORMAL);
164         fontTestBody(tf, R.drawable.black1);
165     }
166 
167     @SmallTest
testBlackBoldFont()168     public void testBlackBoldFont() {
169         // bold attribute on black base font = black
170         Typeface tf = Typeface.create("sans-serif-black", Typeface.BOLD);
171         fontTestBody(tf, R.drawable.black1);
172     }
173 
174     @SmallTest
testBlackItalicFont()175     public void testBlackItalicFont() {
176         Typeface tf = Typeface.create("sans-serif-black", Typeface.ITALIC);
177         fontTestBody(tf, R.drawable.blackitalic1);
178     }
179 
180     @SmallTest
testBlackBoldItalicFont()181     public void testBlackBoldItalicFont() {
182         Typeface tf = Typeface.create("sans-serif-black", Typeface.BOLD | Typeface.ITALIC);
183         fontTestBody(tf, R.drawable.blackitalic1);
184     }
185 
186     /* condensed fonts */
187 
188     @SmallTest
testCondensedFont()189     public void testCondensedFont() {
190         Typeface tf = Typeface.create("sans-serif-condensed", Typeface.NORMAL);
191         fontTestBody(tf, R.drawable.condensed1);
192     }
193 
194     @SmallTest
testCondensedBoldFont()195     public void testCondensedBoldFont() {
196         Typeface tf = Typeface.create("sans-serif-condensed", Typeface.BOLD);
197         fontTestBody(tf, R.drawable.condensedbold1);
198     }
199 
200     @SmallTest
testCondensedItalicFont()201     public void testCondensedItalicFont() {
202         Typeface tf = Typeface.create("sans-serif-condensed", Typeface.ITALIC);
203         fontTestBody(tf, R.drawable.condenseditalic1);
204     }
205 
206     @SmallTest
testCondensedBoldItalicFont()207     public void testCondensedBoldItalicFont() {
208         Typeface tf = Typeface.create("sans-serif-condensed", Typeface.BOLD | Typeface.ITALIC);
209         fontTestBody(tf, R.drawable.condensedbolditalic1);
210     }
211 
212     @SmallTest
testCondensedLightFont()213     public void testCondensedLightFont() {
214         Typeface tf = Typeface.create("sans-serif-condensed-light", Typeface.NORMAL);
215         fontTestBody(tf, R.drawable.condensedlight1);
216     }
217 
218     @SmallTest
testCondensedLightItalicFont()219     public void testCondensedLightItalicFont() {
220         Typeface tf = Typeface.create("sans-serif-condensed-light", Typeface.ITALIC);
221         fontTestBody(tf, R.drawable.condensedlightitalic1);
222     }
223 }
224