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.contacts; 18 19 import android.content.Intent; 20 import android.provider.ContactsContract.CommonDataKinds.Phone; 21 import android.test.AndroidTestCase; 22 import android.test.suitebuilder.annotation.SmallTest; 23 24 /** 25 * Tests for {@link ContactsUtils}. 26 */ 27 @SmallTest 28 public class ContactsUtilsTests extends AndroidTestCase { 29 30 public void testIsGraphicNull() throws Exception { 31 assertFalse(ContactsUtils.isGraphic(null)); 32 } 33 34 public void testIsGraphicEmpty() throws Exception { 35 assertFalse(ContactsUtils.isGraphic("")); 36 } 37 38 public void testIsGraphicSpaces() throws Exception { 39 assertFalse(ContactsUtils.isGraphic(" ")); 40 } 41 42 public void testIsGraphicPunctuation() throws Exception { 43 assertTrue(ContactsUtils.isGraphic(".")); 44 } 45 46 public void testAreObjectsEqual() throws Exception { 47 assertTrue("null:null", ContactsUtils.areObjectsEqual(null, null)); 48 assertTrue("1:1", ContactsUtils.areObjectsEqual(1, 1)); 49 50 assertFalse("null:1", ContactsUtils.areObjectsEqual(null, 1)); 51 assertFalse("1:null", ContactsUtils.areObjectsEqual(1, null)); 52 assertFalse("1:2", ContactsUtils.areObjectsEqual(1, 2)); 53 } 54 55 public void testShouldCollapse() throws Exception { 56 assertCollapses("1", true, null, null, null, null); 57 assertCollapses("2", true, "a", "b", "a", "b"); 58 59 assertCollapses("11", false, "a", null, null, null); 60 assertCollapses("12", false, null, "a", null, null); 61 assertCollapses("13", false, null, null, "a", null); 62 assertCollapses("14", false, null, null, null, "a"); 63 64 assertCollapses("21", false, "a", "b", null, null); 65 assertCollapses("22", false, "a", "b", "a", null); 66 assertCollapses("23", false, "a", "b", null, "b"); 67 assertCollapses("24", false, "a", "b", "a", "x"); 68 assertCollapses("25", false, "a", "b", "x", "b"); 69 70 assertCollapses("31", false, null, null, "a", "b"); 71 assertCollapses("32", false, "a", null, "a", "b"); 72 assertCollapses("33", false, null, "b", "a", "b"); 73 assertCollapses("34", false, "a", "x", "a", "b"); 74 assertCollapses("35", false, "x", "b", "a", "b"); 75 76 assertCollapses("41", true, Phone.CONTENT_ITEM_TYPE, null, Phone.CONTENT_ITEM_TYPE, 77 null); 78 assertCollapses("42", true, Phone.CONTENT_ITEM_TYPE, "1", Phone.CONTENT_ITEM_TYPE, "1"); 79 80 assertCollapses("51", false, Phone.CONTENT_ITEM_TYPE, "1", Phone.CONTENT_ITEM_TYPE, 81 "2"); 82 assertCollapses("52", false, Phone.CONTENT_ITEM_TYPE, "1", Phone.CONTENT_ITEM_TYPE, 83 null); 84 assertCollapses("53", false, Phone.CONTENT_ITEM_TYPE, null, Phone.CONTENT_ITEM_TYPE, 85 "2"); 86 87 // Test phone numbers 88 assertCollapses("60", true, 89 Phone.CONTENT_ITEM_TYPE, "1234567", 90 Phone.CONTENT_ITEM_TYPE, "1234567"); 91 assertCollapses("61", false, 92 Phone.CONTENT_ITEM_TYPE, "1234567", 93 Phone.CONTENT_ITEM_TYPE, "1234568"); 94 assertCollapses("62", true, 95 Phone.CONTENT_ITEM_TYPE, "1234567;0", 96 Phone.CONTENT_ITEM_TYPE, "1234567;0"); 97 assertCollapses("63", false, 98 Phone.CONTENT_ITEM_TYPE, "1234567;89321", 99 Phone.CONTENT_ITEM_TYPE, "1234567;89322"); 100 assertCollapses("64", true, 101 Phone.CONTENT_ITEM_TYPE, "1234567;89321", 102 Phone.CONTENT_ITEM_TYPE, "1234567;89321"); 103 assertCollapses("65", false, 104 Phone.CONTENT_ITEM_TYPE, "1234567;0111111111", 105 Phone.CONTENT_ITEM_TYPE, "1234567;"); 106 assertCollapses("66", false, 107 Phone.CONTENT_ITEM_TYPE, "12345675426;91970xxxxx", 108 Phone.CONTENT_ITEM_TYPE, "12345675426"); 109 assertCollapses("67", false, 110 Phone.CONTENT_ITEM_TYPE, "12345675426;23456xxxxx", 111 Phone.CONTENT_ITEM_TYPE, "12345675426;234567xxxx"); 112 assertCollapses("68", true, 113 Phone.CONTENT_ITEM_TYPE, "1234567;1234567;1234567", 114 Phone.CONTENT_ITEM_TYPE, "1234567;1234567;1234567"); 115 assertCollapses("69", false, 116 Phone.CONTENT_ITEM_TYPE, "1234567;1234567;1234567", 117 Phone.CONTENT_ITEM_TYPE, "1234567;1234567"); 118 119 // test some numbers with country and area code 120 assertCollapses("70", true, 121 Phone.CONTENT_ITEM_TYPE, "+49 (89) 12345678", 122 Phone.CONTENT_ITEM_TYPE, "+49 (89) 12345678"); 123 assertCollapses("71", true, 124 Phone.CONTENT_ITEM_TYPE, "+49 (89) 12345678", 125 Phone.CONTENT_ITEM_TYPE, "+49 (89)12345678"); 126 assertCollapses("72", true, 127 Phone.CONTENT_ITEM_TYPE, "+49 (8092) 1234", 128 Phone.CONTENT_ITEM_TYPE, "+49 (8092)1234"); 129 assertCollapses("73", true, 130 Phone.CONTENT_ITEM_TYPE, "0049 (8092) 1234", 131 Phone.CONTENT_ITEM_TYPE, "+49/80921234"); 132 assertCollapses("74", false, 133 Phone.CONTENT_ITEM_TYPE, "+49 (89) 12345678", 134 Phone.CONTENT_ITEM_TYPE, "+49 (89) 12345679"); 135 136 // test some numbers with wait symbol and area code 137 assertCollapses("80", true, 138 Phone.CONTENT_ITEM_TYPE, "+49 (8092) 1234;89321", 139 Phone.CONTENT_ITEM_TYPE, "+49/80921234;89321"); 140 assertCollapses("81", false, 141 Phone.CONTENT_ITEM_TYPE, "+49 (8092) 1234;89321", 142 Phone.CONTENT_ITEM_TYPE, "+49/80921235;89321"); 143 assertCollapses("82", false, 144 Phone.CONTENT_ITEM_TYPE, "+49 (8092) 1234;89322", 145 Phone.CONTENT_ITEM_TYPE, "+49/80921234;89321"); 146 assertCollapses("83", true, 147 Phone.CONTENT_ITEM_TYPE, "1234567;+49 (8092) 1234", 148 Phone.CONTENT_ITEM_TYPE, "1234567;+49/80921234"); 149 150 // this makes sure that if if two segments are identical, we don't even try to parse 151 // (and therefore allow invalid phone numbers) 152 assertCollapses("84", true, 153 Phone.CONTENT_ITEM_TYPE, "+49/80921234;a89", 154 Phone.CONTENT_ITEM_TYPE, "+49 (8092) 1234;a89"); 155 assertCollapses("85", false, 156 Phone.CONTENT_ITEM_TYPE, "+49/80921234;a89", 157 Phone.CONTENT_ITEM_TYPE, "+49/80921234;b89"); 158 } 159 160 private void assertCollapses(String message, boolean expected, CharSequence mimetype1, 161 CharSequence data1, CharSequence mimetype2, CharSequence data2) { 162 assertEquals(message, expected, 163 ContactsUtils.shouldCollapse(mimetype1, data1, mimetype2, data2)); 164 assertEquals(message, expected, 165 ContactsUtils.shouldCollapse(mimetype2, data2, mimetype1, data1)); 166 167 if (data1 == data2 && data1 != null) { 168 // make sure we also do a test where object equality is not given 169 final CharSequence data2_newref = data2 + ""; 170 171 // this just makes sure the test is working 172 assertFalse(data1 == data2_newref); 173 174 // we have two different instances, now make sure we get the same result as before 175 assertEquals(message, expected, 176 ContactsUtils.shouldCollapse(mimetype1, data1, mimetype2, 177 data2_newref)); 178 assertEquals(message, expected, 179 ContactsUtils.shouldCollapse(mimetype2, data2_newref, mimetype1, 180 data1)); 181 } 182 } 183 184 public void testAreIntentActionEqual() throws Exception { 185 assertTrue("1", ContactsUtils.areIntentActionEqual(null, null)); 186 assertTrue("1", ContactsUtils.areIntentActionEqual(new Intent("a"), new Intent("a"))); 187 188 assertFalse("11", ContactsUtils.areIntentActionEqual(new Intent("a"), null)); 189 assertFalse("12", ContactsUtils.areIntentActionEqual(null, new Intent("a"))); 190 191 assertFalse("21", ContactsUtils.areIntentActionEqual(new Intent("a"), new Intent())); 192 assertFalse("22", ContactsUtils.areIntentActionEqual(new Intent(), new Intent("b"))); 193 assertFalse("23", ContactsUtils.areIntentActionEqual(new Intent("a"), new Intent("b"))); 194 } 195 } 196