1 package com.android.bluetooth.tests; 2 3 import java.io.IOException; 4 import java.util.Date; 5 6 import javax.obex.HeaderSet; 7 import javax.obex.Operation; 8 9 import android.annotation.TargetApi; 10 import android.content.ContentResolver; 11 import android.content.ContentValues; 12 import android.content.Context; 13 import android.net.Uri; 14 import android.provider.ContactsContract; 15 import android.provider.Telephony.Sms; 16 import android.test.AndroidTestCase; 17 import android.util.Log; 18 19 import com.android.bluetooth.map.BluetoothMapConvoContactElement; 20 import com.android.bluetooth.map.BluetoothMapConvoListing; 21 import com.android.bluetooth.map.BluetoothMapConvoListingElement; 22 import com.android.bluetooth.mapapi.BluetoothMapContract; 23 24 /** 25 * Class to hold test data - both the server side data to insert into the databases, and the 26 * validation data to validate the result, when reading back the data. 27 * 28 * Should be data only, not operation specific functionality (client). 29 * 30 * Please try to keep useful functionality call-able from a test case, to make it possible 31 * to call a single test case to e.g. inject some contacts or messages into the database. 32 * 33 */ 34 @TargetApi(20) 35 public class MapTestData extends AndroidTestCase { 36 private static final String TAG = "MapTestData"; 37 38 /* Test validation variables */ 39 static final String TEST_CONTACT_NAME = "Jesus Überboss"; 40 static final String TEST_CONTACT_PHONE = "55566688"; 41 static final String TEST_CONTACT_EMAIL = "boss@the.skyes"; 42 static final int TEST_NUM_CONTACTS = 3; 43 44 static final int TEST_ADD_CONTACT_PER_ITERATIONS = 4; 45 /* I do know this function is deprecated, but I'm unable to find a good alternative 46 * except from taking a copy of the Date.UTC function as suggested. */ 47 // NOTE: This will only set the data on the message - not the lastActivity on SMS/MMS threads 48 static final long TEST_ACTIVITY_BEGIN = Date.UTC( 49 2014-1900, 50 8-1, /* month 0-11*/ 51 22, /*day 1-31 */ 52 22, /*hour*/ 53 15, /*minute*/ 54 20 /*second*/); 55 56 static final String TEST_ACTIVITY_BEGIN_STRING = "20150102T150047"; 57 static final String TEST_ACTIVITY_END_STRING = "20160102T150047"; 58 59 static final int TEST_ACTIVITY_INTERVAL = 5*60*1000; /*ms*/ 60 61 static Context sContext = null; init(Context context)62 public static void init(Context context){ 63 sContext = context; 64 } 65 /** 66 * Adds messages to the SMS message database. 67 */ 68 public static class MapAddSmsMessages implements ISeqStepAction { 69 int mCount; 70 /** 71 * 72 * @param count the number of iterations to execute 73 */ MapAddSmsMessages(int count)74 public MapAddSmsMessages(int count) { 75 mCount = count; 76 } 77 78 @Override execute(SeqStep step, HeaderSet request, Operation op)79 public void execute(SeqStep step, HeaderSet request, Operation op) 80 throws IOException { 81 int count = mCount; // Number of messages in each conversation 82 ContentResolver resolver = sContext.getContentResolver(); 83 84 // Insert some messages 85 insertTestMessages(resolver, step.index, count); 86 87 // Cleanup if needed to avoid duplicates 88 deleteTestContacts(resolver); 89 90 // And now add the contacts 91 setupTestContacts(resolver); 92 } 93 } 94 95 /** 96 * TODO: Only works for filter on TEST_CONTACT_NAME 97 * @param maxCount 98 * @param offset 99 * @param filterContact 100 * @param read 101 * @param reportRead 102 * @param msgCount 103 * @return 104 */ getConvoListingReference(int maxCount, int offset, boolean filterContact, boolean read, boolean reportRead, int msgCount)105 public static BluetoothMapConvoListing getConvoListingReference(int maxCount, int offset, 106 boolean filterContact, boolean read, boolean reportRead, int msgCount){ 107 BluetoothMapConvoListing list = new BluetoothMapConvoListing(); 108 BluetoothMapConvoListingElement element; 109 BluetoothMapConvoContactElement contact; 110 element = new BluetoothMapConvoListingElement(); 111 element.setRead(read, reportRead); 112 element.setVersionCounter(0); 113 contact = new BluetoothMapConvoContactElement(); 114 contact.setName(TEST_CONTACT_NAME); 115 contact.setLastActivity(TEST_ACTIVITY_BEGIN + 116 msgCount*TEST_ADD_CONTACT_PER_ITERATIONS*TEST_ACTIVITY_INTERVAL); 117 element.addContact(contact); 118 list.add(element); 119 return null; 120 } 121 insertTestMessages(ContentResolver resolver, int tag, int count)122 public static void insertTestMessages(ContentResolver resolver, int tag, int count) { 123 ContentValues values[] = new ContentValues[count*4]; // 4 messages/iteration 124 long date = TEST_ACTIVITY_BEGIN; 125 Log.i(TAG, "Preparing messages... with data = " + date); 126 127 for (int x = 0;x < count;x++){ 128 /* NOTE: Update TEST_ADD_CONTACT_PER_ITERATIONS if more messages are added */ 129 ContentValues item = new ContentValues(5); 130 item.put("address", "98765432"); 131 item.put("body", "test message " + x + " step index: " + tag); 132 item.put("date", date+=TEST_ACTIVITY_INTERVAL); 133 item.put("read", "0"); 134 if(x%2 == 0) { 135 item.put("type", Sms.MESSAGE_TYPE_INBOX); 136 } else { 137 item.put("type", Sms.MESSAGE_TYPE_SENT); 138 } 139 values[x] = item; 140 141 item = new ContentValues(5); 142 item.put("address", "23456780"); 143 item.put("body", "test message " + x + " step index: " + tag); 144 item.put("date", date += TEST_ACTIVITY_INTERVAL); 145 item.put("read", "0"); 146 if(x%2 == 0) { 147 item.put("type", Sms.MESSAGE_TYPE_INBOX); 148 } else { 149 item.put("type", Sms.MESSAGE_TYPE_SENT); 150 } 151 values[count+x] = item; 152 153 item = new ContentValues(5); 154 item.put("address", "+4523456780"); 155 item.put("body", "test message "+x+" step index: " + tag); 156 item.put("date", date += TEST_ACTIVITY_INTERVAL); 157 item.put("read", "0"); 158 if(x%2 == 0) { 159 item.put("type", Sms.MESSAGE_TYPE_INBOX); 160 } else { 161 item.put("type", Sms.MESSAGE_TYPE_SENT); 162 } 163 values[2*count+x] = item; 164 165 /* This is the message used for test */ 166 item = new ContentValues(5); 167 item.put("address", TEST_CONTACT_PHONE); 168 item.put("body", "test message "+x+" step index: " + tag); 169 item.put("date", date += TEST_ACTIVITY_INTERVAL); 170 item.put("read", "0"); 171 if(x%2 == 0) { 172 item.put("type", Sms.MESSAGE_TYPE_INBOX); 173 } else { 174 item.put("type", Sms.MESSAGE_TYPE_SENT); 175 } 176 values[3*count+x] = item; 177 } 178 179 Log.i(TAG, "Starting bulk insert..."); 180 resolver.bulkInsert(Uri.parse("content://sms"), values); 181 Log.i(TAG, "Bulk insert done."); 182 } 183 184 /** 185 * Insert a few contacts in the main contact database, using a test account. 186 */ setupTestContacts(ContentResolver resolver)187 public static void setupTestContacts(ContentResolver resolver){ 188 /*TEST_NUM_CONTACTS must be updated if this function is changed */ 189 insertContact(resolver, "Hans Hansen", "98765432", "hans@hansens.global"); 190 insertContact(resolver, "Helle Børgesen", "23456780", "hb@gmail.com"); 191 insertContact(resolver, TEST_CONTACT_NAME, TEST_CONTACT_PHONE, TEST_CONTACT_EMAIL); 192 } 193 194 /** 195 * Helper function to insert a contact 196 * @param name 197 * @param phone 198 * @param email 199 */ insertContact(ContentResolver resolver, String name, String phone, String email)200 private static void insertContact(ContentResolver resolver, String name, String phone, String email) { 201 // Get the account info 202 //Cursor c = resolver.query(uri, projection, selection, selectionArgs, sortOrder) 203 ContentValues item = new ContentValues(3); 204 item.put(ContactsContract.RawContacts.ACCOUNT_TYPE, "test_account"); 205 item.put(ContactsContract.RawContacts.ACCOUNT_NAME, "MAP account"); 206 Uri uri = resolver.insert(ContactsContract.RawContacts.CONTENT_URI, item); 207 Log.i(TAG, "Inserted RawContact: " + uri); 208 long rawId = Long.parseLong(uri.getLastPathSegment()); 209 210 //Now add contact information 211 item = new ContentValues(3); 212 item.put(ContactsContract.Data.RAW_CONTACT_ID, rawId); 213 item.put(ContactsContract.Data.MIMETYPE, 214 ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE); 215 item.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, 216 name); 217 resolver.insert(ContactsContract.Data.CONTENT_URI, item); 218 219 if(phone != null) { 220 item = new ContentValues(3); 221 item.put(ContactsContract.Data.RAW_CONTACT_ID, rawId); 222 item.put(ContactsContract.Data.MIMETYPE, 223 ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE); 224 item.put(ContactsContract.CommonDataKinds.Phone.NUMBER, phone); 225 resolver.insert(ContactsContract.Data.CONTENT_URI, item); 226 } 227 228 if(email != null) { 229 item = new ContentValues(3); 230 item.put(ContactsContract.Data.RAW_CONTACT_ID, rawId); 231 item.put(ContactsContract.Data.MIMETYPE, 232 ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE); 233 item.put(ContactsContract.CommonDataKinds.Email.ADDRESS, email); 234 resolver.insert(ContactsContract.Data.CONTENT_URI, item); 235 } 236 } 237 238 /** 239 * Delete all contacts belonging to the test_account. 240 */ deleteTestContacts(ContentResolver resolver)241 public static void deleteTestContacts(ContentResolver resolver){ 242 resolver.delete(ContactsContract.RawContacts.CONTENT_URI, 243 ContactsContract.RawContacts.ACCOUNT_TYPE + "=\"test_account\"", null); 244 } 245 246 /**************************************************************************** 247 * Small test cases to trigger the functionality without running a sequence. 248 ****************************************************************************/ 249 /** 250 * Insert a few contacts in the main contact database, using a test account. 251 */ testInsertMessages()252 public void testInsertMessages() { 253 ContentResolver resolver = mContext.getContentResolver(); 254 insertTestMessages(resolver, 1234, 10); 255 } 256 testInsert1000Messages()257 public void testInsert1000Messages() { 258 ContentResolver resolver = mContext.getContentResolver(); 259 insertTestMessages(resolver, 1234, 1000); 260 } 261 262 /** 263 * Insert a few contacts in the main contact database, using a test account. 264 */ testSetupContacts()265 public void testSetupContacts() { 266 ContentResolver resolver = mContext.getContentResolver(); 267 setupTestContacts(resolver); 268 } 269 270 /** 271 * Delete all contacts belonging to the test_account. 272 */ testDeleteTestContacts()273 public void testDeleteTestContacts() { 274 ContentResolver resolver = mContext.getContentResolver(); 275 deleteTestContacts(resolver); 276 } 277 testSetup1000Contacts()278 public void testSetup1000Contacts() { 279 ContentResolver resolver = mContext.getContentResolver(); 280 for(int i = 0; i < 1000; i++) { 281 insertContact(resolver, "Hans Hansen " + i, 282 "98765431" + i, "hans" + i + "@hansens.global"); 283 } 284 } 285 286 } 287