Code example for Spannable

Methods: getSelectionEnd, getSpanEnd, getSpanStart, length, subSequence

0
    /** 
     * Verify that the internal state of this class is consistent. 
     */ 
//    @VisibleForTesting 
    void checkInvariant(final Spannable s) {
        int suggestedStart = s.getSpanStart(mSuggested);
        int suggestedEnd = s.getSpanEnd(mSuggested);
        int cursorPos = Selection.getSelectionEnd(s);
        if (suggestedStart == -1 || suggestedEnd == -1) {
            suggestedStart = suggestedEnd = s.length();
        } 
        String userEntered = getUserText();
        Log.d(TAG, "checkInvariant all='" + s + "' (len " + s.length() + ") sug="
                + suggestedStart + ".." + suggestedEnd + " cursor=" + cursorPos +
                " ue='" + userEntered + "' (len " + userEntered.length() + ")");
        int suggestedLen = suggestedEnd - suggestedStart;
        Assert.assertEquals("Sum of user and suggested text lengths doesn't match total length",
                s.length(), userEntered.length() + suggestedLen);
        Assert.assertEquals("End of user entered text doesn't match start of suggested",
                suggestedStart, userEntered.length());
        Assert.assertTrue("user entered text does not match start of buffer",
                userEntered.toString().equalsIgnoreCase(
                        s.subSequence(0, suggestedStart).toString()));
        if (mSuggestedText != null && suggestedStart < s.length()) {
            Assert.assertTrue("User entered is not a prefix of suggested",
                    mSuggestedText.startsWith(userEntered.toString().toLowerCase()));
            Assert.assertTrue("Suggested text does not match buffer contents",
                    mSuggestedText.equalsIgnoreCase(s.toString().toLowerCase()));
        }