summaryrefslogtreecommitdiff
path: root/tests/src/com/android/contacts/format/FormatUtilsTests.java
blob: febed74a3096735468559cfa837c5520cd001f61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Copyright (C) 2011 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.format;

import android.database.CharArrayBuffer;
import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;

/**
 * Test cases for format utility methods.
 */
@SmallTest
public class FormatUtilsTests extends AndroidTestCase {

    public void testOverlapPoint() throws Exception {
        assertEquals(2, FormatUtils.overlapPoint("abcde", "cdefg"));
        assertEquals(-1, FormatUtils.overlapPoint("John Doe", "John Doe"));
        assertEquals(5, FormatUtils.overlapPoint("John Doe", "Doe, John"));
        assertEquals(-1, FormatUtils.overlapPoint("Mr. John Doe", "Mr. Doe, John"));
        assertEquals(13, FormatUtils.overlapPoint("John Herbert Doe", "Doe, John Herbert"));
    }

    public void testCopyToCharArrayBuffer() {
        CharArrayBuffer charArrayBuffer = new CharArrayBuffer(20);
        checkCopyToCharArrayBuffer(charArrayBuffer, null, 0);
        checkCopyToCharArrayBuffer(charArrayBuffer, "", 0);
        checkCopyToCharArrayBuffer(charArrayBuffer, "test", 4);
        // Check that it works after copying something into it.
        checkCopyToCharArrayBuffer(charArrayBuffer, "", 0);
        checkCopyToCharArrayBuffer(charArrayBuffer, "test", 4);
        checkCopyToCharArrayBuffer(charArrayBuffer, null, 0);
        // This requires a resize of the actual buffer.
        checkCopyToCharArrayBuffer(charArrayBuffer, "test test test test test", 24);
    }

    public void testCharArrayBufferToString() {
        checkCharArrayBufferToString("");
        checkCharArrayBufferToString("test");
        checkCharArrayBufferToString("test test test test test");
    }

    /** Checks that copying a string into a {@link CharArrayBuffer} and back works correctly. */
    private void checkCharArrayBufferToString(String text) {
        CharArrayBuffer buffer = new CharArrayBuffer(20);
        FormatUtils.copyToCharArrayBuffer(text, buffer);
        assertEquals(text, FormatUtils.charArrayBufferToString(buffer));
    }

    /**
     * Checks that copying into the char array buffer copies the values correctly.
     */
    private void checkCopyToCharArrayBuffer(CharArrayBuffer buffer, String value, int length) {
        FormatUtils.copyToCharArrayBuffer(value, buffer);
        assertEquals(length, buffer.sizeCopied);
        for (int index = 0; index < length; ++index) {
            assertEquals(value.charAt(index), buffer.data[index]);
        }
    }

    public void testIndexOfWordPrefix_NullPrefix() {
        assertEquals(-1, FormatUtils.indexOfWordPrefix("test", null));
    }

    public void testIndexOfWordPrefix_NullText() {
        assertEquals(-1, FormatUtils.indexOfWordPrefix(null, "TE"));
    }

    public void testIndexOfWordPrefix_MatchingPrefix() {
        checkIndexOfWordPrefix("test", "TE", 0);
        checkIndexOfWordPrefix("Test", "TE", 0);
        checkIndexOfWordPrefix("TEst", "TE", 0);
        checkIndexOfWordPrefix("TEST", "TE", 0);
        checkIndexOfWordPrefix("a test", "TE", 2);
        checkIndexOfWordPrefix("test test", "TE", 0);
        checkIndexOfWordPrefix("a test test", "TE", 2);
    }

    public void testIndexOfWordPrefix_NotMatchingPrefix() {
        checkIndexOfWordPrefix("test", "TA", -1);
        checkIndexOfWordPrefix("test type theme", "TA", -1);
        checkIndexOfWordPrefix("atest retest pretest", "TEST", -1);
        checkIndexOfWordPrefix("tes", "TEST", -1);
    }

    public void testIndexOfWordPrefix_LowerCase() {
        // The prefix match only works if the prefix is un upper case.
        checkIndexOfWordPrefix("test", "te", -1);
    }

    /**
     * Checks that getting the index of a word prefix in the given text returns the expected index.
     *
     * @param text the text in which to look for the word
     * @param wordPrefix the word prefix to look for
     * @param expectedIndex the expected value to be returned by the function
     */
    private void checkIndexOfWordPrefix(String text, String wordPrefix, int expectedIndex) {
        assertEquals(expectedIndex, FormatUtils.indexOfWordPrefix(text, wordPrefix));
    }
}