Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e8f84e89 authored by Chiao Cheng's avatar Chiao Cheng
Browse files

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
parent 1c9e54ba
Loading
Loading
Loading
Loading
+30 −37
Original line number Original line Diff line number Diff line
@@ -16,77 +16,70 @@


package com.android.contacts.format;
package com.android.contacts.format;


import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import android.test.suitebuilder.annotation.SmallTest;
import android.widget.TextView;

import junit.framework.TestCase;


/**
/**
 * Unit tests for {@link PrefixHighlighter}.
 * Unit tests for {@link PrefixHighlighter}.
 */
 */
@SmallTest
@SmallTest
public class PrefixHighligherTest extends AndroidTestCase {
public class PrefixHighligherTest extends TestCase {
    private static final int TEST_PREFIX_HIGHLIGHT_COLOR = 0xFF0000;
    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. */
    /** The object under test. */
    private PrefixHighlighter mPrefixHighlighter;
    private PrefixHighlighter mPrefixHighlighter;
    /** The view to on which the text is set. */
    private TextView mView;


    @Override
    @Override
    protected void setUp() throws Exception {
    protected void setUp() throws Exception {
        super.setUp();
        super.setUp();
        mPrefixHighlighter = new PrefixHighlighter(TEST_PREFIX_HIGHLIGHT_COLOR);
        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() {
    public void testApply_EmptyPrefix() {
        mPrefixHighlighter.setText(mView, "", new char[0]);
        CharSequence seq = mPrefixHighlighter.apply("", new char[0]);
        SpannedTestUtils.checkHtmlText("", mView);
        SpannedTestUtils.assertNotSpanned(seq, "");


        mPrefixHighlighter.setText(mView, "test", new char[0]);
        seq = mPrefixHighlighter.apply("test", new char[0]);
        SpannedTestUtils.checkHtmlText("test", mView);
        SpannedTestUtils.assertNotSpanned(seq, "test");
    }
    }


    public void testSetText_MatchingPrefix() {
    public void testSetText_MatchingPrefix() {
        mPrefixHighlighter.setText(mView, "test", "TE".toCharArray());
        final char[] charArray = "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());
        seq = mPrefixHighlighter.apply("Test", charArray);
        SpannedTestUtils.checkHtmlText(START + "Te" + END + "st", mView);
        SpannedTestUtils.assertPrefixSpan(seq, 0, 1);


        mPrefixHighlighter.setText(mView, "TEst", "TE".toCharArray());
        seq = mPrefixHighlighter.apply("TEst", charArray);
        SpannedTestUtils.checkHtmlText(START + "TE" + END + "st", mView);
        SpannedTestUtils.assertPrefixSpan(seq, 0, 1);


        mPrefixHighlighter.setText(mView, "a test", "TE".toCharArray());
        seq = mPrefixHighlighter.apply("a test", charArray);
        SpannedTestUtils.checkHtmlText("a " + START + "te" + END + "st", mView);
        SpannedTestUtils.assertPrefixSpan(seq, 2, 3);
    }
    }


    public void testSetText_NotMatchingPrefix() {
    public void testSetText_NotMatchingPrefix() {
        mPrefixHighlighter.setText(mView, "test", "TA".toCharArray());
        final CharSequence seq = mPrefixHighlighter.apply("test", "TA".toCharArray());
        SpannedTestUtils.checkHtmlText("test", mView);
        SpannedTestUtils.assertNotSpanned(seq, "test");
    }
    }


    public void testSetText_FirstMatch() {
    public void testSetText_FirstMatch() {
        mPrefixHighlighter.setText(mView, "a test's tests are not tests", "TE".toCharArray());
        final CharSequence seq = mPrefixHighlighter.apply("a test's tests are not tests",
        SpannedTestUtils.checkHtmlText("a " +START + "te" + END + "st's tests are not tests",
                "TE".toCharArray());
                mView);
        SpannedTestUtils.assertPrefixSpan(seq, 2, 3);
    }
    }


    public void testSetText_NoMatchingMiddleOfWord() {
    public void testSetText_NoMatchingMiddleOfWord() {
        mPrefixHighlighter.setText(mView, "atest", "TE".toCharArray());
        final char[] charArray = "TE".toCharArray();
        SpannedTestUtils.checkHtmlText("atest", mView);
        CharSequence seq = mPrefixHighlighter.apply("atest", charArray);
        SpannedTestUtils.assertNotSpanned(seq, "atest");


        mPrefixHighlighter.setText(mView, "atest otest", "TE".toCharArray());
        seq = mPrefixHighlighter.apply("atest otest", charArray);
        SpannedTestUtils.checkHtmlText("atest otest", mView);
        SpannedTestUtils.assertNotSpanned(seq, "atest otest");


        mPrefixHighlighter.setText(mView, "atest test", "TE".toCharArray());
        seq = mPrefixHighlighter.apply("atest test", charArray);
        SpannedTestUtils.checkHtmlText("atest " + START + "te" + END + "st", mView);
        SpannedTestUtils.assertPrefixSpan(seq, 6, 7);
    }
    }
}
}
+35 −0
Original line number Original line Diff line number Diff line
@@ -20,6 +20,7 @@ import android.test.suitebuilder.annotation.SmallTest;
import android.text.Html;
import android.text.Html;
import android.text.Spanned;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;
import android.widget.TextView;


import junit.framework.Assert;
import junit.framework.Assert;
@@ -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);
    }
}
}
+16 −8
Original line number Original line Diff line number Diff line
@@ -21,12 +21,15 @@ import android.database.MatrixCursor;
import android.provider.ContactsContract;
import android.provider.ContactsContract;
import android.test.ActivityInstrumentationTestCase2;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.LargeTest;
import android.test.suitebuilder.annotation.LargeTest;
import android.text.SpannableString;
import android.text.Spanned;
import android.widget.TextView;
import android.widget.TextView;


import com.android.contacts.activities.PeopleActivity;
import com.android.contacts.activities.PeopleActivity;
import com.android.contacts.format.SpannedTestUtils;
import com.android.contacts.format.SpannedTestUtils;
import com.android.contacts.util.IntegrationTestUtils;
import com.android.contacts.util.IntegrationTestUtils;



/**
/**
 * Unit tests for {@link ContactListItemView}.
 * Unit tests for {@link ContactListItemView}.
 *
 *
@@ -66,7 +69,7 @@ public class ContactListItemViewTest extends ActivityInstrumentationTestCase2<Pe


        view.showDisplayName(cursor, 0, ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY);
        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() {
    public void testShowDisplayName_Unknown() {
@@ -76,7 +79,7 @@ public class ContactListItemViewTest extends ActivityInstrumentationTestCase2<Pe
        view.setUnknownNameText("unknown");
        view.setUnknownNameText("unknown");
        view.showDisplayName(cursor, 0, ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY);
        view.showDisplayName(cursor, 0, ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY);


        SpannedTestUtils.checkHtmlText("unknown", view.getNameTextView());
        assertEquals(view.getNameTextView().getText().toString(), "unknown");
    }
    }


    public void testShowDisplayName_WithPrefix() {
    public void testShowDisplayName_WithPrefix() {
@@ -86,8 +89,9 @@ public class ContactListItemViewTest extends ActivityInstrumentationTestCase2<Pe
        view.setHighlightedPrefix("DOE".toCharArray());
        view.setHighlightedPrefix("DOE".toCharArray());
        view.showDisplayName(cursor, 0, ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY);
        view.showDisplayName(cursor, 0, ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY);


        SpannedTestUtils.checkHtmlText("John " + START + "Doe" + END,
        CharSequence seq = view.getNameTextView().getText();
                view.getNameTextView());
        assertEquals("John Doe", seq.toString());
        SpannedTestUtils.assertPrefixSpan(seq, 5, 7);
    }
    }


    public void testShowDisplayName_WithPrefixReversed() {
    public void testShowDisplayName_WithPrefixReversed() {
@@ -97,16 +101,20 @@ public class ContactListItemViewTest extends ActivityInstrumentationTestCase2<Pe
        view.setHighlightedPrefix("DOE".toCharArray());
        view.setHighlightedPrefix("DOE".toCharArray());
        view.showDisplayName(cursor, 0, ContactsContract.Preferences.DISPLAY_ORDER_ALTERNATIVE);
        view.showDisplayName(cursor, 0, ContactsContract.Preferences.DISPLAY_ORDER_ALTERNATIVE);


        SpannedTestUtils.checkHtmlText("John " + START + "Doe" + END,
        CharSequence seq = view.getNameTextView().getText();
                view.getNameTextView());
        assertEquals("John Doe", seq.toString());
        SpannedTestUtils.assertPrefixSpan(seq, 5, 7);
    }
    }


    public void testSetSnippet_Prefix() {
    public void testSetSnippet_Prefix() {
        ContactListItemView view = createView();
        ContactListItemView view = createView();
        view.setHighlightedPrefix("TEST".toCharArray());
        view.setHighlightedPrefix("TEST".toCharArray());
        view.setSnippet("This is a test");
        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. */
    /** Creates the view to be tested. */