• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.cts;
18 
19 import android.graphics.Color;
20 import android.graphics.Paint;
21 import android.graphics.Typeface;
22 import android.test.AndroidTestCase;
23 import android.text.TextPaint;
24 
25 /**
26  * Test {@link TextPaint}.
27  */
28 public class TextPaintTest extends AndroidTestCase {
29     private static final int DEFAULT_PAINT_FLAGS = TextPaint.DEV_KERN_TEXT_FLAG
30             | TextPaint.EMBEDDED_BITMAP_TEXT_FLAG | TextPaint.FILTER_BITMAP_FLAG;
31 
testConstructor()32     public void testConstructor() {
33         TextPaint textPaint;
34 
35         textPaint = new TextPaint();
36         assertEquals(DEFAULT_PAINT_FLAGS | TextPaint.ANTI_ALIAS_FLAG,
37                 textPaint.getFlags());
38 
39         textPaint = new TextPaint(TextPaint.DITHER_FLAG);
40         assertEquals((TextPaint.DITHER_FLAG | DEFAULT_PAINT_FLAGS),
41                 textPaint.getFlags());
42 
43         final Paint paint = new Paint();
44         paint.setTextSize(42f);
45         textPaint = new TextPaint(paint);
46         assertEquals(paint.getTextSize(), textPaint.getTextSize());
47     }
48 
testSet()49     public void testSet() {
50         TextPaint textPaintSrc = new TextPaint(TextPaint.DITHER_FLAG);
51         int[] drawableState = new int[] { 0, 1 };
52         textPaintSrc.bgColor = Color.GREEN;
53         textPaintSrc.baselineShift = 10;
54         textPaintSrc.linkColor = Color.BLUE;
55         textPaintSrc.drawableState = drawableState;
56         textPaintSrc.setTypeface(Typeface.DEFAULT_BOLD);
57         textPaintSrc.underlineColor = 0x00112233;
58         textPaintSrc.underlineThickness = 12.0f;
59 
60         TextPaint textPaint = new TextPaint();
61         assertEquals(0, textPaint.bgColor);
62         assertEquals(0, textPaint.baselineShift);
63         assertEquals(0, textPaint.linkColor);
64         assertNull(textPaint.drawableState);
65         assertNull(textPaint.getTypeface());
66         assertEquals(0, textPaint.underlineColor);
67         assertEquals(0.0f, textPaint.underlineThickness, 0.0f);
68 
69         textPaint.set(textPaintSrc);
70         assertEquals(textPaintSrc.bgColor, textPaint.bgColor);
71         assertEquals(textPaintSrc.baselineShift, textPaint.baselineShift);
72         assertEquals(textPaintSrc.linkColor, textPaint.linkColor);
73         assertSame(textPaintSrc.drawableState, textPaint.drawableState);
74         assertEquals(textPaintSrc.getTypeface(), textPaint.getTypeface());
75         assertEquals(textPaintSrc.getFlags(), textPaint.getFlags());
76         assertEquals(0x00112233, textPaint.underlineColor);
77         assertEquals(12.0f, textPaint.underlineThickness, 0.0f);
78 
79         try {
80             textPaint.set(null);
81             fail("Should throw NullPointerException!");
82         } catch (NullPointerException e) {
83         }
84     }
85 
86     // b/169080922
testInfinityTextSize_doesntCrash()87     public void testInfinityTextSize_doesntCrash() {
88         Paint paint = new Paint();
89         paint.setTextSize(Float.POSITIVE_INFINITY);
90 
91         // Making sure following measureText is not crashing
92         paint.measureText("Hello \uD83D\uDC4B");  // Latin characters and emoji
93     }
94 }
95