PeopleActivityTest.java revision 2b3f3c54d3beb017b2f59f19e9ce0ecc3e039dbc
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    public void testSingleAccountNoGroups() {
106        // This two-pane UI test only makes sense if we run with two panes.
107        // Let's ignore this in the single pane case
108        if (!PhoneCapabilityTester.isUsingTwoPanes(mContext)) return;
109
110        expectSettingsQueriesAndReturnDefault();
111        expectProviderStatusQueryAndReturnNormal();
112        expectGroupsQueryAndReturnEmpty();
113        expectContactListQuery(100);
114        expectContactLookupQuery("lu1", 1, "lu1", 1);
115        expectContactEntityQuery("lu1", 1);
116
117        setActivityIntent(new Intent(Intent.ACTION_DEFAULT));
118
119        PeopleActivity activity = getActivity();
120
121        getInstrumentation().waitForIdleSync();
122
123        ContactBrowseListFragment listFragment = activity.getListFragment();
124        ContactDetailFragment detailFragment = activity.getDetailFragment();
125
126        Loader<?> filterLoader =
127                activity.getLoaderManager().getLoader(R.id.contact_list_filter_loader);
128        Loader<?> listLoader =
129                listFragment.getLoaderManager().getLoader(0);
130
131        // TODO: wait for detail loader
132        // TODO: wait for lookup key loading
133        TestLoaderManager.waitForLoaders(filterLoader, listLoader);
134
135        getInstrumentation().waitForIdleSync();
136
137        mContext.verify();
138
139        TextView nameText = (TextView) detailFragment.getView().findViewById(R.id.name);
140        assertEquals("Contact 1", nameText.getText());
141    }
142
143    private void expectSettingsQueriesAndReturnDefault() {
144        mSettingsProvider
145                .expectQuery(Settings.System.CONTENT_URI)
146                .withProjection(Settings.System.VALUE)
147                .withSelection(Settings.System.NAME + "=?",
148                        ContactsContract.Preferences.DISPLAY_ORDER)
149                .returnRow(ContactsContract.Preferences.DISPLAY_ORDER_PRIMARY)
150                .anyNumberOfTimes();
151        mSettingsProvider
152                .expectQuery(Settings.System.CONTENT_URI)
153                .withProjection(Settings.System.VALUE)
154                .withSelection(Settings.System.NAME + "=?",
155                        ContactsContract.Preferences.SORT_ORDER)
156                .returnRow(ContactsContract.Preferences.SORT_ORDER_PRIMARY)
157                .anyNumberOfTimes();
158    }
159
160    private void expectProviderStatusQueryAndReturnNormal() {
161        mContactsProvider
162                .expectQuery(ProviderStatus.CONTENT_URI)
163                .withProjection(ProviderStatus.STATUS, ProviderStatus.DATA1)
164                .returnRow(ProviderStatus.STATUS_NORMAL, null)
165                .anyNumberOfTimes();
166    }
167
168    private void expectGroupsQueryAndReturnEmpty() {
169        mContactsProvider
170                .expectQuery(Groups.CONTENT_URI)
171                .withAnyProjection()
172                .withAnySelection()
173                .returnEmptyCursor()
174                .anyNumberOfTimes();
175    }
176
177    private void expectContactListQuery(int count) {
178        Uri uri = Contacts.CONTENT_URI.buildUpon()
179                .appendQueryParameter(ContactCounts.ADDRESS_BOOK_INDEX_EXTRAS, "true")
180                .appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY,
181                        String.valueOf(Directory.DEFAULT))
182                .build();
183
184        Query query = mContactsProvider
185                .expectQuery(uri)
186                .withAnyProjection()
187                .withSortOrder(Contacts.SORT_KEY_PRIMARY);
188        for (int i = 1; i <= count; i++) {
189            ContentValues values = new ContentValues();
190            values.put(Contacts._ID, i);
191            values.put(Contacts.DISPLAY_NAME, "Contact " + i);
192            values.put(Contacts.SORT_KEY_PRIMARY, "contact " + i);
193            values.put(Contacts.LOOKUP_KEY, "lu" + i);
194            query.returnRow(values);
195        }
196    }
197
198    private void expectContactLookupQuery(
199            String lookupKey, long id, String returnLookupKey, long returnId) {
200        Uri uri = Contacts.getLookupUri(id, lookupKey);
201        mContactsProvider.expectTypeQuery(uri, Contacts.CONTENT_ITEM_TYPE);
202        mContactsProvider
203                .expectQuery(uri)
204                .withProjection(Contacts._ID, Contacts.LOOKUP_KEY)
205                .returnRow(returnId, returnLookupKey);
206    }
207
208    private void expectContactEntityQuery(String lookupKey, int contactId) {
209        Uri uri = Uri.withAppendedPath(
210                Contacts.getLookupUri(contactId, lookupKey), Contacts.Entity.CONTENT_DIRECTORY);
211        ContentValues row1 = new ContentValues();
212        row1.put(Contacts.Entity.DATA_ID, 1);
213        row1.put(Contacts.Entity.LOOKUP_KEY, lookupKey);
214        row1.put(Contacts.Entity.CONTACT_ID, contactId);
215        row1.put(Contacts.Entity.DISPLAY_NAME, "Contact " + contactId);
216        row1.put(Contacts.Entity.ACCOUNT_NAME, TEST_ACCOUNT);
217        row1.put(Contacts.Entity.ACCOUNT_TYPE, TEST_ACCOUNT_TYPE);
218        mContactsProvider
219                .expectQuery(uri)
220                .withAnyProjection()
221                .withAnySortOrder()
222                .returnRow(row1)
223                .anyNumberOfTimes();
224    }
225}
226