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