Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2016 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 static android.telecom.cts.TestUtils.COMPONENT;
     20 import static android.telecom.cts.TestUtils.PACKAGE;
     21 
     22 import android.content.ComponentName;
     23 import android.net.Uri;
     24 import android.os.Bundle;
     25 import android.telecom.Connection;
     26 import android.telecom.PhoneAccountHandle;
     27 import android.telecom.TelecomManager;
     28 import android.telephony.TelephonyManager;
     29 
     30 import java.util.Collection;
     31 
     32 /**
     33  * Tests valid/invalid incoming calls that are received from the ConnectionService
     34  * and registered through TelecomManager
     35  */
     36 public class IncomingCallTest extends BaseTelecomTestWithMockServices {
     37 
     38     private static final PhoneAccountHandle TEST_INVALID_HANDLE = new PhoneAccountHandle(
     39             new ComponentName(PACKAGE, COMPONENT), "WRONG_ID");
     40 
     41     public void testAddNewIncomingCall_CorrectPhoneAccountHandle() throws Exception {
     42         if (!mShouldTestTelecom) {
     43             return;
     44         }
     45         setupConnectionService(null, FLAG_REGISTER | FLAG_ENABLE);
     46         addAndVerifyNewIncomingCall(createTestNumber(), null);
     47         final Connection connection3 = verifyConnectionForIncomingCall();
     48         Collection<Connection> connections = CtsConnectionService.getAllConnectionsFromTelecom();
     49         assertEquals(1, connections.size());
     50         assertTrue(connections.contains(connection3));
     51     }
     52 
     53     public void testPhoneStateListenerInvokedOnIncomingCall() throws Exception {
     54         if (!mShouldTestTelecom) {
     55             return;
     56         }
     57         setupConnectionService(null, FLAG_REGISTER | FLAG_ENABLE);
     58         Uri testNumber = createTestNumber();
     59         addAndVerifyNewIncomingCall(testNumber, null);
     60         verifyConnectionForIncomingCall();
     61         verifyPhoneStateListenerCallbacksForCall(TelephonyManager.CALL_STATE_RINGING,
     62                 testNumber.getSchemeSpecificPart());
     63     }
     64 
     65     /**
     66      * Tests to be sure that new incoming calls can only be added using a valid PhoneAccountHandle
     67      * (b/26864502). If a PhoneAccount has not been registered for the PhoneAccountHandle, then
     68      * a SecurityException will be thrown.
     69      */
     70     public void testAddNewIncomingCall_IncorrectPhoneAccountHandle() {
     71         if (!mShouldTestTelecom) {
     72             return;
     73         }
     74 
     75         Bundle extras = new Bundle();
     76         extras.putParcelable(TelecomManager.EXTRA_INCOMING_CALL_ADDRESS, createTestNumber());
     77         try {
     78             mTelecomManager.addNewIncomingCall(TEST_INVALID_HANDLE, extras);
     79             fail();
     80         } catch (SecurityException e) {
     81             // This should create a security exception!
     82         }
     83 
     84         assertFalse(CtsConnectionService.isServiceRegisteredToTelecom());
     85     }
     86 
     87     /**
     88      * Tests to be sure that new incoming calls can only be added if a PhoneAccount is enabled
     89      * (b/26864502). If a PhoneAccount is not enabled for the PhoneAccountHandle, then
     90      * a SecurityException will be thrown.
     91      */
     92     public void testAddNewIncomingCall_PhoneAccountNotEnabled() throws Exception {
     93         if (!mShouldTestTelecom) {
     94             return;
     95         }
     96 
     97         // Do not enable PhoneAccount
     98         setupConnectionService(null, FLAG_REGISTER);
     99         assertFalse(mTelecomManager.getPhoneAccount(TestUtils.TEST_PHONE_ACCOUNT_HANDLE)
    100                 .isEnabled());
    101         try {
    102             addAndVerifyNewIncomingCall(createTestNumber(), null);
    103             fail();
    104         } catch (SecurityException e) {
    105             // This should create a security exception!
    106         }
    107 
    108         assertFalse(CtsConnectionService.isServiceRegisteredToTelecom());
    109     }
    110 }
    111