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 
17 package android.text.style.cts;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.junit.Assert.assertEquals;
22 
23 import android.graphics.Bitmap;
24 import android.graphics.Canvas;
25 import android.graphics.Color;
26 import android.graphics.Paint.FontMetricsInt;
27 import android.graphics.Rect;
28 import android.graphics.drawable.Drawable;
29 import android.text.cts.R;
30 import android.text.style.DynamicDrawableSpan;
31 
32 import androidx.test.InstrumentationRegistry;
33 import androidx.test.filters.SmallTest;
34 import androidx.test.runner.AndroidJUnit4;
35 
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 
39 @SmallTest
40 @RunWith(AndroidJUnit4.class)
41 public class DynamicDrawableSpanTest {
42     private static final int DRAWABLE_SIZE = 50;
43 
44     @Test
testConstructor()45     public void testConstructor() {
46         DynamicDrawableSpan d = new MyDynamicDrawableSpan();
47         assertEquals(DynamicDrawableSpan.ALIGN_BOTTOM, d.getVerticalAlignment());
48 
49         d = new MyDynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BASELINE);
50         assertEquals(DynamicDrawableSpan.ALIGN_BASELINE, d.getVerticalAlignment());
51 
52         d = new MyDynamicDrawableSpan(DynamicDrawableSpan.ALIGN_BOTTOM);
53         assertEquals(DynamicDrawableSpan.ALIGN_BOTTOM, d.getVerticalAlignment());
54 
55         d = new MyDynamicDrawableSpan(DynamicDrawableSpan.ALIGN_CENTER);
56         assertEquals(DynamicDrawableSpan.ALIGN_CENTER, d.getVerticalAlignment());
57     }
58 
59     @Test
testGetSize()60     public void testGetSize() {
61         DynamicDrawableSpan dynamicDrawableSpan = new MyDynamicDrawableSpan();
62         FontMetricsInt fm = new FontMetricsInt();
63 
64         assertEquals(0, fm.ascent);
65         assertEquals(0, fm.bottom);
66         assertEquals(0, fm.descent);
67         assertEquals(0, fm.leading);
68         assertEquals(0, fm.top);
69 
70         Rect rect = dynamicDrawableSpan.getDrawable().getBounds();
71         assertEquals(rect.right, dynamicDrawableSpan.getSize(null, null, 0, 0, fm));
72 
73         assertEquals(-rect.bottom, fm.ascent);
74         assertEquals(0, fm.bottom);
75         assertEquals(0, fm.descent);
76         assertEquals(0, fm.leading);
77         assertEquals(-rect.bottom, fm.top);
78 
79         assertEquals(rect.right, dynamicDrawableSpan.getSize(null, null, 0, 0, null));
80     }
81 
82     @Test
testDraw()83     public void testDraw() {
84         DynamicDrawableSpan dynamicDrawableSpan = new MyDynamicDrawableSpan();
85         Canvas canvas = new Canvas();
86         dynamicDrawableSpan.draw(canvas, null /* text */, 0 /* start */, 0 /* end */, 1.0f /* x */,
87                 0 /* top */, 0 /* y */, 1 /* bottom */, null /* paint */);
88     }
89 
90     @Test(expected=NullPointerException.class)
testDrawNullCanvas()91     public void testDrawNullCanvas() {
92         DynamicDrawableSpan dynamicDrawableSpan = new MyDynamicDrawableSpan();
93 
94         dynamicDrawableSpan.draw(null /* canvas */, null /* text */, 0 /* start */, 0 /* end */,
95                 1.0f /* x */, 0 /* top */, 0 /* y */, 1 /* bottom */, null /* paint */);
96     }
97 
98     @Test
testCenterAligned()99     public void testCenterAligned() {
100         final DynamicDrawableSpan dynamicDrawableSpan =
101                 new MyDynamicDrawableSpan(DynamicDrawableSpan.ALIGN_CENTER);
102         final int padding = 10;
103         final int bottom = DRAWABLE_SIZE + padding;
104 
105         final Bitmap bitmap = Bitmap.createBitmap(DRAWABLE_SIZE, bottom, Bitmap.Config.ARGB_8888);
106         final Canvas canvas = new Canvas(bitmap);
107         canvas.drawColor(Color.RED);
108         dynamicDrawableSpan.draw(canvas, null /* text */, 0 /* start */, 0 /* end */, 0f /* x */,
109                 0 /* top */, 0 /* y */, bottom, null /* paint */);
110 
111         // Top should be completely red.
112         for (int i = 0; i < padding / 2; i++) {
113             assertThat(bitmap.getColor(0, i).toArgb()).isEqualTo(Color.RED);
114         }
115         // Center should have been filled with drawable.
116         for (int i = 0; i < DRAWABLE_SIZE; i++) {
117             assertThat(bitmap.getColor(0, i + padding / 2).toArgb()).isNotEqualTo(Color.RED);
118         }
119         // Bottom should be also red.
120         for (int i = 0; i < padding / 2; i++) {
121             assertThat(bitmap.getColor(0, i + DRAWABLE_SIZE + padding / 2).toArgb())
122                     .isEqualTo(Color.RED);
123         }
124         bitmap.recycle();
125     }
126 
127     private class MyDynamicDrawableSpan extends DynamicDrawableSpan {
128         private final Drawable mDrawable;
129 
MyDynamicDrawableSpan()130         public MyDynamicDrawableSpan() {
131             this(ALIGN_BOTTOM);
132         }
133 
MyDynamicDrawableSpan(int verticalAlignment)134         protected MyDynamicDrawableSpan(int verticalAlignment) {
135             super(verticalAlignment);
136             mDrawable = InstrumentationRegistry.getTargetContext().getDrawable(R.drawable.scenery);
137             mDrawable.setBounds(0, 0, DRAWABLE_SIZE, DRAWABLE_SIZE);
138         }
139 
140         @Override
getDrawable()141         public Drawable getDrawable() {
142             return mDrawable;
143         }
144     }
145 }
146