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.graphics.SurfaceTexture;
     22 import android.net.Uri;
     23 import android.os.Bundle;
     24 import android.os.Handler;
     25 import android.os.HandlerThread;
     26 import android.os.Looper;
     27 import android.telecom.Call;
     28 import android.telecom.Connection;
     29 import android.telecom.ConnectionRequest;
     30 import android.telecom.DisconnectCause;
     31 import android.telecom.PhoneAccountHandle;
     32 import android.telecom.RemoteConnection;
     33 import android.telecom.RemoteConnection.VideoProvider;
     34 import android.telecom.StatusHints;
     35 import android.telecom.TelecomManager;
     36 import android.telecom.VideoProfile;
     37 import android.view.Surface;
     38 
     39 import java.util.ArrayList;
     40 import java.util.List;
     41 
     42 /**
     43  * Extended suite of tests that use {@link CtsConnectionService} and {@link MockInCallService} to
     44  * verify the functionality of Remote Connections.
     45  * We make 2 connections on the {@link CtsConnectionService} & we create 2 connections on the
     46  * {@link CtsRemoteConnectionService} via the {@link RemoteConnection} object. We store this
     47  * corresponding RemoteConnection object on the connections to plumb the modifications on
     48  * the connections in {@link CtsConnectionService} to the connections on
     49  * {@link CtsRemoteConnectionService}.
     50  */
     51 public class RemoteConnectionTest extends BaseRemoteTelecomTest {
     52 
     53     MockConnection mConnection;
     54     MockConnection mRemoteConnection;
     55     RemoteConnection mRemoteConnectionObject;
     56 
     57     public void testRemoteConnectionOutgoingCall() {
     58         if (!mShouldTestTelecom) {
     59             return;
     60         }
     61         addRemoteConnectionOutgoingCall();
     62         final Call call = mInCallCallbacks.getService().getLastCall();
     63         assertCallState(call, Call.STATE_DIALING);
     64 
     65         verifyRemoteConnectionObject(mRemoteConnectionObject, mRemoteConnection);
     66 
     67         mConnection.setActive();
     68         mRemoteConnection.setActive();
     69 
     70         assertCallState(call, Call.STATE_ACTIVE);
     71         assertConnectionState(mConnection, Connection.STATE_ACTIVE);
     72         assertRemoteConnectionState(mRemoteConnectionObject, Connection.STATE_ACTIVE);
     73         assertConnectionState(mRemoteConnection, Connection.STATE_ACTIVE);
     74 
     75         call.hold();
     76         assertCallState(call, Call.STATE_HOLDING);
     77         assertConnectionState(mConnection, Connection.STATE_HOLDING);
     78         assertRemoteConnectionState(mRemoteConnectionObject, Connection.STATE_HOLDING);
     79         assertConnectionState(mRemoteConnection, Connection.STATE_HOLDING);
     80 
     81         call.unhold();
     82         assertCallState(call, Call.STATE_ACTIVE);
     83         assertConnectionState(mConnection, Connection.STATE_ACTIVE);
     84         assertRemoteConnectionState(mRemoteConnectionObject, Connection.STATE_ACTIVE);
     85         assertConnectionState(mRemoteConnection, Connection.STATE_ACTIVE);
     86 
     87         call.disconnect();
     88         assertCallState(call, Call.STATE_DISCONNECTED);
     89         assertConnectionState(mConnection, Connection.STATE_DISCONNECTED);
     90         assertRemoteConnectionState(mRemoteConnectionObject, Connection.STATE_DISCONNECTED);
     91         assertConnectionState(mRemoteConnection, Connection.STATE_DISCONNECTED);
     92     }
     93 
     94     public void testRemoteConnectionIncomingCallAccept() {
     95         if (!mShouldTestTelecom) {
     96             return;
     97         }
     98         addRemoteConnectionIncomingCall();
     99         final Call call = mInCallCallbacks.getService().getLastCall();
    100         assertCallState(call, Call.STATE_RINGING);
    101 
    102         verifyRemoteConnectionObject(mRemoteConnectionObject, mRemoteConnection);
    103 
    104         assertConnectionState(mConnection, Connection.STATE_RINGING);
    105         assertRemoteConnectionState(mRemoteConnectionObject, Connection.STATE_RINGING);
    106         assertConnectionState(mRemoteConnection, Connection.STATE_RINGING);
    107 
    108         call.answer(VideoProfile.STATE_AUDIO_ONLY);
    109         assertCallState(call, Call.STATE_ACTIVE);
    110         assertConnectionState(mConnection, Connection.STATE_ACTIVE);
    111         assertRemoteConnectionState(mRemoteConnectionObject, Connection.STATE_ACTIVE);
    112         assertConnectionState(mRemoteConnection, Connection.STATE_ACTIVE);
    113     }
    114 
    115     public void testRemoteConnectionIncomingCallReject() {
    116         if (!mShouldTestTelecom) {
    117             return;
    118         }
    119         addRemoteConnectionIncomingCall();
    120         final Call call = mInCallCallbacks.getService().getLastCall();
    121         assertCallState(call, Call.STATE_RINGING);
    122 
    123         verifyRemoteConnectionObject(mRemoteConnectionObject, mRemoteConnection);
    124 
    125         assertConnectionState(mConnection, Connection.STATE_RINGING);
    126         assertRemoteConnectionState(mRemoteConnectionObject, Connection.STATE_RINGING);
    127         assertConnectionState(mRemoteConnection, Connection.STATE_RINGING);
    128 
    129         call.reject(false, null);
    130         assertCallState(call, Call.STATE_DISCONNECTED);
    131         assertConnectionState(mConnection, Connection.STATE_DISCONNECTED);
    132         assertRemoteConnectionState(mRemoteConnectionObject, Connection.STATE_DISCONNECTED);
    133         assertConnectionState(mRemoteConnection, Connection.STATE_DISCONNECTED);
    134     }
    135 
    136     public void testRemoteConnectionDTMFTone() {
    137         if (!mShouldTestTelecom) {
    138             return;
    139         }
    140         addRemoteConnectionIncomingCall();
    141         final Call call = mInCallCallbacks.getService().getLastCall();
    142         assertCallState(call, Call.STATE_RINGING);
    143 
    144         verifyRemoteConnectionObject(mRemoteConnectionObject, mRemoteConnection);
    145 
    146         assertTrue(mConnection.getDtmfString().isEmpty());
    147         assertTrue(mRemoteConnection.getDtmfString().isEmpty());
    148         call.playDtmfTone('1');
    149         assertDtmfString(mConnection, "1");
    150         assertDtmfString(mRemoteConnection, "1");
    151         call.stopDtmfTone();
    152         assertDtmfString(mConnection, "1.");
    153         assertDtmfString(mRemoteConnection, "1.");
    154         call.playDtmfTone('3');
    155         assertDtmfString(mConnection, "1.3");
    156         assertDtmfString(mRemoteConnection, "1.3");
    157         call.stopDtmfTone();
    158         assertDtmfString(mConnection, "1.3.");
    159         assertDtmfString(mRemoteConnection, "1.3.");
    160     }
    161 
    162     public void testRemoteConnectionCallbacks_StateChange() {
    163         if (!mShouldTestTelecom) {
    164             return;
    165         }
    166 
    167         Handler handler = setupRemoteConnectionCallbacksTest();
    168 
    169         final InvokeCounter callbackInvoker =
    170                 new InvokeCounter("testRemoteConnectionCallbacks_StateChange");
    171         RemoteConnection.Callback callback;
    172 
    173         callback = new RemoteConnection.Callback() {
    174             @Override
    175             public void onStateChanged(RemoteConnection connection, int state) {
    176                 super.onStateChanged(connection, state);
    177                 callbackInvoker.invoke(connection, state);
    178             }
    179         };
    180         mRemoteConnectionObject.registerCallback(callback, handler);
    181         mRemoteConnection.setActive();
    182         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
    183         assertEquals(mRemoteConnectionObject, callbackInvoker.getArgs(0)[0]);
    184         assertEquals(Connection.STATE_ACTIVE, callbackInvoker.getArgs(0)[1]);
    185         mRemoteConnectionObject.unregisterCallback(callback);
    186     }
    187 
    188     public void testRemoteConnectionCallbacks_RingbackRequest() {
    189         if (!mShouldTestTelecom) {
    190             return;
    191         }
    192 
    193         Handler handler = setupRemoteConnectionCallbacksTest();
    194 
    195         final InvokeCounter callbackInvoker =
    196                 new InvokeCounter("testRemoteConnectionCallbacks_RingbackRequest");
    197         RemoteConnection.Callback callback;
    198 
    199         callback = new RemoteConnection.Callback() {
    200             @Override
    201             public void onRingbackRequested(RemoteConnection connection, boolean ringback) {
    202                 super.onRingbackRequested(connection, ringback);
    203                 callbackInvoker.invoke(connection, ringback);
    204             }
    205         };
    206         mRemoteConnectionObject.registerCallback(callback, handler);
    207         mRemoteConnection.setRingbackRequested(true);
    208         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
    209         assertEquals(mRemoteConnectionObject, callbackInvoker.getArgs(0)[0]);
    210         assertTrue((boolean) callbackInvoker.getArgs(0)[1]);
    211         mRemoteConnectionObject.unregisterCallback(callback);
    212     }
    213 
    214     public void testRemoteConnectionCallbacks_ConnectionCapabilities() {
    215         if (!mShouldTestTelecom) {
    216             return;
    217         }
    218 
    219         Handler handler = setupRemoteConnectionCallbacksTest();
    220 
    221         final InvokeCounter callbackInvoker =
    222                 new InvokeCounter("testRemoteConnectionCallbacks_ConnectionCapabilities");
    223         RemoteConnection.Callback callback;
    224 
    225         callback = new RemoteConnection.Callback() {
    226             @Override
    227             public void onConnectionCapabilitiesChanged(
    228                     RemoteConnection connection,
    229                     int connectionCapabilities) {
    230                 super.onConnectionCapabilitiesChanged(connection, connectionCapabilities);
    231                 callbackInvoker.invoke(connection, connectionCapabilities);
    232             }
    233         };
    234         mRemoteConnectionObject.registerCallback(callback, handler);
    235         int capabilities = mRemoteConnection.getConnectionCapabilities() | Connection.CAPABILITY_MUTE;
    236         mRemoteConnection.setConnectionCapabilities(capabilities);
    237         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
    238         assertEquals(mRemoteConnectionObject, callbackInvoker.getArgs(0)[0]);
    239         assertEquals(capabilities, callbackInvoker.getArgs(0)[1]);
    240         mRemoteConnectionObject.unregisterCallback(callback);
    241     }
    242 
    243     public void testRemoteConnectionCallbacks_ConnectionProperties() {
    244         if (!mShouldTestTelecom) {
    245             return;
    246         }
    247 
    248         Handler handler = setupRemoteConnectionCallbacksTest();
    249 
    250         final InvokeCounter callbackInvoker =
    251                 new InvokeCounter("testRemoteConnectionCallbacks_ConnectionCapabilities");
    252         RemoteConnection.Callback callback;
    253 
    254         callback = new RemoteConnection.Callback() {
    255             @Override
    256             public void onConnectionPropertiesChanged(
    257                     RemoteConnection connection,
    258                     int connectionProperties) {
    259                 super.onConnectionPropertiesChanged(connection, connectionProperties);
    260                 callbackInvoker.invoke(connection, connectionProperties);
    261             }
    262         };
    263         mRemoteConnectionObject.registerCallback(callback, handler);
    264         int properties = mRemoteConnection.getConnectionCapabilities()
    265                 | Connection.PROPERTY_IS_EXTERNAL_CALL;
    266         mRemoteConnection.setConnectionProperties(properties);
    267         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
    268         assertEquals(mRemoteConnectionObject, callbackInvoker.getArgs(0)[0]);
    269         assertEquals(properties, callbackInvoker.getArgs(0)[1]);
    270         mRemoteConnectionObject.unregisterCallback(callback);
    271     }
    272 
    273     public void testRemoteConnectionCallbacks_PostDialWait() {
    274         if (!mShouldTestTelecom) {
    275             return;
    276         }
    277 
    278         Handler handler = setupRemoteConnectionCallbacksTest();
    279 
    280         final InvokeCounter callbackInvoker =
    281                 new InvokeCounter("testRemoteConnectionCallbacks_PostDialWait");
    282         RemoteConnection.Callback callback;
    283 
    284         callback = new RemoteConnection.Callback() {
    285             @Override
    286             public void onPostDialWait(RemoteConnection connection,
    287                                        String remainingPostDialSequence) {
    288                 super.onPostDialWait(connection, remainingPostDialSequence);
    289                 callbackInvoker.invoke(connection, remainingPostDialSequence);
    290             }
    291         };
    292         mRemoteConnectionObject.registerCallback(callback, handler);
    293         String postDialSequence = "test";
    294         mRemoteConnection.setPostDialWait(postDialSequence);
    295         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
    296         assertEquals(mRemoteConnectionObject, callbackInvoker.getArgs(0)[0]);
    297         assertEquals(postDialSequence, callbackInvoker.getArgs(0)[1]);
    298         mRemoteConnectionObject.unregisterCallback(callback);
    299     }
    300 
    301     public void testRemoteConnectionCallbacks_PostDialChar() {
    302         if (!mShouldTestTelecom) {
    303             return;
    304         }
    305 
    306         Handler handler = setupRemoteConnectionCallbacksTest();
    307 
    308         final InvokeCounter callbackInvoker =
    309                 new InvokeCounter("testRemoteConnectionCallbacks_PostDialChar");
    310         RemoteConnection.Callback callback;
    311 
    312         callback = new RemoteConnection.Callback() {
    313             @Override
    314             public void onPostDialChar(RemoteConnection connection, char nextChar) {
    315                 super.onPostDialChar(connection, nextChar);
    316                 callbackInvoker.invoke(connection, nextChar);
    317             }
    318         };
    319         mRemoteConnectionObject.registerCallback(callback, handler);
    320         char postDialChar = '3';
    321         ((Connection) mRemoteConnection).setNextPostDialChar(postDialChar);
    322         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
    323         assertEquals(mRemoteConnectionObject, callbackInvoker.getArgs(0)[0]);
    324         assertEquals(postDialChar, callbackInvoker.getArgs(0)[1]);
    325         mRemoteConnectionObject.unregisterCallback(callback);
    326     }
    327 
    328     public void testRemoteConnectionCallbacks_VoipAudio() {
    329         if (!mShouldTestTelecom) {
    330             return;
    331         }
    332 
    333         Handler handler = setupRemoteConnectionCallbacksTest();
    334 
    335         final InvokeCounter callbackInvoker =
    336                 new InvokeCounter("testRemoteConnectionCallbacks_VoipAudio");
    337         RemoteConnection.Callback callback;
    338 
    339         callback = new RemoteConnection.Callback() {
    340             @Override
    341             public void onVoipAudioChanged(RemoteConnection connection, boolean isVoip) {
    342                 super.onVoipAudioChanged(connection, isVoip);
    343                 callbackInvoker.invoke(connection, isVoip);
    344             }
    345         };
    346         mRemoteConnectionObject.registerCallback(callback, handler);
    347         mRemoteConnection.setAudioModeIsVoip(true);
    348         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
    349         assertEquals(mRemoteConnectionObject, callbackInvoker.getArgs(0)[0]);
    350         assertTrue((boolean) callbackInvoker.getArgs(0)[1]);
    351         mRemoteConnectionObject.unregisterCallback(callback);
    352     }
    353 
    354     public void testRemoteConnectionCallbacks_StatusHints() {
    355         if (!mShouldTestTelecom) {
    356             return;
    357         }
    358 
    359         Handler handler = setupRemoteConnectionCallbacksTest();
    360 
    361         final InvokeCounter callbackInvoker =
    362                 new InvokeCounter("testRemoteConnectionCallbacks_StatusHints");
    363         RemoteConnection.Callback callback;
    364 
    365         callback = new RemoteConnection.Callback() {
    366             @Override
    367             public void onStatusHintsChanged(RemoteConnection connection, StatusHints statusHints) {
    368                 super.onStatusHintsChanged(connection, statusHints);
    369                 callbackInvoker.invoke(connection, statusHints);
    370             }
    371         };
    372         mRemoteConnectionObject.registerCallback(callback, handler);
    373         StatusHints hints = new StatusHints("test", null, null);
    374         mRemoteConnection.setStatusHints(hints);
    375         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
    376         assertEquals(mRemoteConnectionObject, callbackInvoker.getArgs(0)[0]);
    377         assertEquals(hints, callbackInvoker.getArgs(0)[1]);
    378         mRemoteConnectionObject.unregisterCallback(callback);
    379     }
    380 
    381     public void testRemoteConnectionCallbacks_AddressChange() {
    382         if (!mShouldTestTelecom) {
    383             return;
    384         }
    385 
    386         Handler handler = setupRemoteConnectionCallbacksTest();
    387 
    388         final InvokeCounter callbackInvoker =
    389                 new InvokeCounter("testRemoteConnectionCallbacks_AddressChange");
    390         RemoteConnection.Callback callback;
    391 
    392         callback = new RemoteConnection.Callback() {
    393             @Override
    394             public void onAddressChanged(RemoteConnection connection, Uri address,
    395                                          int presentation) {
    396                 super.onAddressChanged(connection, address, presentation);
    397                 callbackInvoker.invoke(connection, address, presentation);
    398             }
    399         };
    400         mRemoteConnectionObject.registerCallback(callback, handler);
    401         Uri address = Uri.parse("tel:555");
    402         mRemoteConnection.setAddress(address, TelecomManager.PRESENTATION_ALLOWED);
    403         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
    404         assertEquals(mRemoteConnectionObject, callbackInvoker.getArgs(0)[0]);
    405         assertEquals(address, callbackInvoker.getArgs(0)[1]);
    406         assertEquals(TelecomManager.PRESENTATION_ALLOWED, callbackInvoker.getArgs(0)[2]);
    407         mRemoteConnectionObject.unregisterCallback(callback);
    408     }
    409 
    410     public void testRemoteConnectionCallbacks_CallerDisplayName() {
    411         if (!mShouldTestTelecom) {
    412             return;
    413         }
    414 
    415         Handler handler = setupRemoteConnectionCallbacksTest();
    416 
    417         final InvokeCounter callbackInvoker =
    418                 new InvokeCounter("testRemoteConnectionCallbacks_CallerDisplayName");
    419         RemoteConnection.Callback callback;
    420 
    421         callback = new RemoteConnection.Callback() {
    422             @Override
    423             public void onCallerDisplayNameChanged(
    424                     RemoteConnection connection, String callerDisplayName, int presentation) {
    425                 super.onCallerDisplayNameChanged(connection, callerDisplayName, presentation);
    426                 callbackInvoker.invoke(connection, callerDisplayName, presentation);
    427             }
    428         };
    429         mRemoteConnectionObject.registerCallback(callback, handler);
    430         String callerDisplayName = "test";
    431         mRemoteConnection.setCallerDisplayName(callerDisplayName, TelecomManager.PRESENTATION_ALLOWED);
    432         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
    433         assertEquals(mRemoteConnectionObject, callbackInvoker.getArgs(0)[0]);
    434         assertEquals(callerDisplayName, callbackInvoker.getArgs(0)[1]);
    435         assertEquals(TelecomManager.PRESENTATION_ALLOWED, callbackInvoker.getArgs(0)[2]);
    436         mRemoteConnectionObject.unregisterCallback(callback);
    437     }
    438 
    439     public void testRemoteConnectionCallbacks_VideoState() {
    440         if (!mShouldTestTelecom) {
    441             return;
    442         }
    443 
    444         Handler handler = setupRemoteConnectionCallbacksTest();
    445 
    446         final InvokeCounter callbackInvoker =
    447                 new InvokeCounter("testRemoteConnectionCallbacks_VideoState");
    448         RemoteConnection.Callback callback;
    449 
    450         callback = new RemoteConnection.Callback() {
    451             @Override
    452             public void onVideoStateChanged(RemoteConnection connection, int videoState) {
    453                 super.onVideoStateChanged(connection, videoState);
    454                 callbackInvoker.invoke(connection, videoState);
    455             }
    456         };
    457         mRemoteConnectionObject.registerCallback(callback, handler);
    458         mRemoteConnection.setVideoState(VideoProfile.STATE_BIDIRECTIONAL);
    459         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
    460         assertEquals(mRemoteConnectionObject, callbackInvoker.getArgs(0)[0]);
    461         assertEquals(VideoProfile.STATE_BIDIRECTIONAL, callbackInvoker.getArgs(0)[1]);
    462         mRemoteConnectionObject.unregisterCallback(callback);
    463     }
    464 
    465     public void testRemoteConnectionCallbacks_ConferenceableConnections() {
    466         if (!mShouldTestTelecom) {
    467             return;
    468         }
    469 
    470         Handler handler = setupRemoteConnectionCallbacksTest();
    471 
    472         final InvokeCounter callbackInvoker =
    473                 new InvokeCounter("testRemoteConnectionCallbacks_ConferenceableConnections");
    474         RemoteConnection.Callback callback;
    475 
    476         callback = new RemoteConnection.Callback() {
    477             @Override
    478             public void onConferenceableConnectionsChanged(
    479                     RemoteConnection connection,
    480                     List<RemoteConnection> conferenceableConnections) {
    481                 super.onConferenceableConnectionsChanged(connection, conferenceableConnections);
    482                 callbackInvoker.invoke(connection, conferenceableConnections);
    483             }
    484         };
    485         mRemoteConnectionObject.registerCallback(callback, handler);
    486         //Make the existing call active to add a new call
    487         final Call call = mInCallCallbacks.getService().getLastCall();
    488         mConnection.setActive();
    489         mRemoteConnection.setActive();
    490         assertCallState(call, Call.STATE_ACTIVE);
    491         placeAndVerifyCall();
    492         RemoteConnection newRemoteConnectionObject =
    493                 verifyConnectionForOutgoingCall(1).getRemoteConnection();
    494         MockConnection newConnection = verifyConnectionForOutgoingCallOnRemoteCS(1);
    495         ArrayList<Connection> confList = new ArrayList<>();
    496         confList.add(newConnection);
    497         mRemoteConnection.setConferenceableConnections(confList);
    498         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
    499         assertEquals(mRemoteConnectionObject, callbackInvoker.getArgs(0)[0]);
    500         //assertTrue(((List<RemoteConnection>)callbackInvoker.getArgs(0)[1]).contains(
    501                 //newRemoteConnectionObject)); No "equals" method in RemoteConnection
    502         mRemoteConnectionObject.unregisterCallback(callback);
    503     }
    504 
    505     public void testRemoteConnectionCallbacks_VideoProvider() {
    506         if (!mShouldTestTelecom) {
    507             return;
    508         }
    509 
    510         Handler handler = setupRemoteConnectionCallbacksTest();
    511 
    512         final InvokeCounter callbackInvoker =
    513                 new InvokeCounter("testRemoteConnectionCallbacks_VideoProvider");
    514         RemoteConnection.Callback callback;
    515 
    516         callback = new RemoteConnection.Callback() {
    517             @Override
    518             public void onVideoProviderChanged(
    519                     RemoteConnection connection, VideoProvider videoProvider) {
    520                 super.onVideoProviderChanged(connection, videoProvider);
    521                 callbackInvoker.invoke(connection, videoProvider);
    522             }
    523         };
    524         mRemoteConnectionObject.registerCallback(callback, handler);
    525         mRemoteConnection.createMockVideoProvider();
    526         MockVideoProvider mockVideoProvider = mRemoteConnection.getMockVideoProvider();
    527         mRemoteConnection.setVideoProvider(mockVideoProvider);
    528         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
    529         assertEquals(mRemoteConnectionObject, callbackInvoker.getArgs(0)[0]);
    530         mRemoteConnectionObject.unregisterCallback(callback);
    531     }
    532 
    533     public void testRemoteConnectionCallbacks_Extras() {
    534         if (!mShouldTestTelecom) {
    535             return;
    536         }
    537 
    538         Handler handler = setupRemoteConnectionCallbacksTest();
    539 
    540         final InvokeCounter callbackInvoker =
    541                 new InvokeCounter("testRemoteConnectionCallbacks_Extras");
    542         RemoteConnection.Callback callback;
    543 
    544         callback = new RemoteConnection.Callback() {
    545             @Override
    546             public void onExtrasChanged(RemoteConnection connection, Bundle extras) {
    547                 super.onExtrasChanged(connection, extras);
    548                 callbackInvoker.invoke(connection, extras);
    549             }
    550         };
    551         mRemoteConnectionObject.registerCallback(callback, handler);
    552         Bundle extras = new Bundle();
    553         extras.putString(TelecomManager.EXTRA_CALL_DISCONNECT_MESSAGE, "Test");
    554         mRemoteConnection.setExtras(extras);
    555         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
    556         assertEquals(mRemoteConnectionObject, callbackInvoker.getArgs(0)[0]);
    557         assertTrue(areBundlesEqual(extras, (Bundle) callbackInvoker.getArgs(0)[1]));
    558         mRemoteConnectionObject.unregisterCallback(callback);
    559     }
    560 
    561     /**
    562      * Verifies that a {@link RemoteConnection} receives a
    563      * {@link Connection#sendConnectionEvent(String, Bundle)} notification.
    564      */
    565     public void testRemoteConnectionCallbacks_ConnectionEvent() {
    566         if (!mShouldTestTelecom) {
    567             return;
    568         }
    569 
    570         Handler handler = setupRemoteConnectionCallbacksTest();
    571 
    572         final InvokeCounter callbackInvoker =
    573                 new InvokeCounter("testRemoteConnectionCallbacks_Extras");
    574         RemoteConnection.Callback callback;
    575 
    576         callback = new RemoteConnection.Callback() {
    577             @Override
    578             public void onConnectionEvent(RemoteConnection connection, String event,
    579                     Bundle extras) {
    580                 super.onConnectionEvent(connection, event, extras);
    581                 callbackInvoker.invoke(connection, event, extras);
    582             }
    583         };
    584         mRemoteConnectionObject.registerCallback(callback, handler);
    585         Bundle extras = new Bundle();
    586         extras.putString(TelecomManager.EXTRA_CALL_DISCONNECT_MESSAGE, "Test");
    587         mRemoteConnection.sendConnectionEvent(Connection.EVENT_CALL_PULL_FAILED, extras);
    588         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
    589         assertEquals(mRemoteConnectionObject, callbackInvoker.getArgs(0)[0]);
    590         assertEquals(Connection.EVENT_CALL_PULL_FAILED, callbackInvoker.getArgs(0)[1]);
    591         assertTrue(areBundlesEqual(extras, (Bundle) callbackInvoker.getArgs(0)[2]));
    592         mRemoteConnectionObject.unregisterCallback(callback);
    593     }
    594 
    595     public void testRemoteConnectionCallbacks_Disconnect() {
    596         if (!mShouldTestTelecom) {
    597             return;
    598         }
    599 
    600         Handler handler = setupRemoteConnectionCallbacksTest();
    601 
    602         final InvokeCounter callbackInvoker =
    603                 new InvokeCounter("testRemoteConnectionCallbacks_Disconnect");
    604         RemoteConnection.Callback callback;
    605 
    606         callback = new RemoteConnection.Callback() {
    607             @Override
    608             public void onDisconnected(
    609                     RemoteConnection connection,
    610                     DisconnectCause disconnectCause) {
    611                 super.onDisconnected(connection, disconnectCause);
    612                 callbackInvoker.invoke(connection, disconnectCause);
    613             }
    614         };
    615         mRemoteConnectionObject.registerCallback(callback, handler);
    616         DisconnectCause cause = new DisconnectCause(DisconnectCause.LOCAL);
    617         mRemoteConnection.setDisconnected(cause);
    618         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
    619         assertEquals(mRemoteConnectionObject, callbackInvoker.getArgs(0)[0]);
    620         assertEquals(cause, callbackInvoker.getArgs(0)[1]);
    621         mRemoteConnectionObject.unregisterCallback(callback);
    622     }
    623 
    624     /**
    625      * Verifies that a call to {@link RemoteConnection#pullExternalCall()} is proxied to
    626      * {@link Connection#onPullExternalCall()}.
    627      */
    628     public void testRemoteConnectionCallbacks_PullExternalCall() {
    629         if (!mShouldTestTelecom) {
    630             return;
    631         }
    632 
    633         Handler handler = setupRemoteConnectionCallbacksTest();
    634 
    635         InvokeCounter counter =
    636                 mRemoteConnection.getInvokeCounter(MockConnection.ON_PULL_EXTERNAL_CALL);
    637         mRemoteConnectionObject.pullExternalCall();
    638         counter.waitForCount(1);
    639     }
    640 
    641     public void testRemoteConnectionCallbacks_Destroy() {
    642         if (!mShouldTestTelecom) {
    643             return;
    644         }
    645 
    646         Handler handler = setupRemoteConnectionCallbacksTest();
    647 
    648         final InvokeCounter callbackInvoker =
    649                 new InvokeCounter("testRemoteConnectionCallbacks_Destroy");
    650         RemoteConnection.Callback callback;
    651 
    652         callback = new RemoteConnection.Callback() {
    653             @Override
    654             public void onDestroyed(RemoteConnection connection) {
    655                 super.onDestroyed(connection);
    656                 callbackInvoker.invoke(connection);
    657             }
    658         };
    659         mRemoteConnectionObject.registerCallback(callback, handler);
    660         mRemoteConnection.destroy();
    661         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
    662         assertEquals(mRemoteConnectionObject, callbackInvoker.getArgs(0)[0]);
    663         mRemoteConnectionObject.unregisterCallback(callback);
    664     }
    665 
    666     public void testRemoteConnectionVideoCallbacks_SessionModify() {
    667         if (!mShouldTestTelecom) {
    668             return;
    669         }
    670 
    671         setupRemoteConnectionVideoCallbacksTest();
    672 
    673         final InvokeCounter callbackInvoker =
    674                 new InvokeCounter("testRemoteConnectionVideoCallbacks_SessionModify");
    675         final VideoProvider remoteVideoProvider = mRemoteConnectionObject.getVideoProvider();
    676         final MockVideoProvider mockVideoProvider = mRemoteConnection.getMockVideoProvider();
    677         RemoteConnection.VideoProvider.Callback videoCallback;
    678 
    679         videoCallback = new RemoteConnection.VideoProvider.Callback() {
    680             @Override
    681             public void onSessionModifyRequestReceived(
    682                     VideoProvider videoProvider,
    683                     VideoProfile videoProfile) {
    684                 super.onSessionModifyRequestReceived(videoProvider, videoProfile);
    685                 callbackInvoker.invoke(videoProvider, videoProfile);
    686             }
    687 
    688             @Override
    689             public void onSessionModifyResponseReceived(
    690                     VideoProvider videoProvider,
    691                     int status,
    692                     VideoProfile requestedProfile,
    693                     VideoProfile responseProfile) {
    694                 super.onSessionModifyResponseReceived(videoProvider, status, requestedProfile,
    695                         responseProfile);
    696                 callbackInvoker.invoke(videoProvider, status, requestedProfile, responseProfile);
    697             }
    698         };
    699         remoteVideoProvider.registerCallback(videoCallback);
    700         VideoProfile videoProfile = new VideoProfile(VideoProfile.STATE_BIDIRECTIONAL);
    701         mockVideoProvider.waitForVideoProviderHandler(remoteVideoProvider);
    702         mockVideoProvider.sendMockSessionModifyRequest(videoProfile);
    703         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
    704         assertEquals(remoteVideoProvider, callbackInvoker.getArgs(0)[0]);
    705         assertEquals(videoProfile, callbackInvoker.getArgs(0)[1]);
    706         remoteVideoProvider.unregisterCallback(videoCallback);
    707     }
    708 
    709     public void testRemoteConnectionVideoCallbacks_SessionEvent() {
    710         if (!mShouldTestTelecom) {
    711             return;
    712         }
    713 
    714         setupRemoteConnectionVideoCallbacksTest();
    715 
    716         final InvokeCounter callbackInvoker =
    717                 new InvokeCounter("testRemoteConnectionVideoCallbacks_SessionEvent");
    718         final VideoProvider remoteVideoProvider = mRemoteConnectionObject.getVideoProvider();
    719         final MockVideoProvider mockVideoProvider = mRemoteConnection.getMockVideoProvider();
    720         RemoteConnection.VideoProvider.Callback videoCallback;
    721 
    722         videoCallback = new RemoteConnection.VideoProvider.Callback() {
    723             @Override
    724             public void onCallSessionEvent(VideoProvider videoProvider, int event) {
    725                 super.onCallSessionEvent(videoProvider, event);
    726                 callbackInvoker.invoke(videoProvider, event);
    727             }
    728         };
    729         remoteVideoProvider.registerCallback(videoCallback);
    730         mockVideoProvider.waitForVideoProviderHandler(remoteVideoProvider);
    731         mockVideoProvider.handleCallSessionEvent(Connection.VideoProvider.SESSION_EVENT_RX_PAUSE);
    732         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
    733         assertEquals(remoteVideoProvider, callbackInvoker.getArgs(0)[0]);
    734         assertEquals(Connection.VideoProvider.SESSION_EVENT_RX_PAUSE, callbackInvoker.getArgs(0)[1]);
    735         remoteVideoProvider.unregisterCallback(videoCallback);
    736     }
    737 
    738     public void testRemoteConnectionVideoCallbacks_PeerDimensions() {
    739         if (!mShouldTestTelecom) {
    740             return;
    741         }
    742 
    743         setupRemoteConnectionVideoCallbacksTest();
    744 
    745         final InvokeCounter callbackInvoker =
    746                 new InvokeCounter("testRemoteConnectionVideoCallbacks_PeerDimensions");
    747         final VideoProvider remoteVideoProvider = mRemoteConnectionObject.getVideoProvider();
    748         final MockVideoProvider mockVideoProvider = mRemoteConnection.getMockVideoProvider();
    749         RemoteConnection.VideoProvider.Callback videoCallback;
    750 
    751         videoCallback = new RemoteConnection.VideoProvider.Callback() {
    752             @Override
    753             public void onPeerDimensionsChanged(VideoProvider videoProvider, int width,
    754                                                 int height) {
    755                 super.onPeerDimensionsChanged(videoProvider, width, height);
    756                 callbackInvoker.invoke(videoProvider, width, height);
    757             }
    758         };
    759         remoteVideoProvider.registerCallback(videoCallback);
    760         final int width = 100, heigth = 20;
    761         mockVideoProvider.waitForVideoProviderHandler(remoteVideoProvider);
    762         mockVideoProvider.changePeerDimensions(width, heigth);
    763         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
    764         assertEquals(remoteVideoProvider, callbackInvoker.getArgs(0)[0]);
    765         assertEquals(width, callbackInvoker.getArgs(0)[1]);
    766         assertEquals(heigth, callbackInvoker.getArgs(0)[2]);
    767         remoteVideoProvider.unregisterCallback(videoCallback);
    768     }
    769 
    770     public void testRemoteConnectionVideoCallbacks_CallDataUsage() {
    771         if (!mShouldTestTelecom) {
    772             return;
    773         }
    774 
    775         setupRemoteConnectionVideoCallbacksTest();
    776 
    777         final InvokeCounter callbackInvoker =
    778                 new InvokeCounter("testRemoteConnectionVideoCallbacks_CallDataUsage");
    779         final VideoProvider remoteVideoProvider = mRemoteConnectionObject.getVideoProvider();
    780         final MockVideoProvider mockVideoProvider = mRemoteConnection.getMockVideoProvider();
    781         RemoteConnection.VideoProvider.Callback videoCallback;
    782 
    783         videoCallback = new RemoteConnection.VideoProvider.Callback() {
    784             @Override
    785             public void onCallDataUsageChanged(VideoProvider videoProvider, long dataUsage) {
    786                 super.onCallDataUsageChanged(videoProvider, dataUsage);
    787                 callbackInvoker.invoke(videoProvider, dataUsage);
    788             }
    789         };
    790         remoteVideoProvider.registerCallback(videoCallback);
    791         callbackInvoker.waitForCount(WAIT_FOR_STATE_CHANGE_TIMEOUT_CALLBACK);
    792         long callDataUsage = 10000;
    793         mockVideoProvider.waitForVideoProviderHandler(remoteVideoProvider);
    794         mockVideoProvider.setCallDataUsage(callDataUsage);
    795         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
    796         assertEquals(remoteVideoProvider, callbackInvoker.getArgs(0)[0]);
    797         assertEquals(callDataUsage, callbackInvoker.getArgs(0)[1]);
    798         remoteVideoProvider.unregisterCallback(videoCallback);
    799     }
    800 
    801     public void testRemoteConnectionVideoCallbacks_CameraCapabilities() {
    802         if (!mShouldTestTelecom) {
    803             return;
    804         }
    805 
    806         setupRemoteConnectionVideoCallbacksTest();
    807 
    808         final InvokeCounter callbackInvoker =
    809                 new InvokeCounter("testRemoteConnectionVideoCallbacks_CameraCapabilities");
    810         final VideoProvider remoteVideoProvider = mRemoteConnectionObject.getVideoProvider();
    811         final MockVideoProvider mockVideoProvider = mRemoteConnection.getMockVideoProvider();
    812         RemoteConnection.VideoProvider.Callback videoCallback;
    813 
    814         videoCallback = new RemoteConnection.VideoProvider.Callback() {
    815             @Override
    816             public void onCameraCapabilitiesChanged(
    817                     VideoProvider videoProvider,
    818                     VideoProfile.CameraCapabilities cameraCapabilities) {
    819                 super.onCameraCapabilitiesChanged(videoProvider, cameraCapabilities);
    820                 callbackInvoker.invoke(videoProvider, cameraCapabilities);
    821             }
    822         };
    823         remoteVideoProvider.registerCallback(videoCallback);
    824         VideoProfile.CameraCapabilities capabilities = new VideoProfile.CameraCapabilities(100, 200);
    825         mockVideoProvider.waitForVideoProviderHandler(remoteVideoProvider);
    826         mockVideoProvider.changeCameraCapabilities(capabilities);
    827         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
    828         assertEquals(remoteVideoProvider, callbackInvoker.getArgs(0)[0]);
    829         assertEquals(capabilities, callbackInvoker.getArgs(0)[1]);
    830         remoteVideoProvider.unregisterCallback(videoCallback);
    831     }
    832 
    833     public void testRemoteConnectionVideoCallbacks_VideoQuality() {
    834         if (!mShouldTestTelecom) {
    835             return;
    836         }
    837 
    838         setupRemoteConnectionVideoCallbacksTest();
    839 
    840         final InvokeCounter callbackInvoker =
    841                 new InvokeCounter("testRemoteConnectionVideoCallbacks_VideoQuality");
    842         final VideoProvider remoteVideoProvider = mRemoteConnectionObject.getVideoProvider();
    843         final MockVideoProvider mockVideoProvider = mRemoteConnection.getMockVideoProvider();
    844         RemoteConnection.VideoProvider.Callback videoCallback;
    845 
    846         videoCallback = new RemoteConnection.VideoProvider.Callback() {
    847             @Override
    848             public void onVideoQualityChanged(VideoProvider videoProvider, int videoQuality) {
    849                 super.onVideoQualityChanged(videoProvider, videoQuality);
    850                 callbackInvoker.invoke(videoProvider, videoQuality);
    851             }
    852         };
    853         remoteVideoProvider.registerCallback(videoCallback);
    854         final int videoQuality = 10;
    855         mockVideoProvider.waitForVideoProviderHandler(remoteVideoProvider);
    856         mockVideoProvider.changeVideoQuality(videoQuality);
    857         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
    858         assertEquals(remoteVideoProvider, callbackInvoker.getArgs(0)[0]);
    859         assertEquals(videoQuality, callbackInvoker.getArgs(0)[1]);
    860         remoteVideoProvider.unregisterCallback(videoCallback);
    861     }
    862 
    863     public void testRemoteConnectionVideo_RequestCallDataUsage() {
    864         if (!mShouldTestTelecom) {
    865             return;
    866         }
    867 
    868         final long callDataUsage = 10000;
    869         final InvokeCounter callbackInvoker =
    870                 new InvokeCounter("testRemoteConnectionVideo_RequestCallDataUsage");
    871         final MockVideoProvider mockVideoProvider = new MockVideoProvider(mRemoteConnection) {
    872             @Override
    873             public void onRequestConnectionDataUsage() {
    874                 callbackInvoker.invoke();
    875                 super.setCallDataUsage(callDataUsage);
    876             }
    877         };
    878         setupRemoteConnectionVideoTest(mockVideoProvider);
    879 
    880         final VideoProvider remoteVideoProvider = mRemoteConnectionObject.getVideoProvider();
    881         RemoteConnection.VideoProvider.Callback videoCallback;
    882 
    883         videoCallback = new RemoteConnection.VideoProvider.Callback() {
    884             @Override
    885             public void onCallDataUsageChanged(VideoProvider videoProvider, long dataUsage) {
    886                 super.onCallDataUsageChanged(videoProvider, dataUsage);
    887                 callbackInvoker.invoke(videoProvider, dataUsage);
    888             }
    889         };
    890         remoteVideoProvider.registerCallback(videoCallback);
    891         remoteVideoProvider.requestCallDataUsage();
    892         callbackInvoker.waitForCount(2, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
    893         assertEquals(remoteVideoProvider, callbackInvoker.getArgs(1)[0]);
    894         assertEquals(callDataUsage, callbackInvoker.getArgs(1)[1]);
    895         remoteVideoProvider.unregisterCallback(videoCallback);
    896     }
    897 
    898     public void testRemoteConnectionVideo_RequestCameraCapabilities() {
    899         if (!mShouldTestTelecom) {
    900             return;
    901         }
    902 
    903         final VideoProfile.CameraCapabilities capabilities =
    904                 new VideoProfile.CameraCapabilities(100, 200);
    905         final InvokeCounter callbackInvoker =
    906                 new InvokeCounter("testRemoteConnectionVideo_RequestCameraCapabilities");
    907         final MockVideoProvider mockVideoProvider = new MockVideoProvider(mRemoteConnection) {
    908             @Override
    909             public void onRequestCameraCapabilities() {
    910                 callbackInvoker.invoke();
    911                 super.changeCameraCapabilities(capabilities);
    912             }
    913         };
    914         setupRemoteConnectionVideoTest(mockVideoProvider);
    915 
    916         final VideoProvider remoteVideoProvider = mRemoteConnectionObject.getVideoProvider();
    917         RemoteConnection.VideoProvider.Callback videoCallback;
    918 
    919         videoCallback = new RemoteConnection.VideoProvider.Callback() {
    920             @Override
    921             public void onCameraCapabilitiesChanged(
    922                     VideoProvider videoProvider,
    923                     VideoProfile.CameraCapabilities cameraCapabilities) {
    924                 super.onCameraCapabilitiesChanged(videoProvider, cameraCapabilities);
    925                 callbackInvoker.invoke(videoProvider, cameraCapabilities);
    926             }
    927         };
    928         remoteVideoProvider.registerCallback(videoCallback);
    929         remoteVideoProvider.requestCameraCapabilities();
    930         callbackInvoker.waitForCount(2, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
    931         assertEquals(remoteVideoProvider, callbackInvoker.getArgs(1)[0]);
    932         assertEquals(capabilities, callbackInvoker.getArgs(1)[1]);
    933         remoteVideoProvider.unregisterCallback(videoCallback);
    934     }
    935 
    936     public void testRemoteConnectionVideo_SendSessionModifyRequest() {
    937         if (!mShouldTestTelecom) {
    938             return;
    939         }
    940 
    941         VideoProfile fromVideoProfile = new VideoProfile(VideoProfile.STATE_AUDIO_ONLY);
    942         VideoProfile toVideoProfile =  new VideoProfile(VideoProfile.STATE_BIDIRECTIONAL);
    943         final InvokeCounter callbackInvoker =
    944                 new InvokeCounter("testRemoteConnectionVideo_SendSessionModifyRequest");
    945         final MockVideoProvider mockVideoProvider = new MockVideoProvider(mRemoteConnection) {
    946             @Override
    947             public void onSendSessionModifyRequest(VideoProfile fromProfile,
    948                                                    VideoProfile toProfile) {
    949                 callbackInvoker.invoke(fromProfile, toProfile);
    950                 super.receiveSessionModifyRequest(toProfile);
    951             }
    952         };
    953         setupRemoteConnectionVideoTest(mockVideoProvider);
    954 
    955         final VideoProvider remoteVideoProvider = mRemoteConnectionObject.getVideoProvider();
    956         RemoteConnection.VideoProvider.Callback videoCallback;
    957 
    958         videoCallback = new RemoteConnection.VideoProvider.Callback() {
    959             @Override
    960             public void onSessionModifyRequestReceived(
    961                     VideoProvider videoProvider,
    962                     VideoProfile videoProfile) {
    963                 super.onSessionModifyRequestReceived(videoProvider, videoProfile);
    964                 callbackInvoker.invoke(videoProvider, videoProfile);
    965             }
    966         };
    967         remoteVideoProvider.registerCallback(videoCallback);
    968         remoteVideoProvider.sendSessionModifyRequest(fromVideoProfile, toVideoProfile);
    969         callbackInvoker.waitForCount(2, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
    970         assertEquals(fromVideoProfile, callbackInvoker.getArgs(0)[0]);
    971         assertEquals(toVideoProfile, callbackInvoker.getArgs(0)[1]);
    972         assertEquals(remoteVideoProvider, callbackInvoker.getArgs(1)[0]);
    973         assertEquals(toVideoProfile, callbackInvoker.getArgs(1)[1]);
    974         remoteVideoProvider.unregisterCallback(videoCallback);
    975     }
    976 
    977     public void testRemoteConnectionVideo_SendSessionModifyResponse() {
    978         if (!mShouldTestTelecom) {
    979             return;
    980         }
    981 
    982         VideoProfile toVideoProfile =  new VideoProfile(VideoProfile.STATE_BIDIRECTIONAL);
    983         final InvokeCounter callbackInvoker =
    984                 new InvokeCounter("testRemoteConnectionVideo_SendSessionModifyResponse");
    985         final MockVideoProvider mockVideoProvider = new MockVideoProvider(mRemoteConnection) {
    986             @Override
    987             public void onSendSessionModifyResponse(VideoProfile responseProfile) {
    988                 callbackInvoker.invoke(responseProfile);
    989                 super.receiveSessionModifyResponse(
    990                         Connection.VideoProvider.SESSION_MODIFY_REQUEST_SUCCESS,
    991                         responseProfile, responseProfile);
    992             }
    993         };
    994         setupRemoteConnectionVideoTest(mockVideoProvider);
    995 
    996         final VideoProvider remoteVideoProvider = mRemoteConnectionObject.getVideoProvider();
    997         RemoteConnection.VideoProvider.Callback videoCallback;
    998 
    999         videoCallback = new RemoteConnection.VideoProvider.Callback() {
   1000             @Override
   1001             public void onSessionModifyResponseReceived(
   1002                     VideoProvider videoProvider,
   1003                     int status,
   1004                     VideoProfile requestedProfile,
   1005                     VideoProfile responseProfile) {
   1006                 super.onSessionModifyResponseReceived(videoProvider, status, requestedProfile,
   1007                         responseProfile);
   1008                 callbackInvoker.invoke(videoProvider, status, requestedProfile, responseProfile);
   1009             }
   1010         };
   1011         remoteVideoProvider.registerCallback(videoCallback);
   1012         remoteVideoProvider.sendSessionModifyResponse(toVideoProfile);
   1013         callbackInvoker.waitForCount(2, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
   1014         assertEquals(toVideoProfile, callbackInvoker.getArgs(0)[0]);
   1015         assertEquals(remoteVideoProvider, callbackInvoker.getArgs(1)[0]);
   1016         assertEquals(toVideoProfile, callbackInvoker.getArgs(1)[2]);
   1017         assertEquals(Connection.VideoProvider.SESSION_MODIFY_REQUEST_SUCCESS,
   1018             callbackInvoker.getArgs(1)[1]);
   1019         assertEquals(toVideoProfile, callbackInvoker.getArgs(1)[3]);
   1020         remoteVideoProvider.unregisterCallback(videoCallback);
   1021     }
   1022 
   1023     public void testRemoteConnectionVideo_SetCamera() {
   1024         if (!mShouldTestTelecom) {
   1025             return;
   1026         }
   1027 
   1028         final String newCameraId = "5";
   1029         final VideoProfile.CameraCapabilities capabilities =
   1030             new VideoProfile.CameraCapabilities(100, 200);
   1031         final InvokeCounter callbackInvoker =
   1032                 new InvokeCounter("testRemoteConnectionVideo_SetCamera");
   1033         final MockVideoProvider mockVideoProvider = new MockVideoProvider(mRemoteConnection) {
   1034             @Override
   1035             public void onSetCamera(String cameraId) {
   1036                 callbackInvoker.invoke(cameraId);
   1037                 super.changeCameraCapabilities(capabilities);
   1038             }
   1039         };
   1040         setupRemoteConnectionVideoTest(mockVideoProvider);
   1041 
   1042         final VideoProvider remoteVideoProvider = mRemoteConnectionObject.getVideoProvider();
   1043         RemoteConnection.VideoProvider.Callback videoCallback;
   1044 
   1045         videoCallback = new RemoteConnection.VideoProvider.Callback() {
   1046             @Override
   1047             public void onCameraCapabilitiesChanged(
   1048                     VideoProvider videoProvider,
   1049                     VideoProfile.CameraCapabilities cameraCapabilities) {
   1050                 super.onCameraCapabilitiesChanged(videoProvider, cameraCapabilities);
   1051                 callbackInvoker.invoke(videoProvider, cameraCapabilities);
   1052             }
   1053         };
   1054         remoteVideoProvider.registerCallback(videoCallback);
   1055         remoteVideoProvider.setCamera(newCameraId);
   1056         callbackInvoker.waitForCount(2, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
   1057         assertEquals(newCameraId, callbackInvoker.getArgs(0)[0]);
   1058         assertEquals(remoteVideoProvider, callbackInvoker.getArgs(1)[0]);
   1059         assertEquals(capabilities, callbackInvoker.getArgs(1)[1]);
   1060         remoteVideoProvider.unregisterCallback(videoCallback);
   1061     }
   1062 
   1063     public void testRemoteConnectionVideo_SetDeviceOrientation() {
   1064         if (!mShouldTestTelecom) {
   1065             return;
   1066         }
   1067 
   1068         final int newRotation = 5;
   1069         final InvokeCounter callbackInvoker =
   1070                 new InvokeCounter("testRemoteConnectionVideo_SetDeviceOrientation");
   1071         final MockVideoProvider mockVideoProvider = new MockVideoProvider(mRemoteConnection) {
   1072             @Override
   1073             public void onSetDeviceOrientation(int rotation) {
   1074                 callbackInvoker.invoke(rotation);
   1075             }
   1076         };
   1077         setupRemoteConnectionVideoTest(mockVideoProvider);
   1078 
   1079         final VideoProvider remoteVideoProvider = mRemoteConnectionObject.getVideoProvider();
   1080 
   1081         remoteVideoProvider.setDeviceOrientation(newRotation);
   1082         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
   1083         assertEquals(newRotation, callbackInvoker.getArgs(0)[0]);
   1084     }
   1085 
   1086     public void testRemoteConnectionVideo_SetDisplaySurface() {
   1087         if (!mShouldTestTelecom) {
   1088             return;
   1089         }
   1090 
   1091         final Surface newSurface = new Surface(new SurfaceTexture(1));
   1092         final InvokeCounter callbackInvoker =
   1093                 new InvokeCounter("testRemoteConnectionVideo_SetDisplaySurface");
   1094         final MockVideoProvider mockVideoProvider = new MockVideoProvider(mRemoteConnection) {
   1095             @Override
   1096             public void onSetDisplaySurface(Surface surface) {
   1097                 callbackInvoker.invoke(surface);
   1098             }
   1099         };
   1100         setupRemoteConnectionVideoTest(mockVideoProvider);
   1101 
   1102         final VideoProvider remoteVideoProvider = mRemoteConnectionObject.getVideoProvider();
   1103 
   1104         remoteVideoProvider.setDisplaySurface(newSurface);
   1105         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
   1106         assertEquals(newSurface, callbackInvoker.getArgs(0)[0]);
   1107     }
   1108 
   1109     public void testRemoteConnectionVideo_SetPauseImage() {
   1110         if (!mShouldTestTelecom) {
   1111             return;
   1112         }
   1113 
   1114         final Uri newUri = Uri.parse("content://");
   1115         final InvokeCounter callbackInvoker =
   1116                 new InvokeCounter("testRemoteConnectionVideo_SetPauseImage");
   1117         final MockVideoProvider mockVideoProvider = new MockVideoProvider(mRemoteConnection) {
   1118             @Override
   1119             public void onSetPauseImage(Uri uri) {
   1120                 callbackInvoker.invoke(uri);
   1121             }
   1122         };
   1123         setupRemoteConnectionVideoTest(mockVideoProvider);
   1124 
   1125         final VideoProvider remoteVideoProvider = mRemoteConnectionObject.getVideoProvider();
   1126 
   1127         remoteVideoProvider.setPauseImage(newUri);
   1128         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
   1129         assertEquals(newUri, callbackInvoker.getArgs(0)[0]);
   1130     }
   1131 
   1132     public void testRemoteConnectionVideo_SetPreviewSurface() {
   1133         if (!mShouldTestTelecom) {
   1134             return;
   1135         }
   1136 
   1137         final Surface newSurface = new Surface(new SurfaceTexture(1));
   1138         final InvokeCounter callbackInvoker =
   1139                 new InvokeCounter("testRemoteConnectionVideo_SetPreviewSurface");
   1140         final MockVideoProvider mockVideoProvider = new MockVideoProvider(mRemoteConnection) {
   1141             @Override
   1142             public void onSetPreviewSurface(Surface surface) {
   1143                 callbackInvoker.invoke(surface);
   1144             }
   1145         };
   1146         setupRemoteConnectionVideoTest(mockVideoProvider);
   1147 
   1148         final VideoProvider remoteVideoProvider = mRemoteConnectionObject.getVideoProvider();
   1149 
   1150         remoteVideoProvider.setPreviewSurface(newSurface);
   1151         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
   1152         assertEquals(newSurface, callbackInvoker.getArgs(0)[0]);
   1153     }
   1154 
   1155     public void testRemoteConnectionVideo_SetZoom() {
   1156         if (!mShouldTestTelecom) {
   1157             return;
   1158         }
   1159 
   1160         final float newZoom = 1.0f;
   1161         final InvokeCounter callbackInvoker =
   1162                 new InvokeCounter("testRemoteConnectionVideo_SetPreviewSurface");
   1163         final MockVideoProvider mockVideoProvider = new MockVideoProvider(mRemoteConnection) {
   1164             @Override
   1165             public void onSetZoom(float value) {
   1166                 callbackInvoker.invoke(value);
   1167             }
   1168         };
   1169         setupRemoteConnectionVideoTest(mockVideoProvider);
   1170 
   1171         final VideoProvider remoteVideoProvider = mRemoteConnectionObject.getVideoProvider();
   1172 
   1173         remoteVideoProvider.setZoom(newZoom);
   1174         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
   1175         assertEquals(newZoom, callbackInvoker.getArgs(0)[0]);
   1176     }
   1177 
   1178     private void verifyRemoteConnectionObject(RemoteConnection remoteConnection,
   1179             Connection connection) {
   1180         assertEquals(connection.getAddress(), remoteConnection.getAddress());
   1181         assertEquals(connection.getAddressPresentation(),
   1182                 remoteConnection.getAddressPresentation());
   1183         assertEquals(connection.getCallerDisplayName(), remoteConnection.getCallerDisplayName());
   1184         assertEquals(connection.getCallerDisplayNamePresentation(),
   1185                 remoteConnection.getCallerDisplayNamePresentation());
   1186         assertEquals(connection.getConnectionCapabilities(),
   1187                 remoteConnection.getConnectionCapabilities());
   1188         assertEquals(connection.getConnectionProperties(),
   1189                 remoteConnection.getConnectionProperties());
   1190         assertEquals(connection.getDisconnectCause(), remoteConnection.getDisconnectCause());
   1191         assertEquals(connection.getExtras(), remoteConnection.getExtras());
   1192         assertEquals(connection.getStatusHints(), remoteConnection.getStatusHints());
   1193         assertEquals(VideoProfile.STATE_AUDIO_ONLY, remoteConnection.getVideoState());
   1194         assertNull(remoteConnection.getVideoProvider());
   1195         assertTrue(remoteConnection.getConferenceableConnections().isEmpty());
   1196     }
   1197 
   1198     private void addRemoteConnectionOutgoingCall() {
   1199         try {
   1200             MockConnectionService managerConnectionService = new MockConnectionService() {
   1201                 @Override
   1202                 public Connection onCreateOutgoingConnection(
   1203                         PhoneAccountHandle connectionManagerPhoneAccount,
   1204                         ConnectionRequest request) {
   1205                     MockConnection connection = (MockConnection)super.onCreateOutgoingConnection(
   1206                             connectionManagerPhoneAccount, request);
   1207                     ConnectionRequest remoteRequest = new ConnectionRequest(
   1208                             TEST_REMOTE_PHONE_ACCOUNT_HANDLE,
   1209                             request.getAddress(),
   1210                             request.getExtras());
   1211                     RemoteConnection remoteConnection =
   1212                             CtsConnectionService.createRemoteOutgoingConnectionToTelecom(
   1213                                     TEST_REMOTE_PHONE_ACCOUNT_HANDLE, remoteRequest);
   1214                     connection.setRemoteConnection(remoteConnection);
   1215                     return connection;
   1216                 }
   1217             };
   1218             setupConnectionServices(managerConnectionService, null, FLAG_REGISTER | FLAG_ENABLE);
   1219         } catch(Exception e) {
   1220             fail("Error in setting up the connection services");
   1221         }
   1222         placeAndVerifyCall();
   1223         /**
   1224          * Retrieve the connection from both the connection services and see if the plumbing via
   1225          * RemoteConnection object is working.
   1226          */
   1227         mConnection = verifyConnectionForOutgoingCall();
   1228         mRemoteConnection = verifyConnectionForOutgoingCallOnRemoteCS();
   1229         mRemoteConnectionObject = mConnection.getRemoteConnection();
   1230     }
   1231 
   1232     private void addRemoteConnectionIncomingCall() {
   1233         try {
   1234             MockConnectionService managerConnectionService = new MockConnectionService() {
   1235                 @Override
   1236                 public Connection onCreateIncomingConnection(
   1237                         PhoneAccountHandle connectionManagerPhoneAccount,
   1238                         ConnectionRequest request) {
   1239                     MockConnection connection = (MockConnection)super.onCreateIncomingConnection(
   1240                             connectionManagerPhoneAccount, request);
   1241                     ConnectionRequest remoteRequest = new ConnectionRequest(
   1242                             TEST_REMOTE_PHONE_ACCOUNT_HANDLE,
   1243                             request.getAddress(),
   1244                             request.getExtras());
   1245                     RemoteConnection remoteConnection =
   1246                             CtsConnectionService.createRemoteIncomingConnectionToTelecom(
   1247                                     TEST_REMOTE_PHONE_ACCOUNT_HANDLE, remoteRequest);
   1248                     connection.setRemoteConnection(remoteConnection);
   1249                     return connection;
   1250                 }
   1251             };
   1252             setupConnectionServices(managerConnectionService, null, FLAG_REGISTER | FLAG_ENABLE);
   1253         } catch(Exception e) {
   1254             fail("Error in setting up the connection services");
   1255         }
   1256         addAndVerifyNewIncomingCall(createTestNumber(), null);
   1257         /**
   1258          * Retrieve the connection from both the connection services and see if the plumbing via
   1259          * RemoteConnection object is working.
   1260          */
   1261         mConnection = verifyConnectionForIncomingCall();
   1262         mRemoteConnection = verifyConnectionForIncomingCallOnRemoteCS();
   1263         mRemoteConnectionObject = mConnection.getRemoteConnection();
   1264     }
   1265 
   1266     private Handler setupRemoteConnectionCallbacksTest() {
   1267         addRemoteConnectionOutgoingCall();
   1268         final Call call = mInCallCallbacks.getService().getLastCall();
   1269         assertCallState(call, Call.STATE_DIALING);
   1270         verifyRemoteConnectionObject(mRemoteConnectionObject, mRemoteConnection);
   1271 
   1272         // Create a looper thread for the callbacks.
   1273         HandlerThread workerThread = new HandlerThread("CallbackThread");
   1274         workerThread.start();
   1275         Handler handler = new Handler(workerThread.getLooper());
   1276         return handler;
   1277     }
   1278 
   1279     private Handler setupRemoteConnectionVideoCallbacksTest() {
   1280         addRemoteConnectionOutgoingCall();
   1281         final Call call = mInCallCallbacks.getService().getLastCall();
   1282         assertCallState(call, Call.STATE_DIALING);
   1283         verifyRemoteConnectionObject(mRemoteConnectionObject, mRemoteConnection);
   1284 
   1285         // Create a looper thread for the callbacks.
   1286         HandlerThread workerThread = new HandlerThread("CallbackThread");
   1287         workerThread.start();
   1288         Handler handler = new Handler(workerThread.getLooper());
   1289 
   1290         final InvokeCounter callbackInvoker = new InvokeCounter("RemoteConnectionCallbacks");
   1291 
   1292         RemoteConnection.Callback callback = new RemoteConnection.Callback() {
   1293             @Override
   1294             public void onVideoProviderChanged(
   1295                     RemoteConnection connection, VideoProvider videoProvider) {
   1296                 callbackInvoker.invoke(connection, videoProvider);
   1297             }
   1298         };
   1299         mRemoteConnectionObject.registerCallback(callback, handler);
   1300         mRemoteConnection.createMockVideoProvider();
   1301         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
   1302         assertEquals(mRemoteConnectionObject, callbackInvoker.getArgs(0)[0]);
   1303         mRemoteConnectionObject.unregisterCallback(callback);
   1304         return handler;
   1305     }
   1306 
   1307     private Handler setupRemoteConnectionVideoTest(MockVideoProvider mockVideoProvider) {
   1308         addRemoteConnectionOutgoingCall();
   1309         final Call call = mInCallCallbacks.getService().getLastCall();
   1310         assertCallState(call, Call.STATE_DIALING);
   1311         verifyRemoteConnectionObject(mRemoteConnectionObject, mRemoteConnection);
   1312 
   1313         // Create a looper thread for the callbacks.
   1314         HandlerThread workerThread = new HandlerThread("CallbackThread");
   1315         workerThread.start();
   1316         Handler handler = new Handler(workerThread.getLooper());
   1317 
   1318         final InvokeCounter callbackInvoker = new InvokeCounter("RemoteConnectionCallbacks");
   1319 
   1320         RemoteConnection.Callback callback = new RemoteConnection.Callback() {
   1321             @Override
   1322             public void onVideoProviderChanged(
   1323                     RemoteConnection connection, VideoProvider videoProvider) {
   1324                 callbackInvoker.invoke(connection, videoProvider);
   1325             }
   1326         };
   1327         mRemoteConnectionObject.registerCallback(callback, handler);
   1328         mRemoteConnection.setVideoProvider(mockVideoProvider);
   1329         callbackInvoker.waitForCount(1, WAIT_FOR_STATE_CHANGE_TIMEOUT_MS);
   1330         assertEquals(mRemoteConnectionObject, callbackInvoker.getArgs(0)[0]);
   1331         mRemoteConnectionObject.unregisterCallback(callback);
   1332         return handler;
   1333     }
   1334 }
   1335