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.database.Cursor; 24 import android.net.Uri; 25 import android.provider.ContactsContract; 26 import android.provider.ContactsContract.CommonDataKinds.Im; 27 import android.provider.ContactsContract.Data; 28 import android.provider.ContactsContract.RawContacts; 29 import android.provider.ContactsContract.StatusUpdates; 30 import android.test.AndroidTestCase; 31 32 import java.util.ArrayList; 33 34 public class ContactsContract_StatusUpdatesTest extends AndroidTestCase { 35 36 private static final String ACCOUNT_TYPE = "android.provider.cts"; 37 private static final String ACCOUNT_NAME = "ContactsContract_StatusUpdatesTest"; 38 39 private ContentResolver mResolver; 40 private long dataId; 41 42 @Override 43 protected void setUp() throws Exception { 44 super.setUp(); 45 mResolver = mContext.getContentResolver(); 46 47 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 48 49 ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI) 50 .withValue(RawContacts.ACCOUNT_TYPE, ACCOUNT_TYPE) 51 .withValue(RawContacts.ACCOUNT_NAME, ACCOUNT_NAME) 52 .build()); 53 54 ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) 55 .withValueBackReference(Data.RAW_CONTACT_ID, 0) 56 .withValue(Data.MIMETYPE, Im.CONTENT_ITEM_TYPE) 57 .withValue(Im.TYPE, Im.TYPE_HOME) 58 .withValue(Im.PROTOCOL, Im.PROTOCOL_GOOGLE_TALK) 59 .build()); 60 61 ContentProviderResult[] results = mResolver.applyBatch(ContactsContract.AUTHORITY, ops); 62 assertNotNull(results[0].uri); 63 assertNotNull(results[1].uri); 64 65 dataId = ContentUris.parseId(results[1].uri); 66 } 67 68 public void testInsertStatus() throws Exception { 69 Uri uri = insertStatusUpdate(dataId, StatusUpdates.DO_NOT_DISTURB, null, null); 70 assertPresence(uri, StatusUpdates.DO_NOT_DISTURB); 71 assertStatus(uri, null); 72 assertHasTimestamp(uri, false); 73 assertRowCount(uri, 1); 74 75 Uri uri2 = insertStatusUpdate(dataId, StatusUpdates.AVAILABLE, null, null); 76 assertEquals(uri, uri2); 77 78 assertPresence(uri, StatusUpdates.AVAILABLE); 79 assertStatus(uri, null); 80 assertHasTimestamp(uri, false); 81 assertRowCount(uri, 1); 82 83 Uri uri3 = insertStatusUpdate(dataId, StatusUpdates.AWAY, "Grabbing a byte", null); 84 assertEquals(uri2, uri3); 85 86 assertPresence(uri, StatusUpdates.AWAY); 87 assertStatus(uri, "Grabbing a byte"); 88 assertHasTimestamp(uri, false); 89 assertRowCount(uri, 1); 90 91 // Inserting a new status message causes a timestamp to be inserted 92 Uri uri4 = insertStatusUpdate(dataId, StatusUpdates.IDLE, "Taking a bit of a break", null); 93 assertEquals(uri3, uri4); 94 95 assertPresence(uri, StatusUpdates.IDLE); 96 assertStatus(uri, "Taking a bit of a break"); 97 assertHasTimestamp(uri, true); 98 assertRowCount(uri, 1); 99 } 100 101 private Uri insertStatusUpdate(long dataId, int presence, String status, Long timestamp) 102 throws Exception { 103 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 104 ops.add(ContentProviderOperation.newInsert(StatusUpdates.CONTENT_URI) 105 .withValue(StatusUpdates.DATA_ID, dataId) 106 .withValue(StatusUpdates.PRESENCE, presence) 107 .withValue(StatusUpdates.STATUS, status) 108 .withValue(StatusUpdates.STATUS_TIMESTAMP, timestamp) 109 .withValue(StatusUpdates.PROTOCOL, Im.PROTOCOL_GOOGLE_TALK) 110 .withValue(StatusUpdates.IM_HANDLE, "mrBugDroid1337") 111 .build()); 112 113 ContentProviderResult[] results = mResolver.applyBatch(ContactsContract.AUTHORITY, ops); 114 assertNotNull(results[0].uri); 115 return results[0].uri; 116 } 117 118 private void assertRowCount(Uri uri, int count) throws Exception { 119 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 120 ops.add(ContentProviderOperation.newAssertQuery(uri) 121 .withExpectedCount(count) 122 .build()); 123 124 mResolver.applyBatch(ContactsContract.AUTHORITY, ops); 125 } 126 127 private void assertPresence(Uri uri, int status) throws Exception { 128 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 129 ops.add(ContentProviderOperation.newAssertQuery(uri) 130 .withValue(StatusUpdates.PRESENCE, status) 131 .withExpectedCount(1) 132 .build()); 133 134 mResolver.applyBatch(ContactsContract.AUTHORITY, ops); 135 } 136 137 private void assertStatus(Uri uri, String status) throws Exception { 138 ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); 139 ops.add(ContentProviderOperation.newAssertQuery(uri) 140 .withValue(StatusUpdates.STATUS, status) 141 .withExpectedCount(1) 142 .build()); 143 144 mResolver.applyBatch(ContactsContract.AUTHORITY, ops); 145 } 146 147 private void assertHasTimestamp(Uri uri, boolean hasTimestmap) throws Exception { 148 Cursor cursor = mResolver.query(uri, null, null, null, null); 149 try { 150 assertTrue(cursor.moveToNext()); 151 152 if (hasTimestmap) { 153 assertNotNull(cursor.getString(cursor.getColumnIndexOrThrow( 154 StatusUpdates.STATUS_TIMESTAMP))); 155 } else { 156 assertNull(cursor.getString(cursor.getColumnIndexOrThrow( 157 StatusUpdates.STATUS_TIMESTAMP))); 158 } 159 } finally { 160 cursor.close(); 161 } 162 } 163 164 public void testGetPresencePrecedence() { 165 assertPrecedence(StatusUpdates.AVAILABLE); 166 assertPrecedence(StatusUpdates.AWAY); 167 assertPrecedence(StatusUpdates.DO_NOT_DISTURB); 168 assertPrecedence(StatusUpdates.IDLE); 169 assertPrecedence(StatusUpdates.INVISIBLE); 170 assertPrecedence(StatusUpdates.OFFLINE); 171 } 172 173 private void assertPrecedence(int status) { 174 assertTrue(StatusUpdates.getPresencePrecedence(status) >= 0); 175 } 176 177 public void testGetPresenceIconresourceId() { 178 assertResource(StatusUpdates.AVAILABLE); 179 assertResource(StatusUpdates.AWAY); 180 assertResource(StatusUpdates.DO_NOT_DISTURB); 181 assertResource(StatusUpdates.IDLE); 182 assertResource(StatusUpdates.INVISIBLE); 183 assertResource(StatusUpdates.OFFLINE); 184 } 185 186 private void assertResource(int status) { 187 assertTrue(0 != StatusUpdates.getPresenceIconResourceId(status)); 188 } 189 } 190