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
17package com.android.contacts.activities;
18
19import android.content.ContentUris;
20import android.content.ContentValues;
21import android.net.Uri;
22import android.provider.ContactsContract;
23import android.provider.ContactsContract.Contacts;
24import android.provider.ContactsContract.Directory;
25import android.provider.ContactsContract.Groups;
26import android.provider.ContactsContract.ProviderStatus;
27import android.test.ActivityInstrumentationTestCase2;
28import android.test.suitebuilder.annotation.SmallTest;
29
30import com.android.contacts.ContactPhotoManager;
31import com.android.contacts.ContactsApplication;
32import com.android.contacts.model.AccountTypeManager;
33import com.android.contacts.model.account.AccountType;
34import com.android.contacts.model.account.AccountWithDataSet;
35import com.android.contacts.model.account.BaseAccountType;
36import com.android.contacts.test.mocks.ContactsMockContext;
37import com.android.contacts.test.mocks.MockAccountTypeManager;
38import com.android.contacts.test.mocks.MockContactPhotoManager;
39import com.android.contacts.test.mocks.MockContentProvider;
40import com.android.contacts.test.mocks.MockContentProvider.Query;
41import com.android.contacts.test.mocks.MockSharedPreferences;
42import com.android.contacts.testing.InjectedServices;
43
44/**
45 * This test is so outdated that it's disabled temporarily.  TODO Update the test and re-enable it.
46 *
47 * Tests for {@link PeopleActivity}.
48 *
49 * Running all tests:
50 *
51 *   runtest contacts
52 * or
53 *   adb shell am instrument \
54 *     -w com.android.contacts.tests/android.test.InstrumentationTestRunner
55 *
56 */
57@SmallTest
58public class PeopleActivityTest
59        extends ActivityInstrumentationTestCase2<PeopleActivity>
60{
61    private static final String TEST_ACCOUNT = "testAccount";
62    private static final String TEST_ACCOUNT_TYPE = "testAccountType";
63
64    private ContactsMockContext mContext;
65    private MockContentProvider mContactsProvider;
66    private MockContentProvider mSettingsProvider;
67
68    public PeopleActivityTest() {
69        super(PeopleActivity.class);
70    }
71
72    @Override
73    public void setUp() {
74        mContext = new ContactsMockContext(getInstrumentation().getTargetContext());
75        mContactsProvider = mContext.getContactsProvider();
76        // The ContactsApplication performs this getType query to warm up the provider - see
77        // ContactsApplication#DelayedInitialization.doInBackground
78        mContactsProvider.expectTypeQuery(ContentUris.withAppendedId(Contacts.CONTENT_URI, 1),
79                Contacts.CONTENT_ITEM_TYPE);
80        mSettingsProvider = mContext.getSettingsProvider();
81        InjectedServices services = new InjectedServices();
82        services.setContentResolver(mContext.getContentResolver());
83        services.setSharedPreferences(new MockSharedPreferences());
84        ContactPhotoManager.injectContactPhotoManagerForTesting(new MockContactPhotoManager());
85        AccountType accountType = new BaseAccountType() {
86            @Override
87            public boolean areContactsWritable() {
88                return false;
89            }
90        };
91        accountType.accountType = TEST_ACCOUNT_TYPE;
92
93        AccountWithDataSet account = new AccountWithDataSet(TEST_ACCOUNT, TEST_ACCOUNT_TYPE, null);
94        ContactsApplication.injectServices(services);
95
96        final MockAccountTypeManager mockManager = new MockAccountTypeManager(
97                        new AccountType[] { accountType }, new AccountWithDataSet[] { account });
98        AccountTypeManager.setInstanceForTest(mockManager);
99    }
100
101    @Override
102    protected void tearDown() throws Exception {
103        ContactsApplication.injectServices(null);
104        super.tearDown();
105    }
106
107    private void expectProviderStatusQueryAndReturnNormal() {
108        mContactsProvider
109                .expectQuery(ProviderStatus.CONTENT_URI)
110                .withProjection(ProviderStatus.STATUS)
111                .returnRow(ProviderStatus.STATUS_NORMAL)
112                .anyNumberOfTimes();
113    }
114
115    private void expectGroupsQueryAndReturnEmpty() {
116        mContactsProvider
117                .expectQuery(Groups.CONTENT_URI)
118                .withAnyProjection()
119                .withAnySelection()
120                .returnEmptyCursor()
121                .anyNumberOfTimes();
122    }
123
124    private void expectContactListQuery(int count) {
125        Uri uri = Contacts.CONTENT_URI.buildUpon()
126                .appendQueryParameter(Contacts.EXTRA_ADDRESS_BOOK_INDEX, "true")
127                .appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY,
128                        String.valueOf(Directory.DEFAULT))
129                .build();
130
131        Query query = mContactsProvider
132                .expectQuery(uri)
133                .withAnyProjection()
134                .withSortOrder(Contacts.SORT_KEY_PRIMARY);
135        for (int i = 1; i <= count; i++) {
136            ContentValues values = new ContentValues();
137            values.put(Contacts._ID, i);
138            values.put(Contacts.DISPLAY_NAME, "Contact " + i);
139            values.put(Contacts.SORT_KEY_PRIMARY, "contact " + i);
140            values.put(Contacts.LOOKUP_KEY, "lu" + i);
141            query.returnRow(values);
142        }
143    }
144
145    private void expectContactLookupQuery(
146            String lookupKey, long id, String returnLookupKey, long returnId) {
147        Uri uri = Contacts.getLookupUri(id, lookupKey);
148        mContactsProvider.expectTypeQuery(uri, Contacts.CONTENT_ITEM_TYPE);
149        mContactsProvider
150                .expectQuery(uri)
151                .withProjection(Contacts._ID, Contacts.LOOKUP_KEY)
152                .returnRow(returnId, returnLookupKey);
153    }
154
155    private void expectContactEntityQuery(String lookupKey, int contactId) {
156        Uri uri = Uri.withAppendedPath(
157                Contacts.getLookupUri(contactId, lookupKey), Contacts.Entity.CONTENT_DIRECTORY);
158        ContentValues row1 = new ContentValues();
159        row1.put(Contacts.Entity.DATA_ID, 1);
160        row1.put(Contacts.Entity.LOOKUP_KEY, lookupKey);
161        row1.put(Contacts.Entity.CONTACT_ID, contactId);
162        row1.put(Contacts.Entity.DISPLAY_NAME, "Contact " + contactId);
163        row1.put(Contacts.Entity.ACCOUNT_NAME, TEST_ACCOUNT);
164        row1.put(Contacts.Entity.ACCOUNT_TYPE, TEST_ACCOUNT_TYPE);
165        mContactsProvider
166                .expectQuery(uri)
167                .withAnyProjection()
168                .withAnySortOrder()
169                .returnRow(row1)
170                .anyNumberOfTimes();
171    }
172}
173