Home | History | Annotate | Download | only in cts
      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 static android.telecom.cts.TestUtils.*;
     20 
     21 import android.content.ComponentName;
     22 import android.content.Context;
     23 import android.media.AudioManager;
     24 import android.net.Uri;
     25 import android.telecom.Call;
     26 import android.telecom.Connection;
     27 import android.telecom.ConnectionService;
     28 import android.telecom.PhoneAccountHandle;
     29 
     30 import java.util.Collection;
     31 
     32 /**
     33  * Test some additional {@link ConnectionService} and {@link Connection} APIs not already covered
     34  * by other tests.
     35  */
     36 public class ConnectionServiceTest extends BaseTelecomTestWithMockServices {
     37 
     38     private static final Uri SELF_MANAGED_TEST_ADDRESS =
     39             Uri.fromParts("sip", "call1 (at) test.com", null);
     40 
     41     @Override
     42     protected void setUp() throws Exception {
     43         super.setUp();
     44         mContext = getInstrumentation().getContext();
     45         if (mShouldTestTelecom) {
     46             setupConnectionService(null, FLAG_REGISTER | FLAG_ENABLE);
     47             mTelecomManager.registerPhoneAccount(TestUtils.TEST_SELF_MANAGED_PHONE_ACCOUNT_1);
     48         }
     49     }
     50 
     51     public void testAddExistingConnection() {
     52         if (!mShouldTestTelecom) {
     53             return;
     54         }
     55 
     56         placeAndVerifyCall();
     57         verifyConnectionForOutgoingCall();
     58 
     59         // Add second connection (add existing connection)
     60         final MockConnection connection = new MockConnection();
     61         connection.setOnHold();
     62         CtsConnectionService.addExistingConnectionToTelecom(TEST_PHONE_ACCOUNT_HANDLE, connection);
     63         assertNumCalls(mInCallCallbacks.getService(), 2);
     64         mInCallCallbacks.lock.drainPermits();
     65         final Call call = mInCallCallbacks.getService().getLastCall();
     66         assertCallState(call, Call.STATE_HOLDING);
     67     }
     68 
     69     public void testAddExistingConnection_invalidPhoneAccountPackageName() {
     70         if (!mShouldTestTelecom) {
     71             return;
     72         }
     73 
     74         placeAndVerifyCall();
     75         verifyConnectionForOutgoingCall();
     76 
     77         // Add second connection (add existing connection)
     78         final MockConnection connection = new MockConnection();
     79         connection.setOnHold();
     80         ComponentName invalidName = new ComponentName("com.android.phone",
     81                 "com.android.services.telephony.TelephonyConnectionService");
     82         // This command will fail and a SecurityException will be thrown by Telecom. The Exception
     83         // will then be absorbed by the ConnectionServiceAdapter.
     84         CtsConnectionService.addExistingConnectionToTelecom(new PhoneAccountHandle(invalidName,
     85                 "Test"), connection);
     86         // Make sure that only the original Call exists.
     87         assertNumCalls(mInCallCallbacks.getService(), 1);
     88         mInCallCallbacks.lock.drainPermits();
     89         final Call call = mInCallCallbacks.getService().getLastCall();
     90         assertCallState(call, Call.STATE_DIALING);
     91     }
     92 
     93     public void testAddExistingConnection_invalidPhoneAccountAccountId() {
     94         if (!mShouldTestTelecom) {
     95             return;
     96         }
     97 
     98         placeAndVerifyCall();
     99         verifyConnectionForOutgoingCall();
    100 
    101         // Add second connection (add existing connection)
    102         final MockConnection connection = new MockConnection();
    103         connection.setOnHold();
    104         ComponentName validName = new ComponentName(PACKAGE, COMPONENT);
    105         // This command will fail because the PhoneAccount is not registered to Telecom currently.
    106         CtsConnectionService.addExistingConnectionToTelecom(new PhoneAccountHandle(validName,
    107                 "Invalid Account Id"), connection);
    108         // Make sure that only the original Call exists.
    109         assertNumCalls(mInCallCallbacks.getService(), 1);
    110         mInCallCallbacks.lock.drainPermits();
    111         final Call call = mInCallCallbacks.getService().getLastCall();
    112         assertCallState(call, Call.STATE_DIALING);
    113     }
    114 
    115     public void testVoipAudioModePropagation() throws Exception {
    116         if (!mShouldTestTelecom) {
    117             return;
    118         }
    119 
    120         placeAndVerifyCall();
    121         MockConnection connection = verifyConnectionForOutgoingCall();
    122         connection.setAudioModeIsVoip(true);
    123         waitOnAllHandlers(getInstrumentation());
    124 
    125         AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    126         assertEquals(AudioManager.MODE_IN_COMMUNICATION, audioManager.getMode());
    127         connection.setAudioModeIsVoip(false);
    128         waitOnAllHandlers(getInstrumentation());
    129         assertEquals(AudioManager.MODE_IN_CALL, audioManager.getMode());
    130     }
    131 
    132     public void testConnectionServiceFocusGainedWithNoConnectionService() {
    133         if (!mShouldTestTelecom) {
    134             return;
    135         }
    136 
    137         // WHEN place a managed call
    138         placeAndVerifyCall();
    139 
    140         // THEN managed connection service has gained the focus
    141         assertTrue(connectionService.waitForEvent(
    142                 MockConnectionService.EVENT_CONNECTION_SERVICE_FOCUS_GAINED));
    143     }
    144 
    145     public void testConnectionServiceFocusGainedWithSameConnectionService() {
    146         if (!mShouldTestTelecom) {
    147             return;
    148         }
    149 
    150         // GIVEN a managed call
    151         placeAndVerifyCall();
    152         verifyConnectionForOutgoingCall().setActive();
    153         assertTrue(connectionService.waitForEvent(
    154                 MockConnectionService.EVENT_CONNECTION_SERVICE_FOCUS_GAINED));
    155 
    156         // WHEN place another call has the same ConnectionService as the existing call
    157         placeAndVerifyCall();
    158         verifyConnectionForOutgoingCall();
    159 
    160         // THEN the ConnectionService has not gained the focus again
    161         assertFalse(connectionService.waitForEvent(
    162                 MockConnectionService.EVENT_CONNECTION_SERVICE_FOCUS_GAINED));
    163         // and the ConnectionService didn't lose the focus
    164         assertFalse(connectionService.waitForEvent(
    165                 MockConnectionService.EVENT_CONNECTION_SERVICE_FOCUS_LOST));
    166     }
    167 
    168     public void testConnectionServiceFocusGainedWithDifferentConnectionService() {
    169         if (!mShouldTestTelecom) {
    170             return;
    171         }
    172 
    173         // GIVEN an existing managed call
    174         placeAndVerifyCall();
    175         verifyConnectionForOutgoingCall().setActive();
    176         assertTrue(connectionService.waitForEvent(
    177                 MockConnectionService.EVENT_CONNECTION_SERVICE_FOCUS_GAINED));
    178 
    179         // WHEN a self-managed call is coming
    180         SelfManagedConnection selfManagedConnection =
    181                 addIncomingSelfManagedCall(TEST_SELF_MANAGED_HANDLE_1, SELF_MANAGED_TEST_ADDRESS);
    182 
    183         // THEN the managed ConnectionService has lost the focus
    184         assertTrue(connectionService.waitForEvent(
    185                 MockConnectionService.EVENT_CONNECTION_SERVICE_FOCUS_LOST));
    186         // and the self-managed ConnectionService has gained the focus
    187         assertTrue(CtsSelfManagedConnectionService.getConnectionService().waitForUpdate(
    188                         CtsSelfManagedConnectionService.FOCUS_GAINED_LOCK));
    189 
    190         // Disconnected the self-managed call
    191         selfManagedConnection.disconnectAndDestroy();
    192     }
    193 
    194     private SelfManagedConnection addIncomingSelfManagedCall(
    195             PhoneAccountHandle pah, Uri address) {
    196 
    197         TestUtils.addIncomingCall(getInstrumentation(), mTelecomManager, pah, address);
    198 
    199         // Ensure Telecom bound to the self managed CS
    200         if (!CtsSelfManagedConnectionService.waitForBinding()) {
    201             fail("Could not bind to Self-Managed ConnectionService");
    202         }
    203 
    204         SelfManagedConnection connection = TestUtils.waitForAndGetConnection(address);
    205 
    206         // Active the call
    207         connection.setActive();
    208 
    209         return connection;
    210     }
    211 
    212     public void testGetAllConnections() {
    213         if (!mShouldTestTelecom) {
    214             return;
    215         }
    216 
    217         // Add first connection (outgoing call)
    218         placeAndVerifyCall();
    219         final Connection connection1 = verifyConnectionForOutgoingCall();
    220 
    221         Collection<Connection> connections = CtsConnectionService.getAllConnectionsFromTelecom();
    222         assertEquals(1, connections.size());
    223         assertTrue(connections.contains(connection1));
    224         // Need to move this to active since we reject the 3rd incoming call below if this is in
    225         // dialing state (b/23428950).
    226         connection1.setActive();
    227         assertCallState(mInCallCallbacks.getService().getLastCall(), Call.STATE_ACTIVE);
    228 
    229         // Add second connection (add existing connection)
    230         final Connection connection2 = new MockConnection();
    231         connection2.setActive();
    232         CtsConnectionService.addExistingConnectionToTelecom(TEST_PHONE_ACCOUNT_HANDLE, connection2);
    233         assertNumCalls(mInCallCallbacks.getService(), 2);
    234         mInCallCallbacks.lock.drainPermits();
    235         connections = CtsConnectionService.getAllConnectionsFromTelecom();
    236         assertEquals(2, connections.size());
    237         assertTrue(connections.contains(connection2));
    238 
    239         // Add third connection (incoming call)
    240         addAndVerifyNewIncomingCall(createTestNumber(), null);
    241         final Connection connection3 = verifyConnectionForIncomingCall();
    242         connections = CtsConnectionService.getAllConnectionsFromTelecom();
    243         assertEquals(3, connections.size());
    244         assertTrue(connections.contains(connection3));
    245     }
    246 }
    247