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
19import com.android.cts.stub.R;
20
21
22import android.graphics.Canvas;
23import android.graphics.Rect;
24import android.graphics.Paint.FontMetricsInt;
25import android.graphics.drawable.Drawable;
26import android.test.AndroidTestCase;
27import android.text.style.DynamicDrawableSpan;
28
29public class DynamicDrawableSpanTest extends AndroidTestCase {
30    public void testConstructor() {
31        DynamicDrawableSpan d = new MyDynamicDrawableSpan();
32        assertEquals(DynamicDrawableSpan.ALIGN_BOTTOM, d.getVerticalAlignment());
33
34        d = new MyDynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BASELINE);
35        assertEquals(DynamicDrawableSpan.ALIGN_BASELINE, d.getVerticalAlignment());
36
37        d = new MyDynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BOTTOM);
38        assertEquals(DynamicDrawableSpan.ALIGN_BOTTOM, d.getVerticalAlignment());
39    }
40
41    public void testGetSize() {
42        DynamicDrawableSpan dynamicDrawableSpan = new MyDynamicDrawableSpan();
43        FontMetricsInt fm = new FontMetricsInt();
44
45        assertEquals(0, fm.ascent);
46        assertEquals(0, fm.bottom);
47        assertEquals(0, fm.descent);
48        assertEquals(0, fm.leading);
49        assertEquals(0, fm.top);
50
51        Rect rect = dynamicDrawableSpan.getDrawable().getBounds();
52        assertEquals(rect.right, dynamicDrawableSpan.getSize(null, null, 0, 0, fm));
53
54        assertEquals(-rect.bottom, fm.ascent);
55        assertEquals(0, fm.bottom);
56        assertEquals(0, fm.descent);
57        assertEquals(0, fm.leading);
58        assertEquals(-rect.bottom, fm.top);
59
60        assertEquals(rect.right, dynamicDrawableSpan.getSize(null, null, 0, 0, null));
61    }
62
63    public void testDraw() {
64        DynamicDrawableSpan dynamicDrawableSpan = new MyDynamicDrawableSpan();
65        Canvas canvas = new Canvas();
66        dynamicDrawableSpan.draw(canvas, null, 0, 0, 1.0f, 0, 0, 1, null);
67
68        try {
69            dynamicDrawableSpan.draw(null, null, 0, 0, 1.0f, 0, 0, 1, null);
70            fail("should throw NullPointerException.");
71        } catch (NullPointerException e) {
72            // expected, test success.
73        }
74    }
75
76    /**
77     * The MyDynamicDrawableSpan for test.
78     */
79    private class MyDynamicDrawableSpan extends DynamicDrawableSpan {
80        public MyDynamicDrawableSpan() {
81            super();
82        }
83
84        protected MyDynamicDrawableSpan(int verticalAlignment) {
85            super(verticalAlignment);
86        }
87
88        @Override
89        public Drawable getDrawable() {
90            // implement abstract method
91            return getContext().getResources().getDrawable(R.drawable.scenery);
92        }
93    }
94}
95