FileDocCategorySizeDatePackage
SpannedTest.javaAPI DocAndroid 1.5 API7283Wed May 06 22:42:02 BST 2009com.android.unit_tests

SpannedTest

public class SpannedTest extends TestCase
SpannedTest tests some features of Spanned

Fields Summary
private int
mExpect
Constructors Summary
Methods Summary
private voidcheckPriority(Spannable s)

        s.setSpan(new Object(), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE |
                                      (5 << Spannable.SPAN_PRIORITY_SHIFT));
        s.setSpan(new Object(), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE |
                                      (10 << Spannable.SPAN_PRIORITY_SHIFT));
        s.setSpan(new Object(), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE |
                                      (0 << Spannable.SPAN_PRIORITY_SHIFT));
        s.setSpan(new Object(), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE |
                                      (15 << Spannable.SPAN_PRIORITY_SHIFT));
        s.setSpan(new Object(), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE |
                                      (3 << Spannable.SPAN_PRIORITY_SHIFT));
        s.setSpan(new Object(), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE |
                                      (6 << Spannable.SPAN_PRIORITY_SHIFT));
        s.setSpan(new Object(), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE |
                                      (0 << Spannable.SPAN_PRIORITY_SHIFT));

        Object[] spans = s.getSpans(0, s.length(), Object.class);

        for (int i = 0; i < spans.length - 1; i++) {
            assertEquals((s.getSpanFlags(spans[i]) & Spanned.SPAN_PRIORITY) >=
                         (s.getSpanFlags(spans[i + 1]) & Spanned.SPAN_PRIORITY),
                         true);
        }

        mExpect = 0;

        s.setSpan(new Watcher(2), 0, s.length(), 
                  Spannable.SPAN_INCLUSIVE_INCLUSIVE |
                  (2 << Spannable.SPAN_PRIORITY_SHIFT));
        s.setSpan(new Watcher(4), 0, s.length(), 
                  Spannable.SPAN_INCLUSIVE_INCLUSIVE |
                  (4 << Spannable.SPAN_PRIORITY_SHIFT));
        s.setSpan(new Watcher(1), 0, s.length(), 
                  Spannable.SPAN_INCLUSIVE_INCLUSIVE |
                  (1 << Spannable.SPAN_PRIORITY_SHIFT));
        s.setSpan(new Watcher(3), 0, s.length(), 
                  Spannable.SPAN_INCLUSIVE_INCLUSIVE |
                  (3 << Spannable.SPAN_PRIORITY_SHIFT));

        mExpect = 4;
        s.setSpan(new Object(), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        assertEquals(mExpect, 0);
    
private voidcheckPriority2(SpannableStringBuilder ssb)

        checkPriority(ssb);

        mExpect = 4;
        ssb.insert(3, "something");
        assertEquals(mExpect, 0);
    
public voidtestAppend()

        Object o = new Object();
        SpannableString ss = new SpannableString("Test");
        ss.setSpan(o, 0, ss.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        SpannableStringBuilder ssb = new SpannableStringBuilder();
        ssb.append(ss);
        assertEquals(0, ssb.getSpanStart(o));
        assertEquals(4, ssb.getSpanEnd(o));
        assertEquals(1, ssb.getSpans(0, 4, Object.class).length);

        ssb.insert(0, ss);
        assertEquals(4, ssb.getSpanStart(o));
        assertEquals(8, ssb.getSpanEnd(o));
        assertEquals(0, ssb.getSpans(0, 4, Object.class).length);
        assertEquals(1, ssb.getSpans(4, 8, Object.class).length);
    
public voidtestSpannableString()

        checkPriority(new SpannableString("the quick brown fox"));
    
public voidtestSpannableStringBuilder()

        checkPriority2(new SpannableStringBuilder("the quick brown fox"));
    
public voidtestWrapParcel()

        SpannableString s = new SpannableString("Hello there world");
        CharacterStyle mark = new StyleSpan(Typeface.BOLD);
        s.setSpan(mark, 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        s.setSpan(CharacterStyle.wrap(mark), 3, 7,
                  Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        s.setSpan(new TextAppearanceSpan("mono", 0, -1, null, null), 7, 8,
                  Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        s.setSpan(CharacterStyle.wrap(new TypefaceSpan("mono")), 8, 9,
                  Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        Parcel p = Parcel.obtain();
        TextUtils.writeToParcel(s, p, 0);
        p.setDataPosition(0);

        Spanned s2 = (Spanned) TextUtils.CHAR_SEQUENCE_CREATOR.
                        createFromParcel(p);
        StyleSpan[] style;

        style = s2.getSpans(1, 2, StyleSpan.class);
        assertEquals(1, style.length);
        assertEquals(1, s2.getSpanStart(style[0]));
        assertEquals(2, s2.getSpanEnd(style[0]));

        style = s2.getSpans(3, 7, StyleSpan.class);
        assertEquals(1, style.length);
        assertEquals(3, s2.getSpanStart(style[0]));
        assertEquals(7, s2.getSpanEnd(style[0]));

        TextAppearanceSpan[] appearance = s2.getSpans(7, 8,
                                                TextAppearanceSpan.class);
        assertEquals(1, appearance.length);
        assertEquals(7, s2.getSpanStart(appearance[0]));
        assertEquals(8, s2.getSpanEnd(appearance[0]));

        TypefaceSpan[] tf = s2.getSpans(8, 9, TypefaceSpan.class);
        assertEquals(1, tf.length);
        assertEquals(8, s2.getSpanStart(tf[0]));
        assertEquals(9, s2.getSpanEnd(tf[0]));