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