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