1/*
2 * Copyright (C) 2008 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.graphics;
18
19import android.graphics.Paint;
20import android.graphics.Typeface;
21import android.test.suitebuilder.annotation.MediumTest;
22import android.test.suitebuilder.annotation.SmallTest;
23import junit.framework.TestCase;
24
25
26public class TypefaceTest extends TestCase {
27
28    // create array of all std faces
29    private final Typeface[] mFaces = new Typeface[] {
30        Typeface.create(Typeface.SANS_SERIF, 0),
31        Typeface.create(Typeface.SANS_SERIF, 1),
32        Typeface.create(Typeface.SERIF, 0),
33        Typeface.create(Typeface.SERIF, 1),
34        Typeface.create(Typeface.SERIF, 2),
35        Typeface.create(Typeface.SERIF, 3),
36        Typeface.create(Typeface.MONOSPACE, 0)
37    };
38
39    @SmallTest
40    public void testBasic() throws Exception {
41        assertTrue("basic", Typeface.DEFAULT != null);
42        assertTrue("basic", Typeface.DEFAULT_BOLD != null);
43        assertTrue("basic", Typeface.SANS_SERIF != null);
44        assertTrue("basic", Typeface.SERIF != null);
45        assertTrue("basic", Typeface.MONOSPACE != null);
46    }
47
48    @SmallTest
49    public void testUnique() throws Exception {
50        final int n = mFaces.length;
51        for (int i = 0; i < n; i++) {
52            for (int j = i + 1; j < n; j++) {
53                assertTrue("unique", mFaces[i] != mFaces[j]);
54            }
55        }
56    }
57
58    @SmallTest
59    public void testStyles() throws Exception {
60        assertTrue("style", mFaces[0].getStyle() == Typeface.NORMAL);
61        assertTrue("style", mFaces[1].getStyle() == Typeface.BOLD);
62        assertTrue("style", mFaces[2].getStyle() == Typeface.NORMAL);
63        assertTrue("style", mFaces[3].getStyle() == Typeface.BOLD);
64        assertTrue("style", mFaces[4].getStyle() == Typeface.ITALIC);
65        assertTrue("style", mFaces[5].getStyle() == Typeface.BOLD_ITALIC);
66        assertTrue("style", mFaces[6].getStyle() == Typeface.NORMAL);
67    }
68
69    @MediumTest
70    public void testUniformY() throws Exception {
71        Paint p = new Paint();
72        final int n = mFaces.length;
73        for (int i = 1; i <= 36; i++) {
74            p.setTextSize(i);
75            float ascent = 0;
76            float descent = 0;
77            for (int j = 0; j < n; j++) {
78                p.setTypeface(mFaces[j]);
79                Paint.FontMetrics fm = p.getFontMetrics();
80                if (j == 0) {
81                    ascent = fm.ascent;
82                    descent = fm.descent;
83                } else {
84                    assertTrue("fontMetrics", fm.ascent == ascent);
85                    assertTrue("fontMetrics", fm.descent == descent);
86                }
87            }
88        }
89    }
90
91}
92