1 /*
2  * Copyright (C) 2012 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.util;
18 
19 import android.test.suitebuilder.annotation.SmallTest;
20 
21 import junit.framework.TestCase;
22 
23 /**
24  * Unit tests for {@link SearchUtil}.
25  */
26 @SmallTest
27 public class SearchUtilTest extends TestCase {
28 
testFindMatchingLine()29     public void testFindMatchingLine() {
30         final String actual = "this is a long test string.\nWith potentially many lines.\n" +
31                 "test@google.com\nhello\nblah\n'leading punc";
32 
33         SearchUtil.MatchedLine matched = SearchUtil.findMatchingLine(actual, "poten");
34         assertEquals("With potentially many lines.", matched.line);
35         assertEquals(5, matched.startIndex);
36 
37         // Full line match.
38         matched = SearchUtil.findMatchingLine(actual, "hello");
39         assertEquals("hello", matched.line);
40         assertEquals(0, matched.startIndex);
41 
42         // First line match
43         matched = SearchUtil.findMatchingLine(actual, "this");
44         assertEquals("this is a long test string.", matched.line);
45         assertEquals(0, matched.startIndex);
46 
47         // Last line match
48         matched = SearchUtil.findMatchingLine(actual, "punc");
49         assertEquals("'leading punc", matched.line);
50         assertEquals(9, matched.startIndex);
51     }
52 
testContains()53     public void testContains() {
54         final String actual = "this is a long test string.\nWith potentially many lines.\n" +
55                 "test@google.com\nhello\nblah\n'leading punc";
56         assertEquals(0, SearchUtil.contains(actual, "this"));
57         assertEquals(10, SearchUtil.contains(actual, "lon"));
58 
59         assertEquals(1, SearchUtil.contains("'leading punc", "lead"));
60         assertEquals(9, SearchUtil.contains("'leading punc", "punc"));
61 
62     }
63 
testContainsNotFound()64     public void testContainsNotFound() {
65         final String actual = "this is a long test string.\nWith potentially many lines.\n" +
66                 "test@google.com\nhello\nblah\n'leading punc";
67 
68         // Non-prefix
69         assertEquals(-1, SearchUtil.contains(actual, "ith"));
70         assertEquals(-1, SearchUtil.contains(actual, "ing"));
71 
72         // Complete misses
73         assertEquals(-1, SearchUtil.contains(actual, "thisx"));
74         assertEquals(-1, SearchUtil.contains(actual, "manyx"));
75         assertEquals(-1, SearchUtil.contains(actual, "hellox"));
76 
77         // Test for partial match of start of query to end of line
78         assertEquals(-1, SearchUtil.contains(actual, "punctual"));
79     }
80 
testFindNextTokenStart()81     public void testFindNextTokenStart() {
82         final String actual = "....hello.kitty";
83         //                     012345678901234
84 
85         // Find first token.
86         assertEquals(4, SearchUtil.findNextTokenStart(actual, 0));
87         assertEquals(4, SearchUtil.findNextTokenStart(actual, 1));
88         assertEquals(4, SearchUtil.findNextTokenStart(actual, 2));
89         assertEquals(4, SearchUtil.findNextTokenStart(actual, 3));
90 
91         // Find second token.
92         assertEquals(10, SearchUtil.findNextTokenStart(actual, 4));
93         assertEquals(10, SearchUtil.findNextTokenStart(actual, 5));
94         assertEquals(10, SearchUtil.findNextTokenStart(actual, 6));
95         assertEquals(10, SearchUtil.findNextTokenStart(actual, 7));
96         assertEquals(10, SearchUtil.findNextTokenStart(actual, 8));
97         assertEquals(10, SearchUtil.findNextTokenStart(actual, 9));
98 
99         // No token.
100         assertEquals(actual.length(), SearchUtil.findNextTokenStart(actual, 10));
101         assertEquals(actual.length(), SearchUtil.findNextTokenStart(actual, 11));
102         assertEquals(actual.length(), SearchUtil.findNextTokenStart(actual, 12));
103         assertEquals(actual.length(), SearchUtil.findNextTokenStart(actual, 13));
104         assertEquals(actual.length(), SearchUtil.findNextTokenStart(actual, 14));
105     }
106 
testCleanStartAndEndOfSearchQuery()107     public void testCleanStartAndEndOfSearchQuery() {
108         assertEquals("test", SearchUtil.cleanStartAndEndOfSearchQuery("...test..."));
109         assertEquals("test", SearchUtil.cleanStartAndEndOfSearchQuery(" test "));
110         assertEquals("test", SearchUtil.cleanStartAndEndOfSearchQuery(" ||test"));
111         assertEquals("test", SearchUtil.cleanStartAndEndOfSearchQuery("test.."));
112     }
113 
114 }
115