1/*
2 * Copyright (C) 2009 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.providers.contacts;
18
19import android.test.MoreAsserts;
20import android.test.suitebuilder.annotation.SmallTest;
21
22import java.text.RuleBasedCollator;
23import java.util.Locale;
24
25import junit.framework.TestCase;
26
27/**
28 * Unit tests for {@link NameNormalizer}.
29 *
30 * Run the test like this:
31 * <code>
32   adb shell am instrument -e class com.android.providers.contacts.NameNormalizerTest -w \
33           com.android.providers.contacts.tests/android.test.InstrumentationTestRunner
34 * </code>
35 */
36@SmallTest
37public class NameNormalizerTest extends TestCase {
38
39    private Locale mOriginalLocale;
40
41
42    @Override
43    protected void setUp() throws Exception {
44        super.setUp();
45
46        mOriginalLocale = Locale.getDefault();
47
48        // Run all test in en_US
49        Locale.setDefault(Locale.US);
50    }
51
52    @Override
53    protected void tearDown() throws Exception {
54        Locale.setDefault(mOriginalLocale);
55        super.tearDown();
56    }
57
58    public void testDifferent() {
59        final String name1 = NameNormalizer.normalize("Helene");
60        final String name2 = NameNormalizer.normalize("Francesca");
61        assertFalse(name2.equals(name1));
62    }
63
64    public void testAccents() {
65        final String name1 = NameNormalizer.normalize("Helene");
66        final String name2 = NameNormalizer.normalize("H\u00e9l\u00e8ne");
67        assertTrue(name2.equals(name1));
68    }
69
70    public void testMixedCase() {
71        final String name1 = NameNormalizer.normalize("Helene");
72        final String name2 = NameNormalizer.normalize("hEL\uFF25NE"); // FF25 = FULL WIDTH E
73        assertTrue(name2.equals(name1));
74    }
75
76    public void testNonLetters() {
77        // U+FF1E: 'FULLWIDTH GREATER-THAN SIGN'
78        // U+FF03: 'FULLWIDTH NUMBER SIGN'
79        final String name1 = NameNormalizer.normalize("h-e?l \uFF1ee+\uFF03n=e");
80        final String name2 = NameNormalizer.normalize("helene");
81        assertTrue(name2.equals(name1));
82    }
83
84    public void testComplexityCase() {
85        assertTrue(NameNormalizer.compareComplexity("Helene", "helene") > 0);
86    }
87
88    public void testComplexityAccent() {
89        assertTrue(NameNormalizer.compareComplexity("H\u00e9lene", "Helene") > 0);
90    }
91
92    public void testComplexityLength() {
93        assertTrue(NameNormalizer.compareComplexity("helene2009", "helene") > 0);
94    }
95
96    public void testGetCollators() {
97        final RuleBasedCollator compressing1 = NameNormalizer.getCompressingCollator();
98        final RuleBasedCollator complexity1 = NameNormalizer.getComplexityCollator();
99
100        assertNotNull(compressing1);
101        assertNotNull(complexity1);
102        assertNotSame(compressing1, complexity1);
103
104        // Get again.  Should be cached.
105        final RuleBasedCollator compressing2 = NameNormalizer.getCompressingCollator();
106        final RuleBasedCollator complexity2 = NameNormalizer.getComplexityCollator();
107
108        assertSame(compressing1, compressing2);
109        assertSame(complexity1, complexity2);
110
111        // Change locale -- now new collators should be returned.
112        Locale.setDefault(Locale.FRANCE);
113
114        final RuleBasedCollator compressing3 = NameNormalizer.getCompressingCollator();
115        final RuleBasedCollator complexity3 = NameNormalizer.getComplexityCollator();
116
117        assertNotNull(compressing3);
118        assertNotNull(complexity3);
119        assertNotSame(compressing3, complexity3);
120
121        assertNotSame(compressing1, compressing3);
122        assertNotSame(complexity1, complexity3);
123    }
124}
125