Add CTS tests for default dialer permissions

Tests permissions for the following APIs:
Voicemail read/update/delete
TelecomManager.silenceRinger
TelecomManager.cancelMissedCallsNotification
TelecomManager.handlePinMmi
TelecomManager.getAdnForPhoneAccount

Bug: 20550848
Bug: 20303674
Change-Id: I4604d02b409329fffbc260b3212f3e9f883d7380
diff --git a/tests/tests/telecom/AndroidManifest.xml b/tests/tests/telecom/AndroidManifest.xml
index 91c22f1..700f398 100644
--- a/tests/tests/telecom/AndroidManifest.xml
+++ b/tests/tests/telecom/AndroidManifest.xml
@@ -20,6 +20,8 @@
     <uses-permission android:name="android.permission.CALL_PHONE" />>
     <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
+    <uses-permission android:name="com.android.voicemail.permission.ADD_VOICEMAIL" />
+
     <application>
         <uses-library android:name="android.test.runner" />
 
diff --git a/tests/tests/telecom/src/android/telecom/cts/DefaultDialerOperationsTest.java b/tests/tests/telecom/src/android/telecom/cts/DefaultDialerOperationsTest.java
new file mode 100644
index 0000000..5685bf8
--- /dev/null
+++ b/tests/tests/telecom/src/android/telecom/cts/DefaultDialerOperationsTest.java
@@ -0,0 +1,149 @@
+/*
+ * Copyright (C) 2015 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.telecom.cts;
+
+import android.content.ContentValues;
+import android.content.Context;
+import android.provider.VoicemailContract.Voicemails;
+import android.telecom.PhoneAccountHandle;
+import android.telecom.TelecomManager;
+import android.test.InstrumentationTestCase;
+import android.text.TextUtils;
+
+import java.util.List;
+
+
+/**
+ * Verifies that certain privileged operations can only be performed by the default dialer.
+ */
+public class DefaultDialerOperationsTest extends InstrumentationTestCase {
+    private Context mContext;
+    private TelecomManager mTelecomManager;
+    private PhoneAccountHandle mPhoneAccountHandle;
+    private String mPreviousDefaultDialer = null;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        mContext = getInstrumentation().getContext();
+        mPreviousDefaultDialer = TestUtils.getDefaultDialer(getInstrumentation());
+        mTelecomManager = (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE);
+        final List<PhoneAccountHandle> accounts = mTelecomManager.getCallCapablePhoneAccounts();
+        if (accounts != null && !accounts.isEmpty()) {
+            mPhoneAccountHandle = accounts.get(0);
+        }
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        if (!TextUtils.isEmpty(mPreviousDefaultDialer)) {
+            TestUtils.setDefaultDialer(getInstrumentation(), mPreviousDefaultDialer);
+        }
+        super.tearDown();
+    }
+
+    public void testVoicemailReadWrite_correctlyThrowsSecurityException() throws Exception {
+        try {
+            mContext.getContentResolver().query(Voicemails.CONTENT_URI, null, null, null, null);
+            fail("Reading voicemails should throw SecurityException if not default Dialer");
+        } catch (SecurityException e) {
+        }
+
+        try {
+            mContext.getContentResolver().delete(Voicemails.CONTENT_URI,
+                    Voicemails._ID + "=999 AND 1=2", null);
+            fail("Deleting voicemails should throw SecurityException if not default Dialer");
+        } catch (SecurityException e) {
+        }
+
+        try {
+            mContext.getContentResolver().update(
+                    Voicemails.CONTENT_URI.buildUpon().appendPath("999").build(),
+                    new ContentValues(),
+                    null,
+                    null);
+            fail("Updating voicemails should throw SecurityException if not default Dialer");
+        } catch (SecurityException e) {
+        }
+
+        TestUtils.setDefaultDialer(getInstrumentation(), TestUtils.PACKAGE);
+        // No exception if the calling package is the default dialer.
+        mContext.getContentResolver().query(Voicemails.CONTENT_URI, null, null, null, null);
+        mContext.getContentResolver().delete(Voicemails.CONTENT_URI,
+                Voicemails._ID + "=999 AND 1=2", null);
+    }
+
+    public void testSilenceRinger_correctlyThrowsSecurityException() throws Exception {
+        try {
+            mTelecomManager.silenceRinger();
+            fail("TelecomManager.silenceRinger should throw SecurityException if not default "
+                    + "dialer");
+        } catch (SecurityException e) {
+        }
+
+        TestUtils.setDefaultDialer(getInstrumentation(), TestUtils.PACKAGE);
+        // No exception if the calling package is the default dialer.
+        mTelecomManager.silenceRinger();
+    }
+
+    public void testCancelMissedCallsNotification_correctlyThrowsSecurityException()
+            throws Exception {
+        try {
+            mTelecomManager.cancelMissedCallsNotification();
+            fail("TelecomManager.cancelMissedCallsNotification should throw SecurityException if "
+                    + "not default dialer");
+        } catch (SecurityException e) {
+        }
+
+        TestUtils.setDefaultDialer(getInstrumentation(), TestUtils.PACKAGE);
+        // No exception if the calling package is the default dialer.
+        mTelecomManager.cancelMissedCallsNotification();
+    }
+
+    public void testHandlePinMmi_correctlyThrowsSecurityException()
+            throws Exception {
+        try {
+            mTelecomManager.handleMmi("0");
+            fail("TelecomManager.handleMmi should throw SecurityException if not default dialer");
+        } catch (SecurityException e) {
+        }
+
+        try {
+            mTelecomManager.handleMmi("0", mPhoneAccountHandle);
+            fail("TelecomManager.handleMmi should throw SecurityException if not default dialer");
+        } catch (SecurityException e) {
+        }
+
+        TestUtils.setDefaultDialer(getInstrumentation(), TestUtils.PACKAGE);
+        // No exception if the calling package is the default dialer.
+        mTelecomManager.handleMmi("0");
+        mTelecomManager.handleMmi("0", mPhoneAccountHandle);
+    }
+
+    public void testGetAdnForPhoneAccount_correctlyThrowsSecurityException() throws Exception {
+        try {
+            mTelecomManager.getAdnUriForPhoneAccount(mPhoneAccountHandle);
+            fail("TelecomManager.getAdnUriForPhoneAccount should throw SecurityException if "
+                    + "not default dialer");
+        } catch (SecurityException e) {
+        }
+
+        TestUtils.setDefaultDialer(getInstrumentation(), TestUtils.PACKAGE);
+        // No exception if the calling package is the default dialer.
+        mTelecomManager.getAdnUriForPhoneAccount(mPhoneAccountHandle);
+    }
+}