blob: 8f4f772862b3327b204aedb070b9f2b7cb7a1615 [file] [log] [blame]
Chiao Chenga6b4c792012-10-31 15:18:29 -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
17package com.android.contacts.common.format;
18
19import android.database.CharArrayBuffer;
20import android.test.AndroidTestCase;
21import android.test.suitebuilder.annotation.SmallTest;
22
23/**
24 * Test cases for format utility methods.
25 */
26@SmallTest
27public class FormatUtilsTests extends AndroidTestCase {
28
29 public void testOverlapPoint() throws Exception {
30 assertEquals(2, FormatUtils.overlapPoint("abcde", "cdefg"));
31 assertEquals(-1, FormatUtils.overlapPoint("John Doe", "John Doe"));
32 assertEquals(5, FormatUtils.overlapPoint("John Doe", "Doe, John"));
33 assertEquals(-1, FormatUtils.overlapPoint("Mr. John Doe", "Mr. Doe, John"));
34 assertEquals(13, FormatUtils.overlapPoint("John Herbert Doe", "Doe, John Herbert"));
35 }
36
37 public void testCopyToCharArrayBuffer() {
38 CharArrayBuffer charArrayBuffer = new CharArrayBuffer(20);
39 checkCopyToCharArrayBuffer(charArrayBuffer, null, 0);
40 checkCopyToCharArrayBuffer(charArrayBuffer, "", 0);
41 checkCopyToCharArrayBuffer(charArrayBuffer, "test", 4);
42 // Check that it works after copying something into it.
43 checkCopyToCharArrayBuffer(charArrayBuffer, "", 0);
44 checkCopyToCharArrayBuffer(charArrayBuffer, "test", 4);
45 checkCopyToCharArrayBuffer(charArrayBuffer, null, 0);
46 // This requires a resize of the actual buffer.
47 checkCopyToCharArrayBuffer(charArrayBuffer, "test test test test test", 24);
48 }
49
50 public void testCharArrayBufferToString() {
51 checkCharArrayBufferToString("");
52 checkCharArrayBufferToString("test");
53 checkCharArrayBufferToString("test test test test test");
54 }
55
56 /** Checks that copying a string into a {@link CharArrayBuffer} and back works correctly. */
57 private void checkCharArrayBufferToString(String text) {
58 CharArrayBuffer buffer = new CharArrayBuffer(20);
59 FormatUtils.copyToCharArrayBuffer(text, buffer);
60 assertEquals(text, FormatUtils.charArrayBufferToString(buffer));
61 }
62
63 /**
64 * Checks that copying into the char array buffer copies the values correctly.
65 */
66 private void checkCopyToCharArrayBuffer(CharArrayBuffer buffer, String value, int length) {
67 FormatUtils.copyToCharArrayBuffer(value, buffer);
68 assertEquals(length, buffer.sizeCopied);
69 for (int index = 0; index < length; ++index) {
70 assertEquals(value.charAt(index), buffer.data[index]);
71 }
72 }
73
74 public void testIndexOfWordPrefix_NullPrefix() {
75 assertEquals(-1, FormatUtils.indexOfWordPrefix("test", null));
76 }
77
78 public void testIndexOfWordPrefix_NullText() {
Chiao Chenga1554ef2012-12-21 15:45:54 -080079 assertEquals(-1, FormatUtils.indexOfWordPrefix(null, "TE"));
Chiao Chenga6b4c792012-10-31 15:18:29 -070080 }
81
82 public void testIndexOfWordPrefix_MatchingPrefix() {
83 checkIndexOfWordPrefix("test", "TE", 0);
84 checkIndexOfWordPrefix("Test", "TE", 0);
85 checkIndexOfWordPrefix("TEst", "TE", 0);
86 checkIndexOfWordPrefix("TEST", "TE", 0);
87 checkIndexOfWordPrefix("a test", "TE", 2);
88 checkIndexOfWordPrefix("test test", "TE", 0);
89 checkIndexOfWordPrefix("a test test", "TE", 2);
90 }
91
92 public void testIndexOfWordPrefix_NotMatchingPrefix() {
93 checkIndexOfWordPrefix("test", "TA", -1);
94 checkIndexOfWordPrefix("test type theme", "TA", -1);
95 checkIndexOfWordPrefix("atest retest pretest", "TEST", -1);
96 checkIndexOfWordPrefix("tes", "TEST", -1);
97 }
98
99 public void testIndexOfWordPrefix_LowerCase() {
100 // The prefix match only works if the prefix is un upper case.
101 checkIndexOfWordPrefix("test", "te", -1);
102 }
103
104 /**
105 * Checks that getting the index of a word prefix in the given text returns the expected index.
106 *
107 * @param text the text in which to look for the word
108 * @param wordPrefix the word prefix to look for
109 * @param expectedIndex the expected value to be returned by the function
110 */
111 private void checkIndexOfWordPrefix(String text, String wordPrefix, int expectedIndex) {
Chiao Chenga1554ef2012-12-21 15:45:54 -0800112 assertEquals(expectedIndex, FormatUtils.indexOfWordPrefix(text, wordPrefix));
Chiao Chenga6b4c792012-10-31 15:18:29 -0700113 }
114}