Add CTS test for ContactsContract#Data and ContactsContract#CommonDataKinds#Photo
diff --git a/tests/tests/provider/src/android/provider/cts/ContactsContract_DataTest.java b/tests/tests/provider/src/android/provider/cts/ContactsContract_DataTest.java
new file mode 100644
index 0000000..b4b4f2b
--- /dev/null
+++ b/tests/tests/provider/src/android/provider/cts/ContactsContract_DataTest.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright (C) 2010 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 dalvik.annotation.TestLevel;
+import dalvik.annotation.TestTargetClass;
+import dalvik.annotation.TestTargetNew;
+
+import android.content.ContentResolver;
+import android.content.IContentProvider;
+import android.net.Uri;
+import android.provider.ContactsContract;
+import android.provider.ContactsContract.Data;
+import android.provider.ContactsContract.RawContacts;
+import android.provider.ContactsContract.CommonDataKinds.StructuredName;
+import android.provider.cts.ContactsContract_TestDataBuilder.TestContact;
+import android.provider.cts.ContactsContract_TestDataBuilder.TestData;
+import android.provider.cts.ContactsContract_TestDataBuilder.TestRawContact;
+import android.test.InstrumentationTestCase;
+
+@TestTargetClass(ContactsContract.Data.class)
+public class ContactsContract_DataTest extends InstrumentationTestCase {
+    private ContentResolver mResolver;
+    private ContactsContract_TestDataBuilder mBuilder;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        mResolver = getInstrumentation().getTargetContext().getContentResolver();
+        IContentProvider provider = mResolver.acquireProvider(ContactsContract.AUTHORITY);
+        mBuilder = new ContactsContract_TestDataBuilder(provider);
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+        mBuilder.cleanup();
+    }
+
+    @TestTargetNew(
+            level = TestLevel.PARTIAL_COMPLETE,
+            notes = "Test Data#getContactLookupUri(ContentResolver resolver, Uri dataUri) " +
+                    "using source id",
+            method = "Data#getContactLookupUri",
+            args = {android.content.ContentResolver.class, Uri.class}
+    )
+    public void testGetLookupUriBySourceId() throws Exception {
+        TestRawContact rawContact = mBuilder.newRawContact()
+                .with(RawContacts.ACCOUNT_TYPE, "test_type")
+                .with(RawContacts.ACCOUNT_NAME, "test_name")
+                .with(RawContacts.SOURCE_ID, "source_id")
+                .insert();
+
+        // TODO remove this. The method under test is currently broken: it will not
+        // work without at least one data row in the raw contact.
+        TestData data = rawContact.newDataRow(StructuredName.CONTENT_ITEM_TYPE)
+                .with(StructuredName.DISPLAY_NAME, "test name")
+                .insert();
+
+        Uri lookupUri = Data.getContactLookupUri(mResolver, data.getUri());
+        assertNotNull("Could not produce a lookup URI", lookupUri);
+
+        TestContact lookupContact = mBuilder.newContact().setUri(lookupUri).load();
+        assertEquals("Lookup URI matched the wrong contact",
+                lookupContact.getId(), data.load().getRawContact().load().getContactId());
+    }
+
+    @TestTargetNew(
+            level = TestLevel.PARTIAL_COMPLETE,
+            notes = "Test Data#getContactLookupUri(ContentResolver resolver, Uri dataUri) " +
+                    "using display name",
+            method = "Data#getContactLookupUri",
+            args = {android.content.ContentResolver.class, Uri.class}
+    )
+    public void testGetLookupUriByDisplayName() throws Exception {
+        TestRawContact rawContact = mBuilder.newRawContact()
+                .with(RawContacts.ACCOUNT_TYPE, "test_type")
+                .with(RawContacts.ACCOUNT_NAME, "test_name")
+                .insert();
+        TestData data = rawContact.newDataRow(StructuredName.CONTENT_ITEM_TYPE)
+                .with(StructuredName.DISPLAY_NAME, "test name")
+                .insert();
+
+        Uri lookupUri = Data.getContactLookupUri(mResolver, data.getUri());
+        assertNotNull("Could not produce a lookup URI", lookupUri);
+
+        TestContact lookupContact = mBuilder.newContact().setUri(lookupUri).load();
+        assertEquals("Lookup URI matched the wrong contact",
+                lookupContact.getId(), data.load().getRawContact().load().getContactId());
+    }
+}
+
diff --git a/tests/tests/provider/src/android/provider/cts/ContactsContract_PhotoTest.java b/tests/tests/provider/src/android/provider/cts/ContactsContract_PhotoTest.java
new file mode 100644
index 0000000..daec382
--- /dev/null
+++ b/tests/tests/provider/src/android/provider/cts/ContactsContract_PhotoTest.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright (C) 2010 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 dalvik.annotation.TestLevel;
+import dalvik.annotation.TestTargetClass;
+import dalvik.annotation.TestTargetNew;
+import dalvik.annotation.TestTargets;
+
+import android.content.ContentResolver;
+import android.content.IContentProvider;
+import android.provider.ContactsContract;
+import android.provider.ContactsContract.CommonDataKinds.Photo;
+import android.provider.cts.ContactsContract_TestDataBuilder.TestData;
+import android.provider.cts.ContactsContract_TestDataBuilder.TestRawContact;
+import android.test.InstrumentationTestCase;
+
+@TestTargetClass(Photo.class)
+public class ContactsContract_PhotoTest extends InstrumentationTestCase {
+    private ContactsContract_TestDataBuilder mBuilder;
+
+    private static final byte[] TEST_PHOTO_DATA = "ABCDEFG".getBytes();
+    private static final byte[] EMPTY_TEST_PHOTO_DATA = "".getBytes();
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        ContentResolver contentResolver =
+                getInstrumentation().getTargetContext().getContentResolver();
+        IContentProvider provider = contentResolver.acquireProvider(ContactsContract.AUTHORITY);
+        mBuilder = new ContactsContract_TestDataBuilder(provider);
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+        mBuilder.cleanup();
+    }
+
+    @TestTargets({
+        @TestTargetNew(
+            level = TestLevel.PARTIAL_COMPLETE,
+            notes = "Tests INSERT operation for photo"
+        )
+    })
+
+    public void testAddPhoto() throws Exception {
+        TestRawContact rawContact = mBuilder.newRawContact().insert();
+        TestData photoData = rawContact.newDataRow(Photo.CONTENT_ITEM_TYPE)
+                .with(Photo.PHOTO, TEST_PHOTO_DATA)
+                .insert();
+
+        photoData.load();
+        photoData.assertColumn(Photo.RAW_CONTACT_ID, rawContact.getId());
+        photoData.assertColumn(Photo.PHOTO, TEST_PHOTO_DATA);
+    }
+
+    @TestTargets({
+        @TestTargetNew(
+            level = TestLevel.PARTIAL_COMPLETE,
+            notes = "Tests INSERT operation for empty photo"
+        )
+    })
+
+    public void testAddEmptyPhoto() throws Exception {
+        TestRawContact rawContact = mBuilder.newRawContact().insert();
+        TestData photoData = rawContact.newDataRow(Photo.CONTENT_ITEM_TYPE)
+                .with(Photo.PHOTO, EMPTY_TEST_PHOTO_DATA)
+                .insert();
+
+        photoData.load();
+        photoData.assertColumn(Photo.RAW_CONTACT_ID, rawContact.getId());
+        photoData.assertColumn(Photo.PHOTO, EMPTY_TEST_PHOTO_DATA);
+    }
+}
+
diff --git a/tests/tests/provider/src/android/provider/cts/ContactsContract_RawContactsTest.java b/tests/tests/provider/src/android/provider/cts/ContactsContract_RawContactsTest.java
index c22f00f..7dc4393 100644
--- a/tests/tests/provider/src/android/provider/cts/ContactsContract_RawContactsTest.java
+++ b/tests/tests/provider/src/android/provider/cts/ContactsContract_RawContactsTest.java
@@ -51,9 +51,9 @@
 
     @TestTargetNew(
             level = TestLevel.PARTIAL_COMPLETE,
-            notes = "Test getContactLookupUri(ContentResolver resolver, Uri rawContactUri) " +
-                    "using source id",
-            method = "getContactLookupUri",
+            notes = "Test RawContacts#getContactLookupUri(ContentResolver resolver, Uri " +
+                    "rawContactUri) using source id",
+            method = "RawContacts#getContactLookupUri",
             args = {android.content.ContentResolver.class, Uri.class}
     )
     public void testGetLookupUriBySourceId() throws Exception {
@@ -79,9 +79,9 @@
 
     @TestTargetNew(
             level = TestLevel.PARTIAL_COMPLETE,
-            notes = "Test getContactLookupUri(ContentResolver resolver, Uri rawContactUri) " +
-                    "using display name",
-            method = "getContactLookupUri",
+            notes = "Test RawContacts#getContactLookupUri(ContentResolver resolver, Uri " +
+                    "rawContactUri) using display name",
+            method = "RawContacts#getContactLookupUri",
             args = {android.content.ContentResolver.class, Uri.class}
     )
     public void testGetLookupUriByDisplayName() throws Exception {
diff --git a/tests/tests/provider/src/android/provider/cts/ContactsContract_TestDataBuilder.java b/tests/tests/provider/src/android/provider/cts/ContactsContract_TestDataBuilder.java
index ab59082..0c04aa6 100644
--- a/tests/tests/provider/src/android/provider/cts/ContactsContract_TestDataBuilder.java
+++ b/tests/tests/provider/src/android/provider/cts/ContactsContract_TestDataBuilder.java
@@ -37,7 +37,10 @@
 import java.util.Map;
 import java.util.Set;
 
+import java.util.Arrays;
+
 import junit.framework.Assert;
+import junit.framework.ComparisonFailure;
 
 /**
  * A test data builder for ContactsContract tests.
@@ -111,6 +114,12 @@
         }
 
         @SuppressWarnings("unchecked")
+        public T with(String key, byte[] value) {
+            mValues.put(key, value);
+            return (T)this;
+        }
+
+        @SuppressWarnings("unchecked")
         public T insert() throws Exception {
             insertDependent();
             mCreatedRows.add(this);
@@ -197,6 +206,22 @@
             assertEquals(value, mCursor.getString(getColumnIndex(columnName)));
         }
 
+        public void assertColumn(String columnName, byte[] value) {
+            assertEquals(value, mCursor.getBlob(getColumnIndex(columnName)));
+        }
+
+        public void assertEquals(byte[] expected, byte[] actual) {
+            assertEquals(null, expected, actual);
+        }
+
+        public void assertEquals(String message, byte[] expected, byte[] actual) {
+            if (expected == null && actual == null)
+                return;
+            if (expected != null && Arrays.equals(actual, expected))
+                return;
+            throw new ComparisonFailure(message, expected.toString(), actual.toString());
+        }
+
         private int getColumnIndex(String columnName) {
             int index = mCursor.getColumnIndex(columnName);
             assertTrue("No such column: " + columnName +
@@ -292,6 +317,14 @@
             mValues.put(Data.RAW_CONTACT_ID, mRawContact.getId());
             return super.insertDependent();
         }
+
+        public long getRawContactId() {
+            return getLong(Data.RAW_CONTACT_ID);
+        }
+
+        public TestRawContact getRawContact() throws Exception {
+            return mRawContact;
+        }
     }
 
     public ContactsContract_TestDataBuilder(IContentProvider provider) {