PeopleActivityTest.java revision 51ada3680de8700bc273a7e652886823f4c1981f
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 com.android.contacts.ContactPhotoManager;
20import com.android.contacts.ContactsApplication;
21import com.android.contacts.R;
22import com.android.contacts.detail.ContactDetailFragment;
23import com.android.contacts.interactions.TestLoaderManager;
24import com.android.contacts.list.ContactBrowseListFragment;
25import com.android.contacts.model.AccountType;
26import com.android.contacts.model.AccountTypeManager;
27import com.android.contacts.model.AccountWithDataSet;
28import com.android.contacts.model.BaseAccountType;
29import com.android.contacts.test.InjectedServices;
30import com.android.contacts.tests.mocks.ContactsMockContext;
31import com.android.contacts.tests.mocks.MockAccountTypeManager;
32import com.android.contacts.tests.mocks.MockContactPhotoManager;
33import com.android.contacts.tests.mocks.MockContentProvider;
34import com.android.contacts.tests.mocks.MockContentProvider.Query;
35import com.android.contacts.tests.mocks.MockSharedPreferences;
36import com.android.contacts.util.PhoneCapabilityTester;
37
38import android.content.ContentValues;
39import android.content.Intent;
40import android.content.Loader;
41import android.net.Uri;
42import android.os.AsyncTask;
43import android.provider.ContactsContract;
44import android.provider.ContactsContract.ContactCounts;
45import android.provider.ContactsContract.Contacts;
46import android.provider.ContactsContract.Directory;
47import android.provider.ContactsContract.Groups;
48import android.provider.ContactsContract.ProviderStatus;
49import android.provider.Settings;
50import android.test.ActivityInstrumentationTestCase2;
51import android.test.suitebuilder.annotation.Smoke;
52import android.widget.TextView;
53
54/**
55 * Tests for {@link PeopleActivity}.
56 *
57 * Running all tests:
58 *
59 *   runtest contacts
60 * or
61 *   adb shell am instrument \
62 *     -w com.android.contacts.tests/android.test.InstrumentationTestRunner
63 */
64@Smoke
65public class PeopleActivityTest
66        extends ActivityInstrumentationTestCase2<PeopleActivity>
67{
68    static {
69        // AsyncTask class needs to be initialized on the main thread.
70        AsyncTask.init();
71    }
72
73    private static final String TEST_ACCOUNT = "testAccount";
74    private static final String TEST_ACCOUNT_TYPE = "testAccountType";
75
76    private ContactsMockContext mContext;
77    private MockContentProvider mContactsProvider;
78    private MockContentProvider mSettingsProvider;
79
80    public PeopleActivityTest() {
81        super(PeopleActivity.class);
82    }
83
84    @Override
85    public void setUp() {
86        mContext = new ContactsMockContext(getInstrumentation().getTargetContext());
87        mContactsProvider = mContext.getContactsProvider();
88        mSettingsProvider = mContext.getSettingsProvider();
89        InjectedServices services = new InjectedServices();
90        services.setContentResolver(mContext.getContentResolver());
91        services.setSharedPreferences(new MockSharedPreferences());
92        services.setSystemService(ContactPhotoManager.CONTACT_PHOTO_SERVICE,
93                new MockContactPhotoManager());
94        AccountType accountType = new BaseAccountType();
95        accountType.accountType = TEST_ACCOUNT_TYPE;
96
97        AccountWithDataSet account = new AccountWithDataSet(TEST_ACCOUNT, TEST_ACCOUNT_TYPE, null);
98
99        services.setSystemService(AccountTypeManager.ACCOUNT_TYPE_SERVICE,
100                new MockAccountTypeManager(
101                        new AccountType[] { accountType }, new AccountWithDataSet[] { account }));
102        ContactsApplication.injectServices(services);
103    }
104
105    @Override
106    protected void tearDown() throws Exception {
107        ContactsApplication.injectServices(null);
108        super.tearDown();
109    }
110
111    public void testSingleAccountNoGroups() {
112        // This two-pane UI test only makes sense if we run with two panes.
113        // Let's ignore this in the single pane case
114        if (!PhoneCapabilityTester.isUsingTwoPanes(mContext)) return;
115
116        expectSettingsQueriesAndReturnDefault();
117        expectProviderStatusQueryAndReturnNormal();
118        expectGroupsQueryAndReturnEmpty();
119        expectContactListQuery(100);
120        expectContactLookupQuery("lu1", 1, "lu1", 1);
121        expectContactEntityQuery("lu1", 1);
122
123        setActivityIntent(new Intent(Intent.ACTION_DEFAULT));
124
125        PeopleActivity activity = getActivity();
126
127        getInstrumentation().waitForIdleSync();
128
129        ContactBrowseListFragment listFragment = activity.getListFragment();
130        ContactDetailFragment detailFragment = activity.getDetailFragment();
131
132        Loader<?> filterLoader =
133                activity.getLoaderManager().getLoader(R.id.contact_list_filter_loader);
134        Loader<?> listLoader =
135                listFragment.getLoaderManager().getLoader(0);
136
137        // TODO: wait for detail loader
138        // TODO: wait for lookup key loading
139        TestLoaderManager.waitForLoaders(filterLoader, listLoader);
140
141        getInstrumentation().waitForIdleSync();
142
143        mContext.verify();
144
145        TextView nameText = (TextView) detailFragment.getView().findViewById(R.id.name);
146        assertEquals("Contact 1", nameText.getText());
147    }
148
149    private void expectSettingsQueriesAndReturnDefault() {
150        mSettingsProvider
151                .expectQuery(Settings.System.CONTENT_URI)
152                .withProjection(Settings.System.VALUE)
153                .withSelection(Settings.System.NAME + "=?",
154                        ContactsContract.Preferences.DISPLAY_ORDER)
155                .returnRow(ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY)
156                .anyNumberOfTimes();
157        mSettingsProvider
158                .expectQuery(Settings.System.CONTENT_URI)
159                .withProjection(Settings.System.VALUE)
160                .withSelection(Settings.System.NAME + "=?",
161                        ContactsContract.Preferences.SORT_ORDER)
162                .returnRow(ContactsContract.Preferences.SORT_ORDER_PRIMARY)
163                .anyNumberOfTimes();
164    }
165
166    private void expectProviderStatusQueryAndReturnNormal() {
167        mContactsProvider
168                .expectQuery(ProviderStatus.CONTENT_URI)
169                .withProjection(ProviderStatus.STATUS, ProviderStatus.DATA1)
170                .returnRow(ProviderStatus.STATUS_NORMAL, null)
171                .anyNumberOfTimes();
172    }
173
174    private void expectGroupsQueryAndReturnEmpty() {
175        mContactsProvider
176                .expectQuery(Groups.CONTENT_URI)
177                .withAnyProjection()
178                .withAnySelection()
179                .returnEmptyCursor()
180                .anyNumberOfTimes();
181    }
182
183    private void expectContactListQuery(int count) {
184        Uri uri = Contacts.CONTENT_URI.buildUpon()
185                .appendQueryParameter(ContactCounts.ADDRESS_BOOK_INDEX_EXTRAS, "true")
186                .appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY,
187                        String.valueOf(Directory.DEFAULT))
188                .build();
189
190        Query query = mContactsProvider
191                .expectQuery(uri)
192                .withAnyProjection()
193                .withSortOrder(Contacts.SORT_KEY_PRIMARY);
194        for (int i = 1; i <= count; i++) {
195            ContentValues values = new ContentValues();
196            values.put(Contacts._ID, i);
197            values.put(Contacts.DISPLAY_NAME, "Contact " + i);
198            values.put(Contacts.SORT_KEY_PRIMARY, "contact " + i);
199            values.put(Contacts.LOOKUP_KEY, "lu" + i);
200            query.returnRow(values);
201        }
202    }
203
204    private void expectContactLookupQuery(
205            String lookupKey, long id, String returnLookupKey, long returnId) {
206        Uri uri = Contacts.getLookupUri(id, lookupKey);
207        mContactsProvider.expectTypeQuery(uri, Contacts.CONTENT_ITEM_TYPE);
208        mContactsProvider
209                .expectQuery(uri)
210                .withProjection(Contacts._ID, Contacts.LOOKUP_KEY)
211                .returnRow(returnId, returnLookupKey);
212    }
213
214    private void expectContactEntityQuery(String lookupKey, int contactId) {
215        Uri uri = Uri.withAppendedPath(
216                Contacts.getLookupUri(contactId, lookupKey), Contacts.Entity.CONTENT_DIRECTORY);
217        ContentValues row1 = new ContentValues();
218        row1.put(Contacts.Entity.DATA_ID, 1);
219        row1.put(Contacts.Entity.LOOKUP_KEY, lookupKey);
220        row1.put(Contacts.Entity.CONTACT_ID, contactId);
221        row1.put(Contacts.Entity.DISPLAY_NAME, "Contact " + contactId);
222        row1.put(Contacts.Entity.ACCOUNT_NAME, TEST_ACCOUNT);
223        row1.put(Contacts.Entity.ACCOUNT_TYPE, TEST_ACCOUNT_TYPE);
224        mContactsProvider
225                .expectQuery(uri)
226                .withAnyProjection()
227                .withAnySortOrder()
228                .returnRow(row1)
229                .anyNumberOfTimes();
230    }
231}
232