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