1 package com.android.contacts; 2 3 import android.provider.ContactsContract.CommonDataKinds.Phone; 4 import android.test.suitebuilder.annotation.SmallTest; 5 6 import junit.framework.TestCase; 7 8 /** 9 * Tests for MoreContactsUtils. 10 */ 11 @SmallTest 12 public class MoreContactUtilsTest extends TestCase { 13 testShouldCollapse()14 public void testShouldCollapse() throws Exception { 15 assertCollapses("1", true, null, null, null, null); 16 assertCollapses("2", true, "a", "b", "a", "b"); 17 18 assertCollapses("11", false, "a", null, null, null); 19 assertCollapses("12", false, null, "a", null, null); 20 assertCollapses("13", false, null, null, "a", null); 21 assertCollapses("14", false, null, null, null, "a"); 22 23 assertCollapses("21", false, "a", "b", null, null); 24 assertCollapses("22", false, "a", "b", "a", null); 25 assertCollapses("23", false, "a", "b", null, "b"); 26 assertCollapses("24", false, "a", "b", "a", "x"); 27 assertCollapses("25", false, "a", "b", "x", "b"); 28 29 assertCollapses("31", false, null, null, "a", "b"); 30 assertCollapses("32", false, "a", null, "a", "b"); 31 assertCollapses("33", false, null, "b", "a", "b"); 32 assertCollapses("34", false, "a", "x", "a", "b"); 33 assertCollapses("35", false, "x", "b", "a", "b"); 34 35 assertCollapses("41", true, Phone.CONTENT_ITEM_TYPE, null, Phone.CONTENT_ITEM_TYPE, null); 36 assertCollapses("42", true, Phone.CONTENT_ITEM_TYPE, "1", Phone.CONTENT_ITEM_TYPE, "1"); 37 38 assertCollapses("51", false, Phone.CONTENT_ITEM_TYPE, "1", Phone.CONTENT_ITEM_TYPE, "2"); 39 assertCollapses("52", false, Phone.CONTENT_ITEM_TYPE, "1", Phone.CONTENT_ITEM_TYPE, null); 40 assertCollapses("53", false, Phone.CONTENT_ITEM_TYPE, null, Phone.CONTENT_ITEM_TYPE, "2"); 41 42 // Test phone numbers 43 assertCollapses("60", true, Phone.CONTENT_ITEM_TYPE, "1234567", Phone.CONTENT_ITEM_TYPE, 44 "1234567"); 45 assertCollapses("61", false, Phone.CONTENT_ITEM_TYPE, "1234567", Phone.CONTENT_ITEM_TYPE, 46 "1234568"); 47 assertCollapses("62", true, Phone.CONTENT_ITEM_TYPE, "1234567;0", Phone.CONTENT_ITEM_TYPE, 48 "1234567;0"); 49 assertCollapses("63", false, Phone.CONTENT_ITEM_TYPE, "1234567;89321", 50 Phone.CONTENT_ITEM_TYPE, "1234567;89322"); 51 assertCollapses("64", true, Phone.CONTENT_ITEM_TYPE, "1234567;89321", 52 Phone.CONTENT_ITEM_TYPE, "1234567;89321"); 53 assertCollapses("65", false, Phone.CONTENT_ITEM_TYPE, "1234567;0111111111", 54 Phone.CONTENT_ITEM_TYPE, "1234567;"); 55 assertCollapses("66", false, Phone.CONTENT_ITEM_TYPE, "12345675426;91970xxxxx", 56 Phone.CONTENT_ITEM_TYPE, "12345675426"); 57 assertCollapses("67", false, Phone.CONTENT_ITEM_TYPE, "12345675426;23456xxxxx", 58 Phone.CONTENT_ITEM_TYPE, "12345675426;234567xxxx"); 59 assertCollapses("68", true, Phone.CONTENT_ITEM_TYPE, "1234567;1234567;1234567", 60 Phone.CONTENT_ITEM_TYPE, "1234567;1234567;1234567"); 61 assertCollapses("69", false, Phone.CONTENT_ITEM_TYPE, "1234567;1234567;1234567", 62 Phone.CONTENT_ITEM_TYPE, "1234567;1234567"); 63 64 // test some numbers with country and area code 65 assertCollapses("70", true, Phone.CONTENT_ITEM_TYPE, "+49 (89) 12345678", 66 Phone.CONTENT_ITEM_TYPE, "+49 (89) 12345678"); 67 assertCollapses("71", true, Phone.CONTENT_ITEM_TYPE, "+49 (89) 12345678", 68 Phone.CONTENT_ITEM_TYPE, "+49 (89)12345678"); 69 assertCollapses("72", true, Phone.CONTENT_ITEM_TYPE, "+49 (8092) 1234", 70 Phone.CONTENT_ITEM_TYPE, "+49 (8092)1234"); 71 assertCollapses("73", false, Phone.CONTENT_ITEM_TYPE, "0049 (8092) 1234", 72 Phone.CONTENT_ITEM_TYPE, "+49/80921234"); 73 assertCollapses("74", false, Phone.CONTENT_ITEM_TYPE, "+49 (89) 12345678", 74 Phone.CONTENT_ITEM_TYPE, "+49 (89) 12345679"); 75 76 // test special handling of collapsing country code for NANP region only 77 // This is non symmetrical, because we prefer the number with the +1. 78 assertEquals("100", true, MoreContactUtils.shouldCollapse(Phone.CONTENT_ITEM_TYPE, 79 "+1 (415) 555-1212", Phone.CONTENT_ITEM_TYPE, "(415) 555-1212")); 80 assertEquals("101", true, MoreContactUtils.shouldCollapse(Phone.CONTENT_ITEM_TYPE, 81 "+14155551212", Phone.CONTENT_ITEM_TYPE, "4155551212")); 82 assertEquals("102", false, MoreContactUtils.shouldCollapse(Phone.CONTENT_ITEM_TYPE, 83 "(415) 555-1212", Phone.CONTENT_ITEM_TYPE, "+1 (415) 555-1212")); 84 assertEquals("103", false, MoreContactUtils.shouldCollapse(Phone.CONTENT_ITEM_TYPE, 85 "4155551212", Phone.CONTENT_ITEM_TYPE, "+14155551212")); 86 // Require explicit +1 country code declaration to collapse 87 assertEquals("104", false, MoreContactUtils.shouldCollapse(Phone.CONTENT_ITEM_TYPE, 88 "1-415-555-1212", Phone.CONTENT_ITEM_TYPE, "415-555-1212")); 89 assertEquals("105", false, MoreContactUtils.shouldCollapse(Phone.CONTENT_ITEM_TYPE, 90 "14155551212", Phone.CONTENT_ITEM_TYPE, "4155551212")); 91 assertEquals("106", false, MoreContactUtils.shouldCollapse(Phone.CONTENT_ITEM_TYPE, 92 "+1 (415) 555-1212", Phone.CONTENT_ITEM_TYPE, " 1 (415) 555-1212")); 93 assertEquals("107", false, MoreContactUtils.shouldCollapse(Phone.CONTENT_ITEM_TYPE, 94 "+14155551212", Phone.CONTENT_ITEM_TYPE, " 14155551212")); 95 assertEquals("108", false, MoreContactUtils.shouldCollapse(Phone.CONTENT_ITEM_TYPE, 96 "1 (415) 555-1212", Phone.CONTENT_ITEM_TYPE, "+1 (415) 555-1212")); 97 assertEquals("109", false, MoreContactUtils.shouldCollapse(Phone.CONTENT_ITEM_TYPE, 98 "14155551212", Phone.CONTENT_ITEM_TYPE, "+14155551212")); 99 100 // test some numbers with wait symbol and area code 101 assertCollapses("200", true, Phone.CONTENT_ITEM_TYPE, "+49 (8092) 1234;89321", 102 Phone.CONTENT_ITEM_TYPE, "+49/80921234;89321"); 103 assertCollapses("201", false, Phone.CONTENT_ITEM_TYPE, "+49 (8092) 1234;89321", 104 Phone.CONTENT_ITEM_TYPE, "+49/80921235;89321"); 105 assertCollapses("202", false, Phone.CONTENT_ITEM_TYPE, "+49 (8092) 1234;89322", 106 Phone.CONTENT_ITEM_TYPE, "+49/80921234;89321"); 107 assertCollapses("203", true, Phone.CONTENT_ITEM_TYPE, "1234567;+49 (8092) 1234", 108 Phone.CONTENT_ITEM_TYPE, "1234567;+49/80921234"); 109 110 assertCollapses("300", true, Phone.CONTENT_ITEM_TYPE, "", Phone.CONTENT_ITEM_TYPE, ""); 111 112 assertCollapses("301", false, Phone.CONTENT_ITEM_TYPE, "1", Phone.CONTENT_ITEM_TYPE, ""); 113 114 assertCollapses("302", false, Phone.CONTENT_ITEM_TYPE, "", Phone.CONTENT_ITEM_TYPE, "1"); 115 116 assertCollapses("303", true, Phone.CONTENT_ITEM_TYPE, "---", Phone.CONTENT_ITEM_TYPE, "---"); 117 118 assertCollapses("304", false, Phone.CONTENT_ITEM_TYPE, "1-/().", Phone.CONTENT_ITEM_TYPE, 119 "--$%1"); 120 121 // Test numbers using keypad letters. This is non-symmetrical, because we prefer 122 // the version with letters. 123 assertEquals("400", true, MoreContactUtils.shouldCollapse(Phone.CONTENT_ITEM_TYPE, 124 "abcdefghijklmnopqrstuvwxyz", Phone.CONTENT_ITEM_TYPE, 125 "22233344455566677778889999")); 126 assertEquals("401", false, MoreContactUtils.shouldCollapse(Phone.CONTENT_ITEM_TYPE, 127 "22233344455566677778889999", Phone.CONTENT_ITEM_TYPE, 128 "abcdefghijklmnopqrstuvwxyz")); 129 130 assertCollapses("402", false, Phone.CONTENT_ITEM_TYPE, "1;2", Phone.CONTENT_ITEM_TYPE, 131 "12"); 132 133 assertCollapses("403", false, Phone.CONTENT_ITEM_TYPE, "1,2", Phone.CONTENT_ITEM_TYPE, 134 "12"); 135 } 136 testShouldCollapse_collapsesSameNumberWithDifferentFormats()137 public void testShouldCollapse_collapsesSameNumberWithDifferentFormats() { 138 assertEquals("1", true, MoreContactUtils.shouldCollapse(Phone.CONTENT_ITEM_TYPE, 139 "555-1212", Phone.CONTENT_ITEM_TYPE, "5551212")); 140 assertEquals("1", true, MoreContactUtils.shouldCollapse(Phone.CONTENT_ITEM_TYPE, 141 "415-555-1212", Phone.CONTENT_ITEM_TYPE, "(415) 555-1212")); 142 assertEquals("2", true, MoreContactUtils.shouldCollapse(Phone.CONTENT_ITEM_TYPE, 143 "4155551212", Phone.CONTENT_ITEM_TYPE, "(415) 555-1212")); 144 assertEquals("3", true, MoreContactUtils.shouldCollapse(Phone.CONTENT_ITEM_TYPE, 145 "1-415-555-1212", Phone.CONTENT_ITEM_TYPE, "1 (415) 555-1212")); 146 assertEquals("4", true, MoreContactUtils.shouldCollapse(Phone.CONTENT_ITEM_TYPE, 147 "14155551212", Phone.CONTENT_ITEM_TYPE, "1 (415) 555-1212")); 148 } 149 assertCollapses(String message, boolean expected, CharSequence mimetype1, CharSequence data1, CharSequence mimetype2, CharSequence data2)150 private void assertCollapses(String message, boolean expected, CharSequence mimetype1, 151 CharSequence data1, CharSequence mimetype2, CharSequence data2) { 152 assertEquals(message, expected, MoreContactUtils.shouldCollapse(mimetype1, data1, mimetype2, 153 data2)); 154 assertEquals(message, expected, MoreContactUtils.shouldCollapse(mimetype2, data2, mimetype1, 155 data1)); 156 157 // If data1 and data2 are the same instance, make sure the same test passes with different 158 // instances. 159 if (data1 == data2 && data1 != null) { 160 // Create a different instance 161 final CharSequence data2_newref = new StringBuilder(data2).append("").toString(); 162 163 if (data1 == data2_newref) { 164 // In some cases no matter what we do the runtime reuses the same instance, so 165 // we can't do the "different instance" test. 166 return; 167 } 168 169 // we have two different instances, now make sure we get the same result as before 170 assertEquals(message, expected, MoreContactUtils.shouldCollapse(mimetype1, data1, 171 mimetype2, data2_newref)); 172 assertEquals(message, expected, MoreContactUtils.shouldCollapse(mimetype2, data2_newref, 173 mimetype1, data1)); 174 } 175 } 176 } 177