aboutsummaryrefslogtreecommitdiff
path: root/tests/src/com/android/providers/contacts/NameLookupBuilderTest.java
blob: f674dc9fbd1da8aa017c4a71787cf01979fe3cb7 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Copyright (C) 2009 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.providers.contacts;

import android.provider.ContactsContract.FullNameStyle;
import android.test.suitebuilder.annotation.SmallTest;

import junit.framework.TestCase;

import java.text.Collator;
import java.util.Arrays;
import java.util.Locale;

/**
 * Unit tests for {@link NameLookupBuilder}.
 *
 * Run the test like this:
 * <code>
 * adb shell am instrument -e class com.android.providers.contacts.NameLookupBuilderTest -w \
 *         com.android.providers.contacts.tests/android.test.InstrumentationTestRunner
 * </code>
 */
@SmallTest
public class NameLookupBuilderTest extends TestCase {

    private static class TestNameLookupBuilder extends NameLookupBuilder {

        StringBuilder sb = new StringBuilder();

        public TestNameLookupBuilder(NameSplitter splitter) {
            super(splitter);
        }

        @Override
        protected String normalizeName(String name) {

            // TO make the test more readable, simply return the name unnormalized
            return name;
        }

        public String inserted() {
            return sb.toString();
        }

        @Override
        protected void insertNameLookup(long rawContactId, long dataId, int lookupType,
                String string) {
            sb.append("(").append(lookupType).append(":").append(string).append(")");
        }
    }

    private TestNameLookupBuilder mBuilder;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mBuilder = new TestNameLookupBuilder(
                new NameSplitter("Mr", "", "", "", Locale.getDefault()));
    }

    public void testEmptyName() {
        mBuilder.insertNameLookup(0, 0, "", FullNameStyle.UNDEFINED);
        assertEquals("", mBuilder.inserted());
    }

    public void testSingleUniqueName() {
        mBuilder.insertNameLookup(0, 0, "Foo", FullNameStyle.UNDEFINED);
        assertEquals("(0:Foo)(2:Foo)", mBuilder.inserted());
    }

    public void testSingleUniqueNameWithPrefix() {
        mBuilder.insertNameLookup(0, 0, "Mr. Foo", FullNameStyle.UNDEFINED);
        assertEquals("(0:Mr.Foo)(2:MrFoo)(1:Foo.Mr)(2:FooMr)", mBuilder.inserted());
    }

    public void testTwoUniqueNames() {
        mBuilder.insertNameLookup(0, 0, "Foo Bar", FullNameStyle.UNDEFINED);
        assertEquals("(0:Foo.Bar)(2:FooBar)(1:Bar.Foo)(2:BarFoo)", mBuilder.inserted());
    }

    public void testThreeUniqueNames() {
        mBuilder.insertNameLookup(0, 0, "Foo Bar Baz", FullNameStyle.UNDEFINED);

        // All permutations
        assertEquals(
                "(0:Foo.Bar.Baz)(2:FooBarBaz)" +
                "(1:Foo.Baz.Bar)(2:FooBazBar)" +

                "(1:Bar.Foo.Baz)(2:BarFooBaz)" +
                "(1:Bar.Baz.Foo)(2:BarBazFoo)" +

                "(1:Baz.Bar.Foo)(2:BazBarFoo)" +
                "(1:Baz.Foo.Bar)(2:BazFooBar)", mBuilder.inserted());
    }

    public void testFourUniqueNames() {
        mBuilder.insertNameLookup(0, 0, "Foo Bar Baz Biz", FullNameStyle.UNDEFINED);

        // All permutations
        assertEquals(
                "(0:Foo.Bar.Baz.Biz)(2:FooBarBazBiz)" +
                "(1:Foo.Bar.Biz.Baz)(2:FooBarBizBaz)" +
                "(1:Foo.Baz.Bar.Biz)(2:FooBazBarBiz)" +
                "(1:Foo.Baz.Biz.Bar)(2:FooBazBizBar)" +
                "(1:Foo.Biz.Baz.Bar)(2:FooBizBazBar)" +
                "(1:Foo.Biz.Bar.Baz)(2:FooBizBarBaz)" +

                "(1:Bar.Foo.Baz.Biz)(2:BarFooBazBiz)" +
                "(1:Bar.Foo.Biz.Baz)(2:BarFooBizBaz)" +
                "(1:Bar.Baz.Foo.Biz)(2:BarBazFooBiz)" +
                "(1:Bar.Baz.Biz.Foo)(2:BarBazBizFoo)" +
                "(1:Bar.Biz.Baz.Foo)(2:BarBizBazFoo)" +
                "(1:Bar.Biz.Foo.Baz)(2:BarBizFooBaz)" +

                "(1:Baz.Bar.Foo.Biz)(2:BazBarFooBiz)" +
                "(1:Baz.Bar.Biz.Foo)(2:BazBarBizFoo)" +
                "(1:Baz.Foo.Bar.Biz)(2:BazFooBarBiz)" +
                "(1:Baz.Foo.Biz.Bar)(2:BazFooBizBar)" +
                "(1:Baz.Biz.Foo.Bar)(2:BazBizFooBar)" +
                "(1:Baz.Biz.Bar.Foo)(2:BazBizBarFoo)" +

                "(1:Biz.Bar.Baz.Foo)(2:BizBarBazFoo)" +
                "(1:Biz.Bar.Foo.Baz)(2:BizBarFooBaz)" +
                "(1:Biz.Baz.Bar.Foo)(2:BizBazBarFoo)" +
                "(1:Biz.Baz.Foo.Bar)(2:BizBazFooBar)" +
                "(1:Biz.Foo.Baz.Bar)(2:BizFooBazBar)" +
                "(1:Biz.Foo.Bar.Baz)(2:BizFooBarBaz)", mBuilder.inserted());
    }

    public void testChineseName() {
        // Only run this test when Chinese collation is supported
        if (!Arrays.asList(Collator.getAvailableLocales()).contains(Locale.CHINA)) {
            return;
        }

        mBuilder.insertNameLookup(0, 0, "\u695A\u8FAD", FullNameStyle.CHINESE);
        assertEquals(
                "(0:\u695A\u8FAD)" +
                "(2:\u695A\u8FAD)",
                mBuilder.inserted());
    }

    public void testKoreanName() {
        // Only run this test when Korean collation is supported.
        if (!Arrays.asList(Collator.getAvailableLocales()).contains(Locale.KOREA)) {
            return;
        }

        // Lee Sang Il
        mBuilder.insertNameLookup(0, 0, "\uC774\uC0C1\uC77C", FullNameStyle.KOREAN);
        assertEquals(
                "(0:\uC774\uC0C1\uC77C)" + // Lee Sang Il
                "(2:\uC774\uC0C1\uC77C)",
                mBuilder.inserted());
    }

    public void testKoreanNameWithTwoCharactersFamilyName() {
        // Only run this test when Chinese collation is supported.
        if (!Arrays.asList(Collator.getAvailableLocales()).contains(Locale.KOREA)) {
            return;
        }

        // Sun Woo Young Nyeu
        mBuilder.insertNameLookup(0, 0, "\uC120\uC6B0\uC6A9\uB140", FullNameStyle.KOREAN);
        assertEquals(
                "(0:\uC120\uC6B0\uC6A9\uB140)" + // Sun Woo Young Nyeu
                "(2:\uC120\uC6B0\uC6A9\uB140)",  // Sun Woo Young Nyeu
                mBuilder.inserted());
    }

    public void testMultiwordName() {
        mBuilder.insertNameLookup(0, 0, "Jo Jeffrey John Jessy Longname", FullNameStyle.UNDEFINED);
        String actual = mBuilder.inserted();

        // Exact name
        assertTrue(actual.contains("(0:Jo.Jeffrey.John.Jessy.Longname)"));

        // Full collation key
        assertTrue(actual.contains("(2:JoJeffreyJohnJessyLongname)"));

        // Variant: four longest parts
        assertTrue(actual.contains("(1:Longname.Jeffrey.Jessy.John)"));

        // All individual words
        assertTrue(actual.contains("(2:Jo"));
        assertTrue(actual.contains("(2:Jeffrey"));
        assertTrue(actual.contains("(2:John"));
        assertTrue(actual.contains("(2:Jessy"));
        assertTrue(actual.contains("(2:Longname"));
    }
}