Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 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 | |
| 17 | package com.android.contacts.activities; |
| 18 | |
Yorke Lee | 7039633 | 2014-05-28 12:16:35 -0700 | [diff] [blame] | 19 | import android.content.ContentUris; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 20 | import android.content.ContentValues; |
| 21 | import android.content.Intent; |
| 22 | import android.content.Loader; |
| 23 | import android.net.Uri; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 24 | import android.provider.ContactsContract; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 25 | import android.provider.ContactsContract.Contacts; |
| 26 | import android.provider.ContactsContract.Directory; |
| 27 | import android.provider.ContactsContract.Groups; |
| 28 | import android.provider.ContactsContract.ProviderStatus; |
| 29 | import android.provider.Settings; |
| 30 | import android.test.ActivityInstrumentationTestCase2; |
| 31 | import android.test.suitebuilder.annotation.SmallTest; |
| 32 | import android.widget.TextView; |
| 33 | |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 34 | import com.android.contacts.ContactsApplication; |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 35 | import com.android.contacts.R; |
Chiao Cheng | 34793c4 | 2012-10-31 11:10:58 -0700 | [diff] [blame] | 36 | import com.android.contacts.common.ContactPhotoManager; |
Yorke Lee | 7039633 | 2014-05-28 12:16:35 -0700 | [diff] [blame] | 37 | import com.android.contacts.common.testing.InjectedServices; |
Chiao Cheng | dabac3f | 2012-11-01 17:03:07 -0700 | [diff] [blame] | 38 | import com.android.contacts.common.test.mocks.ContactsMockContext; |
| 39 | import com.android.contacts.common.test.mocks.MockContentProvider; |
| 40 | import com.android.contacts.common.test.mocks.MockContentProvider.Query; |
Flavio Lerda | 34ce5e9 | 2011-06-06 15:25:17 +0100 | [diff] [blame] | 41 | import com.android.contacts.interactions.TestLoaderManager; |
Dmitri Plotnikov | ab75f82 | 2011-01-10 11:21:59 -0800 | [diff] [blame] | 42 | import com.android.contacts.list.ContactBrowseListFragment; |
Chiao Cheng | 0d5588d | 2012-11-26 15:34:14 -0800 | [diff] [blame] | 43 | import com.android.contacts.common.model.AccountTypeManager; |
Chiao Cheng | 428f008 | 2012-11-13 18:38:56 -0800 | [diff] [blame] | 44 | import com.android.contacts.common.model.account.AccountType; |
| 45 | import com.android.contacts.common.model.account.AccountWithDataSet; |
| 46 | import com.android.contacts.common.model.account.BaseAccountType; |
Yorke Lee | c9bb217 | 2014-07-10 11:38:34 -0700 | [diff] [blame] | 47 | import com.android.contacts.common.preference.ContactsPreferences; |
Yorke Lee | cd321f6 | 2013-10-28 15:20:15 -0700 | [diff] [blame] | 48 | import com.android.contacts.common.test.mocks.MockAccountTypeManager; |
| 49 | import com.android.contacts.common.test.mocks.MockContactPhotoManager; |
| 50 | import com.android.contacts.common.test.mocks.MockSharedPreferences; |
Daniel Lehmann | 57b4c0d | 2011-05-04 22:34:14 -0700 | [diff] [blame] | 51 | import com.android.contacts.util.PhoneCapabilityTester; |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 52 | |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 53 | /** |
Makoto Onuki | e7d15fb | 2011-08-24 18:20:22 -0700 | [diff] [blame] | 54 | * This test is so outdated that it's disabled temporarily. TODO Update the test and re-enable it. |
| 55 | * |
Katherine Kuan | 9856fce | 2011-06-01 10:24:09 -0700 | [diff] [blame] | 56 | * Tests for {@link PeopleActivity}. |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 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 |
Makoto Onuki | e7d15fb | 2011-08-24 18:20:22 -0700 | [diff] [blame] | 64 | * |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 65 | */ |
Daisuke Miyakawa | 0454960 | 2011-11-23 13:25:10 -0800 | [diff] [blame] | 66 | @SmallTest |
Katherine Kuan | 9856fce | 2011-06-01 10:24:09 -0700 | [diff] [blame] | 67 | public class PeopleActivityTest |
| 68 | extends ActivityInstrumentationTestCase2<PeopleActivity> |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 69 | { |
Dmitri Plotnikov | ab75f82 | 2011-01-10 11:21:59 -0800 | [diff] [blame] | 70 | private static final String TEST_ACCOUNT = "testAccount"; |
| 71 | private static final String TEST_ACCOUNT_TYPE = "testAccountType"; |
| 72 | |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 73 | private ContactsMockContext mContext; |
| 74 | private MockContentProvider mContactsProvider; |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 75 | private MockContentProvider mSettingsProvider; |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 76 | |
Katherine Kuan | 9856fce | 2011-06-01 10:24:09 -0700 | [diff] [blame] | 77 | public PeopleActivityTest() { |
| 78 | super(PeopleActivity.class); |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void setUp() { |
Chiao Cheng | dabac3f | 2012-11-01 17:03:07 -0700 | [diff] [blame] | 83 | mContext = new ContactsMockContext(getInstrumentation().getTargetContext()); |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 84 | mContactsProvider = mContext.getContactsProvider(); |
Yorke Lee | 7039633 | 2014-05-28 12:16:35 -0700 | [diff] [blame] | 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); |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 89 | mSettingsProvider = mContext.getSettingsProvider(); |
| 90 | InjectedServices services = new InjectedServices(); |
| 91 | services.setContentResolver(mContext.getContentResolver()); |
Dmitri Plotnikov | 072d911 | 2011-01-09 15:48:11 -0800 | [diff] [blame] | 92 | services.setSharedPreferences(new MockSharedPreferences()); |
Andrew Lee | f26fd6d | 2015-03-27 15:12:55 -0700 | [diff] [blame] | 93 | ContactPhotoManager.injectContactPhotoManagerForTesting(new MockContactPhotoManager()); |
Daniel Lehmann | 96e87fd | 2011-09-20 21:31:37 -0700 | [diff] [blame] | 94 | AccountType accountType = new BaseAccountType() { |
| 95 | @Override |
| 96 | public boolean areContactsWritable() { |
| 97 | return false; |
| 98 | } |
| 99 | }; |
Dmitri Plotnikov | ab75f82 | 2011-01-10 11:21:59 -0800 | [diff] [blame] | 100 | accountType.accountType = TEST_ACCOUNT_TYPE; |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 101 | |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 102 | AccountWithDataSet account = new AccountWithDataSet(TEST_ACCOUNT, TEST_ACCOUNT_TYPE, null); |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 103 | ContactsApplication.injectServices(services); |
Chiao Cheng | dabac3f | 2012-11-01 17:03:07 -0700 | [diff] [blame] | 104 | |
| 105 | final MockAccountTypeManager mockManager = new MockAccountTypeManager( |
| 106 | new AccountType[] { accountType }, new AccountWithDataSet[] { account }); |
| 107 | AccountTypeManager.setInstanceForTest(mockManager); |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Hugo Hudson | 51ada36 | 2011-08-05 14:36:14 +0100 | [diff] [blame] | 110 | @Override |
| 111 | protected void tearDown() throws Exception { |
| 112 | ContactsApplication.injectServices(null); |
| 113 | super.tearDown(); |
| 114 | } |
| 115 | |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 116 | private void expectProviderStatusQueryAndReturnNormal() { |
| 117 | mContactsProvider |
| 118 | .expectQuery(ProviderStatus.CONTENT_URI) |
Brian Attwell | c3d202c | 2015-01-08 15:01:03 -0800 | [diff] [blame] | 119 | .withProjection(ProviderStatus.STATUS) |
| 120 | .returnRow(ProviderStatus.STATUS_NORMAL) |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 121 | .anyNumberOfTimes(); |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | private void expectGroupsQueryAndReturnEmpty() { |
| 125 | mContactsProvider |
| 126 | .expectQuery(Groups.CONTENT_URI) |
| 127 | .withAnyProjection() |
| 128 | .withAnySelection() |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 129 | .returnEmptyCursor() |
| 130 | .anyNumberOfTimes(); |
| 131 | } |
| 132 | |
Dmitri Plotnikov | ab75f82 | 2011-01-10 11:21:59 -0800 | [diff] [blame] | 133 | private void expectContactListQuery(int count) { |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 134 | Uri uri = Contacts.CONTENT_URI.buildUpon() |
Yorke Lee | 9d2b6d5 | 2014-09-04 14:58:56 -0700 | [diff] [blame] | 135 | .appendQueryParameter(Contacts.EXTRA_ADDRESS_BOOK_INDEX, "true") |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 136 | .appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, |
| 137 | String.valueOf(Directory.DEFAULT)) |
| 138 | .build(); |
| 139 | |
Dmitri Plotnikov | ab75f82 | 2011-01-10 11:21:59 -0800 | [diff] [blame] | 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); |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 174 | mContactsProvider |
| 175 | .expectQuery(uri) |
| 176 | .withAnyProjection() |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 177 | .withAnySortOrder() |
Dmitri Plotnikov | ab75f82 | 2011-01-10 11:21:59 -0800 | [diff] [blame] | 178 | .returnRow(row1) |
| 179 | .anyNumberOfTimes(); |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 180 | } |
| 181 | } |