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 org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNotNull; 21 22 import android.graphics.Typeface; 23 import android.os.Parcel; 24 import android.support.test.filters.SmallTest; 25 import android.support.test.runner.AndroidJUnit4; 26 import android.text.TextPaint; 27 import android.text.style.StyleSpan; 28 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 32 @SmallTest 33 @RunWith(AndroidJUnit4.class) 34 public class StyleSpanTest { 35 @Test 36 public void testConstructor() { 37 StyleSpan styleSpan = new StyleSpan(2); 38 39 Parcel p = Parcel.obtain(); 40 try { 41 styleSpan.writeToParcel(p, 0); 42 p.setDataPosition(0); 43 StyleSpan fromParcel = new StyleSpan(p); 44 assertEquals(2, fromParcel.getStyle()); 45 new StyleSpan(-2); 46 } finally { 47 p.recycle(); 48 } 49 } 50 51 @Test 52 public void testGetStyle() { 53 StyleSpan styleSpan = new StyleSpan(2); 54 assertEquals(2, styleSpan.getStyle()); 55 56 styleSpan = new StyleSpan(-2); 57 assertEquals(-2, styleSpan.getStyle()); 58 } 59 60 @Test 61 public void testUpdateMeasureState() { 62 StyleSpan styleSpan = new StyleSpan(Typeface.BOLD); 63 64 TextPaint tp = new TextPaint(); 65 Typeface tf = Typeface.defaultFromStyle(Typeface.NORMAL); 66 tp.setTypeface(tf); 67 68 assertNotNull(tp.getTypeface()); 69 assertEquals(Typeface.NORMAL, tp.getTypeface().getStyle()); 70 71 styleSpan.updateMeasureState(tp); 72 73 assertNotNull(tp.getTypeface()); 74 assertEquals(Typeface.BOLD, tp.getTypeface().getStyle()); 75 } 76 77 @Test(expected=NullPointerException.class) 78 public void testUpdateMeasureStateNull() { 79 StyleSpan styleSpan = new StyleSpan(Typeface.BOLD); 80 81 styleSpan.updateMeasureState(null); 82 } 83 84 @Test 85 public void testUpdateDrawState() { 86 StyleSpan styleSpan = new StyleSpan(Typeface.BOLD); 87 88 TextPaint tp = new TextPaint(); 89 Typeface tf = Typeface.defaultFromStyle(Typeface.NORMAL); 90 tp.setTypeface(tf); 91 92 assertNotNull(tp.getTypeface()); 93 assertEquals(Typeface.NORMAL, tp.getTypeface().getStyle()); 94 95 styleSpan.updateDrawState(tp); 96 97 assertNotNull(tp.getTypeface()); 98 assertEquals(Typeface.BOLD, tp.getTypeface().getStyle()); 99 } 100 101 @Test(expected=NullPointerException.class) 102 public void testUpdateDrawStateNull() { 103 StyleSpan styleSpan = new StyleSpan(Typeface.BOLD); 104 105 styleSpan.updateDrawState(null); 106 } 107 108 @Test 109 public void testDescribeContents() { 110 StyleSpan styleSpan = new StyleSpan(Typeface.BOLD); 111 styleSpan.describeContents(); 112 } 113 114 @Test 115 public void testGetSpanTypeId() { 116 StyleSpan styleSpan = new StyleSpan(Typeface.BOLD); 117 styleSpan.getSpanTypeId(); 118 } 119 120 @Test 121 public void testWriteToParcel() { 122 Parcel p = Parcel.obtain(); 123 try { 124 StyleSpan styleSpan = new StyleSpan(Typeface.BOLD); 125 styleSpan.writeToParcel(p, 0); 126 p.setDataPosition(0); 127 StyleSpan newSpan = new StyleSpan(p); 128 assertEquals(Typeface.BOLD, newSpan.getStyle()); 129 } finally { 130 p.recycle(); 131 } 132 } 133 } 134