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