1 package org.robolectric.shadows; 2 3 import static com.google.common.truth.Truth.assertThat; 4 5 import android.text.SpannableString; 6 import android.text.style.URLSpan; 7 import android.text.style.UnderlineSpan; 8 import androidx.test.ext.junit.runners.AndroidJUnit4; 9 import org.junit.Before; 10 import org.junit.Test; 11 import org.junit.runner.RunWith; 12 13 @RunWith(AndroidJUnit4.class) 14 public class ShadowSpannableStringTest { 15 private static final String TEST_STRING = "Visit us at http://www.foobar.com for more selections"; 16 17 private SpannableString spanStr; 18 19 @Before 20 public void setUp() throws Exception { 21 spanStr = new SpannableString(TEST_STRING); 22 } 23 24 @Test 25 public void testToString() { 26 assertThat(spanStr.toString()).isSameAs(TEST_STRING); 27 } 28 29 @Test 30 public void testSetSpan() { 31 URLSpan s1 = new URLSpan("http://www.foobar.com"); 32 UnderlineSpan s2 = new UnderlineSpan(); 33 spanStr.setSpan(s1, 12, 33, 0); 34 spanStr.setSpan(s2, 1, 10, 0); 35 36 assertBothSpans(s1, s2); 37 } 38 39 @Test 40 public void testRemoveSpan() { 41 URLSpan s1 = new URLSpan("http://www.foobar.com"); 42 UnderlineSpan s2 = new UnderlineSpan(); 43 spanStr.setSpan(s1, 12, 33, 0); 44 spanStr.setSpan(s2, 1, 10, 0); 45 spanStr.removeSpan(s1); 46 47 Object[] spans = spanStr.getSpans(0, TEST_STRING.length(), Object.class); 48 assertThat(spans).isNotNull(); 49 assertThat(spans.length).isEqualTo(1); 50 assertThat((UnderlineSpan) spans[0]).isSameAs(s2); 51 } 52 53 @Test 54 public void testGetSpans() { 55 URLSpan s1 = new URLSpan("http://www.foobar.com"); 56 UnderlineSpan s2 = new UnderlineSpan(); 57 spanStr.setSpan(s1, 1, 10, 0); 58 spanStr.setSpan(s2, 20, 30, 0); 59 60 Object[] spans = spanStr.getSpans(0, TEST_STRING.length(), Object.class); 61 assertThat(spans).isNotNull(); 62 assertThat(spans.length).isEqualTo(2); 63 assertBothSpans(s1, s2); 64 65 spans = spanStr.getSpans(0, TEST_STRING.length(), URLSpan.class); 66 assertThat(spans).isNotNull(); 67 assertThat(spans.length).isEqualTo(1); 68 assertThat((URLSpan) spans[0]).isSameAs(s1); 69 70 spans = spanStr.getSpans(11, 35, Object.class); 71 assertThat(spans).isNotNull(); 72 assertThat(spans.length).isEqualTo(1); 73 assertThat((UnderlineSpan) spans[0]).isSameAs(s2); 74 75 spans = spanStr.getSpans(21, 35, Object.class); 76 assertThat(spans).isNotNull(); 77 assertThat(spans.length).isEqualTo(1); 78 assertThat((UnderlineSpan) spans[0]).isSameAs(s2); 79 80 spans = spanStr.getSpans(5, 15, Object.class); 81 assertThat(spans).isNotNull(); 82 assertThat(spans.length).isEqualTo(1); 83 assertThat((URLSpan) spans[0]).isSameAs(s1); 84 } 85 86 private void assertBothSpans(URLSpan s1, UnderlineSpan s2) { 87 Object[] spans = spanStr.getSpans(0, TEST_STRING.length(), Object.class); 88 if (spans[0] instanceof URLSpan) { 89 assertThat((URLSpan) spans[0]).isSameAs(s1); 90 } else { 91 assertThat((UnderlineSpan) spans[0]).isSameAs(s2); 92 } 93 if (spans[1] instanceof UnderlineSpan) { 94 assertThat((UnderlineSpan) spans[1]).isSameAs(s2); 95 } else { 96 assertThat((URLSpan) spans[1]).isSameAs(s1); 97 } 98 } 99 100 } 101