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 top = 10;
104         final int bottom = top + DRAWABLE_SIZE + padding;
105 
106         final Bitmap bitmap = Bitmap.createBitmap(DRAWABLE_SIZE, bottom, Bitmap.Config.ARGB_8888);
107         final Canvas canvas = new Canvas(bitmap);
108         canvas.drawColor(Color.RED);
109         dynamicDrawableSpan.draw(canvas, null /* text */, 0 /* start */, 0 /* end */, 0f /* x */,
110                 top, 0 /* y */, bottom, null /* paint */);
111 
112         // Top should be completely red.
113         for (int i = 0; i < top + padding / 2; i++) {
114             assertThat(bitmap.getColor(0, i).toArgb()).isEqualTo(Color.RED);
115         }
116         // Center should have been filled with drawable.
117         for (int i = 0; i < DRAWABLE_SIZE; i++) {
118             assertThat(bitmap.getColor(0, top + i + padding / 2).toArgb()).isNotEqualTo(Color.RED);
119         }
120         // Bottom should be also red.
121         for (int i = 0; i < padding / 2; i++) {
122             assertThat(bitmap.getColor(0, top + i + DRAWABLE_SIZE + padding / 2).toArgb())
123                     .isEqualTo(Color.RED);
124         }
125         bitmap.recycle();
126     }
127 
128     private class MyDynamicDrawableSpan extends DynamicDrawableSpan {
129         private final Drawable mDrawable;
130 
MyDynamicDrawableSpan()131         public MyDynamicDrawableSpan() {
132             this(ALIGN_BOTTOM);
133         }
134 
MyDynamicDrawableSpan(int verticalAlignment)135         protected MyDynamicDrawableSpan(int verticalAlignment) {
136             super(verticalAlignment);
137             mDrawable = InstrumentationRegistry.getTargetContext().getDrawable(R.drawable.scenery);
138             mDrawable.setBounds(0, 0, DRAWABLE_SIZE, DRAWABLE_SIZE);
139         }
140 
141         @Override
getDrawable()142         public Drawable getDrawable() {
143             return mDrawable;
144         }
145     }
146 }
147