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.text.style.cts;
18
19
20import android.graphics.Typeface;
21import android.os.Parcel;
22import android.text.TextPaint;
23import android.text.style.StyleSpan;
24
25import junit.framework.TestCase;
26
27public class StyleSpanTest extends TestCase {
28    public void testConstructor() {
29        StyleSpan styleSpan = new StyleSpan(2);
30
31        Parcel p = Parcel.obtain();
32        try {
33            styleSpan.writeToParcel(p, 0);
34            p.setDataPosition(0);
35            StyleSpan fromParcel = new StyleSpan(p);
36            assertEquals(2, fromParcel.getStyle());
37            new StyleSpan(-2);
38        } finally {
39            p.recycle();
40        }
41    }
42
43    public void testGetStyle() {
44        StyleSpan styleSpan = new StyleSpan(2);
45        assertEquals(2, styleSpan.getStyle());
46
47        styleSpan = new StyleSpan(-2);
48        assertEquals(-2, styleSpan.getStyle());
49    }
50
51    public void testUpdateMeasureState() {
52        StyleSpan styleSpan = new StyleSpan(Typeface.BOLD);
53
54        TextPaint tp = new TextPaint();
55        Typeface tf = Typeface.defaultFromStyle(Typeface.NORMAL);
56        tp.setTypeface(tf);
57
58        assertNotNull(tp.getTypeface());
59        assertEquals(Typeface.NORMAL, tp.getTypeface().getStyle());
60
61        styleSpan.updateMeasureState(tp);
62
63        assertNotNull(tp.getTypeface());
64        assertEquals(Typeface.BOLD, tp.getTypeface().getStyle());
65
66        try {
67            styleSpan.updateMeasureState(null);
68            fail("should throw NullPointerException.");
69        } catch (NullPointerException e) {
70            // expected, test success.
71        }
72    }
73
74    public void testUpdateDrawState() {
75        StyleSpan styleSpan = new StyleSpan(Typeface.BOLD);
76
77        TextPaint tp = new TextPaint();
78        Typeface tf = Typeface.defaultFromStyle(Typeface.NORMAL);
79        tp.setTypeface(tf);
80
81        assertNotNull(tp.getTypeface());
82        assertEquals(Typeface.NORMAL, tp.getTypeface().getStyle());
83
84        styleSpan.updateDrawState(tp);
85
86        assertNotNull(tp.getTypeface());
87        assertEquals(Typeface.BOLD, tp.getTypeface().getStyle());
88
89        try {
90            styleSpan.updateDrawState(null);
91            fail("should throw NullPointerException.");
92        } catch (NullPointerException e) {
93            // expected, test success.
94        }
95    }
96    public void testDescribeContents() {
97        StyleSpan styleSpan = new StyleSpan(Typeface.BOLD);
98        styleSpan.describeContents();
99    }
100
101    public void testGetSpanTypeId() {
102        StyleSpan styleSpan = new StyleSpan(Typeface.BOLD);
103        styleSpan.getSpanTypeId();
104    }
105
106    public void testWriteToParcel() {
107        Parcel p = Parcel.obtain();
108        try {
109            StyleSpan styleSpan = new StyleSpan(Typeface.BOLD);
110            styleSpan.writeToParcel(p, 0);
111            p.setDataPosition(0);
112            StyleSpan newSpan = new StyleSpan(p);
113            assertEquals(Typeface.BOLD, newSpan.getStyle());
114        } finally {
115            p.recycle();
116        }
117    }
118}
119