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 */ 16 17 package com.android.providers.contacts; 18 19 import static com.android.providers.contacts.ContactsActor.PACKAGE_GREY; 20 21 import android.accounts.Account; 22 import android.content.ContentUris; 23 import android.content.ContentValues; 24 import android.database.Cursor; 25 import android.net.Uri; 26 import android.provider.ContactsContract; 27 import android.provider.ContactsContract.CommonDataKinds.GroupMembership; 28 import android.provider.ContactsContract.Contacts; 29 import android.provider.ContactsContract.Data; 30 import android.provider.ContactsContract.Directory; 31 import android.test.suitebuilder.annotation.MediumTest; 32 33 import com.android.providers.contacts.testutil.RawContactUtil; 34 35 36 /** 37 * Unit tests for {@link ContactsProvider2}, directory functionality. 38 * 39 * Run the test like this: 40 * <code> 41 * adb shell am instrument -e class com.android.providers.contacts.DirectoryTest -w \ 42 * com.android.providers.contacts.tests/android.test.InstrumentationTestRunner 43 * </code> 44 */ 45 @MediumTest 46 public class DirectoryTest extends BaseContactsProvider2Test { 47 48 protected String getContextPackageName() { 49 return getContext().getPackageName(); 50 } 51 52 public void testDefaultDirectory() { 53 ContentValues values = new ContentValues(); 54 Uri defaultDirectoryUri = 55 ContentUris.withAppendedId(Directory.CONTENT_URI, Directory.DEFAULT); 56 57 values.put(Directory.PACKAGE_NAME, getContext().getPackageName()); 58 values.put(Directory.DIRECTORY_AUTHORITY, ContactsContract.AUTHORITY); 59 values.put(Directory.TYPE_RESOURCE_ID, R.string.default_directory); 60 values.put(Directory.EXPORT_SUPPORT, Directory.EXPORT_SUPPORT_NONE); 61 values.putNull(Directory.ACCOUNT_NAME); 62 values.putNull(Directory.ACCOUNT_TYPE); 63 values.putNull(Directory.DISPLAY_NAME); 64 65 assertStoredValues(defaultDirectoryUri, values); 66 } 67 68 public void testInvisibleLocalDirectory() { 69 ContentValues values = new ContentValues(); 70 Uri defaultDirectoryUri = 71 ContentUris.withAppendedId(Directory.CONTENT_URI, Directory.LOCAL_INVISIBLE); 72 73 values.put(Directory.PACKAGE_NAME, getContext().getPackageName()); 74 values.put(Directory.DIRECTORY_AUTHORITY, ContactsContract.AUTHORITY); 75 values.put(Directory.TYPE_RESOURCE_ID, R.string.local_invisible_directory); 76 values.put(Directory.EXPORT_SUPPORT, Directory.EXPORT_SUPPORT_NONE); 77 values.putNull(Directory.ACCOUNT_NAME); 78 values.putNull(Directory.ACCOUNT_TYPE); 79 values.putNull(Directory.DISPLAY_NAME); 80 81 assertStoredValues(defaultDirectoryUri, values); 82 } 83 84 public void testForwardingToLocalContacts() { 85 long contactId = queryContactId(RawContactUtil.createRawContactWithName(mResolver, "John", 86 "Doe")); 87 88 Uri contentUri = Contacts.CONTENT_URI.buildUpon().appendQueryParameter( 89 ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(Directory.DEFAULT)).build(); 90 91 Cursor cursor = mResolver.query(contentUri, 92 new String[]{Contacts._ID, Contacts.DISPLAY_NAME}, null, null, null); 93 assertNotNull(cursor); 94 assertEquals(1, cursor.getCount()); 95 cursor.moveToFirst(); 96 assertEquals(contactId, cursor.getLong(0)); 97 assertEquals("John Doe", cursor.getString(1)); 98 cursor.close(); 99 } 100 101 public void testForwardingToLocalInvisibleContacts() { 102 103 // Visible because there is no account 104 long contactId1 = queryContactId(RawContactUtil.createRawContactWithName(mResolver, "Bob", 105 "Parr")); 106 107 Account account = new Account("accountName", "accountType"); 108 long groupId = createGroup(account, "sid", "def", 109 0 /* visible */, true /* auto-add */, false /* fav */); 110 long contactId2 = queryContactId(RawContactUtil.createRawContactWithName(mResolver, "Helen", 111 "Parr", account)); 112 113 Uri contentUri = Contacts.CONTENT_URI.buildUpon().appendQueryParameter( 114 ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(Directory.LOCAL_INVISIBLE)) 115 .build(); 116 117 Cursor cursor = mResolver.query(contentUri, 118 new String[]{Contacts._ID, Contacts.DISPLAY_NAME}, null, null, null); 119 assertEquals(0, cursor.getCount()); 120 cursor.close(); 121 122 // Hide by removing from the default group 123 mResolver.delete(Data.CONTENT_URI, 124 Data.MIMETYPE + "=? AND " + GroupMembership.GROUP_ROW_ID + "=?", 125 new String[] { GroupMembership.CONTENT_ITEM_TYPE, String.valueOf(groupId) }); 126 127 cursor = mResolver.query(contentUri, 128 new String[]{Contacts._ID, Contacts.DISPLAY_NAME}, null, null, null); 129 assertNotNull(cursor); 130 assertEquals(1, cursor.getCount()); 131 cursor.moveToFirst(); 132 assertEquals(contactId2, cursor.getLong(0)); 133 assertEquals("Helen Parr", cursor.getString(1)); 134 cursor.close(); 135 } 136 } 137 138