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