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; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 21 | import android.net.Uri; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 22 | import android.provider.ContactsContract; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 23 | import android.provider.ContactsContract.Contacts; |
| 24 | import android.provider.ContactsContract.Directory; |
| 25 | import android.provider.ContactsContract.Groups; |
| 26 | import android.provider.ContactsContract.ProviderStatus; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 27 | import android.test.ActivityInstrumentationTestCase2; |
| 28 | import android.test.suitebuilder.annotation.SmallTest; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 29 | |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 30 | import com.android.contacts.ContactPhotoManager; |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 31 | import com.android.contacts.ContactsApplication; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 32 | import com.android.contacts.model.AccountTypeManager; |
| 33 | import com.android.contacts.model.account.AccountType; |
| 34 | import com.android.contacts.model.account.AccountWithDataSet; |
| 35 | import com.android.contacts.model.account.BaseAccountType; |
Gary Mai | 0a49afa | 2016-12-05 15:53:58 -0800 | [diff] [blame] | 36 | import com.android.contacts.test.mocks.ContactsMockContext; |
| 37 | import com.android.contacts.test.mocks.MockAccountTypeManager; |
| 38 | import com.android.contacts.test.mocks.MockContactPhotoManager; |
| 39 | import com.android.contacts.test.mocks.MockContentProvider; |
| 40 | import com.android.contacts.test.mocks.MockContentProvider.Query; |
| 41 | import com.android.contacts.test.mocks.MockSharedPreferences; |
| 42 | import com.android.contacts.testing.InjectedServices; |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 43 | |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 44 | /** |
Makoto Onuki | e7d15fb | 2011-08-24 18:20:22 -0700 | [diff] [blame] | 45 | * This test is so outdated that it's disabled temporarily. TODO Update the test and re-enable it. |
| 46 | * |
Katherine Kuan | 9856fce | 2011-06-01 10:24:09 -0700 | [diff] [blame] | 47 | * Tests for {@link PeopleActivity}. |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 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 |
Makoto Onuki | e7d15fb | 2011-08-24 18:20:22 -0700 | [diff] [blame] | 55 | * |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 56 | */ |
Daisuke Miyakawa | 0454960 | 2011-11-23 13:25:10 -0800 | [diff] [blame] | 57 | @SmallTest |
Katherine Kuan | 9856fce | 2011-06-01 10:24:09 -0700 | [diff] [blame] | 58 | public class PeopleActivityTest |
| 59 | extends ActivityInstrumentationTestCase2<PeopleActivity> |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 60 | { |
Dmitri Plotnikov | ab75f82 | 2011-01-10 11:21:59 -0800 | [diff] [blame] | 61 | private static final String TEST_ACCOUNT = "testAccount"; |
| 62 | private static final String TEST_ACCOUNT_TYPE = "testAccountType"; |
| 63 | |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 64 | private ContactsMockContext mContext; |
| 65 | private MockContentProvider mContactsProvider; |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 66 | private MockContentProvider mSettingsProvider; |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 67 | |
Katherine Kuan | 9856fce | 2011-06-01 10:24:09 -0700 | [diff] [blame] | 68 | public PeopleActivityTest() { |
| 69 | super(PeopleActivity.class); |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void setUp() { |
Chiao Cheng | dabac3f | 2012-11-01 17:03:07 -0700 | [diff] [blame] | 74 | mContext = new ContactsMockContext(getInstrumentation().getTargetContext()); |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 75 | mContactsProvider = mContext.getContactsProvider(); |
Yorke Lee | 7039633 | 2014-05-28 12:16:35 -0700 | [diff] [blame] | 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); |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 80 | mSettingsProvider = mContext.getSettingsProvider(); |
| 81 | InjectedServices services = new InjectedServices(); |
| 82 | services.setContentResolver(mContext.getContentResolver()); |
Dmitri Plotnikov | 072d911 | 2011-01-09 15:48:11 -0800 | [diff] [blame] | 83 | services.setSharedPreferences(new MockSharedPreferences()); |
Andrew Lee | f26fd6d | 2015-03-27 15:12:55 -0700 | [diff] [blame] | 84 | ContactPhotoManager.injectContactPhotoManagerForTesting(new MockContactPhotoManager()); |
Daniel Lehmann | 96e87fd | 2011-09-20 21:31:37 -0700 | [diff] [blame] | 85 | AccountType accountType = new BaseAccountType() { |
| 86 | @Override |
| 87 | public boolean areContactsWritable() { |
| 88 | return false; |
| 89 | } |
| 90 | }; |
Dmitri Plotnikov | ab75f82 | 2011-01-10 11:21:59 -0800 | [diff] [blame] | 91 | accountType.accountType = TEST_ACCOUNT_TYPE; |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 92 | |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 93 | AccountWithDataSet account = new AccountWithDataSet(TEST_ACCOUNT, TEST_ACCOUNT_TYPE, null); |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 94 | ContactsApplication.injectServices(services); |
Chiao Cheng | dabac3f | 2012-11-01 17:03:07 -0700 | [diff] [blame] | 95 | |
| 96 | final MockAccountTypeManager mockManager = new MockAccountTypeManager( |
| 97 | new AccountType[] { accountType }, new AccountWithDataSet[] { account }); |
| 98 | AccountTypeManager.setInstanceForTest(mockManager); |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Hugo Hudson | 51ada36 | 2011-08-05 14:36:14 +0100 | [diff] [blame] | 101 | @Override |
| 102 | protected void tearDown() throws Exception { |
| 103 | ContactsApplication.injectServices(null); |
| 104 | super.tearDown(); |
| 105 | } |
| 106 | |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 107 | private void expectProviderStatusQueryAndReturnNormal() { |
| 108 | mContactsProvider |
| 109 | .expectQuery(ProviderStatus.CONTENT_URI) |
Brian Attwell | c3d202c | 2015-01-08 15:01:03 -0800 | [diff] [blame] | 110 | .withProjection(ProviderStatus.STATUS) |
| 111 | .returnRow(ProviderStatus.STATUS_NORMAL) |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 112 | .anyNumberOfTimes(); |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | private void expectGroupsQueryAndReturnEmpty() { |
| 116 | mContactsProvider |
| 117 | .expectQuery(Groups.CONTENT_URI) |
| 118 | .withAnyProjection() |
| 119 | .withAnySelection() |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 120 | .returnEmptyCursor() |
| 121 | .anyNumberOfTimes(); |
| 122 | } |
| 123 | |
Dmitri Plotnikov | ab75f82 | 2011-01-10 11:21:59 -0800 | [diff] [blame] | 124 | private void expectContactListQuery(int count) { |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 125 | Uri uri = Contacts.CONTENT_URI.buildUpon() |
Yorke Lee | 9d2b6d5 | 2014-09-04 14:58:56 -0700 | [diff] [blame] | 126 | .appendQueryParameter(Contacts.EXTRA_ADDRESS_BOOK_INDEX, "true") |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 127 | .appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, |
| 128 | String.valueOf(Directory.DEFAULT)) |
| 129 | .build(); |
| 130 | |
Dmitri Plotnikov | ab75f82 | 2011-01-10 11:21:59 -0800 | [diff] [blame] | 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); |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 165 | mContactsProvider |
| 166 | .expectQuery(uri) |
| 167 | .withAnyProjection() |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 168 | .withAnySortOrder() |
Dmitri Plotnikov | ab75f82 | 2011-01-10 11:21:59 -0800 | [diff] [blame] | 169 | .returnRow(row1) |
| 170 | .anyNumberOfTimes(); |
Dmitri Plotnikov | 32bac64 | 2011-01-06 18:43:46 -0800 | [diff] [blame] | 171 | } |
| 172 | } |