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