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