1/* 2 * Copyright (C) 2010 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.dialer.interactions; 18 19import android.content.ContentUris; 20import android.content.Context; 21import android.content.DialogInterface.OnDismissListener; 22import android.content.Intent; 23import android.net.Uri; 24import android.provider.ContactsContract.CommonDataKinds.Phone; 25import android.provider.ContactsContract.CommonDataKinds.SipAddress; 26import android.provider.ContactsContract.Contacts; 27import android.provider.ContactsContract.Data; 28import android.provider.ContactsContract.RawContacts; 29import android.test.InstrumentationTestCase; 30import android.test.suitebuilder.annotation.SmallTest; 31 32import com.android.contacts.common.test.mocks.ContactsMockContext; 33import com.android.contacts.common.test.mocks.MockContentProvider; 34import com.android.contacts.common.test.mocks.MockContentProvider.Query; 35import com.android.contacts.common.util.ContactDisplayUtils; 36import com.android.dialer.interactions.PhoneNumberInteraction.PhoneItem; 37import com.android.dialer.util.TestConstants; 38 39import java.util.ArrayList; 40import java.util.List; 41 42/** 43 * Tests for {@link com.android.contacts.common.interactions.PhoneNumberInteraction}. 44 * 45 * adb shell am instrument \ 46 * -w com.android.dialer.tests/android.test.InstrumentationTestRunner 47 */ 48@SmallTest 49public class PhoneNumberInteractionTest extends InstrumentationTestCase { 50 private final static class TestPhoneNumberInteraction extends PhoneNumberInteraction { 51 private ArrayList<PhoneItem> mPhoneList; 52 53 public TestPhoneNumberInteraction(Context context, int interactionType, 54 OnDismissListener dismissListener) { 55 super(context, interactionType, dismissListener); 56 } 57 58 @Override 59 void showDisambiguationDialog(ArrayList<PhoneItem> phoneList) { 60 this.mPhoneList = phoneList; 61 } 62 } 63 64 private ContactsMockContext mContext; 65 private MockContentProvider mContactsProvider; 66 67 @Override 68 protected void setUp() throws Exception { 69 super.setUp(); 70 mContext = new ContactsMockContext(getInstrumentation().getTargetContext()); 71 mContactsProvider = mContext.getContactsProvider(); 72 } 73 74 @Override 75 protected void tearDown() throws Exception { 76 mContactsProvider.verify(); 77 super.tearDown(); 78 } 79 80 public void testSendSmsWhenOnlyOneNumberAvailable() { 81 Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, 13); 82 expectQuery(contactUri) 83 .returnRow(1, "123", 0, null, null, Phone.TYPE_HOME, null, 84 Phone.CONTENT_ITEM_TYPE, 13); 85 86 TestPhoneNumberInteraction interaction = new TestPhoneNumberInteraction( 87 mContext, ContactDisplayUtils.INTERACTION_SMS, null); 88 89 interaction.startInteraction(contactUri); 90 interaction.getLoader().waitForLoader(); 91 92 Intent intent = mContext.getIntentForStartActivity(); 93 assertNotNull(intent); 94 95 assertEquals(Intent.ACTION_SENDTO, intent.getAction()); 96 assertEquals("sms:123", intent.getDataString()); 97 } 98 99 public void testSendSmsWhenDataIdIsProvided() { 100 Uri dataUri = ContentUris.withAppendedId(Data.CONTENT_URI, 1); 101 expectQuery(dataUri, true /* isDataUri */ ) 102 .returnRow(1, "987", 0, null, null, Phone.TYPE_HOME, null, 103 Phone.CONTENT_ITEM_TYPE, 1); 104 105 TestPhoneNumberInteraction interaction = new TestPhoneNumberInteraction( 106 mContext, ContactDisplayUtils.INTERACTION_SMS, null); 107 108 interaction.startInteraction(dataUri); 109 interaction.getLoader().waitForLoader(); 110 111 Intent intent = mContext.getIntentForStartActivity(); 112 assertNotNull(intent); 113 114 assertEquals(Intent.ACTION_SENDTO, intent.getAction()); 115 assertEquals("sms:987", intent.getDataString()); 116 } 117 118 public void testSendSmsWhenThereIsPrimaryNumber() { 119 Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, 13); 120 expectQuery(contactUri) 121 .returnRow(1, "123", 0, null, null, Phone.TYPE_HOME, null, 122 Phone.CONTENT_ITEM_TYPE, 13) 123 .returnRow(2, "456", 1, null, null, Phone.TYPE_HOME, null, 124 Phone.CONTENT_ITEM_TYPE, 13); 125 126 TestPhoneNumberInteraction interaction = new TestPhoneNumberInteraction( 127 mContext, ContactDisplayUtils.INTERACTION_SMS, null); 128 129 interaction.startInteraction(contactUri); 130 interaction.getLoader().waitForLoader(); 131 132 Intent intent = mContext.getIntentForStartActivity(); 133 assertNotNull(intent); 134 135 assertEquals(Intent.ACTION_SENDTO, intent.getAction()); 136 assertEquals("sms:456", intent.getDataString()); 137 } 138 139 public void testShouldCollapseWith() { 140 PhoneNumberInteraction.PhoneItem phoneItem1 = new PhoneNumberInteraction.PhoneItem(); 141 PhoneNumberInteraction.PhoneItem phoneItem2 = new PhoneNumberInteraction.PhoneItem(); 142 143 phoneItem1.phoneNumber = "123"; 144 phoneItem2.phoneNumber = "123"; 145 146 assertTrue(phoneItem1.shouldCollapseWith(phoneItem2, mContext)); 147 148 phoneItem1.phoneNumber = "123"; 149 phoneItem2.phoneNumber = "456"; 150 151 assertFalse(phoneItem1.shouldCollapseWith(phoneItem2, mContext)); 152 153 phoneItem1.phoneNumber = "123#,123"; 154 phoneItem2.phoneNumber = "123#,456"; 155 156 assertFalse(phoneItem1.shouldCollapseWith(phoneItem2, mContext)); 157 } 158 159 public void testCallNumberWhenThereAreDuplicates() { 160 Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, 13); 161 expectQuery(contactUri) 162 .returnRow(1, "123", 0, null, null, Phone.TYPE_HOME, null, 163 Phone.CONTENT_ITEM_TYPE, 13) 164 .returnRow(2, "123", 0, null, null, Phone.TYPE_WORK, null, 165 Phone.CONTENT_ITEM_TYPE, 13); 166 167 TestPhoneNumberInteraction interaction = new TestPhoneNumberInteraction( 168 mContext, ContactDisplayUtils.INTERACTION_CALL, null); 169 170 interaction.startInteraction(contactUri); 171 interaction.getLoader().waitForLoader(); 172 173 Intent intent = mContext.getIntentForStartActivity(); 174 assertNotNull(intent); 175 176 assertEquals(TestConstants.CALL_INTENT_ACTION, intent.getAction()); 177 assertEquals("tel:123", intent.getDataString()); 178 } 179 180 public void testCallWithSip() { 181 Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, 13); 182 expectQuery(contactUri) 183 .returnRow(1, "example@example.com", 0, null, null, Phone.TYPE_HOME, null, 184 SipAddress.CONTENT_ITEM_TYPE, 13); 185 TestPhoneNumberInteraction interaction = new TestPhoneNumberInteraction( 186 mContext, ContactDisplayUtils.INTERACTION_CALL, null); 187 188 interaction.startInteraction(contactUri); 189 interaction.getLoader().waitForLoader(); 190 191 Intent intent = mContext.getIntentForStartActivity(); 192 assertNotNull(intent); 193 194 assertEquals(TestConstants.CALL_INTENT_ACTION, intent.getAction()); 195 assertEquals("sip:example%40example.com", intent.getDataString()); 196 } 197 198 public void testShowDisambigDialogForCalling() { 199 Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, 13); 200 expectQuery(contactUri) 201 .returnRow(1, "123", 0, "account", null, Phone.TYPE_HOME, "label", 202 Phone.CONTENT_ITEM_TYPE, 13) 203 .returnRow(2, "456", 0, null, null, Phone.TYPE_WORK, null, 204 Phone.CONTENT_ITEM_TYPE, 13); 205 206 TestPhoneNumberInteraction interaction = new TestPhoneNumberInteraction( 207 mContext, ContactDisplayUtils.INTERACTION_CALL, null); 208 209 interaction.startInteraction(contactUri); 210 interaction.getLoader().waitForLoader(); 211 212 List<PhoneItem> items = interaction.mPhoneList; 213 assertNotNull(items); 214 assertEquals(2, items.size()); 215 216 PhoneItem item = items.get(0); 217 assertEquals(1, item.id); 218 assertEquals("123", item.phoneNumber); 219 assertEquals("account", item.accountType); 220 assertEquals(Phone.TYPE_HOME, item.type); 221 assertEquals("label", item.label); 222 } 223 224 private Query expectQuery(Uri contactUri) { 225 return expectQuery(contactUri, false); 226 } 227 228 private Query expectQuery(Uri uri, boolean isDataUri) { 229 final Uri dataUri; 230 if (isDataUri) { 231 dataUri = uri; 232 } else { 233 dataUri = Uri.withAppendedPath(uri, Contacts.Data.CONTENT_DIRECTORY); 234 } 235 return mContactsProvider 236 .expectQuery(dataUri) 237 .withProjection( 238 Phone._ID, 239 Phone.NUMBER, 240 Phone.IS_SUPER_PRIMARY, 241 RawContacts.ACCOUNT_TYPE, 242 RawContacts.DATA_SET, 243 Phone.TYPE, 244 Phone.LABEL, 245 Phone.MIMETYPE, 246 Phone.CONTACT_ID) 247 .withSelection("mimetype IN ('vnd.android.cursor.item/phone_v2'," 248 + " 'vnd.android.cursor.item/sip_address') AND data1 NOT NULL"); 249 } 250} 251