1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 package android.pim.vcard; 17 18 import android.content.ContentResolver; 19 import android.content.ContentValues; 20 import android.content.Entity; 21 import android.content.EntityIterator; 22 import android.database.Cursor; 23 import android.net.Uri; 24 import android.provider.ContactsContract.Contacts; 25 import android.provider.ContactsContract.Data; 26 import android.provider.ContactsContract.RawContacts; 27 import android.test.mock.MockContentResolver; 28 import android.test.mock.MockCursor; 29 30 import junit.framework.TestCase; 31 32 import java.util.ArrayList; 33 import java.util.Iterator; 34 import java.util.List; 35 36 /* package */ public class ExportTestResolver extends MockContentResolver { 37 ExportTestProvider mProvider; 38 public ExportTestResolver(TestCase testCase) { 39 mProvider = new ExportTestProvider(testCase); 40 addProvider(VCardComposer.VCARD_TEST_AUTHORITY, mProvider); 41 addProvider(RawContacts.CONTENT_URI.getAuthority(), mProvider); 42 } 43 44 public ContactEntry addInputContactEntry() { 45 return mProvider.buildInputEntry(); 46 } 47 } 48 49 /* package */ class MockEntityIterator implements EntityIterator { 50 List<Entity> mEntityList; 51 Iterator<Entity> mIterator; 52 53 public MockEntityIterator(List<ContentValues> contentValuesList) { 54 mEntityList = new ArrayList<Entity>(); 55 Entity entity = new Entity(new ContentValues()); 56 for (ContentValues contentValues : contentValuesList) { 57 entity.addSubValue(Data.CONTENT_URI, contentValues); 58 } 59 mEntityList.add(entity); 60 mIterator = mEntityList.iterator(); 61 } 62 63 public boolean hasNext() { 64 return mIterator.hasNext(); 65 } 66 67 public Entity next() { 68 return mIterator.next(); 69 } 70 71 public void remove() { 72 throw new UnsupportedOperationException("remove not supported"); 73 } 74 75 public void reset() { 76 mIterator = mEntityList.iterator(); 77 } 78 79 public void close() { 80 } 81 } 82 83 /** 84 * Represents one contact, which should contain multiple ContentValues like 85 * StructuredName, Email, etc. 86 */ 87 /* package */ class ContactEntry { 88 private final List<ContentValues> mContentValuesList = new ArrayList<ContentValues>(); 89 90 public ContentValuesBuilder addContentValues(String mimeType) { 91 ContentValues contentValues = new ContentValues(); 92 contentValues.put(Data.MIMETYPE, mimeType); 93 mContentValuesList.add(contentValues); 94 return new ContentValuesBuilder(contentValues); 95 } 96 97 public List<ContentValues> getList() { 98 return mContentValuesList; 99 } 100 } 101 102 /* package */ class ExportTestProvider extends MockContentProvider { 103 final private TestCase mTestCase; 104 final private ArrayList<ContactEntry> mContactEntryList = new ArrayList<ContactEntry>(); 105 106 public ExportTestProvider(TestCase testCase) { 107 mTestCase = testCase; 108 } 109 110 public ContactEntry buildInputEntry() { 111 ContactEntry contactEntry = new ContactEntry(); 112 mContactEntryList.add(contactEntry); 113 return contactEntry; 114 } 115 116 /** 117 * <p> 118 * An old method which had existed but was removed from ContentResolver. 119 * </p> 120 * <p> 121 * We still keep using this method since we don't have a propeer way to know 122 * which value in the ContentValue corresponds to the entry in Contacts database. 123 * </p> 124 * <p> 125 * Detail: 126 * There's an easy way to know which index "family name" corresponds to, via 127 * {@link android.provider.ContactsContract}. 128 * FAMILY_NAME equals DATA3, so the corresponding index 129 * for "family name" should be 2 (note that index is 0-origin). 130 * However, we cannot know what the index 2 corresponds to; it may be "family name", 131 * "label" for now, but may be the other some column in the future. We don't have 132 * convenient way to know the original data structure. 133 * </p> 134 */ 135 public EntityIterator queryEntities(Uri uri, 136 String selection, String[] selectionArgs, String sortOrder) { 137 mTestCase.assertTrue(uri != null); 138 mTestCase.assertTrue(ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())); 139 final String authority = uri.getAuthority(); 140 mTestCase.assertTrue(RawContacts.CONTENT_URI.getAuthority().equals(authority)); 141 mTestCase.assertTrue((Data.CONTACT_ID + "=?").equals(selection)); 142 mTestCase.assertEquals(1, selectionArgs.length); 143 final int id = Integer.parseInt(selectionArgs[0]); 144 mTestCase.assertTrue(id >= 0 && id < mContactEntryList.size()); 145 146 return new MockEntityIterator(mContactEntryList.get(id).getList()); 147 } 148 149 @Override 150 public Cursor query(Uri uri,String[] projection, 151 String selection, String[] selectionArgs, String sortOrder) { 152 mTestCase.assertTrue(VCardComposer.CONTACTS_TEST_CONTENT_URI.equals(uri)); 153 // In this test, following arguments are not supported. 154 mTestCase.assertNull(selection); 155 mTestCase.assertNull(selectionArgs); 156 mTestCase.assertNull(sortOrder); 157 158 return new MockCursor() { 159 int mCurrentPosition = -1; 160 161 @Override 162 public int getCount() { 163 return mContactEntryList.size(); 164 } 165 166 @Override 167 public boolean moveToFirst() { 168 mCurrentPosition = 0; 169 return true; 170 } 171 172 @Override 173 public boolean moveToNext() { 174 if (mCurrentPosition < mContactEntryList.size()) { 175 mCurrentPosition++; 176 return true; 177 } else { 178 return false; 179 } 180 } 181 182 @Override 183 public boolean isBeforeFirst() { 184 return mCurrentPosition < 0; 185 } 186 187 @Override 188 public boolean isAfterLast() { 189 return mCurrentPosition >= mContactEntryList.size(); 190 } 191 192 @Override 193 public int getColumnIndex(String columnName) { 194 mTestCase.assertEquals(Contacts._ID, columnName); 195 return 0; 196 } 197 198 @Override 199 public int getInt(int columnIndex) { 200 mTestCase.assertEquals(0, columnIndex); 201 mTestCase.assertTrue(mCurrentPosition >= 0 202 && mCurrentPosition < mContactEntryList.size()); 203 return mCurrentPosition; 204 } 205 206 @Override 207 public String getString(int columnIndex) { 208 return String.valueOf(getInt(columnIndex)); 209 } 210 211 @Override 212 public void close() { 213 } 214 }; 215 } 216 } 217