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