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 com.android.server.telecom.tests;
18 
19 import com.android.internal.telecom.IInCallAdapter;
20 import com.android.internal.telecom.IInCallService;
21 
22 import org.mockito.Mockito;
23 
24 import android.os.Bundle;
25 import android.os.IBinder;
26 import android.os.IInterface;
27 import android.os.RemoteException;
28 import android.telecom.AudioState;
29 import android.telecom.CallAudioState;
30 import android.telecom.ParcelableCall;
31 
32 import java.util.HashMap;
33 import java.util.Map;
34 import java.util.concurrent.CountDownLatch;
35 import java.util.concurrent.TimeUnit;
36 
37 /**
38  * Controls a test {@link IInCallService} as would be provided by an InCall UI on a system.
39  */
40 public class InCallServiceFixture implements TestFixture<IInCallService> {
41 
42     public String mLatestCallId;
43     public IInCallAdapter mInCallAdapter;
44     public CallAudioState mCallAudioState;
45     public final Map<String, ParcelableCall> mCallById = new HashMap<>();
46     public final Map<String, String> mPostDialById = new HashMap<>();
47     public final Map<String, String> mPostDialWaitById = new HashMap<>();
48     public boolean mBringToForeground;
49     public boolean mShowDialpad;
50     public boolean mCanAddCall;
51     public boolean mSilenceRinger;
52     public CountDownLatch mLock = new CountDownLatch(1);
53 
54     public class FakeInCallService extends IInCallService.Stub {
55         @Override
setInCallAdapter(IInCallAdapter inCallAdapter)56         public void setInCallAdapter(IInCallAdapter inCallAdapter) throws RemoteException {
57             if (mInCallAdapter != null && inCallAdapter != null) {
58                 throw new RuntimeException("Adapter is already set");
59             }
60             if (mInCallAdapter == null && inCallAdapter == null) {
61                 throw new RuntimeException("Adapter was never set");
62             }
63             mInCallAdapter = inCallAdapter;
64         }
65 
66         @Override
addCall(ParcelableCall call)67         public void addCall(ParcelableCall call) throws RemoteException {
68             if (mCallById.containsKey(call.getId())) {
69                 throw new RuntimeException("Call " + call.getId() + " already added");
70             }
71             mCallById.put(call.getId(), call);
72             mLatestCallId = call.getId();
73         }
74 
75         @Override
updateCall(ParcelableCall call)76         public void updateCall(ParcelableCall call) throws RemoteException {
77             if (!mCallById.containsKey(call.getId())) {
78                 throw new RuntimeException("Call " + call.getId() + " not added yet");
79             }
80             mCallById.put(call.getId(), call);
81             mLatestCallId = call.getId();
82             mLock.countDown();
83         }
84 
85         @Override
setPostDial(String callId, String remaining)86         public void setPostDial(String callId, String remaining) throws RemoteException {
87             mPostDialWaitById.remove(callId);
88             mPostDialById.put(callId, remaining);
89         }
90 
91         @Override
setPostDialWait(String callId, String remaining)92         public void setPostDialWait(String callId, String remaining) throws RemoteException {
93             mPostDialById.remove(callId);
94             mPostDialWaitById.put(callId, remaining);
95         }
96 
97         @Override
onCallAudioStateChanged(CallAudioState audioState)98         public void onCallAudioStateChanged(CallAudioState audioState) throws RemoteException {
99             mCallAudioState = audioState;
100         }
101 
102         @Override
bringToForeground(boolean showDialpad)103         public void bringToForeground(boolean showDialpad) throws RemoteException {
104             mBringToForeground = true;
105             mShowDialpad = showDialpad;
106         }
107 
108         @Override
onCanAddCallChanged(boolean canAddCall)109         public void onCanAddCallChanged(boolean canAddCall) throws RemoteException {
110             mCanAddCall = canAddCall;
111         }
112 
113         @Override
silenceRinger()114         public void silenceRinger() throws RemoteException {
115             mSilenceRinger = true;
116         }
117 
118         @Override
onConnectionEvent(String callId, String event, Bundle extras)119         public void onConnectionEvent(String callId, String event, Bundle extras)
120                 throws RemoteException {
121         }
122 
123         @Override
onRttUpgradeRequest(String callId, int id)124         public void onRttUpgradeRequest(String callId, int id) throws RemoteException {
125         }
126 
127         @Override
onRttInitiationFailure(String callId, int reason)128         public void onRttInitiationFailure(String callId, int reason) throws RemoteException {
129         }
130 
131         @Override
asBinder()132         public IBinder asBinder() {
133             return this;
134         }
135 
136         @Override
queryLocalInterface(String descriptor)137         public IInterface queryLocalInterface(String descriptor) {
138             return this;
139         }
140     }
141 
142     private IInCallService.Stub mInCallServiceFake = new FakeInCallService();
143     private IInCallService.Stub mInCallServiceSpy = Mockito.spy(mInCallServiceFake);
144 
InCallServiceFixture()145     public InCallServiceFixture() throws Exception { }
146 
147     @Override
getTestDouble()148     public IInCallService getTestDouble() {
149         return mInCallServiceSpy;
150     }
151 
getCall(String id)152     public ParcelableCall getCall(String id) {
153         return mCallById.get(id);
154     }
155 
getInCallAdapter()156     public IInCallAdapter getInCallAdapter() {
157         return mInCallAdapter;
158     }
159 
waitForUpdate()160     public void waitForUpdate() {
161         try {
162             mLock.await(5000, TimeUnit.MILLISECONDS);
163         } catch (InterruptedException ie) {
164             return;
165         }
166         mLock = new CountDownLatch(1);
167     }
168 }
169