1194d2514e818128c478c2b8e693715affa324ee9Scott Su/*
2194d2514e818128c478c2b8e693715affa324ee9Scott Su * Copyright (C) 2008 The Android Open Source Project
3194d2514e818128c478c2b8e693715affa324ee9Scott Su *
4194d2514e818128c478c2b8e693715affa324ee9Scott Su * Licensed under the Apache License, Version 2.0 (the "License");
5194d2514e818128c478c2b8e693715affa324ee9Scott Su * you may not use this file except in compliance with the License.
6194d2514e818128c478c2b8e693715affa324ee9Scott Su * You may obtain a copy of the License at
7194d2514e818128c478c2b8e693715affa324ee9Scott Su *
8194d2514e818128c478c2b8e693715affa324ee9Scott Su *      http://www.apache.org/licenses/LICENSE-2.0
9194d2514e818128c478c2b8e693715affa324ee9Scott Su *
10194d2514e818128c478c2b8e693715affa324ee9Scott Su * Unless required by applicable law or agreed to in writing, software
11194d2514e818128c478c2b8e693715affa324ee9Scott Su * distributed under the License is distributed on an "AS IS" BASIS,
12194d2514e818128c478c2b8e693715affa324ee9Scott Su * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13194d2514e818128c478c2b8e693715affa324ee9Scott Su * See the License for the specific language governing permissions and
14194d2514e818128c478c2b8e693715affa324ee9Scott Su * limitations under the License.
15194d2514e818128c478c2b8e693715affa324ee9Scott Su */
16194d2514e818128c478c2b8e693715affa324ee9Scott Su
17194d2514e818128c478c2b8e693715affa324ee9Scott Supackage android.text.style.cts;
18194d2514e818128c478c2b8e693715affa324ee9Scott Su
19194d2514e818128c478c2b8e693715affa324ee9Scott Su
20194d2514e818128c478c2b8e693715affa324ee9Scott Suimport android.graphics.Bitmap;
21194d2514e818128c478c2b8e693715affa324ee9Scott Suimport android.graphics.Canvas;
22194d2514e818128c478c2b8e693715affa324ee9Scott Suimport android.graphics.Paint.FontMetricsInt;
23194d2514e818128c478c2b8e693715affa324ee9Scott Suimport android.test.AndroidTestCase;
24194d2514e818128c478c2b8e693715affa324ee9Scott Suimport android.text.Html;
25194d2514e818128c478c2b8e693715affa324ee9Scott Suimport android.text.Layout;
26194d2514e818128c478c2b8e693715affa324ee9Scott Suimport android.text.Spanned;
27194d2514e818128c478c2b8e693715affa324ee9Scott Suimport android.text.StaticLayout;
28194d2514e818128c478c2b8e693715affa324ee9Scott Suimport android.text.TextPaint;
29194d2514e818128c478c2b8e693715affa324ee9Scott Suimport android.text.style.IconMarginSpan;
30194d2514e818128c478c2b8e693715affa324ee9Scott Su
31194d2514e818128c478c2b8e693715affa324ee9Scott Supublic class IconMarginSpanTest extends AndroidTestCase {
32194d2514e818128c478c2b8e693715affa324ee9Scott Su    private static final int WIDTH = 80;
33194d2514e818128c478c2b8e693715affa324ee9Scott Su    private static final int HEIGHT = 120;
34194d2514e818128c478c2b8e693715affa324ee9Scott Su    private static final int[] COLOR = new int[WIDTH * HEIGHT];
35194d2514e818128c478c2b8e693715affa324ee9Scott Su    private static final Bitmap BITMAP_80X120 =
36194d2514e818128c478c2b8e693715affa324ee9Scott Su        Bitmap.createBitmap(COLOR, WIDTH, HEIGHT, Bitmap.Config.RGB_565);
37194d2514e818128c478c2b8e693715affa324ee9Scott Su
38194d2514e818128c478c2b8e693715affa324ee9Scott Su    public void testConstructor() {
39194d2514e818128c478c2b8e693715affa324ee9Scott Su        new IconMarginSpan(BITMAP_80X120);
40194d2514e818128c478c2b8e693715affa324ee9Scott Su        new IconMarginSpan(BITMAP_80X120, 1);
41194d2514e818128c478c2b8e693715affa324ee9Scott Su        new IconMarginSpan(null, -1);
42194d2514e818128c478c2b8e693715affa324ee9Scott Su    }
43194d2514e818128c478c2b8e693715affa324ee9Scott Su
44194d2514e818128c478c2b8e693715affa324ee9Scott Su    public void testGetLeadingMargin() {
45194d2514e818128c478c2b8e693715affa324ee9Scott Su        IconMarginSpan iconMarginSpan = new IconMarginSpan(BITMAP_80X120, 1);
46194d2514e818128c478c2b8e693715affa324ee9Scott Su        int leadingMargin1 = iconMarginSpan.getLeadingMargin(true);
47194d2514e818128c478c2b8e693715affa324ee9Scott Su
48194d2514e818128c478c2b8e693715affa324ee9Scott Su        iconMarginSpan = new IconMarginSpan(BITMAP_80X120, 2);
49194d2514e818128c478c2b8e693715affa324ee9Scott Su        int leadingMargin2 = iconMarginSpan.getLeadingMargin(true);
50194d2514e818128c478c2b8e693715affa324ee9Scott Su
51194d2514e818128c478c2b8e693715affa324ee9Scott Su        assertTrue(leadingMargin2 > leadingMargin1);
52194d2514e818128c478c2b8e693715affa324ee9Scott Su    }
53194d2514e818128c478c2b8e693715affa324ee9Scott Su
54194d2514e818128c478c2b8e693715affa324ee9Scott Su    public void testDrawLeadingMargin() {
55194d2514e818128c478c2b8e693715affa324ee9Scott Su        IconMarginSpan iconMarginSpan = new IconMarginSpan(BITMAP_80X120, 0);
56194d2514e818128c478c2b8e693715affa324ee9Scott Su        Canvas c = new Canvas();
57194d2514e818128c478c2b8e693715affa324ee9Scott Su        Spanned text = Html.fromHtml("<b>hello</b>");
58194d2514e818128c478c2b8e693715affa324ee9Scott Su        TextPaint p = new TextPaint();
59194d2514e818128c478c2b8e693715affa324ee9Scott Su        Layout layout = new StaticLayout("cts test.", p, 200, Layout.Alignment.ALIGN_NORMAL,
60194d2514e818128c478c2b8e693715affa324ee9Scott Su                1, 0, true);
61194d2514e818128c478c2b8e693715affa324ee9Scott Su        iconMarginSpan.drawLeadingMargin(c, p, 0, 0, 0, 0, 0, text, 0, 0, true, layout);
62194d2514e818128c478c2b8e693715affa324ee9Scott Su
63194d2514e818128c478c2b8e693715affa324ee9Scott Su        try {
64194d2514e818128c478c2b8e693715affa324ee9Scott Su            iconMarginSpan.chooseHeight(null, 0, 0, 0, 0, null);
65194d2514e818128c478c2b8e693715affa324ee9Scott Su            fail("should throw NullPointerException.");
66194d2514e818128c478c2b8e693715affa324ee9Scott Su        } catch (NullPointerException e) {
67194d2514e818128c478c2b8e693715affa324ee9Scott Su            // expected, test success.
68194d2514e818128c478c2b8e693715affa324ee9Scott Su        }
69194d2514e818128c478c2b8e693715affa324ee9Scott Su
70194d2514e818128c478c2b8e693715affa324ee9Scott Su        try {
71194d2514e818128c478c2b8e693715affa324ee9Scott Su            iconMarginSpan.chooseHeight("cts test.", 0, 0, 0, 0, null);
72194d2514e818128c478c2b8e693715affa324ee9Scott Su            fail("When try to use a String as the text, should throw ClassCastException.");
73194d2514e818128c478c2b8e693715affa324ee9Scott Su        } catch (ClassCastException e) {
74194d2514e818128c478c2b8e693715affa324ee9Scott Su            // expected, test success.
75194d2514e818128c478c2b8e693715affa324ee9Scott Su        }
76194d2514e818128c478c2b8e693715affa324ee9Scott Su    }
77194d2514e818128c478c2b8e693715affa324ee9Scott Su
78194d2514e818128c478c2b8e693715affa324ee9Scott Su    public void testChooseHeight() {
79194d2514e818128c478c2b8e693715affa324ee9Scott Su        IconMarginSpan iconMarginSpan = new IconMarginSpan(BITMAP_80X120, 0);
80194d2514e818128c478c2b8e693715affa324ee9Scott Su
81194d2514e818128c478c2b8e693715affa324ee9Scott Su        Spanned text = Html.fromHtml("cts test.");
82194d2514e818128c478c2b8e693715affa324ee9Scott Su        FontMetricsInt fm = new FontMetricsInt();
83194d2514e818128c478c2b8e693715affa324ee9Scott Su
84194d2514e818128c478c2b8e693715affa324ee9Scott Su        assertEquals(0, fm.ascent);
85194d2514e818128c478c2b8e693715affa324ee9Scott Su        assertEquals(0, fm.bottom);
86194d2514e818128c478c2b8e693715affa324ee9Scott Su        assertEquals(0, fm.descent);
87194d2514e818128c478c2b8e693715affa324ee9Scott Su        assertEquals(0, fm.leading);
88194d2514e818128c478c2b8e693715affa324ee9Scott Su        assertEquals(0, fm.top);
89194d2514e818128c478c2b8e693715affa324ee9Scott Su
90194d2514e818128c478c2b8e693715affa324ee9Scott Su        iconMarginSpan.chooseHeight(text, 0, -1, 0, 0, fm);
91194d2514e818128c478c2b8e693715affa324ee9Scott Su
92194d2514e818128c478c2b8e693715affa324ee9Scott Su        assertEquals(0, fm.ascent);
93194d2514e818128c478c2b8e693715affa324ee9Scott Su        assertEquals(HEIGHT, fm.bottom);
94194d2514e818128c478c2b8e693715affa324ee9Scott Su        assertEquals(HEIGHT, fm.descent);
95194d2514e818128c478c2b8e693715affa324ee9Scott Su        assertEquals(0, fm.leading);
96194d2514e818128c478c2b8e693715affa324ee9Scott Su        assertEquals(0, fm.top);
97194d2514e818128c478c2b8e693715affa324ee9Scott Su
98194d2514e818128c478c2b8e693715affa324ee9Scott Su        try {
99194d2514e818128c478c2b8e693715affa324ee9Scott Su            iconMarginSpan.chooseHeight(null, 0, 0, 0, 0, null);
100194d2514e818128c478c2b8e693715affa324ee9Scott Su            fail("should throw NullPointerException.");
101194d2514e818128c478c2b8e693715affa324ee9Scott Su        } catch (NullPointerException e) {
102194d2514e818128c478c2b8e693715affa324ee9Scott Su            // expected, test success.
103194d2514e818128c478c2b8e693715affa324ee9Scott Su        }
104194d2514e818128c478c2b8e693715affa324ee9Scott Su
105194d2514e818128c478c2b8e693715affa324ee9Scott Su        try {
106194d2514e818128c478c2b8e693715affa324ee9Scott Su            iconMarginSpan.chooseHeight("cts test.", 0, 0, 0, 0, null);
107194d2514e818128c478c2b8e693715affa324ee9Scott Su            fail("When try to use a String as the text, should throw ClassCastException.");
108194d2514e818128c478c2b8e693715affa324ee9Scott Su        } catch (ClassCastException e) {
109194d2514e818128c478c2b8e693715affa324ee9Scott Su            // expected, test success.
110194d2514e818128c478c2b8e693715affa324ee9Scott Su        }
111194d2514e818128c478c2b8e693715affa324ee9Scott Su    }
112194d2514e818128c478c2b8e693715affa324ee9Scott Su}
113