1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.contacts.format; 18 19 import android.test.AndroidTestCase; 20 import android.widget.TextView; 21 22 /** 23 * Unit tests for {@link PrefixHighlighter}. 24 */ 25 public class PrefixHighligherTest extends AndroidTestCase { 26 private static final int TEST_PREFIX_HIGHLIGHT_COLOR = 0xFF0000; 27 /** The HTML code used to mark the start of the highlighted part. */ 28 private static final String START = "<font color =\"#1ff0000\">"; 29 /** The HTML code used to mark the end of the highlighted part. */ 30 private static final String END = "</font>"; 31 32 /** The object under test. */ 33 private PrefixHighlighter mPrefixHighlighter; 34 /** The view to on which the text is set. */ 35 private TextView mView; 36 37 @Override 38 protected void setUp() throws Exception { 39 super.setUp(); 40 mPrefixHighlighter = new PrefixHighlighter(TEST_PREFIX_HIGHLIGHT_COLOR); 41 mView = new TextView(getContext()); 42 // This guarantees that the text will be stored as a spannable so that we can determine 43 // which styles have been applied to it. 44 mView.setText("", TextView.BufferType.SPANNABLE); 45 } 46 47 public void testSetText_EmptyPrefix() { 48 mPrefixHighlighter.setText(mView, "", new char[0]); 49 SpannedTestUtils.checkHtmlText("", mView); 50 51 mPrefixHighlighter.setText(mView, "test", new char[0]); 52 SpannedTestUtils.checkHtmlText("test", mView); 53 } 54 55 public void testSetText_MatchingPrefix() { 56 mPrefixHighlighter.setText(mView, "test", "TE".toCharArray()); 57 SpannedTestUtils.checkHtmlText(START + "te" + END + "st", mView); 58 59 mPrefixHighlighter.setText(mView, "Test", "TE".toCharArray()); 60 SpannedTestUtils.checkHtmlText(START + "Te" + END + "st", mView); 61 62 mPrefixHighlighter.setText(mView, "TEst", "TE".toCharArray()); 63 SpannedTestUtils.checkHtmlText(START + "TE" + END + "st", mView); 64 65 mPrefixHighlighter.setText(mView, "a test", "TE".toCharArray()); 66 SpannedTestUtils.checkHtmlText("a " + START + "te" + END + "st", mView); 67 } 68 69 public void testSetText_NotMatchingPrefix() { 70 mPrefixHighlighter.setText(mView, "test", "TA".toCharArray()); 71 SpannedTestUtils.checkHtmlText("test", mView); 72 } 73 74 public void testSetText_FirstMatch() { 75 mPrefixHighlighter.setText(mView, "a test's tests are not tests", "TE".toCharArray()); 76 SpannedTestUtils.checkHtmlText("a " +START + "te" + END + "st's tests are not tests", 77 mView); 78 } 79 80 public void testSetText_NoMatchingMiddleOfWord() { 81 mPrefixHighlighter.setText(mView, "atest", "TE".toCharArray()); 82 SpannedTestUtils.checkHtmlText("atest", mView); 83 84 mPrefixHighlighter.setText(mView, "atest otest", "TE".toCharArray()); 85 SpannedTestUtils.checkHtmlText("atest otest", mView); 86 87 mPrefixHighlighter.setText(mView, "atest test", "TE".toCharArray()); 88 SpannedTestUtils.checkHtmlText("atest " + START + "te" + END + "st", mView); 89 } 90 } 91