1 /* 2 * Copyright (C) 2012 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 android.provider.cts.contacts; 18 19 import android.content.ContentProviderOperation; 20 import android.content.ContentProviderResult; 21 import android.content.ContentResolver; 22 import android.content.ContentUris; 23 import android.content.ContentValues; 24 import android.database.Cursor; 25 import android.net.Uri; 26 import android.provider.ContactsContract; 27 import android.provider.ContactsContract.Data; 28 import android.provider.ContactsContract.RawContacts; 29 import android.provider.ContactsContract.StreamItems; 30 import android.test.AndroidTestCase; 31 32 import java.util.ArrayList; 33 34 public class ContactsContract_StreamItemsTest extends AndroidTestCase { 35 36 private static final String ACCOUNT_TYPE = "com.android.cts"; 37 private static final String ACCOUNT_NAME = "ContactsContract_StreamItemsTest"; 38 39 private static final String INSERT_TEXT = "Wrote a test for the StreamItems class"; 40 private static final long INSERT_TIMESTAMP = 3007; 41 private static final String INSERT_COMMENTS = "1337 people reshared this"; 42 43 private static final String UPDATE_TEXT = "Wrote more tests for the StreamItems class"; 44 private static final long UPDATE_TIMESTAMP = 8008; 45 private static final String UPDATE_COMMENTS = "3007 people reshared this"; 46 47 private ContentResolver mResolver; 48 49 @Override 50 protected void setUp() throws Exception { 51 super.setUp(); 52 mResolver = mContext.getContentResolver(); 53 } 54 55 public void testContentDirectoryUri() throws Exception { 56 long rawContactId = insertRawContact(mResolver); 57 Uri streamItemUri = insertViaContentDirectoryUri(mResolver, rawContactId); 58 long streamItemId = ContentUris.parseId(streamItemUri); 59 assertTrue(streamItemId != -1); 60 61 // Check that the provider returns the stream id in it's URI. 62 assertEquals(streamItemUri, 63 ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId).buildUpon() 64 .appendPath(RawContacts.StreamItems.CONTENT_DIRECTORY) 65 .appendPath(Long.toString(streamItemId)) 66 .build()); 67 68 // Check that the provider stored what we put into it. 69 assertInsertedItem(streamItemUri); 70 71 // Update the stream item. 72 ContentValues values = new ContentValues(); 73 values.put(Data.RAW_CONTACT_ID, rawContactId); 74 values.put(RawContacts.ACCOUNT_TYPE, ACCOUNT_TYPE); 75 values.put(RawContacts.ACCOUNT_NAME, ACCOUNT_NAME); 76 values.put(StreamItems.TEXT, UPDATE_TEXT); 77 values.put(StreamItems.TIMESTAMP, UPDATE_TIMESTAMP); 78 values.put(StreamItems.COMMENTS, UPDATE_COMMENTS); 79 80 assertEquals(1, mResolver.update(streamItemUri, values, null, null)); 81 assertUpdatedItem(streamItemUri); 82 } 83 84 static long insertRawContact(ContentResolver resolver) { 85 // Create a contact to attach the stream item to it. 86 ContentValues values = new ContentValues(); 87 values.put(RawContacts.ACCOUNT_TYPE, ACCOUNT_TYPE); 88 values.put(RawContacts.ACCOUNT_NAME, ACCOUNT_NAME); 89 90 Uri contactUri = resolver.insert(RawContacts.CONTENT_URI, values); 91 long rawContactId = ContentUris.parseId(contactUri); 92 assertTrue(rawContactId != -1); 93 return rawContactId; 94 } 95 96 static Uri insertViaContentDirectoryUri(ContentResolver resolver, long rawContactId) { 97 // Attach a stream item to the contact. 98 ContentValues values = new ContentValues(); 99 values.put(RawContacts.ACCOUNT_TYPE, ACCOUNT_TYPE); 100 values.put(RawContacts.ACCOUNT_NAME, ACCOUNT_NAME); 101 values.put(StreamItems.TEXT, INSERT_TEXT); 102 values.put(StreamItems.TIMESTAMP, INSERT_TIMESTAMP); 103 values.put(StreamItems.COMMENTS, INSERT_COMMENTS); 104 105 Uri contactStreamUri = Uri.withAppendedPath( 106 ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId), 107 RawContacts.StreamItems.CONTENT_DIRECTORY); 108 return resolver.insert(contactStreamUri, values); 109 } 110 111 public void testContentUri() throws Exception { 112 // Create a contact with one stream item in it. 113 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 114 115 ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI) 116 .withValue(RawContacts.ACCOUNT_TYPE, ACCOUNT_TYPE) 117 .withValue(RawContacts.ACCOUNT_NAME, ACCOUNT_NAME) 118 .build()); 119 120 ops.add(ContentProviderOperation.newInsert(StreamItems.CONTENT_URI) 121 .withValueBackReference(Data.RAW_CONTACT_ID, 0) 122 .withValue(RawContacts.ACCOUNT_TYPE, ACCOUNT_TYPE) 123 .withValue(RawContacts.ACCOUNT_NAME, ACCOUNT_NAME) 124 .withValue(StreamItems.TEXT, INSERT_TEXT) 125 .withValue(StreamItems.TIMESTAMP, INSERT_TIMESTAMP) 126 .withValue(StreamItems.COMMENTS, INSERT_COMMENTS) 127 .build()); 128 129 ContentProviderResult[] results = mResolver.applyBatch(ContactsContract.AUTHORITY, ops); 130 long rawContactId = ContentUris.parseId(results[0].uri); 131 assertTrue(rawContactId != -1); 132 133 Uri streamItemUri = results[1].uri; 134 long streamItemId = ContentUris.parseId(streamItemUri); 135 assertTrue(streamItemId != -1); 136 137 // Check that the provider returns the stream id in it's URI. 138 assertEquals(streamItemUri, 139 ContentUris.withAppendedId(StreamItems.CONTENT_URI, streamItemId)); 140 141 // Check that the provider stored what we put into it. 142 assertInsertedItem(streamItemUri); 143 144 // Update the stream item. 145 ops.clear(); 146 ops.add(ContentProviderOperation.newUpdate(streamItemUri) 147 .withValue(Data.RAW_CONTACT_ID, rawContactId) 148 .withValue(RawContacts.ACCOUNT_TYPE, ACCOUNT_TYPE) 149 .withValue(RawContacts.ACCOUNT_NAME, ACCOUNT_NAME) 150 .withValue(StreamItems.TEXT, UPDATE_TEXT) 151 .withValue(StreamItems.TIMESTAMP, UPDATE_TIMESTAMP) 152 .withValue(StreamItems.COMMENTS, UPDATE_COMMENTS) 153 .build()); 154 155 results = mResolver.applyBatch(ContactsContract.AUTHORITY, ops); 156 assertEquals(Integer.valueOf(1), results[0].count); 157 assertUpdatedItem(streamItemUri); 158 } 159 160 private void assertInsertedItem(Uri itemUri) { 161 assertStreamItem(itemUri, INSERT_TEXT, INSERT_TIMESTAMP, INSERT_COMMENTS); 162 } 163 164 private void assertUpdatedItem(Uri itemUri) { 165 assertStreamItem(itemUri, UPDATE_TEXT, UPDATE_TIMESTAMP, UPDATE_COMMENTS); 166 } 167 168 private void assertStreamItem(Uri uri, String text, long timestamp, String comments) { 169 Cursor cursor = mResolver.query(uri, null, null, null, null); 170 try { 171 assertTrue(cursor.moveToFirst()); 172 assertEquals(text, cursor.getString( 173 cursor.getColumnIndexOrThrow(StreamItems.TEXT))); 174 assertEquals(timestamp, cursor.getLong( 175 cursor.getColumnIndexOrThrow(StreamItems.TIMESTAMP))); 176 assertEquals(comments, cursor.getString( 177 cursor.getColumnIndexOrThrow(StreamItems.COMMENTS))); 178 } finally { 179 cursor.close(); 180 } 181 } 182 } 183