diff options
author | Bjorn Bringert <bringert@android.com> | 2010-04-14 14:25:09 +0100 |
---|---|---|
committer | Bjorn Bringert <bringert@android.com> | 2010-08-18 15:40:06 +0100 |
commit | ac13ddd04d665442de846b59234bdc936a6699b4 (patch) | |
tree | d8433dd2827bd52189d29199267d24116f6c1e52 /tests | |
parent | 53fac8f99f3884c372c907a76766d27fa9e1d95f (diff) | |
download | packages_providers_ContactsProvider-ac13ddd04d665442de846b59234bdc936a6699b4.zip packages_providers_ContactsProvider-ac13ddd04d665442de846b59234bdc936a6699b4.tar.gz packages_providers_ContactsProvider-ac13ddd04d665442de846b59234bdc936a6699b4.tar.bz2 |
Use new public API for memory-backed file descriptors
Bug http://b/issue?id=2595601
Change-Id: Ia274e4c5d7098054bec189e28f2172fb864e6e8c
Diffstat (limited to 'tests')
-rw-r--r-- | tests/src/com/android/providers/contacts/ContactsProvider2Test.java | 22 | ||||
-rw-r--r-- | tests/src/com/android/providers/contacts/RestrictionExceptionsTest.java | 21 |
2 files changed, 35 insertions, 8 deletions
diff --git a/tests/src/com/android/providers/contacts/ContactsProvider2Test.java b/tests/src/com/android/providers/contacts/ContactsProvider2Test.java index eea7157..ec8522e 100644 --- a/tests/src/com/android/providers/contacts/ContactsProvider2Test.java +++ b/tests/src/com/android/providers/contacts/ContactsProvider2Test.java @@ -63,6 +63,7 @@ import android.test.suitebuilder.annotation.LargeTest; import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import java.text.Collator; import java.util.Arrays; import java.util.Locale; @@ -2707,17 +2708,28 @@ public class ContactsProvider2Test extends BaseContactsProvider2Test { assertEquals(ContentUris.parseId(photoUri), twigId); } - public void testOpenAssertFileDescriptorForPhoto() throws Exception { + public void testInputStreamForPhoto() throws Exception { long rawContactId = createRawContact(); Uri photoUri = insertPhoto(rawContactId); - AssetFileDescriptor fd = mResolver.openAssetFileDescriptor(photoUri, "r"); - assertEquals(loadTestPhoto().length, fd.getLength()); + assertInputStreamContent(loadTestPhoto(), mResolver.openInputStream(photoUri)); Uri contactPhotoUri = Uri.withAppendedPath( ContentUris.withAppendedId(Contacts.CONTENT_URI, queryContactId(rawContactId)), Contacts.Photo.CONTENT_DIRECTORY); - fd = mResolver.openAssetFileDescriptor(contactPhotoUri, "r"); - assertEquals(loadTestPhoto().length, fd.getLength()); + assertInputStreamContent(loadTestPhoto(), mResolver.openInputStream(contactPhotoUri)); + } + + private static void assertInputStreamContent(byte[] expected, InputStream is) + throws IOException { + try { + byte[] observed = new byte[expected.length]; + int count = is.read(observed); + assertEquals(expected.length, count); + assertEquals(-1, is.read()); + MoreAsserts.assertEquals(expected, observed); + } finally { + is.close(); + } } public void testSuperPrimaryPhoto() { diff --git a/tests/src/com/android/providers/contacts/RestrictionExceptionsTest.java b/tests/src/com/android/providers/contacts/RestrictionExceptionsTest.java index 6209e54..3fbf548 100644 --- a/tests/src/com/android/providers/contacts/RestrictionExceptionsTest.java +++ b/tests/src/com/android/providers/contacts/RestrictionExceptionsTest.java @@ -18,6 +18,7 @@ package com.android.providers.contacts; import static com.android.providers.contacts.ContactsActor.PACKAGE_GREY; import static com.android.providers.contacts.ContactsActor.PACKAGE_RED; + import android.content.ContentUris; import android.content.ContentValues; import android.content.Context; @@ -35,6 +36,8 @@ import android.provider.ContactsContract.CommonDataKinds.Email; import android.test.AndroidTestCase; import android.test.suitebuilder.annotation.LargeTest; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.InputStream; /** @@ -190,9 +193,7 @@ public class RestrictionExceptionsTest extends AndroidTestCase { final Uri shareUri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookupKey); final AssetFileDescriptor file = mRed.resolver.openAssetFileDescriptor(shareUri, "r"); final InputStream in = file.createInputStream(); - final byte[] buf = new byte[in.available()]; - in.read(buf); - in.close(); + final byte[] buf = readInputStream(in); final String card = new String(buf); assertNotSame(0, card.length()); @@ -201,6 +202,20 @@ public class RestrictionExceptionsTest extends AndroidTestCase { assertTrue(card.indexOf(PHONE_GREY) == -1); } + private static byte[] readInputStream(InputStream in) throws IOException { + try { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + byte[] buf = new byte[4096]; + int count; + while ((count = in.read(buf)) != -1) { + out.write(buf, 0, count); + } + return out.toByteArray(); + } finally { + in.close(); + } + } + public void testContactsLiveFolder() { final long greyContact = mGrey.createContact(true, GENERIC_NAME); final long greyPhone = mGrey.createPhone(greyContact, PHONE_GREY); |