1 /* 2 * Copyright (C) 2015 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.telecom.cts; 18 19 import android.content.Context; 20 import android.telecom.TelecomManager; 21 import android.test.InstrumentationTestCase; 22 import android.text.TextUtils; 23 24 /** 25 * Verifies that certain privileged operations can only be performed by the default dialer. 26 */ 27 public class DefaultDialerOperationsNoPermissionsTest extends InstrumentationTestCase { 28 private Context mContext; 29 private TelecomManager mTelecomManager; 30 private String mPreviousDefaultDialer = null; 31 private String mSystemDialer = null; 32 33 @Override 34 protected void setUp() throws Exception { 35 super.setUp(); 36 mContext = getInstrumentation().getContext(); 37 if (!TestUtils.shouldTestTelecom(mContext)) { 38 return; 39 } 40 TestUtils.PACKAGE = mContext.getPackageName(); 41 mPreviousDefaultDialer = TestUtils.getDefaultDialer(getInstrumentation()); 42 // Reset the current dialer to the system dialer, to ensure that we start each test 43 // without being the default dialer. 44 mSystemDialer = TestUtils.getSystemDialer(getInstrumentation()); 45 if (!TextUtils.isEmpty(mSystemDialer)) { 46 TestUtils.setDefaultDialer(getInstrumentation(), mSystemDialer); 47 } 48 mTelecomManager = (TelecomManager) mContext.getSystemService(Context.TELECOM_SERVICE); 49 } 50 51 @Override 52 protected void tearDown() throws Exception { 53 if (!TextUtils.isEmpty(mPreviousDefaultDialer)) { 54 // Restore the default dialer to whatever the default dialer was before the tests 55 // were started. This may or may not be the system dialer. 56 TestUtils.setDefaultDialer(getInstrumentation(), mPreviousDefaultDialer); 57 } 58 super.tearDown(); 59 } 60 61 public void testShowInCallScreenPermissions() throws Exception { 62 if (!TestUtils.shouldTestTelecom(mContext)) { 63 return; 64 } 65 verifyForReadPhoneStateOrDefaultDialer(new Runnable() { 66 @Override 67 public void run() { 68 mTelecomManager.showInCallScreen(false); 69 } 70 }, "showInCallScreen"); 71 } 72 73 public void testGetCallCapableAccountsPermissions() throws Exception { 74 if (!TestUtils.shouldTestTelecom(mContext)) { 75 return; 76 } 77 verifyForReadPhoneStateOrDefaultDialer(new Runnable() { 78 @Override 79 public void run() { 80 mTelecomManager.getCallCapablePhoneAccounts(); 81 } 82 }, "getCallCapableAccounts"); 83 } 84 85 public void testGetDefaultOutgoingPhoneAccount() throws Exception { 86 if (!TestUtils.shouldTestTelecom(mContext)) { 87 return; 88 } 89 verifyForReadPhoneStateOrDefaultDialer(new Runnable() { 90 @Override 91 public void run() { 92 mTelecomManager.getDefaultOutgoingPhoneAccount("tel"); 93 } 94 }, "getDefaultOutgoingPhoneAccount"); 95 } 96 97 public void testGetLine1Number() throws Exception { 98 if (!TestUtils.shouldTestTelecom(mContext)) { 99 return; 100 } 101 verifyForReadPhoneStateOrDefaultDialer(new Runnable() { 102 @Override 103 public void run() { 104 mTelecomManager.getLine1Number(null); 105 } 106 }, "getLine1Number"); 107 } 108 109 public void testGetVoicemailNumber() throws Exception { 110 if (!TestUtils.shouldTestTelecom(mContext)) { 111 return; 112 } 113 verifyForReadPhoneStateOrDefaultDialer(new Runnable() { 114 @Override 115 public void run() { 116 mTelecomManager.getVoiceMailNumber(null); 117 } 118 }, "getVoiceMailNumber"); 119 } 120 121 public void testIsVoicemailNumber() throws Exception { 122 if (!TestUtils.shouldTestTelecom(mContext)) { 123 return; 124 } 125 verifyForReadPhoneStateOrDefaultDialer(new Runnable() { 126 @Override 127 public void run() { 128 mTelecomManager.isVoiceMailNumber(null, null); 129 } 130 }, "isVoiceMailNumber"); 131 } 132 133 public void testIsInCall() throws Exception { 134 if (!TestUtils.shouldTestTelecom(mContext)) { 135 return; 136 } 137 verifyForReadPhoneStateOrDefaultDialer(new Runnable() { 138 @Override 139 public void run() { 140 mTelecomManager.isInCall(); 141 } 142 }, "isInCall"); 143 } 144 145 private void verifyForReadPhoneStateOrDefaultDialer(Runnable runnable, String methodName) 146 throws Exception{ 147 try { 148 runnable.run(); 149 fail("TelecomManager." + methodName + " should throw SecurityException if no " 150 + "READ_PHONE_STATE permission"); 151 } catch (SecurityException e) { 152 } 153 154 TestUtils.setDefaultDialer(getInstrumentation(), TestUtils.PACKAGE); 155 runnable.run(); 156 } 157 } 158