Loading tests/src/com/android/contacts/format/PrefixHighligherTest.java +30 −37 Original line number Diff line number Diff line Loading @@ -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(); 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, "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("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); } } tests/src/com/android/contacts/format/SpannedTestUtils.java +35 −0 Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ import android.test.suitebuilder.annotation.SmallTest; 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; Loading @@ -45,4 +46,38 @@ public class SpannedTestUtils { } } /** * 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); } } tests/src/com/android/contacts/list/ContactListItemViewTest.java +16 −8 Original line number Diff line number Diff line Loading @@ -21,12 +21,15 @@ import android.database.MatrixCursor; 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}. * Loading Loading @@ -66,7 +69,7 @@ public class ContactListItemViewTest extends ActivityInstrumentationTestCase2<Pe 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() { Loading @@ -76,7 +79,7 @@ public class ContactListItemViewTest extends ActivityInstrumentationTestCase2<Pe 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() { Loading @@ -86,8 +89,9 @@ public class ContactListItemViewTest extends ActivityInstrumentationTestCase2<Pe 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() { Loading @@ -97,16 +101,20 @@ public class ContactListItemViewTest extends ActivityInstrumentationTestCase2<Pe 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. */ Loading Loading
tests/src/com/android/contacts/format/PrefixHighligherTest.java +30 −37 Original line number Diff line number Diff line Loading @@ -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(); 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, "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("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); } }
tests/src/com/android/contacts/format/SpannedTestUtils.java +35 −0 Original line number Diff line number Diff line Loading @@ -20,6 +20,7 @@ import android.test.suitebuilder.annotation.SmallTest; 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; Loading @@ -45,4 +46,38 @@ public class SpannedTestUtils { } } /** * 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); } }
tests/src/com/android/contacts/list/ContactListItemViewTest.java +16 −8 Original line number Diff line number Diff line Loading @@ -21,12 +21,15 @@ import android.database.MatrixCursor; 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}. * Loading Loading @@ -66,7 +69,7 @@ public class ContactListItemViewTest extends ActivityInstrumentationTestCase2<Pe 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() { Loading @@ -76,7 +79,7 @@ public class ContactListItemViewTest extends ActivityInstrumentationTestCase2<Pe 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() { Loading @@ -86,8 +89,9 @@ public class ContactListItemViewTest extends ActivityInstrumentationTestCase2<Pe 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() { Loading @@ -97,16 +101,20 @@ public class ContactListItemViewTest extends ActivityInstrumentationTestCase2<Pe 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. */ Loading