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