summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorChiao Cheng <chiaocheng@google.com>2012-12-27 17:14:53 -0800
committerChiao Cheng <chiaocheng@google.com>2012-12-28 13:41:06 -0800
commitcae605bed4d43e1925fc8c1803def0ef1d0924a5 (patch)
treefbd24732239cc297e67bca29acb0d5a81ab1b96f /tests
parent32c1bff720d06296b00ee827e06f69422ed4ae5c (diff)
downloadandroid_packages_apps_ContactsCommon-cae605bed4d43e1925fc8c1803def0ef1d0924a5.tar.gz
android_packages_apps_ContactsCommon-cae605bed4d43e1925fc8c1803def0ef1d0924a5.tar.bz2
android_packages_apps_ContactsCommon-cae605bed4d43e1925fc8c1803def0ef1d0924a5.zip
Find proper snippet in multi-line and large result.
Previous CL removed the use ContactsContract.snippetize(). This method found the proper line in a multi-line results and then snippetized the line if it was too long. This CL adds that functionality back without using snippetize(). This new code is faster because it only does text intensive processing when the text is long. The previous snippetize method did processing for all strings. In addition, the old method iterated over the snippet multiple times (i.e. first with contains, then subsequently tokenizes, etc). This change re-uses the initial search results and remembers the search information so multiple iterations are not necessary. In addition, tokenizing has been optimized based on the match. We only need to chop off excess content rather than tokenizing the whole string. This CL also makes snippet more friend to landscape mode. Previously, the number of snippet tokens shown was hard-coded to 2 on each side. Furthermore, this caused longer tokens to exceed screen real estate. Now the number of tokens shown are determined more accurately by character count versus real estate. In landscape mode, this allows us to show much more of the search result. Finally, fixed a highlight problem when the search query contained punctuation. For example, a search for {.ben.} would not match {ben}. This Cl resolves that issue by cleaning the search query. Bug: 5929143 Change-Id: I5c368e6de8b34ca912f86926f862a02f95199fa7
Diffstat (limited to 'tests')
-rw-r--r--tests/src/com/android/contacts/common/util/SearchUtilTest.java111
1 files changed, 111 insertions, 0 deletions
diff --git a/tests/src/com/android/contacts/common/util/SearchUtilTest.java b/tests/src/com/android/contacts/common/util/SearchUtilTest.java
new file mode 100644
index 00000000..e03e871e
--- /dev/null
+++ b/tests/src/com/android/contacts/common/util/SearchUtilTest.java
@@ -0,0 +1,111 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.contacts.common.util;
+
+import android.test.suitebuilder.annotation.SmallTest;
+
+import junit.framework.TestCase;
+
+/**
+ * Unit tests for {@link SearchUtil}.
+ */
+@SmallTest
+public class SearchUtilTest extends TestCase {
+
+ public void testFindMatchingLine() {
+ final String actual = "this is a long test string.\nWith potentially many lines.\n" +
+ "test@google.com\nhello\nblah\n'leading punc";
+
+ SearchUtil.MatchedLine matched = SearchUtil.findMatchingLine(actual, "poten");
+ assertEquals("With potentially many lines.", matched.line);
+ assertEquals(5, matched.startIndex);
+
+ // Full line match.
+ matched = SearchUtil.findMatchingLine(actual, "hello");
+ assertEquals("hello", matched.line);
+ assertEquals(0, matched.startIndex);
+
+ // First line match
+ matched = SearchUtil.findMatchingLine(actual, "this");
+ assertEquals("this is a long test string.", matched.line);
+ assertEquals(0, matched.startIndex);
+
+ // Last line match
+ matched = SearchUtil.findMatchingLine(actual, "punc");
+ assertEquals("'leading punc", matched.line);
+ assertEquals(9, matched.startIndex);
+ }
+
+ public void testContains() {
+ final String actual = "this is a long test string.\nWith potentially many lines.\n" +
+ "test@google.com\nhello\nblah\n'leading punc";
+ assertEquals(0, SearchUtil.contains(actual, "this"));
+ assertEquals(10, SearchUtil.contains(actual, "lon"));
+
+ assertEquals(1, SearchUtil.contains("'leading punc", "lead"));
+ assertEquals(9, SearchUtil.contains("'leading punc", "punc"));
+
+ }
+
+ public void testContainsNotFound() {
+ final String actual = "this is a long test string.\nWith potentially many lines.\n" +
+ "test@google.com\nhello\nblah\n'leading punc";
+
+ // Non-prefix
+ assertEquals(-1, SearchUtil.contains(actual, "ith"));
+ assertEquals(-1, SearchUtil.contains(actual, "ing"));
+
+ // Complete misses
+ assertEquals(-1, SearchUtil.contains(actual, "thisx"));
+ assertEquals(-1, SearchUtil.contains(actual, "manyx"));
+ assertEquals(-1, SearchUtil.contains(actual, "hellox"));
+ }
+
+ public void testFindNextTokenStart() {
+ final String actual = "....hello.kitty";
+ // 012345678901234
+
+ // Find first token.
+ assertEquals(4, SearchUtil.findNextTokenStart(actual, 0));
+ assertEquals(4, SearchUtil.findNextTokenStart(actual, 1));
+ assertEquals(4, SearchUtil.findNextTokenStart(actual, 2));
+ assertEquals(4, SearchUtil.findNextTokenStart(actual, 3));
+
+ // Find second token.
+ assertEquals(10, SearchUtil.findNextTokenStart(actual, 4));
+ assertEquals(10, SearchUtil.findNextTokenStart(actual, 5));
+ assertEquals(10, SearchUtil.findNextTokenStart(actual, 6));
+ assertEquals(10, SearchUtil.findNextTokenStart(actual, 7));
+ assertEquals(10, SearchUtil.findNextTokenStart(actual, 8));
+ assertEquals(10, SearchUtil.findNextTokenStart(actual, 9));
+
+ // No token.
+ assertEquals(actual.length(), SearchUtil.findNextTokenStart(actual, 10));
+ assertEquals(actual.length(), SearchUtil.findNextTokenStart(actual, 11));
+ assertEquals(actual.length(), SearchUtil.findNextTokenStart(actual, 12));
+ assertEquals(actual.length(), SearchUtil.findNextTokenStart(actual, 13));
+ assertEquals(actual.length(), SearchUtil.findNextTokenStart(actual, 14));
+ }
+
+ public void testCleanStartAndEndOfSearchQuery() {
+ assertEquals("test", SearchUtil.cleanStartAndEndOfSearchQuery("...test..."));
+ assertEquals("test", SearchUtil.cleanStartAndEndOfSearchQuery(" test "));
+ assertEquals("test", SearchUtil.cleanStartAndEndOfSearchQuery(" ||test"));
+ assertEquals("test", SearchUtil.cleanStartAndEndOfSearchQuery("test.."));
+ }
+
+}