InCallServiceFixture.java revision 2a66f7b906b225413ae33f72e70a75e4f9c883c0
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.IBinder;
25import android.os.IInterface;
26import android.os.RemoteException;
27import android.telecom.AudioState;
28import android.telecom.CallAudioState;
29import android.telecom.ParcelableCall;
30
31import java.util.HashMap;
32import java.util.Map;
33
34/**
35 * Controls a test {@link IInCallService} as would be provided by an InCall UI on a system.
36 */
37public class InCallServiceFixture implements TestFixture<IInCallService> {
38
39    public String mLatestCallId;
40    public IInCallAdapter mInCallAdapter;
41    public CallAudioState mCallAudioState;
42    public final Map<String, ParcelableCall> mCallById = new HashMap<>();
43    public final Map<String, String> mPostDialById = new HashMap<>();
44    public final Map<String, String> mPostDialWaitById = new HashMap<>();
45    public boolean mBringToForeground;
46    public boolean mShowDialpad;
47    public boolean mCanAddCall;
48
49    public class FakeInCallService extends IInCallService.Stub {
50        @Override
51        public void setInCallAdapter(IInCallAdapter inCallAdapter) throws RemoteException {
52            if (mInCallAdapter != null && inCallAdapter != null) {
53                throw new RuntimeException("Adapter is already set");
54            }
55            if (mInCallAdapter == null && inCallAdapter == null) {
56                throw new RuntimeException("Adapter was never set");
57            }
58            mInCallAdapter = inCallAdapter;
59        }
60
61        @Override
62        public void addCall(ParcelableCall call) throws RemoteException {
63            if (mCallById.containsKey(call.getId())) {
64                throw new RuntimeException("Call " + call.getId() + " already added");
65            }
66            mCallById.put(call.getId(), call);
67            mLatestCallId = call.getId();
68        }
69
70        @Override
71        public void updateCall(ParcelableCall call) throws RemoteException {
72            if (!mCallById.containsKey(call.getId())) {
73                throw new RuntimeException("Call " + call.getId() + " not added yet");
74            }
75            mCallById.put(call.getId(), call);
76            mLatestCallId = call.getId();
77        }
78
79        @Override
80        public void setPostDial(String callId, String remaining) throws RemoteException {
81            mPostDialWaitById.remove(callId);
82            mPostDialById.put(callId, remaining);
83        }
84
85        @Override
86        public void setPostDialWait(String callId, String remaining) throws RemoteException {
87            mPostDialById.remove(callId);
88            mPostDialWaitById.put(callId, remaining);
89        }
90
91        @Override
92        public void onCallAudioStateChanged(CallAudioState audioState) throws RemoteException {
93            mCallAudioState = audioState;
94        }
95
96        @Override
97        public void bringToForeground(boolean showDialpad) throws RemoteException {
98            mBringToForeground = true;
99            mShowDialpad = showDialpad;
100        }
101
102        @Override
103        public void onCanAddCallChanged(boolean canAddCall) throws RemoteException {
104            mCanAddCall = canAddCall;
105        }
106
107        @Override
108        public IBinder asBinder() {
109            return this;
110        }
111
112        @Override
113        public IInterface queryLocalInterface(String descriptor) {
114            return this;
115        }
116    }
117
118    private IInCallService.Stub mInCallServiceFake = new FakeInCallService();
119    private IInCallService.Stub mInCallServiceSpy = Mockito.spy(mInCallServiceFake);
120
121    public InCallServiceFixture() throws Exception { }
122
123    @Override
124    public IInCallService getTestDouble() {
125        return mInCallServiceSpy;
126    }
127
128    public ParcelableCall getCall(String id) {
129        return mCallById.get(id);
130    }
131}
132