Code example for SpannableStringBuilder

Methods: delete, getSelectionEnd, length, setSelection, toString

0
    public void testRemoveLastChars() { 
        final String init = "65012345678";
        final String result1 = "(650) 123-4567";
        TextWatcher textWatcher = getTextWatcher();
        // Remove the last char. 
        SpannableStringBuilder number = new SpannableStringBuilder(init);
        int len = number.length();
        textWatcher.beforeTextChanged(number, len - 1, 1, 0);
        number.delete(len - 1, len);
        Selection.setSelection(number, number.length());
        textWatcher.onTextChanged(number, number.length() - 1, 1, 0);
        textWatcher.afterTextChanged(number);
        assertEquals(result1, number.toString());
        assertEquals(result1.length(), Selection.getSelectionEnd(number));
        // Remove last 5 chars 
        final String result2 = "(650) 123";
        textWatcher.beforeTextChanged(number, number.length() - 4, 4, 0);
        number.delete(number.length() - 5, number.length());
        Selection.setSelection(number, number.length());
        textWatcher.onTextChanged(number, number.length(), 4, 0);
        textWatcher.afterTextChanged(number);
        assertEquals(result2, number.toString());
        assertEquals(result2.length(), Selection.getSelectionEnd(number));
    } 
 
    public void testInsertChars() { 
        final String init = "(650) 23";
        final String expected1 = "(650) 123";