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
asBinder()124         public IBinder asBinder() {
125             return this;
126         }
127 
128         @Override
queryLocalInterface(String descriptor)129         public IInterface queryLocalInterface(String descriptor) {
130             return this;
131         }
132     }
133 
134     private IInCallService.Stub mInCallServiceFake = new FakeInCallService();
135     private IInCallService.Stub mInCallServiceSpy = Mockito.spy(mInCallServiceFake);
136 
InCallServiceFixture()137     public InCallServiceFixture() throws Exception { }
138 
139     @Override
getTestDouble()140     public IInCallService getTestDouble() {
141         return mInCallServiceSpy;
142     }
143 
getCall(String id)144     public ParcelableCall getCall(String id) {
145         return mCallById.get(id);
146     }
147 
getInCallAdapter()148     public IInCallAdapter getInCallAdapter() {
149         return mInCallAdapter;
150     }
151 
waitForUpdate()152     public void waitForUpdate() {
153         try {
154             mLock.await(5000, TimeUnit.MILLISECONDS);
155         } catch (InterruptedException ie) {
156             return;
157         }
158         mLock = new CountDownLatch(1);
159     }
160 }
161