GlobalSearchSupportTest.java revision 6bc46c9f22aaa9e68f344b171426fc686d3b536a
1/*
2 * Copyright (C) 2009 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 */
16package com.android.providers.contacts;
17
18import android.accounts.Account;
19import android.app.SearchManager;
20import android.content.ContentUris;
21import android.content.ContentValues;
22import android.database.Cursor;
23import android.database.DatabaseUtils;
24import android.net.Uri;
25import android.provider.ContactsContract;
26import android.provider.Contacts.Intents;
27import android.provider.ContactsContract.Data;
28import android.provider.ContactsContract.Presence;
29import android.provider.ContactsContract.RawContacts;
30import android.provider.ContactsContract.CommonDataKinds.Email;
31import android.provider.ContactsContract.CommonDataKinds.Im;
32import android.provider.ContactsContract.CommonDataKinds.Organization;
33import android.provider.ContactsContract.CommonDataKinds.Phone;
34import android.provider.ContactsContract.CommonDataKinds.Photo;
35import android.test.suitebuilder.annotation.LargeTest;
36
37import java.io.IOException;
38
39/**
40 * Unit tests for {@link GlobalSearchSupport}.
41 *
42 * Run the test like this:
43 * <code>
44 * adb shell am instrument -w \
45 *         com.android.providers.contacts.tests/android.test.InstrumentationTestRunner
46 * </code>
47 */
48@LargeTest
49public class GlobalSearchSupportTest extends BaseContactsProvider2Test {
50    public void testSearchSuggestionsNotInVisibleGroup() throws Exception {
51        Account account = new Account("actname", "acttype");
52        long rawContactId = createRawContact(account);
53        insertStructuredName(rawContactId, "Deer", "Dough");
54
55        Uri searchUri = new Uri.Builder().scheme("content").authority(ContactsContract.AUTHORITY)
56                .appendPath(SearchManager.SUGGEST_URI_PATH_QUERY).appendPath("D").build();
57
58        // If the contact is not in the "my contacts" group, nothing should be found
59        Cursor c = mResolver.query(searchUri, null, null, null, null);
60        assertEquals(0, c.getCount());
61        c.close();
62    }
63
64    public void testSearchSuggestionsByName() throws Exception {
65        long groupId = createGroup(mAccount, "gsid1", "title1");
66
67        assertSearchSuggestion(groupId,
68                true,  // name
69                true,  // photo
70                false, // organization
71                false, // phone
72                false, // email
73                "D",   // query
74                true,  // expect icon URI
75                null, "Deer Dough", null);
76
77        assertSearchSuggestion(groupId,
78                true,  // name
79                true,  // photo
80                true,  // organization
81                false, // phone
82                false, // email
83                "D",   // query
84                true,  // expect icon URI
85                null, "Deer Dough", "Google");
86
87        assertSearchSuggestion(groupId,
88                true,  // name
89                true,  // photo
90                false, // organization
91                true,  // phone
92                false, // email
93                "D",   // query
94                true,  // expect icon URI
95                null, "Deer Dough", "1-800-4664-411");
96
97        assertSearchSuggestion(groupId,
98                true,  // name
99                true,  // photo
100                false, // organization
101                false, // phone
102                true,  // email
103                "D",   // query
104                true,  // expect icon URI
105                String.valueOf(Presence.getPresenceIconResourceId(Presence.OFFLINE)),
106                "Deer Dough", "foo@acme.com");
107
108        assertSearchSuggestion(groupId,
109                true,  // name
110                false, // photo
111                true,  // organization
112                false, // phone
113                false, // email
114                "D",   // query
115                false, // expect icon URI
116                null, "Deer Dough", "Google");
117    }
118
119    private void assertSearchSuggestion(long groupId, boolean name, boolean photo,
120            boolean organization, boolean phone, boolean email, String query,
121            boolean expectIcon1Uri, String expectedIcon2, String expectedText1, String expectedText2)
122            throws IOException {
123        ContentValues values = new ContentValues();
124
125        long rawContactId = createRawContact();
126        insertGroupMembership(rawContactId, groupId);
127
128        if (name) {
129            insertStructuredName(rawContactId, "Deer", "Dough");
130        }
131
132        final Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);
133        if (photo) {
134            values.clear();
135            byte[] photoData = loadTestPhoto();
136            values.put(Data.RAW_CONTACT_ID, rawContactId);
137            values.put(Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE);
138            values.put(Photo.PHOTO, photoData);
139            mResolver.insert(Data.CONTENT_URI, values);
140        }
141
142        if (organization) {
143            values.clear();
144            values.put(Data.RAW_CONTACT_ID, rawContactId);
145            values.put(Data.MIMETYPE, Organization.CONTENT_ITEM_TYPE);
146            values.put(Organization.TYPE, Organization.TYPE_WORK);
147            values.put(Organization.COMPANY, "Google");
148            mResolver.insert(Data.CONTENT_URI, values);
149        }
150
151        if (email) {
152            values.clear();
153            values.put(Data.RAW_CONTACT_ID, rawContactId);
154            values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
155            values.put(Email.TYPE, Email.TYPE_WORK);
156            values.put(Email.DATA, "foo@acme.com");
157            mResolver.insert(Data.CONTENT_URI, values);
158
159            int protocol = Im.PROTOCOL_GOOGLE_TALK;
160
161            values.clear();
162            values.put(Presence.PROTOCOL, protocol);
163            values.put(Presence.IM_HANDLE, "foo@acme.com");
164            values.put(Presence.IM_ACCOUNT, "foo");
165            values.put(Presence.PRESENCE_STATUS, Presence.OFFLINE);
166            values.put(Presence.PRESENCE_CUSTOM_STATUS, "Coding for Android");
167            mResolver.insert(Presence.CONTENT_URI, values);
168        }
169
170        if (phone) {
171            values.clear();
172            values.put(Data.RAW_CONTACT_ID, rawContactId);
173            values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
174            values.put(Data.IS_PRIMARY, 1);
175            values.put(Phone.TYPE, Phone.TYPE_HOME);
176            values.put(Phone.NUMBER, "1-800-4664-411");
177            mResolver.insert(Data.CONTENT_URI, values);
178        }
179
180        long contactId = queryContactId(rawContactId);
181        Uri searchUri = new Uri.Builder().scheme("content").authority(ContactsContract.AUTHORITY)
182                .appendPath(SearchManager.SUGGEST_URI_PATH_QUERY).appendPath(query).build();
183
184        Cursor c = mResolver.query(searchUri, null, null, null, null);
185        assertEquals(1, c.getCount());
186        c.moveToFirst();
187        values.clear();
188
189        // SearchManager does not declare a constant for _id
190        values.put("_id", contactId);
191        values.put(SearchManager.SUGGEST_COLUMN_TEXT_1, expectedText1);
192        values.put(SearchManager.SUGGEST_COLUMN_TEXT_2, expectedText2);
193
194        String icon1 = c.getString(c.getColumnIndex(SearchManager.SUGGEST_COLUMN_ICON_1));
195        if (expectIcon1Uri) {
196            assertTrue(icon1.startsWith("content:"));
197        } else {
198            assertEquals(String.valueOf(com.android.internal.R.drawable.ic_contact_picture), icon1);
199        }
200
201        values.put(SearchManager.SUGGEST_COLUMN_ICON_2, expectedIcon2);
202        values.put(SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID, contactId);
203        values.put(SearchManager.SUGGEST_COLUMN_SHORTCUT_ID, contactId);
204        assertCursorValues(c, values);
205        c.close();
206
207        // See if the same result is returned by a shortcut refresh
208        Uri shortcutsUri = ContactsContract.AUTHORITY_URI.buildUpon()
209                .appendPath(SearchManager.SUGGEST_URI_PATH_SHORTCUT).build();
210        Uri refreshUri = ContentUris.withAppendedId(shortcutsUri, contactId);
211
212        String[] projection = new String[]{
213                SearchManager.SUGGEST_COLUMN_ICON_1,
214                SearchManager.SUGGEST_COLUMN_ICON_2,
215                SearchManager.SUGGEST_COLUMN_TEXT_1,
216                SearchManager.SUGGEST_COLUMN_TEXT_2,
217                SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID,
218                SearchManager.SUGGEST_COLUMN_SHORTCUT_ID,
219                "_id",
220        };
221
222        c = mResolver.query(refreshUri, projection, null, null, null);
223        try {
224            assertEquals("Record count", 1, c.getCount());
225            c.moveToFirst();
226            assertCursorValues(c, values);
227        } finally {
228            c.close();
229        }
230
231        // Cleanup
232        mResolver.delete(rawContactUri, null, null);
233    }
234
235    public void testSearchSuggestionsByPhoneNumber() throws Exception {
236        ContentValues values = new ContentValues();
237
238        Uri searchUri = new Uri.Builder().scheme("content").authority(ContactsContract.AUTHORITY)
239                .appendPath(SearchManager.SUGGEST_URI_PATH_QUERY).appendPath("12345").build();
240
241        Cursor c = mResolver.query(searchUri, null, null, null, null);
242        DatabaseUtils.dumpCursor(c);
243        assertEquals(2, c.getCount());
244        c.moveToFirst();
245
246        values.put(SearchManager.SUGGEST_COLUMN_TEXT_1, "Dial number");
247        values.put(SearchManager.SUGGEST_COLUMN_TEXT_2, "using 12345");
248        values.put(SearchManager.SUGGEST_COLUMN_ICON_1,
249                String.valueOf(com.android.internal.R.drawable.call_contact));
250        values.put(SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
251                Intents.SEARCH_SUGGESTION_DIAL_NUMBER_CLICKED);
252        values.put(SearchManager.SUGGEST_COLUMN_INTENT_DATA, "tel:12345");
253        values.putNull(SearchManager.SUGGEST_COLUMN_SHORTCUT_ID);
254        assertCursorValues(c, values);
255
256        c.moveToNext();
257        values.clear();
258        values.put(SearchManager.SUGGEST_COLUMN_TEXT_1, "Create contact");
259        values.put(SearchManager.SUGGEST_COLUMN_TEXT_2, "using 12345");
260        values.put(SearchManager.SUGGEST_COLUMN_ICON_1,
261                String.valueOf(com.android.internal.R.drawable.create_contact));
262        values.put(SearchManager.SUGGEST_COLUMN_INTENT_ACTION,
263                Intents.SEARCH_SUGGESTION_CREATE_CONTACT_CLICKED);
264        values.put(SearchManager.SUGGEST_COLUMN_INTENT_DATA, "tel:12345");
265        values.put(SearchManager.SUGGEST_COLUMN_SHORTCUT_ID,
266                SearchManager.SUGGEST_NEVER_MAKE_SHORTCUT);
267        assertCursorValues(c, values);
268        c.close();
269    }
270}
271
272