1package com.xtremelabs.robolectric.shadows;
2
3import static org.hamcrest.CoreMatchers.equalTo;
4import static org.hamcrest.CoreMatchers.notNullValue;
5import static org.hamcrest.CoreMatchers.sameInstance;
6import static org.junit.Assert.assertThat;
7
8import com.xtremelabs.robolectric.WithTestDefaultsRunner;
9
10import org.junit.Before;
11import org.junit.Test;
12import org.junit.runner.RunWith;
13
14import android.text.SpannableString;
15import android.text.style.URLSpan;
16import android.text.style.UnderlineSpan;
17
18@RunWith(WithTestDefaultsRunner.class)
19public class SpannableStringTest {
20
21    private SpannableString spanStr;
22
23    private static final String TEST_STRING =
24            "Visit us at http://www.foobar.com for more selections";
25
26    @Before
27    public void setUp() throws Exception {
28        spanStr = new SpannableString(TEST_STRING);
29    }
30
31    @Test
32    public void testToString() {
33        assertThat(spanStr.toString(), sameInstance(TEST_STRING));
34    }
35
36    @Test
37    public void testSetSpan() {
38        URLSpan s1 = new URLSpan("http://www.foobar.com");
39        UnderlineSpan s2 = new UnderlineSpan();
40        spanStr.setSpan(s1, 12, 33, 0);
41        spanStr.setSpan(s2, 1, 10, 0);
42
43        assertBothSpans(s1, s2);
44    }
45
46    @Test
47    public void testRemoveSpan() {
48        URLSpan s1 = new URLSpan("http://www.foobar.com");
49        UnderlineSpan s2 = new UnderlineSpan();
50        spanStr.setSpan(s1, 12, 33, 0);
51        spanStr.setSpan(s2, 1, 10, 0);
52        spanStr.removeSpan(s1);
53
54        Object[] spans = spanStr.getSpans(0, TEST_STRING.length(), Object.class);
55        assertThat(spans, notNullValue());
56        assertThat(spans.length, equalTo(1));
57        assertThat((UnderlineSpan) spans[0], sameInstance(s2));
58    }
59
60    @Test
61    public void testGetSpans() {
62        URLSpan s1 = new URLSpan("http://www.foobar.com");
63        UnderlineSpan s2 = new UnderlineSpan();
64        spanStr.setSpan(s1, 1, 10, 0);
65        spanStr.setSpan(s2, 20, 30, 0);
66
67        Object[] spans = spanStr.getSpans(0, TEST_STRING.length(), Object.class);
68        assertThat(spans, notNullValue());
69        assertThat(spans.length, equalTo(2));
70        assertBothSpans(s1, s2);
71
72        spans = spanStr.getSpans(0, TEST_STRING.length(), URLSpan.class);
73        assertThat(spans, notNullValue());
74        assertThat(spans.length, equalTo(1));
75        assertThat((URLSpan) spans[0], sameInstance(s1));
76
77        spans = spanStr.getSpans(11, 35, Object.class);
78        assertThat(spans, notNullValue());
79        assertThat(spans.length, equalTo(1));
80        assertThat((UnderlineSpan) spans[0], sameInstance(s2));
81
82        spans = spanStr.getSpans(21, 35, Object.class);
83        assertThat(spans, notNullValue());
84        assertThat(spans.length, equalTo(1));
85        assertThat((UnderlineSpan) spans[0], sameInstance(s2));
86
87        spans = spanStr.getSpans(5, 15, Object.class);
88        assertThat(spans, notNullValue());
89        assertThat(spans.length, equalTo(1));
90        assertThat((URLSpan) spans[0], sameInstance(s1));
91    }
92
93    @Test
94    public void testGetSpanStart() {
95        URLSpan s1 = new URLSpan("http://www.foobar.com");
96        UnderlineSpan s2 = new UnderlineSpan();
97
98        assertThat(spanStr.getSpanStart(s1), equalTo(-1));
99        assertThat(spanStr.getSpanStart(s2), equalTo(-1));
100
101        spanStr.setSpan(s1, 0, 4, 0);
102        assertThat(spanStr.getSpanStart(s1), equalTo(0));
103
104        spanStr.setSpan(s2, 2, TEST_STRING.length(), 0);
105        assertThat(spanStr.getSpanStart(s2), equalTo(2));
106    }
107
108    @Test
109    public void testGetSpanEnd() {
110        URLSpan s1 = new URLSpan("http://www.foobar.com");
111        UnderlineSpan s2 = new UnderlineSpan();
112
113        assertThat(spanStr.getSpanEnd(s1), equalTo(-1));
114        assertThat(spanStr.getSpanEnd(s2), equalTo(-1));
115
116        spanStr.setSpan(s1, 0, 4, 0);
117        assertThat(spanStr.getSpanEnd(s1), equalTo(4));
118
119        spanStr.setSpan(s2, 2, TEST_STRING.length(), 0);
120        assertThat(spanStr.getSpanEnd(s2), equalTo(TEST_STRING.length()));
121    }
122
123    private void assertBothSpans(URLSpan s1, UnderlineSpan s2) {
124        Object[] spans = spanStr.getSpans(0, TEST_STRING.length(), Object.class);
125        if (spans[0] instanceof URLSpan) {
126            assertThat((URLSpan) spans[0], sameInstance(s1));
127        } else {
128            assertThat((UnderlineSpan) spans[0], sameInstance(s2));
129        }
130        if (spans[1] instanceof UnderlineSpan) {
131            assertThat((UnderlineSpan) spans[1], sameInstance(s2));
132        } else {
133            assertThat((URLSpan) spans[1], sameInstance(s1));
134        }
135    }
136}
137