Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Spannable and its color from TextView to write a unit test

private void createStringEndingInRedColor(TextView tv, String word1, String word2) {
    Spannable word = new SpannableString(word1);
    tv.setText(word);

    Spannable wordTwo = new SpannableString(word2);

    wordTwo.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(Color.RED)), 0, wordTwo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    tv.append(wordTwo);
}

I'm trying to write a unit test (using Robolectric) for the TextView tv to ensure that wordTwo is Color.RED. However, I only have a reference to TextView tv. How does one go about such a task?

like image 797
sudocoder Avatar asked Oct 28 '14 14:10

sudocoder


1 Answers

You can get the color of the Spannable from TextView using getSpans() method

ForegroundColorSpan[] colorSpans = ((SpannableString)textView.getText()).getSpans(0, textView.getText().length(), ForegroundColorSpan.class);
assertTrue(colorSpans[0].getForegroundColor() == Color.RED)
like image 109
Nikola Despotoski Avatar answered Nov 15 '22 19:11

Nikola Despotoski