summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/dialer/interactions/PhoneNumberInteractionTest.java
blob: f86675e5727cc6ffb5e846ceb8a336c6a31c327b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.dialer.interactions;

import android.content.ContentUris;
import android.content.Context;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.SipAddress;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;
import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.SmallTest;

import com.android.contacts.common.test.mocks.ContactsMockContext;
import com.android.contacts.common.test.mocks.MockContentProvider;
import com.android.contacts.common.test.mocks.MockContentProvider.Query;
import com.android.contacts.common.util.ContactDisplayUtils;
import com.android.dialer.interactions.PhoneNumberInteraction.PhoneItem;

import java.util.ArrayList;
import java.util.List;

/**
 * Tests for {@link com.android.contacts.common.interactions.PhoneNumberInteraction}.
 *
 * Running all tests:
 *
 *   runtest contacts
 * or
 *   adb shell am instrument \
 *     -w com.android.contacts.tests/android.test.InstrumentationTestRunner
 */
@SmallTest
public class PhoneNumberInteractionTest extends InstrumentationTestCase {

    static {
        // AsyncTask class needs to be initialized on the main thread.
        AsyncTask.init();
    }

    private final static class TestPhoneNumberInteraction extends PhoneNumberInteraction {
        private ArrayList<PhoneItem> mPhoneList;

        public TestPhoneNumberInteraction(Context context, int interactionType,
                OnDismissListener dismissListener) {
            super(context, interactionType, dismissListener);
        }

        @Override
        void showDisambiguationDialog(ArrayList<PhoneItem> phoneList) {
            this.mPhoneList = phoneList;
        }
    }

    private ContactsMockContext mContext;
    private MockContentProvider mContactsProvider;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mContext = new ContactsMockContext(getInstrumentation().getTargetContext());
        mContactsProvider = mContext.getContactsProvider();
    }

    @Override
    protected void tearDown() throws Exception {
        mContactsProvider.verify();
        super.tearDown();
    }

    public void testSendSmsWhenOnlyOneNumberAvailable() {
        Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, 13);
        expectQuery(contactUri)
                .returnRow(1, "123", 0, null, null, Phone.TYPE_HOME, null,
                        Phone.CONTENT_ITEM_TYPE);

        TestPhoneNumberInteraction interaction = new TestPhoneNumberInteraction(
                mContext, ContactDisplayUtils.INTERACTION_SMS, null);

        interaction.startInteraction(contactUri);
        interaction.getLoader().waitForLoader();

        Intent intent = mContext.getIntentForStartActivity();
        assertNotNull(intent);

        assertEquals(Intent.ACTION_SENDTO, intent.getAction());
        assertEquals("sms:123", intent.getDataString());
    }

    public void testSendSmsWhenDataIdIsProvided() {
        Uri dataUri = ContentUris.withAppendedId(Data.CONTENT_URI, 1);
        expectQuery(dataUri, true /* isDataUri */ )
                .returnRow(1, "987", 0, null, null, Phone.TYPE_HOME, null,
                        Phone.CONTENT_ITEM_TYPE);

        TestPhoneNumberInteraction interaction = new TestPhoneNumberInteraction(
                mContext, ContactDisplayUtils.INTERACTION_SMS, null);

        interaction.startInteraction(dataUri);
        interaction.getLoader().waitForLoader();

        Intent intent = mContext.getIntentForStartActivity();
        assertNotNull(intent);

        assertEquals(Intent.ACTION_SENDTO, intent.getAction());
        assertEquals("sms:987", intent.getDataString());
    }

    public void testSendSmsWhenThereIsPrimaryNumber() {
        Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, 13);
        expectQuery(contactUri)
                .returnRow(
                        1, "123", 0, null, null, Phone.TYPE_HOME, null, Phone.CONTENT_ITEM_TYPE)
                .returnRow(
                        2, "456", 1, null, null, Phone.TYPE_HOME, null, Phone.CONTENT_ITEM_TYPE);

        TestPhoneNumberInteraction interaction = new TestPhoneNumberInteraction(
                mContext, ContactDisplayUtils.INTERACTION_SMS, null);

        interaction.startInteraction(contactUri);
        interaction.getLoader().waitForLoader();

        Intent intent = mContext.getIntentForStartActivity();
        assertNotNull(intent);

        assertEquals(Intent.ACTION_SENDTO, intent.getAction());
        assertEquals("sms:456", intent.getDataString());
    }

    public void testShouldCollapseWith() {
        PhoneNumberInteraction.PhoneItem phoneItem1 = new PhoneNumberInteraction.PhoneItem();
        PhoneNumberInteraction.PhoneItem phoneItem2 = new PhoneNumberInteraction.PhoneItem();

        phoneItem1.phoneNumber = "123";
        phoneItem2.phoneNumber = "123";

        assertTrue(phoneItem1.shouldCollapseWith(phoneItem2));

        phoneItem1.phoneNumber = "123";
        phoneItem2.phoneNumber = "456";

        assertFalse(phoneItem1.shouldCollapseWith(phoneItem2));

        phoneItem1.phoneNumber = "123#,123";
        phoneItem2.phoneNumber = "123#,456";

        assertFalse(phoneItem1.shouldCollapseWith(phoneItem2));
    }

    public void testCallNumberWhenThereAreDuplicates() {
        Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, 13);
        expectQuery(contactUri)
                .returnRow(1, "123", 0, null, null, Phone.TYPE_HOME, null,
                        Phone.CONTENT_ITEM_TYPE)
                .returnRow(2, "123", 0, null, null, Phone.TYPE_WORK, null,
                        Phone.CONTENT_ITEM_TYPE);

        TestPhoneNumberInteraction interaction = new TestPhoneNumberInteraction(
                mContext, ContactDisplayUtils.INTERACTION_CALL, null);

        interaction.startInteraction(contactUri);
        interaction.getLoader().waitForLoader();

        Intent intent = mContext.getIntentForStartActivity();
        assertNotNull(intent);

        assertEquals(Intent.ACTION_CALL_PRIVILEGED, intent.getAction());
        assertEquals("tel:123", intent.getDataString());
    }

    public void testCallWithSip() {
        Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, 13);
        expectQuery(contactUri)
                .returnRow(1, "example@example.com", 0, null, null, Phone.TYPE_HOME, null,
                        SipAddress.CONTENT_ITEM_TYPE);
        TestPhoneNumberInteraction interaction = new TestPhoneNumberInteraction(
                mContext, ContactDisplayUtils.INTERACTION_CALL, null);

        interaction.startInteraction(contactUri);
        interaction.getLoader().waitForLoader();

        Intent intent = mContext.getIntentForStartActivity();
        assertNotNull(intent);

        assertEquals(Intent.ACTION_CALL_PRIVILEGED, intent.getAction());
        assertEquals("sip:example%40example.com", intent.getDataString());
    }

    public void testShowDisambigDialogForCalling() {
        Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, 13);
        expectQuery(contactUri)
                .returnRow(1, "123", 0, "account", null, Phone.TYPE_HOME, "label",
                        Phone.CONTENT_ITEM_TYPE)
                .returnRow(2, "456", 0, null, null, Phone.TYPE_WORK, null,
                        Phone.CONTENT_ITEM_TYPE);

        TestPhoneNumberInteraction interaction = new TestPhoneNumberInteraction(
                mContext, ContactDisplayUtils.INTERACTION_CALL, null);

        interaction.startInteraction(contactUri);
        interaction.getLoader().waitForLoader();

        List<PhoneItem> items = interaction.mPhoneList;
        assertNotNull(items);
        assertEquals(2, items.size());

        PhoneItem item = items.get(0);
        assertEquals(1, item.id);
        assertEquals("123", item.phoneNumber);
        assertEquals("account", item.accountType);
        assertEquals(Phone.TYPE_HOME, item.type);
        assertEquals("label", item.label);
    }

    private Query expectQuery(Uri contactUri) {
        return expectQuery(contactUri, false);
    }

    private Query expectQuery(Uri uri, boolean isDataUri) {
        final Uri dataUri;
        if (isDataUri) {
            dataUri = uri;
        } else {
            dataUri = Uri.withAppendedPath(uri, Contacts.Data.CONTENT_DIRECTORY);
        }
        return mContactsProvider
                .expectQuery(dataUri)
                .withProjection(
                        Phone._ID,
                        Phone.NUMBER,
                        Phone.IS_SUPER_PRIMARY,
                        RawContacts.ACCOUNT_TYPE,
                        RawContacts.DATA_SET,
                        Phone.TYPE,
                        Phone.LABEL,
                        Phone.MIMETYPE)
                .withSelection("mimetype IN ('vnd.android.cursor.item/phone_v2',"
                        + " 'vnd.android.cursor.item/sip_address') AND data1 NOT NULL");
    }
}