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