blob: 211d9440bc130928b01be98a3beb5aadd303af1d [file] [log] [blame]
Christine Chen3efbe592013-07-08 18:05:03 -07001/*
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
Gary Mai69c182a2016-12-05 13:07:03 -080017package com.android.contacts.format;
Christine Chen3efbe592013-07-08 18:05:03 -070018
Yorke Lee8b7f84a2013-09-12 12:38:41 -070019import android.graphics.Typeface;
Christine Chen3efbe592013-07-08 18:05:03 -070020import android.test.suitebuilder.annotation.SmallTest;
Christine Chenccba9502013-07-12 12:04:54 -070021import android.text.SpannableString;
Christine Chen3efbe592013-07-08 18:05:03 -070022
Christine Chen3efbe592013-07-08 18:05:03 -070023import junit.framework.TestCase;
24
25/**
26 * Unit tests for {@link TextHighlighter}.
27 */
28@SmallTest
29public class TextHighlighterTest extends TestCase {
30 private static final int TEST_PREFIX_HIGHLIGHT_COLOR = 0xFF0000;
31
32 /** The object under test. */
33 private TextHighlighter mTextHighlighter;
34
35 @Override
36 protected void setUp() throws Exception {
37 super.setUp();
Yorke Lee8b7f84a2013-09-12 12:38:41 -070038 mTextHighlighter = new TextHighlighter(Typeface.BOLD);
Christine Chen3efbe592013-07-08 18:05:03 -070039 }
40
41 public void testApply_EmptyPrefix() {
42 CharSequence seq = mTextHighlighter.applyPrefixHighlight("", "");
43 SpannedTestUtils.assertNotSpanned(seq, "");
44
45 seq = mTextHighlighter.applyPrefixHighlight("test", "");
46 SpannedTestUtils.assertNotSpanned(seq, "test");
47 }
48
49 public void testSetText_MatchingPrefix() {
50 final String prefix = "TE";
51
52 CharSequence seq = mTextHighlighter.applyPrefixHighlight("test", prefix);
53 SpannedTestUtils.assertPrefixSpan(seq, 0, 1);
54
55 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("a test", prefix);
62 SpannedTestUtils.assertPrefixSpan(seq, 2, 3);
63 }
64
65 public void testSetText_NotMatchingPrefix() {
66 final CharSequence seq = mTextHighlighter.applyPrefixHighlight("test", "TA");
67 SpannedTestUtils.assertNotSpanned(seq, "test");
68 }
69
70 public void testSetText_FirstMatch() {
71 final CharSequence seq = mTextHighlighter.applyPrefixHighlight(
72 "a test's tests are not tests", "TE");
73 SpannedTestUtils.assertPrefixSpan(seq, 2, 3);
74 }
75
76 public void testSetText_NoMatchingMiddleOfWord() {
77 final String prefix = "TE";
78 CharSequence seq = mTextHighlighter.applyPrefixHighlight("atest", prefix);
79 SpannedTestUtils.assertNotSpanned(seq, "atest");
80
81 seq = mTextHighlighter.applyPrefixHighlight("atest otest", prefix);
82 SpannedTestUtils.assertNotSpanned(seq, "atest otest");
83
84 seq = mTextHighlighter.applyPrefixHighlight("atest test", prefix);
85 SpannedTestUtils.assertPrefixSpan(seq, 6, 7);
86 }
87
Christine Chen3efbe592013-07-08 18:05:03 -070088 public void testSetMask_Highlight() {
Christine Chenccba9502013-07-12 12:04:54 -070089 final SpannableString testString1 = new SpannableString("alongtest");
90 mTextHighlighter.applyMaskingHighlight(testString1, 2, 4);
Yorke Lee426e5d42014-03-18 13:07:00 -070091 assertEquals(2, SpannedTestUtils.getNextTransition(testString1, 0));
92 assertEquals(4, SpannedTestUtils.getNextTransition(testString1, 2));
Christine Chenccba9502013-07-12 12:04:54 -070093
94 mTextHighlighter.applyMaskingHighlight(testString1, 3, 6);
Yorke Lee426e5d42014-03-18 13:07:00 -070095 assertEquals(2, SpannedTestUtils.getNextTransition(testString1, 0));
96 assertEquals(4, SpannedTestUtils.getNextTransition(testString1, 3));
Christine Chenccba9502013-07-12 12:04:54 -070097
98 mTextHighlighter.applyMaskingHighlight(testString1, 4, 5);
Yorke Lee426e5d42014-03-18 13:07:00 -070099 assertEquals(3, SpannedTestUtils.getNextTransition(testString1, 2));
Christine Chenccba9502013-07-12 12:04:54 -0700100
101 mTextHighlighter.applyMaskingHighlight(testString1, 7, 8);
Yorke Lee426e5d42014-03-18 13:07:00 -0700102 assertEquals(6, SpannedTestUtils.getNextTransition(testString1, 5));
103 assertEquals(7, SpannedTestUtils.getNextTransition(testString1, 6));
104 assertEquals(8, SpannedTestUtils.getNextTransition(testString1, 7));
Christine Chen3efbe592013-07-08 18:05:03 -0700105 }
106}