Christine Chen | 3efbe59 | 2013-07-08 18:05:03 -0700 | [diff] [blame] | 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 | |
Walter Jang | b219848 | 2016-08-12 10:37:54 -0700 | [diff] [blame] | 17 | package com.android.contacts.common.format; |
Christine Chen | 3efbe59 | 2013-07-08 18:05:03 -0700 | [diff] [blame] | 18 | |
Yorke Lee | 8b7f84a | 2013-09-12 12:38:41 -0700 | [diff] [blame] | 19 | import android.graphics.Typeface; |
Christine Chen | 3efbe59 | 2013-07-08 18:05:03 -0700 | [diff] [blame] | 20 | import android.test.suitebuilder.annotation.SmallTest; |
Christine Chen | ccba950 | 2013-07-12 12:04:54 -0700 | [diff] [blame] | 21 | import android.text.SpannableString; |
Christine Chen | 3efbe59 | 2013-07-08 18:05:03 -0700 | [diff] [blame] | 22 | |
| 23 | import com.android.contacts.common.format.SpannedTestUtils; |
Yorke Lee | 426e5d4 | 2014-03-18 13:07:00 -0700 | [diff] [blame] | 24 | import com.android.contacts.common.format.TextHighlighter; |
Christine Chen | 3efbe59 | 2013-07-08 18:05:03 -0700 | [diff] [blame] | 25 | |
| 26 | import junit.framework.TestCase; |
| 27 | |
| 28 | /** |
| 29 | * Unit tests for {@link TextHighlighter}. |
| 30 | */ |
| 31 | @SmallTest |
| 32 | public class TextHighlighterTest extends TestCase { |
| 33 | private static final int TEST_PREFIX_HIGHLIGHT_COLOR = 0xFF0000; |
| 34 | |
| 35 | /** The object under test. */ |
| 36 | private TextHighlighter mTextHighlighter; |
| 37 | |
| 38 | @Override |
| 39 | protected void setUp() throws Exception { |
| 40 | super.setUp(); |
Yorke Lee | 8b7f84a | 2013-09-12 12:38:41 -0700 | [diff] [blame] | 41 | mTextHighlighter = new TextHighlighter(Typeface.BOLD); |
Christine Chen | 3efbe59 | 2013-07-08 18:05:03 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | public void testApply_EmptyPrefix() { |
| 45 | CharSequence seq = mTextHighlighter.applyPrefixHighlight("", ""); |
| 46 | SpannedTestUtils.assertNotSpanned(seq, ""); |
| 47 | |
| 48 | seq = mTextHighlighter.applyPrefixHighlight("test", ""); |
| 49 | SpannedTestUtils.assertNotSpanned(seq, "test"); |
| 50 | } |
| 51 | |
| 52 | public void testSetText_MatchingPrefix() { |
| 53 | final String prefix = "TE"; |
| 54 | |
| 55 | CharSequence seq = mTextHighlighter.applyPrefixHighlight("test", prefix); |
| 56 | SpannedTestUtils.assertPrefixSpan(seq, 0, 1); |
| 57 | |
| 58 | seq = mTextHighlighter.applyPrefixHighlight("Test", prefix); |
| 59 | SpannedTestUtils.assertPrefixSpan(seq, 0, 1); |
| 60 | |
| 61 | seq = mTextHighlighter.applyPrefixHighlight("TEst", prefix); |
| 62 | SpannedTestUtils.assertPrefixSpan(seq, 0, 1); |
| 63 | |
| 64 | seq = mTextHighlighter.applyPrefixHighlight("a test", prefix); |
| 65 | SpannedTestUtils.assertPrefixSpan(seq, 2, 3); |
| 66 | } |
| 67 | |
| 68 | public void testSetText_NotMatchingPrefix() { |
| 69 | final CharSequence seq = mTextHighlighter.applyPrefixHighlight("test", "TA"); |
| 70 | SpannedTestUtils.assertNotSpanned(seq, "test"); |
| 71 | } |
| 72 | |
| 73 | public void testSetText_FirstMatch() { |
| 74 | final CharSequence seq = mTextHighlighter.applyPrefixHighlight( |
| 75 | "a test's tests are not tests", "TE"); |
| 76 | SpannedTestUtils.assertPrefixSpan(seq, 2, 3); |
| 77 | } |
| 78 | |
| 79 | public void testSetText_NoMatchingMiddleOfWord() { |
| 80 | final String prefix = "TE"; |
| 81 | CharSequence seq = mTextHighlighter.applyPrefixHighlight("atest", prefix); |
| 82 | SpannedTestUtils.assertNotSpanned(seq, "atest"); |
| 83 | |
| 84 | seq = mTextHighlighter.applyPrefixHighlight("atest otest", prefix); |
| 85 | SpannedTestUtils.assertNotSpanned(seq, "atest otest"); |
| 86 | |
| 87 | seq = mTextHighlighter.applyPrefixHighlight("atest test", prefix); |
| 88 | SpannedTestUtils.assertPrefixSpan(seq, 6, 7); |
| 89 | } |
| 90 | |
Christine Chen | 3efbe59 | 2013-07-08 18:05:03 -0700 | [diff] [blame] | 91 | public void testSetMask_Highlight() { |
Christine Chen | ccba950 | 2013-07-12 12:04:54 -0700 | [diff] [blame] | 92 | final SpannableString testString1 = new SpannableString("alongtest"); |
| 93 | mTextHighlighter.applyMaskingHighlight(testString1, 2, 4); |
Yorke Lee | 426e5d4 | 2014-03-18 13:07:00 -0700 | [diff] [blame] | 94 | assertEquals(2, SpannedTestUtils.getNextTransition(testString1, 0)); |
| 95 | assertEquals(4, SpannedTestUtils.getNextTransition(testString1, 2)); |
Christine Chen | ccba950 | 2013-07-12 12:04:54 -0700 | [diff] [blame] | 96 | |
| 97 | mTextHighlighter.applyMaskingHighlight(testString1, 3, 6); |
Yorke Lee | 426e5d4 | 2014-03-18 13:07:00 -0700 | [diff] [blame] | 98 | assertEquals(2, SpannedTestUtils.getNextTransition(testString1, 0)); |
| 99 | assertEquals(4, SpannedTestUtils.getNextTransition(testString1, 3)); |
Christine Chen | ccba950 | 2013-07-12 12:04:54 -0700 | [diff] [blame] | 100 | |
| 101 | mTextHighlighter.applyMaskingHighlight(testString1, 4, 5); |
Yorke Lee | 426e5d4 | 2014-03-18 13:07:00 -0700 | [diff] [blame] | 102 | assertEquals(3, SpannedTestUtils.getNextTransition(testString1, 2)); |
Christine Chen | ccba950 | 2013-07-12 12:04:54 -0700 | [diff] [blame] | 103 | |
| 104 | mTextHighlighter.applyMaskingHighlight(testString1, 7, 8); |
Yorke Lee | 426e5d4 | 2014-03-18 13:07:00 -0700 | [diff] [blame] | 105 | assertEquals(6, SpannedTestUtils.getNextTransition(testString1, 5)); |
| 106 | assertEquals(7, SpannedTestUtils.getNextTransition(testString1, 6)); |
| 107 | assertEquals(8, SpannedTestUtils.getNextTransition(testString1, 7)); |
Christine Chen | 3efbe59 | 2013-07-08 18:05:03 -0700 | [diff] [blame] | 108 | } |
| 109 | } |