InCallServiceFixture.java revision aeece4ec4184b76e0ac2e8a012af05638ad866f6
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 void onRttInitiationFailure(String callId, int reason) throws RemoteException {
129        }
130
131        @Override
132        public IBinder asBinder() {
133            return this;
134        }
135
136        @Override
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
145    public InCallServiceFixture() throws Exception { }
146
147    @Override
148    public IInCallService getTestDouble() {
149        return mInCallServiceSpy;
150    }
151
152    public ParcelableCall getCall(String id) {
153        return mCallById.get(id);
154    }
155
156    public IInCallAdapter getInCallAdapter() {
157        return mInCallAdapter;
158    }
159
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