InCallServiceFixture.java revision dd68bc36a3278557b1c4d9183ed9e3dee077eb20
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
17package com.android.server.telecom.tests;
18
19import com.android.internal.telecom.IInCallAdapter;
20import com.android.internal.telecom.IInCallService;
21
22import org.mockito.Mockito;
23
24import android.os.Bundle;
25import android.os.IBinder;
26import android.os.IInterface;
27import android.os.RemoteException;
28import android.telecom.AudioState;
29import android.telecom.CallAudioState;
30import android.telecom.ParcelableCall;
31
32import java.util.HashMap;
33import java.util.Map;
34import java.util.concurrent.CountDownLatch;
35import java.util.concurrent.TimeUnit;
36
37/**
38 * Controls a test {@link IInCallService} as would be provided by an InCall UI on a system.
39 */
40public 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
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
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
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
86        public void setPostDial(String callId, String remaining) throws RemoteException {
87            mPostDialWaitById.remove(callId);
88            mPostDialById.put(callId, remaining);
89        }
90
91        @Override
92        public void setPostDialWait(String callId, String remaining) throws RemoteException {
93            mPostDialById.remove(callId);
94            mPostDialWaitById.put(callId, remaining);
95        }
96
97        @Override
98        public void onCallAudioStateChanged(CallAudioState audioState) throws RemoteException {
99            mCallAudioState = audioState;
100        }
101
102        @Override
103        public void bringToForeground(boolean showDialpad) throws RemoteException {
104            mBringToForeground = true;
105            mShowDialpad = showDialpad;
106        }
107
108        @Override
109        public void onCanAddCallChanged(boolean canAddCall) throws RemoteException {
110            mCanAddCall = canAddCall;
111        }
112
113        @Override
114        public void silenceRinger() throws RemoteException {
115            mSilenceRinger = true;
116        }
117
118        @Override
119        public void onConnectionEvent(String callId, String event, Bundle extras)
120                throws RemoteException {
121        }
122
123        @Override
124        public void onRttUpgradeRequest(String callId, int id) throws RemoteException {
125        }
126
127        @Override
128        public IBinder asBinder() {
129            return this;
130        }
131
132        @Override
133        public IInterface queryLocalInterface(String descriptor) {
134            return this;
135        }
136    }
137
138    private IInCallService.Stub mInCallServiceFake = new FakeInCallService();
139    private IInCallService.Stub mInCallServiceSpy = Mockito.spy(mInCallServiceFake);
140
141    public InCallServiceFixture() throws Exception { }
142
143    @Override
144    public IInCallService getTestDouble() {
145        return mInCallServiceSpy;
146    }
147
148    public ParcelableCall getCall(String id) {
149        return mCallById.get(id);
150    }
151
152    public IInCallAdapter getInCallAdapter() {
153        return mInCallAdapter;
154    }
155
156    public void waitForUpdate() {
157        try {
158            mLock.await(5000, TimeUnit.MILLISECONDS);
159        } catch (InterruptedException ie) {
160            return;
161        }
162        mLock = new CountDownLatch(1);
163    }
164}
165