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.CallAudioState.*;
     20 
     21 import android.net.Uri;
     22 import android.os.Bundle;
     23 import android.telecom.CallAudioState;
     24 import android.telecom.Connection;
     25 import android.telecom.DisconnectCause;
     26 import android.telecom.PhoneAccountHandle;
     27 import android.telecom.RemoteConnection;
     28 import android.telecom.VideoProfile;
     29 import android.telecom.cts.TestUtils.InvokeCounter;
     30 import android.util.SparseArray;
     31 
     32 /**
     33  * {@link Connection} subclass that immediately performs any state changes that are a result of
     34  * callbacks sent from Telecom.
     35  */
     36 public class MockConnection extends Connection {
     37     public static final int ON_POST_DIAL_WAIT = 1;
     38     public static final int ON_CALL_EVENT = 2;
     39     public static final int ON_PULL_EXTERNAL_CALL = 3;
     40     public static final int ON_EXTRAS_CHANGED = 4;
     41     public static final int ON_START_RTT = 5;
     42     public static final int ON_RTT_REQUEST_RESPONSE = 6;
     43     public static final int ON_STOP_RTT = 7;
     44     public static final int ON_DEFLECT = 8;
     45 
     46     private CallAudioState mCallAudioState =
     47             new CallAudioState(false, CallAudioState.ROUTE_EARPIECE, ROUTE_EARPIECE | ROUTE_SPEAKER);
     48     private int mState = STATE_NEW;
     49     public int videoState = VideoProfile.STATE_AUDIO_ONLY;
     50     private String mDtmfString = "";
     51     private MockVideoProvider mMockVideoProvider;
     52     private PhoneAccountHandle mPhoneAccountHandle;
     53     private RemoteConnection mRemoteConnection = null;
     54     private RttTextStream mRttTextStream;
     55 
     56     private SparseArray<InvokeCounter> mInvokeCounterMap = new SparseArray<>(10);
     57 
     58     @Override
     59     public void onAnswer() {
     60         super.onAnswer();
     61     }
     62 
     63     @Override
     64     public void onAnswer(int videoState) {
     65         super.onAnswer(videoState);
     66         this.videoState = videoState;
     67         setActive();
     68         if (mRemoteConnection != null) {
     69             mRemoteConnection.answer();
     70         }
     71     }
     72 
     73     @Override
     74     public void onReject() {
     75         super.onReject();
     76         setDisconnected(new DisconnectCause(DisconnectCause.REJECTED));
     77         if (mRemoteConnection != null) {
     78             mRemoteConnection.reject();
     79         }
     80         destroy();
     81     }
     82 
     83     @Override
     84     public void onReject(String reason) {
     85         super.onReject();
     86         setDisconnected(new DisconnectCause(DisconnectCause.REJECTED, reason));
     87         if (mRemoteConnection != null) {
     88             mRemoteConnection.reject();
     89         }
     90         destroy();
     91     }
     92 
     93     @Override
     94     public void onHold() {
     95         super.onHold();
     96         setOnHold();
     97         if (mRemoteConnection != null) {
     98             mRemoteConnection.hold();
     99         }
    100     }
    101 
    102     @Override
    103     public void onUnhold() {
    104         super.onUnhold();
    105         setActive();
    106         if (mRemoteConnection != null) {
    107             mRemoteConnection.unhold();
    108         }
    109     }
    110 
    111     @Override
    112     public void onDisconnect() {
    113         super.onDisconnect();
    114         setDisconnected(new DisconnectCause(DisconnectCause.LOCAL));
    115         if (mRemoteConnection != null) {
    116             mRemoteConnection.disconnect();
    117         }
    118         destroy();
    119     }
    120 
    121     @Override
    122     public void onAbort() {
    123         super.onAbort();
    124         setDisconnected(new DisconnectCause(DisconnectCause.UNKNOWN));
    125         if (mRemoteConnection != null) {
    126             mRemoteConnection.abort();
    127         }
    128         destroy();
    129     }
    130 
    131     @Override
    132     public void onPlayDtmfTone(char c) {
    133         super.onPlayDtmfTone(c);
    134         mDtmfString += c;
    135         if (mRemoteConnection != null) {
    136             mRemoteConnection.playDtmfTone(c);
    137         }
    138     }
    139 
    140     @Override
    141     public void onStopDtmfTone() {
    142         super.onStopDtmfTone();
    143         mDtmfString += ".";
    144         if (mRemoteConnection != null) {
    145             mRemoteConnection.stopDtmfTone();
    146         }
    147     }
    148 
    149     @Override
    150     public void onCallAudioStateChanged(CallAudioState state) {
    151         super.onCallAudioStateChanged(state);
    152         mCallAudioState = state;
    153         if (mRemoteConnection != null) {
    154             mRemoteConnection.setCallAudioState(state);
    155         }
    156     }
    157 
    158     @Override
    159     public void onStateChanged(int state) {
    160         super.onStateChanged(state);
    161         mState = state;
    162     }
    163 
    164     @Override
    165     public void onPostDialContinue(boolean proceed) {
    166         super.onPostDialContinue(proceed);
    167         if (mInvokeCounterMap.get(ON_POST_DIAL_WAIT) != null) {
    168             mInvokeCounterMap.get(ON_POST_DIAL_WAIT).invoke(proceed);
    169         }
    170     }
    171 
    172     @Override
    173     public void onCallEvent(String event, Bundle extras) {
    174         super.onCallEvent(event, extras);
    175         if (mInvokeCounterMap.get(ON_CALL_EVENT) != null) {
    176             mInvokeCounterMap.get(ON_CALL_EVENT).invoke(event, extras);
    177         }
    178     }
    179 
    180     @Override
    181     public void onPullExternalCall() {
    182         super.onPullExternalCall();
    183         if (mInvokeCounterMap.get(ON_PULL_EXTERNAL_CALL) != null) {
    184             mInvokeCounterMap.get(ON_PULL_EXTERNAL_CALL).invoke();
    185         }
    186     }
    187 
    188     @Override
    189     public void onExtrasChanged(Bundle extras) {
    190         super.onExtrasChanged(extras);
    191         if (mInvokeCounterMap.get(ON_EXTRAS_CHANGED) != null) {
    192             mInvokeCounterMap.get(ON_EXTRAS_CHANGED).invoke(extras);
    193         }
    194     }
    195 
    196     @Override
    197     public void onStartRtt(RttTextStream rttTextStream) {
    198         super.onStartRtt(rttTextStream);
    199         if (mInvokeCounterMap.get(ON_START_RTT) != null) {
    200             mInvokeCounterMap.get(ON_START_RTT).invoke(rttTextStream);
    201         }
    202     }
    203 
    204     @Override
    205     public void handleRttUpgradeResponse(RttTextStream rttTextStream) {
    206         super.handleRttUpgradeResponse(rttTextStream);
    207         if (rttTextStream != null) {
    208             setRttTextStream(rttTextStream);
    209             setConnectionProperties(getConnectionProperties() | PROPERTY_IS_RTT);
    210         }
    211 
    212         if (mInvokeCounterMap.get(ON_RTT_REQUEST_RESPONSE) != null) {
    213             mInvokeCounterMap.get(ON_RTT_REQUEST_RESPONSE).invoke(rttTextStream);
    214         }
    215     }
    216 
    217     @Override
    218     public void onStopRtt() {
    219         super.onStopRtt();
    220 
    221         if (mInvokeCounterMap.get(ON_STOP_RTT) != null) {
    222             mInvokeCounterMap.get(ON_STOP_RTT).invoke();
    223         }
    224     }
    225 
    226     @Override
    227     public void onDeflect(Uri address) {
    228         if (mInvokeCounterMap.get(ON_DEFLECT) != null) {
    229             mInvokeCounterMap.get(ON_DEFLECT).invoke(address);
    230         }
    231     }
    232 
    233     public int getCurrentState()  {
    234         return mState;
    235     }
    236 
    237     public CallAudioState getCurrentCallAudioState() {
    238         return mCallAudioState;
    239     }
    240 
    241     public String getDtmfString() {
    242         return mDtmfString;
    243     }
    244 
    245     public InvokeCounter getInvokeCounter(int counterIndex) {
    246         if (mInvokeCounterMap.get(counterIndex) == null) {
    247             mInvokeCounterMap.put(counterIndex,
    248                     new InvokeCounter(getCounterLabel(counterIndex)));
    249         }
    250         return mInvokeCounterMap.get(counterIndex);
    251     }
    252 
    253     /**
    254      * Creates a mock video provider for this connection.
    255      */
    256     public void createMockVideoProvider() {
    257         final MockVideoProvider mockVideoProvider = new MockVideoProvider(this);
    258         mMockVideoProvider = mockVideoProvider;
    259         setVideoProvider(mockVideoProvider);
    260     }
    261 
    262     public void sendMockVideoQuality(int videoQuality) {
    263         if (mMockVideoProvider == null) {
    264             return;
    265         }
    266         mMockVideoProvider.sendMockVideoQuality(videoQuality);
    267     }
    268 
    269     public void sendMockCallSessionEvent(int event) {
    270         if (mMockVideoProvider == null) {
    271             return;
    272         }
    273         mMockVideoProvider.sendMockCallSessionEvent(event);
    274     }
    275 
    276     public void sendMockPeerWidth(int width) {
    277         if (mMockVideoProvider == null) {
    278             return;
    279         }
    280         mMockVideoProvider.sendMockPeerWidth(width);
    281     }
    282 
    283     public void sendMockSessionModifyRequest(VideoProfile request) {
    284         if (mMockVideoProvider == null) {
    285             return;
    286         }
    287         mMockVideoProvider.sendMockSessionModifyRequest(request);
    288     }
    289 
    290     public MockVideoProvider getMockVideoProvider() {
    291         return mMockVideoProvider;
    292     }
    293 
    294     public void setPhoneAccountHandle(PhoneAccountHandle handle)  {
    295         mPhoneAccountHandle = handle;
    296     }
    297 
    298     public PhoneAccountHandle getPhoneAccountHandle()  {
    299         return mPhoneAccountHandle;
    300     }
    301 
    302     public void setRemoteConnection(RemoteConnection remoteConnection)  {
    303         mRemoteConnection = remoteConnection;
    304     }
    305 
    306     public RemoteConnection getRemoteConnection()  {
    307         return mRemoteConnection;
    308     }
    309 
    310     public void setRttTextStream(RttTextStream rttTextStream) {
    311         mRttTextStream = rttTextStream;
    312     }
    313 
    314     public RttTextStream getRttTextStream() {
    315         return mRttTextStream;
    316     }
    317 
    318     private static String getCounterLabel(int counterIndex) {
    319         switch (counterIndex) {
    320             case ON_POST_DIAL_WAIT:
    321                 return "onPostDialWait";
    322             case ON_CALL_EVENT:
    323                 return "onCallEvent";
    324             case ON_PULL_EXTERNAL_CALL:
    325                 return "onPullExternalCall";
    326             case ON_EXTRAS_CHANGED:
    327                 return "onExtrasChanged";
    328             case ON_START_RTT:
    329                 return "onStartRtt";
    330             case ON_RTT_REQUEST_RESPONSE:
    331                 return "onRttRequestResponse";
    332             case ON_STOP_RTT:
    333                 return "onStopRtt";
    334             case ON_DEFLECT:
    335                 return "onDeflect";
    336             default:
    337                 return "Callback";
    338         }
    339     }
    340 }
    341