Some Tests for ContactsContract_StreamItemPhotos
Bug 5810254
Exercise the 2 insertion examples on:
http://developer.android.com/reference/android/provider/ContactsContract.StreamItemPhotos.html
Change-Id: I790757741bf8309b02fa24eb5a6092cba4fe3d59
diff --git a/tests/src/android/provider/cts/FileCopyHelper.java b/tests/src/android/provider/cts/FileCopyHelper.java
index 114c3ad..88ed648 100644
--- a/tests/src/android/provider/cts/FileCopyHelper.java
+++ b/tests/src/android/provider/cts/FileCopyHelper.java
@@ -18,8 +18,8 @@
import android.content.Context;
+import java.io.ByteArrayOutputStream;
import java.io.File;
-import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -112,4 +112,20 @@
output.close();
}
}
+
+ public static byte[] readInputStreamFully(InputStream is) {
+ ByteArrayOutputStream os = new ByteArrayOutputStream();
+ byte[] buffer = new byte[10000];
+ int count;
+ try {
+ while ((count = is.read(buffer)) != -1) {
+ os.write(buffer, 0, count);
+ }
+ is.close();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ return os.toByteArray();
+ }
+
}
diff --git a/tests/src/android/provider/cts/PhotoUtil.java b/tests/src/android/provider/cts/PhotoUtil.java
new file mode 100644
index 0000000..e4f03e0
--- /dev/null
+++ b/tests/src/android/provider/cts/PhotoUtil.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.provider.cts;
+
+import com.android.cts.stub.R;
+
+import android.content.Context;
+
+import java.io.InputStream;
+
+public class PhotoUtil {
+
+ public static byte[] getTestPhotoData(Context context) {
+ InputStream input = context.getResources().openRawResource(R.drawable.testimage);
+ return FileCopyHelper.readInputStreamFully(input);
+ }
+}
diff --git a/tests/tests/provider/src/android/provider/cts/ContactsContract_PhotoTest.java b/tests/tests/provider/src/android/provider/cts/ContactsContract_PhotoTest.java
index edab42f..b41cbb2 100644
--- a/tests/tests/provider/src/android/provider/cts/ContactsContract_PhotoTest.java
+++ b/tests/tests/provider/src/android/provider/cts/ContactsContract_PhotoTest.java
@@ -16,9 +16,8 @@
package android.provider.cts;
-import com.android.cts.stub.R;
-
import android.content.ContentResolver;
+import android.content.Context;
import android.content.IContentProvider;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Photo;
@@ -28,7 +27,6 @@
import android.provider.cts.ContactsContract_TestDataBuilder.TestRawContact;
import android.test.InstrumentationTestCase;
-import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -37,12 +35,16 @@
private static final byte[] EMPTY_TEST_PHOTO_DATA = "".getBytes();
+ private Context mContext;
+
private ContentResolver mResolver;
@Override
protected void setUp() throws Exception {
super.setUp();
- mResolver = getInstrumentation().getTargetContext().getContentResolver();
+
+ mContext= getInstrumentation().getTargetContext();
+ mResolver = mContext.getContentResolver();
IContentProvider provider = mResolver.acquireProvider(ContactsContract.AUTHORITY);
mBuilder = new ContactsContract_TestDataBuilder(provider);
}
@@ -62,7 +64,7 @@
assertNull(Contacts.openContactPhotoInputStream(mResolver, contact.getUri(), false));
TestData photoData = rawContact.newDataRow(Photo.CONTENT_ITEM_TYPE)
- .with(Photo.PHOTO, getTestPhotoData())
+ .with(Photo.PHOTO, PhotoUtil.getTestPhotoData(mContext))
.insert();
photoData.load();
@@ -92,31 +94,10 @@
assertNull(Contacts.openContactPhotoInputStream(mResolver, contact.getUri(), false));
}
- private byte[] getTestPhotoData() {
- InputStream input = getInstrumentation().getTargetContext().getResources()
- .openRawResource(R.drawable.testimage);
- return readInputStreamFully(input);
- }
-
- protected byte[] readInputStreamFully(InputStream is) {
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- byte[] buffer = new byte[10000];
- int count;
- try {
- while ((count = is.read(buffer)) != -1) {
- os.write(buffer, 0, count);
- }
- is.close();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- return os.toByteArray();
- }
-
private void assertPhotoStream(InputStream photoStream) throws IOException {
try {
assertNotNull(photoStream);
- byte[] actualBytes = readInputStreamFully(photoStream);
+ byte[] actualBytes = FileCopyHelper.readInputStreamFully(photoStream);
assertTrue(actualBytes.length > 0);
} finally {
if (photoStream != null) {
diff --git a/tests/tests/provider/src/android/provider/cts/ContactsContract_StreamItemPhotosTest.java b/tests/tests/provider/src/android/provider/cts/ContactsContract_StreamItemPhotosTest.java
new file mode 100644
index 0000000..d45e824
--- /dev/null
+++ b/tests/tests/provider/src/android/provider/cts/ContactsContract_StreamItemPhotosTest.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.provider.cts;
+
+import android.content.ContentResolver;
+import android.content.ContentUris;
+import android.content.ContentValues;
+import android.net.Uri;
+import android.provider.ContactsContract.StreamItemPhotos;
+import android.provider.ContactsContract.StreamItems;
+import android.test.AndroidTestCase;
+
+public class ContactsContract_StreamItemPhotosTest extends AndroidTestCase {
+
+ private ContentResolver mResolver;
+
+ private Uri mStreamItemUri;
+
+ private long mStreamItemId;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ mResolver = mContext.getContentResolver();
+
+ long rawContactId = ContactsContract_StreamItemsTest.insertRawContact(mResolver);
+ mStreamItemUri = ContactsContract_StreamItemsTest.insertViaContentDirectoryUri(mResolver,
+ rawContactId);
+ mStreamItemId = ContentUris.parseId(mStreamItemUri);
+ assertTrue(mStreamItemId != -1);
+ }
+
+ public void testContentDirectoryUri() {
+ byte[] photoData = PhotoUtil.getTestPhotoData(mContext);
+ ContentValues values = new ContentValues();
+ values.put(StreamItemPhotos.SORT_INDEX, 1);
+ values.put(StreamItemPhotos.PHOTO, photoData);
+
+ Uri insertUri = Uri.withAppendedPath(
+ ContentUris.withAppendedId(StreamItems.CONTENT_URI, mStreamItemId),
+ StreamItems.StreamItemPhotos.CONTENT_DIRECTORY);
+ Uri uri = mResolver.insert(insertUri, values);
+ long photoId = ContentUris.parseId(uri);
+ assertTrue(photoId != -1);
+ assertEquals(Uri.withAppendedPath(insertUri, Long.toString(photoId)), uri);
+ }
+
+ public void testContentPhotoUri() {
+ byte[] photoData = PhotoUtil.getTestPhotoData(mContext);
+ ContentValues values = new ContentValues();
+ values.put(StreamItemPhotos.STREAM_ITEM_ID, mStreamItemId);
+ values.put(StreamItemPhotos.SORT_INDEX, 1);
+ values.put(StreamItemPhotos.PHOTO, photoData);
+
+ Uri uri = mResolver.insert(StreamItems.CONTENT_PHOTO_URI, values);
+ long photoId = ContentUris.parseId(uri);
+ assertTrue(photoId != -1);
+ assertEquals(Uri.withAppendedPath(StreamItems.CONTENT_PHOTO_URI,
+ Long.toString(photoId)), uri);
+ }
+}
diff --git a/tests/tests/provider/src/android/provider/cts/ContactsContract_StreamItemsTest.java b/tests/tests/provider/src/android/provider/cts/ContactsContract_StreamItemsTest.java
index 5ca04ab..f8135aa 100644
--- a/tests/tests/provider/src/android/provider/cts/ContactsContract_StreamItemsTest.java
+++ b/tests/tests/provider/src/android/provider/cts/ContactsContract_StreamItemsTest.java
@@ -53,27 +53,8 @@
}
public void testContentDirectoryUri() throws Exception {
- // Create a contact to attach the stream item to it.
- ContentValues values = new ContentValues();
- values.put(RawContacts.ACCOUNT_TYPE, ACCOUNT_TYPE);
- values.put(RawContacts.ACCOUNT_NAME, ACCOUNT_NAME);
-
- Uri contactUri = mResolver.insert(RawContacts.CONTENT_URI, values);
- long rawContactId = ContentUris.parseId(contactUri);
- assertTrue(rawContactId != -1);
-
- // Attach a stream item to the contact.
- values.clear();
- values.put(RawContacts.ACCOUNT_TYPE, ACCOUNT_TYPE);
- values.put(RawContacts.ACCOUNT_NAME, ACCOUNT_NAME);
- values.put(StreamItems.TEXT, INSERT_TEXT);
- values.put(StreamItems.TIMESTAMP, INSERT_TIMESTAMP);
- values.put(StreamItems.COMMENTS, INSERT_COMMENTS);
-
- Uri contactStreamUri = Uri.withAppendedPath(
- ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId),
- RawContacts.StreamItems.CONTENT_DIRECTORY);
- Uri streamItemUri = mResolver.insert(contactStreamUri, values);
+ long rawContactId = insertRawContact(mResolver);
+ Uri streamItemUri = insertViaContentDirectoryUri(mResolver, rawContactId);
long streamItemId = ContentUris.parseId(streamItemUri);
assertTrue(streamItemId != -1);
@@ -88,7 +69,7 @@
assertInsertedItem(streamItemUri);
// Update the stream item.
- values.clear();
+ ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(RawContacts.ACCOUNT_TYPE, ACCOUNT_TYPE);
values.put(RawContacts.ACCOUNT_NAME, ACCOUNT_NAME);
@@ -100,6 +81,33 @@
assertUpdatedItem(streamItemUri);
}
+ static long insertRawContact(ContentResolver resolver) {
+ // Create a contact to attach the stream item to it.
+ ContentValues values = new ContentValues();
+ values.put(RawContacts.ACCOUNT_TYPE, ACCOUNT_TYPE);
+ values.put(RawContacts.ACCOUNT_NAME, ACCOUNT_NAME);
+
+ Uri contactUri = resolver.insert(RawContacts.CONTENT_URI, values);
+ long rawContactId = ContentUris.parseId(contactUri);
+ assertTrue(rawContactId != -1);
+ return rawContactId;
+ }
+
+ static Uri insertViaContentDirectoryUri(ContentResolver resolver, long rawContactId) {
+ // Attach a stream item to the contact.
+ ContentValues values = new ContentValues();
+ values.put(RawContacts.ACCOUNT_TYPE, ACCOUNT_TYPE);
+ values.put(RawContacts.ACCOUNT_NAME, ACCOUNT_NAME);
+ values.put(StreamItems.TEXT, INSERT_TEXT);
+ values.put(StreamItems.TIMESTAMP, INSERT_TIMESTAMP);
+ values.put(StreamItems.COMMENTS, INSERT_COMMENTS);
+
+ Uri contactStreamUri = Uri.withAppendedPath(
+ ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId),
+ RawContacts.StreamItems.CONTENT_DIRECTORY);
+ return resolver.insert(contactStreamUri, values);
+ }
+
public void testContentUri() throws Exception {
// Create a contact with one stream item in it.
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();