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.assertTrue; 21 22 import android.content.Context; 23 import android.graphics.Canvas; 24 import android.graphics.Paint.FontMetricsInt; 25 import android.graphics.drawable.Drawable; 26 import android.support.test.InstrumentationRegistry; 27 import android.support.test.filters.SmallTest; 28 import android.support.test.runner.AndroidJUnit4; 29 import android.text.Html; 30 import android.text.Layout; 31 import android.text.Spanned; 32 import android.text.StaticLayout; 33 import android.text.TextPaint; 34 import android.text.cts.R; 35 import android.text.style.DrawableMarginSpan; 36 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 41 @SmallTest 42 @RunWith(AndroidJUnit4.class) 43 public class DrawableMarginSpanTest { 44 private Context mContext; 45 private Drawable mDrawable; 46 47 @Before 48 public void setup() { 49 mContext = InstrumentationRegistry.getTargetContext(); 50 mDrawable = mContext.getDrawable(R.drawable.scenery); 51 } 52 53 @Test 54 public void testConstructor() { 55 Drawable d = mContext.getDrawable(R.drawable.pass); 56 57 new DrawableMarginSpan(d); 58 new DrawableMarginSpan(d, 1); 59 new DrawableMarginSpan(null, -1); 60 } 61 62 @Test 63 public void testGetLeadingMargin() { 64 DrawableMarginSpan drawableMarginSpan = new DrawableMarginSpan(mDrawable, 1); 65 int leadingMargin1 = drawableMarginSpan.getLeadingMargin(true); 66 67 drawableMarginSpan = new DrawableMarginSpan(mDrawable, 10); 68 int leadingMargin2 = drawableMarginSpan.getLeadingMargin(true); 69 70 assertTrue(leadingMargin2 > leadingMargin1); 71 } 72 73 @Test 74 public void testDrawLeadingMargin() { 75 DrawableMarginSpan drawableMarginSpan = new DrawableMarginSpan(mDrawable, 0); 76 77 assertEquals(0, mDrawable.getBounds().top); 78 assertEquals(0, mDrawable.getBounds().bottom); 79 assertEquals(0, mDrawable.getBounds().left); 80 assertEquals(0, mDrawable.getBounds().right); 81 82 Canvas canvas = new Canvas(); 83 Spanned text = Html.fromHtml("<b>hello</b>"); 84 TextPaint paint = new TextPaint(); 85 Layout layout = new StaticLayout("cts test.", paint, 200, 86 Layout.Alignment.ALIGN_NORMAL, 1, 0, true); 87 88 int x = 10; 89 drawableMarginSpan.drawLeadingMargin(canvas, null, x, 0, 0, 90 0, 0, text, 0, 0, true, layout); 91 92 // 0 means the top location 93 assertEquals(0, mDrawable.getBounds().top); 94 assertEquals(mDrawable.getIntrinsicHeight(), mDrawable.getBounds().bottom); 95 assertEquals(x, mDrawable.getBounds().left); 96 assertEquals(x + mDrawable.getIntrinsicWidth(), mDrawable.getBounds().right); 97 } 98 99 @Test(expected=NullPointerException.class) 100 public void testDrawLeadingMarginNull() { 101 DrawableMarginSpan drawableMarginSpan = new DrawableMarginSpan(mDrawable, 0); 102 103 drawableMarginSpan.drawLeadingMargin(null, null, 0, 0, 0, 0, 0, 104 null, 0, 0, false, null); 105 } 106 107 @Test(expected=ClassCastException.class) 108 public void testDrawLeadingMarginString() { 109 DrawableMarginSpan drawableMarginSpan = new DrawableMarginSpan(mDrawable, 0); 110 111 // When try to use a String as the text, should throw ClassCastException 112 drawableMarginSpan.drawLeadingMargin(null, null, 0, 0, 0, 0, 0, 113 "cts test.", 0, 0, false, null); 114 } 115 116 @Test 117 public void testChooseHeight() { 118 DrawableMarginSpan drawableMarginSpan = new DrawableMarginSpan(mDrawable, 0); 119 120 Spanned text = Html.fromHtml("cts test."); 121 FontMetricsInt fm = new FontMetricsInt(); 122 123 assertEquals(0, fm.ascent); 124 assertEquals(0, fm.bottom); 125 assertEquals(0, fm.descent); 126 assertEquals(0, fm.leading); 127 assertEquals(0, fm.top); 128 129 // should not change ascent, leading and top. 130 drawableMarginSpan.chooseHeight(text, 0, text.getSpanEnd(drawableMarginSpan), 0, 0, fm); 131 132 assertEquals(0, fm.ascent); 133 // do not know what they should be, because missed javadoc. only check they are positive. 134 assertTrue(fm.bottom > 0); 135 assertTrue(fm.descent > 0); 136 assertEquals(0, fm.leading); 137 assertEquals(0, fm.top); 138 } 139 140 @Test(expected=NullPointerException.class) 141 public void testChooseHeightNull() { 142 DrawableMarginSpan drawableMarginSpan = new DrawableMarginSpan(mDrawable, 0); 143 144 drawableMarginSpan.chooseHeight(null, 0, 0, 0, 0, null); 145 } 146 147 148 @Test(expected=ClassCastException.class) 149 public void testChooseHeightString() { 150 DrawableMarginSpan drawableMarginSpan = new DrawableMarginSpan(mDrawable, 0); 151 152 // When try to use a String as the text, should throw ClassCastException 153 drawableMarginSpan.chooseHeight("cts test.", 0, 0, 0, 0, null); 154 } 155 } 156