Fixing unit test to not rely on html markup.

These unit tests broke due to a change in the framework Html class and markup.
https://googleplex-android-review.googlesource.com/#/c/236067/

Since the unit tests have nothing to do with html, it is better to
change the unit tests so they do not rely on the html markup.

Bug: 7352945
Change-Id: I3340575c08687fe481e91b3897037960911b1bbb
diff --git a/tests/src/com/android/contacts/format/PrefixHighligherTest.java b/tests/src/com/android/contacts/format/PrefixHighligherTest.java
index 668330b..7f6e491 100644
--- a/tests/src/com/android/contacts/format/PrefixHighligherTest.java
+++ b/tests/src/com/android/contacts/format/PrefixHighligherTest.java
@@ -16,77 +16,70 @@
 
 package com.android.contacts.format;
 
-import android.test.AndroidTestCase;
 import android.test.suitebuilder.annotation.SmallTest;
-import android.widget.TextView;
+
+import junit.framework.TestCase;
 
 /**
  * Unit tests for {@link PrefixHighlighter}.
  */
 @SmallTest
-public class PrefixHighligherTest extends AndroidTestCase {
+public class PrefixHighligherTest extends TestCase {
     private static final int TEST_PREFIX_HIGHLIGHT_COLOR = 0xFF0000;
-    /** The HTML code used to mark the start of the highlighted part. */
-    private static final String START = "<font color =\"#1ff0000\">";
-    /** The HTML code used to mark the end of the highlighted part. */
-    private static final String END = "</font>";
 
     /** The object under test. */
     private PrefixHighlighter mPrefixHighlighter;
-    /** The view to on which the text is set. */
-    private TextView mView;
 
     @Override
     protected void setUp() throws Exception {
         super.setUp();
         mPrefixHighlighter = new PrefixHighlighter(TEST_PREFIX_HIGHLIGHT_COLOR);
-        mView = new TextView(getContext());
-        // This guarantees that the text will be stored as a spannable so that we can determine
-        // which styles have been applied to it.
-        mView.setText("", TextView.BufferType.SPANNABLE);
     }
 
-    public void testSetText_EmptyPrefix() {
-        mPrefixHighlighter.setText(mView, "", new char[0]);
-        SpannedTestUtils.checkHtmlText("", mView);
+    public void testApply_EmptyPrefix() {
+        CharSequence seq = mPrefixHighlighter.apply("", new char[0]);
+        SpannedTestUtils.assertNotSpanned(seq, "");
 
-        mPrefixHighlighter.setText(mView, "test", new char[0]);
-        SpannedTestUtils.checkHtmlText("test", mView);
+        seq = mPrefixHighlighter.apply("test", new char[0]);
+        SpannedTestUtils.assertNotSpanned(seq, "test");
     }
 
     public void testSetText_MatchingPrefix() {
-        mPrefixHighlighter.setText(mView, "test", "TE".toCharArray());
-        SpannedTestUtils.checkHtmlText(START + "te" + END + "st", mView);
+        final char[] charArray = "TE".toCharArray();
 
-        mPrefixHighlighter.setText(mView, "Test", "TE".toCharArray());
-        SpannedTestUtils.checkHtmlText(START + "Te" + END + "st", mView);
+        CharSequence seq = mPrefixHighlighter.apply("test", charArray);
+        SpannedTestUtils.assertPrefixSpan(seq, 0, 1);
 
-        mPrefixHighlighter.setText(mView, "TEst", "TE".toCharArray());
-        SpannedTestUtils.checkHtmlText(START + "TE" + END + "st", mView);
+        seq = mPrefixHighlighter.apply("Test", charArray);
+        SpannedTestUtils.assertPrefixSpan(seq, 0, 1);
 
-        mPrefixHighlighter.setText(mView, "a test", "TE".toCharArray());
-        SpannedTestUtils.checkHtmlText("a " + START + "te" + END + "st", mView);
+        seq = mPrefixHighlighter.apply("TEst", charArray);
+        SpannedTestUtils.assertPrefixSpan(seq, 0, 1);
+
+        seq = mPrefixHighlighter.apply("a test", charArray);
+        SpannedTestUtils.assertPrefixSpan(seq, 2, 3);
     }
 
     public void testSetText_NotMatchingPrefix() {
-        mPrefixHighlighter.setText(mView, "test", "TA".toCharArray());
-        SpannedTestUtils.checkHtmlText("test", mView);
+        final CharSequence seq = mPrefixHighlighter.apply("test", "TA".toCharArray());
+        SpannedTestUtils.assertNotSpanned(seq, "test");
     }
 
     public void testSetText_FirstMatch() {
-        mPrefixHighlighter.setText(mView, "a test's tests are not tests", "TE".toCharArray());
-        SpannedTestUtils.checkHtmlText("a " +START + "te" + END + "st's tests are not tests",
-                mView);
+        final CharSequence seq = mPrefixHighlighter.apply("a test's tests are not tests",
+                "TE".toCharArray());
+        SpannedTestUtils.assertPrefixSpan(seq, 2, 3);
     }
 
     public void testSetText_NoMatchingMiddleOfWord() {
-        mPrefixHighlighter.setText(mView, "atest", "TE".toCharArray());
-        SpannedTestUtils.checkHtmlText("atest", mView);
+        final char[] charArray = "TE".toCharArray();
+        CharSequence seq = mPrefixHighlighter.apply("atest", charArray);
+        SpannedTestUtils.assertNotSpanned(seq, "atest");
 
-        mPrefixHighlighter.setText(mView, "atest otest", "TE".toCharArray());
-        SpannedTestUtils.checkHtmlText("atest otest", mView);
+        seq = mPrefixHighlighter.apply("atest otest", charArray);
+        SpannedTestUtils.assertNotSpanned(seq, "atest otest");
 
-        mPrefixHighlighter.setText(mView, "atest test", "TE".toCharArray());
-        SpannedTestUtils.checkHtmlText("atest " + START + "te" + END + "st", mView);
+        seq = mPrefixHighlighter.apply("atest test", charArray);
+        SpannedTestUtils.assertPrefixSpan(seq, 6, 7);
     }
 }
diff --git a/tests/src/com/android/contacts/format/SpannedTestUtils.java b/tests/src/com/android/contacts/format/SpannedTestUtils.java
index ce228a7..6fa028d 100644
--- a/tests/src/com/android/contacts/format/SpannedTestUtils.java
+++ b/tests/src/com/android/contacts/format/SpannedTestUtils.java
@@ -20,6 +20,7 @@
 import android.text.Html;
 import android.text.Spanned;
 import android.text.TextUtils;
+import android.text.style.ForegroundColorSpan;
 import android.widget.TextView;
 
 import junit.framework.Assert;
@@ -45,4 +46,38 @@
         }
     }
 
+
+    /**
+     * Assert span exists in the correct location.
+     *
+     * @param seq The spannable string to check.
+     * @param start The starting index.
+     * @param end The ending index.
+     */
+    public static void assertPrefixSpan(CharSequence seq, int start, int end) {
+        Assert.assertTrue(seq instanceof Spanned);
+        Spanned spannable = (Spanned) seq;
+
+        if (start > 0) {
+            Assert.assertEquals(0, getNumForegroundColorSpansBetween(spannable, 0, start - 1));
+        }
+        Assert.assertEquals(1, getNumForegroundColorSpansBetween(spannable, start, end));
+        Assert.assertEquals(0, getNumForegroundColorSpansBetween(spannable, end + 1,
+                spannable.length() - 1));
+    }
+
+    private static int getNumForegroundColorSpansBetween(Spanned value, int start, int end) {
+        return value.getSpans(start, end, ForegroundColorSpan.class).length;
+    }
+
+    /**
+     * Asserts that the given character sequence is not a Spanned object and text is correct.
+     *
+     * @param seq The sequence to check.
+     * @param expected The expected text.
+     */
+    public static void assertNotSpanned(CharSequence seq, String expected) {
+        Assert.assertFalse(seq instanceof Spanned);
+        Assert.assertEquals(expected, seq);
+    }
 }
diff --git a/tests/src/com/android/contacts/list/ContactListItemViewTest.java b/tests/src/com/android/contacts/list/ContactListItemViewTest.java
index 748876f..7847ebb 100644
--- a/tests/src/com/android/contacts/list/ContactListItemViewTest.java
+++ b/tests/src/com/android/contacts/list/ContactListItemViewTest.java
@@ -21,12 +21,15 @@
 import android.provider.ContactsContract;
 import android.test.ActivityInstrumentationTestCase2;
 import android.test.suitebuilder.annotation.LargeTest;
+import android.text.SpannableString;
+import android.text.Spanned;
 import android.widget.TextView;
 
 import com.android.contacts.activities.PeopleActivity;
 import com.android.contacts.format.SpannedTestUtils;
 import com.android.contacts.util.IntegrationTestUtils;
 
+
 /**
  * Unit tests for {@link ContactListItemView}.
  *
@@ -66,7 +69,7 @@
 
         view.showDisplayName(cursor, 0, ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY);
 
-        SpannedTestUtils.checkHtmlText("John Doe", view.getNameTextView());
+        assertEquals(view.getNameTextView().getText().toString(), "John Doe");
     }
 
     public void testShowDisplayName_Unknown() {
@@ -76,7 +79,7 @@
         view.setUnknownNameText("unknown");
         view.showDisplayName(cursor, 0, ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY);
 
-        SpannedTestUtils.checkHtmlText("unknown", view.getNameTextView());
+        assertEquals(view.getNameTextView().getText().toString(), "unknown");
     }
 
     public void testShowDisplayName_WithPrefix() {
@@ -86,8 +89,9 @@
         view.setHighlightedPrefix("DOE".toCharArray());
         view.showDisplayName(cursor, 0, ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY);
 
-        SpannedTestUtils.checkHtmlText("John " + START + "Doe" + END,
-                view.getNameTextView());
+        CharSequence seq = view.getNameTextView().getText();
+        assertEquals("John Doe", seq.toString());
+        SpannedTestUtils.assertPrefixSpan(seq, 5, 7);
     }
 
     public void testShowDisplayName_WithPrefixReversed() {
@@ -97,16 +101,20 @@
         view.setHighlightedPrefix("DOE".toCharArray());
         view.showDisplayName(cursor, 0, ContactsContract.Preferences.DISPLAY_ORDER_ALTERNATIVE);
 
-        SpannedTestUtils.checkHtmlText("John " + START + "Doe" + END,
-                view.getNameTextView());
+        CharSequence seq = view.getNameTextView().getText();
+        assertEquals("John Doe", seq.toString());
+        SpannedTestUtils.assertPrefixSpan(seq, 5, 7);
     }
 
     public void testSetSnippet_Prefix() {
         ContactListItemView view = createView();
         view.setHighlightedPrefix("TEST".toCharArray());
         view.setSnippet("This is a test");
-        SpannedTestUtils.checkHtmlText("This is a " + START + "test" + END,
-                view.getSnippetView());
+
+        CharSequence seq = view.getSnippetView().getText();
+
+        assertEquals("This is a test", seq.toString());
+        SpannedTestUtils.assertPrefixSpan(seq, 10, 13);
     }
 
     /** Creates the view to be tested. */