Add READ_PHONE_STATE permission CTS tests

READ_PHONE_STATE permission tests need to be in a separate
package that doesn't have the READ_PHONE_STATE permission in its
manifest.

Also rename existing DefaultDialer tests to make them
less verbose.

Bug: 20303674

Change-Id: I84ff215db48384805d0a14920a3fbec008afc73b
diff --git a/CtsTestCaseList.mk b/CtsTestCaseList.mk
index 4e1e008..f683840 100644
--- a/CtsTestCaseList.mk
+++ b/CtsTestCaseList.mk
@@ -165,6 +165,7 @@
     CtsSignatureTestCases \
     CtsSpeechTestCases \
     CtsTelecomTestCases \
+    CtsTelecomTestCases2 \
     CtsTelephonyTestCases \
     CtsTextTestCases \
     CtsTextureViewTestCases \
diff --git a/tests/tests/telecom/src/android/telecom/cts/DefaultDialerOperationsTest.java b/tests/tests/telecom/src/android/telecom/cts/DefaultDialerOperationsTest.java
index 947ebc2..b2d6fb6 100644
--- a/tests/tests/telecom/src/android/telecom/cts/DefaultDialerOperationsTest.java
+++ b/tests/tests/telecom/src/android/telecom/cts/DefaultDialerOperationsTest.java
@@ -62,7 +62,7 @@
         assertEquals(TestUtils.PACKAGE, mTelecomManager.getDefaultDialerPackage());
     }
 
-    public void testVoicemailReadWrite_correctlyThrowsSecurityException() throws Exception {
+    public void testVoicemailReadWritePermissions() throws Exception {
         try {
             mContext.getContentResolver().query(Voicemails.CONTENT_URI, null, null, null, null);
             fail("Reading voicemails should throw SecurityException if not default Dialer");
@@ -93,7 +93,7 @@
                 Voicemails._ID + "=999 AND 1=2", null);
     }
 
-    public void testSilenceRinger_correctlyThrowsSecurityException() throws Exception {
+    public void testSilenceRingerPermissions() throws Exception {
         try {
             mTelecomManager.silenceRinger();
             fail("TelecomManager.silenceRinger should throw SecurityException if not default "
@@ -106,7 +106,7 @@
         mTelecomManager.silenceRinger();
     }
 
-    public void testCancelMissedCallsNotification_correctlyThrowsSecurityException()
+    public void testCancelMissedCallsNotificationPermissions()
             throws Exception {
         try {
             mTelecomManager.cancelMissedCallsNotification();
@@ -120,7 +120,7 @@
         mTelecomManager.cancelMissedCallsNotification();
     }
 
-    public void testHandlePinMmi_correctlyThrowsSecurityException()
+    public void testHandlePinMmPermissions()
             throws Exception {
         try {
             mTelecomManager.handleMmi("0");
@@ -140,7 +140,7 @@
         mTelecomManager.handleMmi("0", mPhoneAccountHandle);
     }
 
-    public void testGetAdnForPhoneAccount_correctlyThrowsSecurityException() throws Exception {
+    public void testGetAdnForPhoneAccountPermissions() throws Exception {
         try {
             mTelecomManager.getAdnUriForPhoneAccount(mPhoneAccountHandle);
             fail("TelecomManager.getAdnUriForPhoneAccount should throw SecurityException if "
diff --git a/tests/tests/telecom/src/android/telecom/cts/TestUtils.java b/tests/tests/telecom/src/android/telecom/cts/TestUtils.java
index 8cca04c..a178b66 100644
--- a/tests/tests/telecom/src/android/telecom/cts/TestUtils.java
+++ b/tests/tests/telecom/src/android/telecom/cts/TestUtils.java
@@ -34,7 +34,9 @@
     static final boolean HAS_TELECOM = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
     static final long WAIT_FOR_STATE_CHANGE_TIMEOUT_MS = 10000;
 
-    public static final String PACKAGE = "com.android.cts.telecom";
+    // Non-final to allow modification by tests not in this package (e.g. permission-related
+    // tests in the Telecom2 test package.
+    public static String PACKAGE = "com.android.cts.telecom";
     public static final String COMPONENT = "android.telecom.cts.MockConnectionService";
     public static final String ACCOUNT_ID = "xtstest_CALL_PROVIDER_ID";
 
diff --git a/tests/tests/telecom2/Android.mk b/tests/tests/telecom2/Android.mk
new file mode 100644
index 0000000..fa23492
--- /dev/null
+++ b/tests/tests/telecom2/Android.mk
@@ -0,0 +1,36 @@
+# 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.
+
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_PACKAGE_NAME := CtsTelecomTestCases2
+
+# Don't include this package in any target.
+LOCAL_MODULE_TAGS := optional
+
+# When built, explicitly put it in the data partition.
+LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
+
+LOCAL_STATIC_JAVA_LIBRARIES := ctstestrunner
+
+src_dirs := src \
+    ../telecom/src
+
+LOCAL_SRC_FILES := $(call all-java-files-under, $(src_dirs))
+
+LOCAL_SDK_VERSION := current
+
+include $(BUILD_CTS_PACKAGE)
diff --git a/tests/tests/telecom2/AndroidManifest.xml b/tests/tests/telecom2/AndroidManifest.xml
new file mode 100644
index 0000000..e618768
--- /dev/null
+++ b/tests/tests/telecom2/AndroidManifest.xml
@@ -0,0 +1,83 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 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.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.android.cts.telecom2">
+    <uses-sdk android:minSdkVersion="21" />
+
+    <!--
+        This app contains tests to verify Telecom's behavior when the app is missing certain
+        permissions.
+    -->
+
+    <application>
+        <uses-library android:name="android.test.runner" />
+
+        <service android:name="android.telecom.cts.MockConnectionService"
+            android:permission="android.permission.BIND_TELECOM_CONNECTION_SERVICE" >
+            <intent-filter>
+                <action android:name="android.telecom.ConnectionService" />
+            </intent-filter>
+        </service>
+
+        <service android:name="android.telecom.cts.MockInCallService"
+            android:permission="android.permission.BIND_INCALL_SERVICE" >
+            <intent-filter>
+                <action android:name="android.telecom.InCallService"/>
+            </intent-filter>
+        </service>
+
+        <activity android:name="android.telecom.cts.MockDialerActivity">
+            <intent-filter>
+                <action android:name="android.intent.action.DIAL" />
+                <category android:name="android.intent.category.DEFAULT" />
+                <category android:name="android.intent.category.BROWSABLE" />
+                <data android:mimeType="vnd.android.cursor.item/phone" />
+                <data android:mimeType="vnd.android.cursor.item/person" />
+            </intent-filter>
+            <intent-filter>
+                <action android:name="android.intent.action.DIAL" />
+                <category android:name="android.intent.category.DEFAULT" />
+                <category android:name="android.intent.category.BROWSABLE" />
+                <data android:scheme="voicemail" />
+            </intent-filter>
+            <intent-filter>
+                <action android:name="android.intent.action.DIAL" />
+                <category android:name="android.intent.category.DEFAULT" />
+            </intent-filter>
+            <intent-filter>
+                <action android:name="android.intent.action.VIEW" />
+                <action android:name="android.intent.action.DIAL" />
+                <category android:name="android.intent.category.DEFAULT" />
+                <category android:name="android.intent.category.BROWSABLE" />
+                <data android:scheme="tel" />
+            </intent-filter>
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.DEFAULT" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+    </application>
+
+    <instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
+                     android:targetPackage="com.android.cts.telecom2"
+                     android:label="CTS tests for android.telecom package">
+        <meta-data android:name="listener"
+            android:value="com.android.cts.runner.CtsTestRunListener" />
+    </instrumentation>
+</manifest>
+
diff --git a/tests/tests/telecom2/src/android/telecom/cts/DefaultDialerOperationsNoPermissionsTest.java b/tests/tests/telecom2/src/android/telecom/cts/DefaultDialerOperationsNoPermissionsTest.java
new file mode 100644
index 0000000..4629992
--- /dev/null
+++ b/tests/tests/telecom2/src/android/telecom/cts/DefaultDialerOperationsNoPermissionsTest.java
@@ -0,0 +1,115 @@
+/*
+ * 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.Context;
+import android.telecom.TelecomManager;
+import android.test.InstrumentationTestCase;
+import android.text.TextUtils;
+
+/**
+ * Verifies that certain privileged operations can only be performed by the default dialer.
+ */
+public class DefaultDialerOperationsNoPermissionsTest extends InstrumentationTestCase {
+    private Context mContext;
+    private TelecomManager mTelecomManager;
+    private String mPreviousDefaultDialer = null;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        mContext = getInstrumentation().getContext();
+        TestUtils.PACKAGE = mContext.getPackageName();
+        mPreviousDefaultDialer = TestUtils.getDefaultDialer(getInstrumentation());
+        mTelecomManager = (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE);
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        if (!TextUtils.isEmpty(mPreviousDefaultDialer)) {
+            TestUtils.setDefaultDialer(getInstrumentation(), mPreviousDefaultDialer);
+        }
+        super.tearDown();
+    }
+
+    public void testShowInCallScreenPermissions() throws Exception {
+        verifyForReadPhoneStateOrDefaultDialer(new Runnable() {
+            @Override
+            public void run() {
+                mTelecomManager.showInCallScreen(false);
+            }
+        }, "showInCallScreen");
+    }
+
+    public void testGetCallCapableAccountsPermissions() throws Exception {
+        verifyForReadPhoneStateOrDefaultDialer(new Runnable() {
+            @Override
+            public void run() {
+                mTelecomManager.getCallCapablePhoneAccounts();
+            }
+        }, "getCallCapableAccounts");
+    }
+
+    public void testGetDefaultOutgoingPhoneAccount() throws Exception {
+        verifyForReadPhoneStateOrDefaultDialer(new Runnable() {
+            @Override
+            public void run() {
+                mTelecomManager.getDefaultOutgoingPhoneAccount("tel");
+            }
+        }, "getDefaultOutgoingPhoneAccount");
+    }
+
+    public void testGetLine1Number() throws Exception {
+        verifyForReadPhoneStateOrDefaultDialer(new Runnable() {
+            @Override
+            public void run() {
+                mTelecomManager.getLine1Number(null);
+            }
+        }, "getLine1Number");
+    }
+
+    public void testGetVoicemailNumber() throws Exception {
+        verifyForReadPhoneStateOrDefaultDialer(new Runnable() {
+            @Override
+            public void run() {
+                mTelecomManager.getVoiceMailNumber(null);
+            }
+        }, "getVoiceMailNumber");
+    }
+
+    public void testIsInCall() throws Exception {
+        verifyForReadPhoneStateOrDefaultDialer(new Runnable() {
+            @Override
+            public void run() {
+                mTelecomManager.isInCall();
+            }
+        }, "isInCall");
+    }
+
+    private void verifyForReadPhoneStateOrDefaultDialer(Runnable runnable, String methodName)
+            throws Exception{
+        try {
+            runnable.run();
+            fail("TelecomManager." + methodName + " should throw SecurityException if no "
+                    + "READ_PHONE_STATE permission");
+        } catch (SecurityException e) {
+        }
+
+        TestUtils.setDefaultDialer(getInstrumentation(), TestUtils.PACKAGE);
+        runnable.run();
+    }
+}