SmsProvider TestCases

Below listed all the testcases of Smsprovider class.

Bug: 135951939
Test: run atest CtsTelephonyProviderTestCases:SmsTest
Change-Id: I66256c7b9056b97bfa6c8063af20d8e17fc82179
diff --git a/tests/tests/telephonyprovider/src/android/telephonyprovider/cts/SmsTest.java b/tests/tests/telephonyprovider/src/android/telephonyprovider/cts/SmsTest.java
new file mode 100644
index 0000000..9445b0d
--- /dev/null
+++ b/tests/tests/telephonyprovider/src/android/telephonyprovider/cts/SmsTest.java
@@ -0,0 +1,283 @@
+/*
+ * Copyright (C) 2019 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.telephonyprovider.cts;
+
+import static androidx.test.InstrumentationRegistry.getInstrumentation;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.content.ContentResolver;
+import android.content.ContentValues;
+import android.database.Cursor;
+import android.net.Uri;
+import android.provider.Telephony;
+
+import androidx.test.filters.SmallTest;
+
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+
+@SmallTest
+public class SmsTest {
+
+    private static final String TEST_SMS_BODY = "TEST_SMS_BODY";
+    private static final String TEST_ADDRESS = "+19998880001";
+    private static final int TEST_THREAD_ID_1 = 101;
+    private ContentResolver mContentResolver;
+    private SmsTestHelper mSmsTestHelper;
+
+    @BeforeClass
+    public static void ensureDefaultSmsApp() {
+        DefaultSmsAppHelper.ensureDefaultSmsApp();
+    }
+
+    @AfterClass
+    public static void cleanup() {
+        ContentResolver contentResolver = getInstrumentation().getContext().getContentResolver();
+        contentResolver.delete(Telephony.Sms.CONTENT_URI, null, null);
+        contentResolver.delete(Telephony.Threads.CONTENT_URI, null, null);
+    }
+
+    @Before
+    public void setupTestEnvironment() {
+        cleanup();
+        mContentResolver = getInstrumentation().getContext().getContentResolver();
+        mSmsTestHelper = new SmsTestHelper();
+    }
+
+    /**
+     * Asserts that a URI returned from an SMS insert operation represents a pass Insert.
+     */
+    @Test
+    public void testSmsInsert() {
+        Uri uri = mSmsTestHelper.insertTestSms(TEST_ADDRESS, TEST_SMS_BODY);
+        assertThat(uri).isNotNull();
+
+        Cursor cursor = mContentResolver.query(uri, null, null, null);
+        assertThat(cursor.getCount()).isEqualTo(1);
+        cursor.moveToNext();
+
+        String actualSmsBody = cursor.getString(cursor.getColumnIndex(Telephony.Sms.BODY));
+        assertThat(actualSmsBody).isEqualTo(TEST_SMS_BODY);
+    }
+
+    /**
+     * The purpose of this test is to perform delete operation and assert that SMS is deleted.
+     */
+    @Test
+    public void testSmsDelete() {
+        Uri uri = mSmsTestHelper.insertTestSms(TEST_ADDRESS, TEST_SMS_BODY);
+        assertThat(uri).isNotNull();
+
+        int deletedRows = mContentResolver.delete(uri, null, null);
+
+        assertThat(deletedRows).isEqualTo(1);
+
+        Cursor cursor = mContentResolver.query(uri, null, null, null);
+        assertThat(cursor.getCount()).isEqualTo(0);
+    }
+
+    /**
+     * The purpose of this test is to update the message body and verify the message body is
+     * updated.
+     */
+    @Test
+    public void testSmsUpdate() {
+        String testSmsBodyUpdate = "TEST_SMS_BODY_UPDATED";
+        Uri uri = mSmsTestHelper.insertTestSms(TEST_ADDRESS, TEST_SMS_BODY);
+        assertThat(uri).isNotNull();
+
+        mSmsTestHelper.assertSmsColumnEquals(Telephony.Sms.BODY, uri, TEST_SMS_BODY);
+
+        ContentValues values = new ContentValues();
+        values.put(Telephony.Sms.ADDRESS, TEST_ADDRESS);
+        values.put(Telephony.Sms.BODY, testSmsBodyUpdate);
+
+        mContentResolver.update(uri, values, null, null);
+
+        mSmsTestHelper.assertSmsColumnEquals(Telephony.Sms.BODY, uri, testSmsBodyUpdate);
+    }
+
+    @Test
+    public void testInsertSmsFromSubid_verifySmsFromNotOtherSubId() {
+        int subId = 1;
+
+        ContentValues values = new ContentValues();
+        values.put(Telephony.Sms.BODY, TEST_SMS_BODY);
+        values.put(Telephony.Sms.SUBSCRIPTION_ID, subId);
+        Uri uri = mContentResolver.insert(Telephony.Sms.CONTENT_URI, values);
+
+        assertThat(uri).isNotNull();
+
+        mSmsTestHelper.assertSmsColumnEquals(Telephony.Sms.SUBSCRIPTION_ID, uri,
+                String.valueOf(subId));
+    }
+
+    @Test
+    public void testInsertSms_canUpdateSeen() {
+        Uri uri = mSmsTestHelper.insertTestSms(TEST_ADDRESS, TEST_SMS_BODY);
+        assertThat(uri).isNotNull();
+
+        Cursor cursor = mContentResolver.query(uri, null, null, null);
+        assertThat(cursor.getCount()).isEqualTo(1);
+
+        final ContentValues updateValues = new ContentValues();
+        updateValues.put(Telephony.Sms.SEEN, 1);
+
+        int cursorUpdate = mContentResolver.update(uri, updateValues, null, null);
+
+        assertThat(cursorUpdate).isEqualTo(1);
+
+        mSmsTestHelper.assertSmsColumnEquals(Telephony.Sms.SEEN, uri, String.valueOf(1));
+    }
+
+    @Test
+    public void testInsertSms_canUpdateSmsStatus() {
+        Uri uri = mSmsTestHelper.insertTestSms(TEST_ADDRESS, TEST_SMS_BODY);
+        assertThat(uri).isNotNull();
+
+        Cursor cursor = mContentResolver.query(uri, null, null, null);
+
+        assertThat(cursor.getCount()).isEqualTo(1);
+        // STATUS_FAILED = 64;  0x40
+        mSmsTestHelper.assertUpdateSmsStatus(Telephony.Sms.STATUS_FAILED, uri);
+        // STATUS_PENDING = 32;  0x20
+        mSmsTestHelper.assertUpdateSmsStatus(Telephony.Sms.STATUS_PENDING, uri);
+        //  STATUS_COMPLETE = 0; 0x0
+        mSmsTestHelper.assertUpdateSmsStatus(Telephony.Sms.STATUS_COMPLETE, uri);
+    }
+
+    @Test
+    public void testInsertSms_canUpdateSmsType() {
+        Uri uri = mSmsTestHelper.insertTestSms(TEST_ADDRESS, TEST_SMS_BODY);
+        assertThat(uri).isNotNull();
+
+        Cursor cursor = mContentResolver.query(uri, null, null, null);
+        assertThat(cursor.getCount()).isEqualTo(1);
+        // MESSAGE_TYPE_INBOX = 1;  0x1
+        mSmsTestHelper.assertUpdateSmsType(Telephony.Sms.MESSAGE_TYPE_INBOX, uri);
+        // MESSAGE_TYPE_SENT = 2;  0x2
+        mSmsTestHelper.assertUpdateSmsType(Telephony.Sms.MESSAGE_TYPE_SENT, uri);
+    }
+
+    // Queries for a thread ID returns the same and correct thread ID.
+    @Test
+    public void testQueryThreadId_returnSameThreadId() {
+        ContentValues values = new ContentValues();
+        values.put(Telephony.Sms.THREAD_ID, TEST_THREAD_ID_1);
+        values.put(Telephony.Sms.BODY, TEST_SMS_BODY);
+        Uri uri = mContentResolver.insert(Telephony.Sms.CONTENT_URI, values);
+
+        assertThat(uri).isNotNull();
+
+        mSmsTestHelper.assertSmsColumnEquals(Telephony.Sms.THREAD_ID, uri,
+                String.valueOf(TEST_THREAD_ID_1));
+    }
+
+    /**
+     * Asserts that content provider bulk insert and returns the same count while query.
+     */
+    @Test
+    public void testSmsBulkInsert() {
+        ContentValues[] smsContentValues = new ContentValues[] {
+                mSmsTestHelper.createSmsValues(mSmsTestHelper.SMS_ADDRESS_BODY_1),
+                mSmsTestHelper.createSmsValues(mSmsTestHelper.SMS_ADDRESS_BODY_2)};
+
+        int count = mContentResolver.bulkInsert(Telephony.Sms.CONTENT_URI, smsContentValues);
+        mSmsTestHelper.assertBulkSmsContentEqual(count, smsContentValues);
+    }
+
+    /**
+     * Asserts that SMS inserted is auto populated with default values as mentioned in the table
+     * schema.
+     */
+    @Test
+    public void testDefaultValuesAreInsertedInSmsTable() {
+        Uri uri = mSmsTestHelper.insertTestSms(TEST_ADDRESS, TEST_SMS_BODY);
+        assertThat(uri).isNotNull();
+
+        Cursor cursor = mContentResolver.query(Telephony.Sms.CONTENT_URI, null, null, null);
+        assertThat(cursor.getCount()).isEqualTo(1);
+        cursor.moveToNext();
+
+        assertThat(
+            cursor.getInt(cursor.getColumnIndex(Telephony.Sms.READ))).isEqualTo(0);
+
+        assertThat(
+            cursor.getInt(cursor.getColumnIndex(Telephony.Sms.STATUS))).isEqualTo(-1);
+
+        assertThat(
+            cursor.getInt(cursor.getColumnIndex(Telephony.Sms.DATE_SENT))).isEqualTo(0);
+
+        assertThat(
+            cursor.getInt(cursor.getColumnIndex(Telephony.Sms.LOCKED))).isEqualTo(0);
+
+        assertThat(
+            cursor.getInt(cursor.getColumnIndex(Telephony.Sms.SUBSCRIPTION_ID))).isEqualTo(-1);
+
+        assertThat(
+            cursor.getInt(cursor.getColumnIndex(Telephony.Sms.ERROR_CODE))).isEqualTo(0);
+
+        assertThat(
+            cursor.getInt(cursor.getColumnIndex(Telephony.Sms.SEEN))).isEqualTo(0);
+    }
+
+    @Test
+    public void testDeleteSms_ifLastSmsDeletedThenThreadIsDeleted() {
+        int testThreadId2 = 102;
+
+        Uri uri1 = mSmsTestHelper
+                .insertTestSmsWithThread(TEST_SMS_BODY, TEST_ADDRESS, TEST_THREAD_ID_1);
+        assertThat(uri1).isNotNull();
+
+        Uri uri2 = mSmsTestHelper
+                .insertTestSmsWithThread(TEST_SMS_BODY, TEST_ADDRESS, testThreadId2);
+        assertThat(uri2).isNotNull();
+
+        int deletedRows = mContentResolver.delete(uri1, null, null);
+        assertThat(deletedRows).isEqualTo(1);
+
+        Cursor cursor = mContentResolver.query(Telephony.Sms.CONTENT_URI, null, null, null);
+        assertThat(cursor.getCount()).isEqualTo(1);
+        cursor.moveToNext();
+
+        int thread_id = cursor.getInt(cursor.getColumnIndex(Telephony.Sms.THREAD_ID));
+        assertThat(thread_id).isNotEqualTo(TEST_THREAD_ID_1);
+    }
+
+    /**
+     * Asserts that a Emoji SMS body returned from an SMS insert operation are equal
+     */
+    @Test
+    public void testInsertEmoji_andVerify() {
+        String testSmsBodyEmoji = "\uD83D\uDE0D\uD83D\uDE02"
+                + "\uD83D\uDE1B\uD83D\uDE00\uD83D\uDE1E☺️\uD83D\uDE1B"
+                + "\uD83D\uDE1E☺️\uD83D\uDE0D";
+
+        Uri uri = mSmsTestHelper.insertTestSms(TEST_ADDRESS, testSmsBodyEmoji);
+
+        assertThat(uri).isNotNull();
+
+        mSmsTestHelper.assertSmsColumnEquals(Telephony.Sms.BODY, uri,
+                String.valueOf(testSmsBodyEmoji));
+    }
+
+}
+
diff --git a/tests/tests/telephonyprovider/src/android/telephonyprovider/cts/SmsTestHelper.java b/tests/tests/telephonyprovider/src/android/telephonyprovider/cts/SmsTestHelper.java
new file mode 100644
index 0000000..fc668e2
--- /dev/null
+++ b/tests/tests/telephonyprovider/src/android/telephonyprovider/cts/SmsTestHelper.java
@@ -0,0 +1,117 @@
+/*
+ * Copyright (C) 2019 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.telephonyprovider.cts;
+
+import static androidx.test.InstrumentationRegistry.getInstrumentation;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.content.ContentResolver;
+import android.content.ContentValues;
+import android.database.Cursor;
+import android.net.Uri;
+import android.provider.Telephony;
+
+class SmsTestHelper {
+
+    private ContentResolver mContentResolver;
+    private ContentValues mContentValues;
+    public static final String SMS_ADDRESS_BODY_1 = "sms CTS text 1",
+            SMS_ADDRESS_BODY_2 = "sms CTS text 2";
+    SmsTestHelper() {
+        mContentResolver = getInstrumentation().getContext().getContentResolver();
+        mContentValues = new ContentValues();
+    }
+
+    public Uri insertTestSms(String testAddress, String testSmsBody) {
+        mContentValues.put(Telephony.Sms.ADDRESS, testAddress);
+        mContentValues.put(Telephony.Sms.BODY, testSmsBody);
+        return mContentResolver.insert(Telephony.Sms.CONTENT_URI, mContentValues);
+    }
+
+    public ContentValues createSmsValues(String messageBody) {
+        ContentValues smsRow = new ContentValues();
+        smsRow.put(Telephony.Sms.BODY, messageBody);
+        return smsRow;
+    }
+
+    public Uri insertTestSmsWithThread(String testAddress, String testSmsBody, int threadId) {
+        mContentValues.put(Telephony.Sms.ADDRESS, testAddress);
+        mContentValues.put(Telephony.Sms.BODY, testSmsBody);
+        mContentValues.put(Telephony.Sms.THREAD_ID, threadId);
+
+        return mContentResolver.insert(Telephony.Sms.CONTENT_URI, mContentValues);
+    }
+    /**
+     * Asserts the verify message details
+     */
+    public void assertSmsColumnEquals(String columnName, Uri uri, String expectedValue) {
+        Cursor cursor = mContentResolver.query(uri, null, null, null);
+
+        assertThat(cursor.getCount()).isEqualTo(1);
+        assertThat(cursor.moveToNext()).isEqualTo(true);
+
+        assertThat(cursor.getString(cursor.getColumnIndex(columnName))).isEqualTo(expectedValue);
+    }
+
+    public void assertBulkSmsContentEqual(int count, ContentValues[] smsContentValues) {
+        Cursor cursor = mContentResolver.query(Telephony.Sms.CONTENT_URI, null, null, null);
+
+        assertThat(count).isEqualTo(smsContentValues.length);
+        assertThat(cursor.getCount()).isEqualTo(smsContentValues.length);
+
+        assertThat(cursor.moveToNext()).isEqualTo(true);
+        assertThat(cursor.getString(cursor.getColumnIndex(Telephony.Sms.BODY)))
+                .isEqualTo(SMS_ADDRESS_BODY_2);
+
+        assertThat(cursor.moveToNext()).isEqualTo(true);
+        assertThat(cursor.getString(cursor.getColumnIndex(Telephony.Sms.BODY)))
+                .isEqualTo(SMS_ADDRESS_BODY_1);
+
+    }
+    public void assertUpdateSmsStatus(int status, Uri uri) {
+        ContentValues mContentValues = new ContentValues();
+        mContentValues.put(Telephony.Sms.STATUS, status);
+
+        int cursorUpdate = mContentResolver.update(uri, mContentValues, null, null);
+        assertThat(cursorUpdate).isEqualTo(1);
+
+        Cursor cursorStatus = mContentResolver.query(uri, null, null, null);
+        assertThat(cursorStatus.getCount()).isEqualTo(1);
+        cursorStatus.moveToNext();
+
+        int statusResult = cursorStatus.getInt(cursorStatus.getColumnIndex(Telephony.Sms.STATUS));
+        assertThat(statusResult).isAnyOf(0, 32, 64);
+        assertThat(statusResult).isEqualTo(status);
+    }
+
+    public void assertUpdateSmsType(int type, Uri uri) {
+        mContentValues.put(Telephony.Sms.TYPE, type);
+
+        int cursorUpdate = mContentResolver.update(uri, mContentValues, null, null);
+        assertThat(cursorUpdate).isEqualTo(1);
+
+        Cursor cursorStatus = mContentResolver.query(uri, null, null, null);
+        assertThat(cursorStatus.getCount()).isEqualTo(1);
+        cursorStatus.moveToNext();
+
+        int typeResult = cursorStatus.getInt(cursorStatus.getColumnIndex(Telephony.Sms.TYPE));
+        assertThat(typeResult).isAnyOf(1, 2);
+        assertThat(typeResult).isEqualTo(type);
+    }
+}
+