/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.server.telecom.tests; import com.android.internal.annotations.VisibleForTesting; import com.android.internal.telecom.IInCallAdapter; import com.android.internal.telecom.IInCallService; import org.mockito.Mockito; import android.os.Bundle; import android.os.IBinder; import android.os.IInterface; import android.os.RemoteException; import android.telecom.CallAudioState; import android.telecom.CallEndpoint; import android.telecom.ParcelableCall; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; /** * Controls a test {@link IInCallService} as would be provided by an InCall UI on a system. */ public class InCallServiceFixture implements TestFixture { public static boolean sIgnoreOverrideAdapterFlag = false; public String mLatestCallId; public IInCallAdapter mInCallAdapter; public CallAudioState mCallAudioState; public final Map mCallById = new HashMap<>(); public final Map mPostDialById = new HashMap<>(); public final Map mPostDialWaitById = new HashMap<>(); public boolean mBringToForeground; public boolean mShowDialpad; public boolean mCanAddCall; public boolean mSilenceRinger; public CountDownLatch mUpdateCallLock = new CountDownLatch(1); public CountDownLatch mAddCallLock = new CountDownLatch(1); @VisibleForTesting public static void setIgnoreOverrideAdapterFlag(boolean flag) { sIgnoreOverrideAdapterFlag = flag; } public class FakeInCallService extends IInCallService.Stub { @Override public void setInCallAdapter(IInCallAdapter inCallAdapter) throws RemoteException { // sIgnoreOverrideAdapterFlag is being used to verify a scenario where the InCallAdapter // gets set twice (secondary user places MO/MT call). if (mInCallAdapter != null && inCallAdapter != null && !sIgnoreOverrideAdapterFlag) { throw new RuntimeException("Adapter is already set"); } if (mInCallAdapter == null && inCallAdapter == null) { throw new RuntimeException("Adapter was never set"); } mInCallAdapter = inCallAdapter; } @Override public void addCall(ParcelableCall call) throws RemoteException { if (mCallById.containsKey(call.getId())) { throw new RuntimeException("Call " + call.getId() + " already added"); } mLatestCallId = call.getId(); mCallById.put(call.getId(), call); mAddCallLock.countDown(); } @Override public void updateCall(ParcelableCall call) throws RemoteException { if (!mCallById.containsKey(call.getId())) { // This used to throw an exception, however the actual InCallService implementation // ignores updates for calls which don't yet exist. This is not a problem as when // a call is added to an InCallService its entire state is parceled and sent to the // InCallService. return; } mLatestCallId = call.getId(); mCallById.put(call.getId(), call); mUpdateCallLock.countDown(); } @Override public void setPostDial(String callId, String remaining) throws RemoteException { mPostDialWaitById.remove(callId); mPostDialById.put(callId, remaining); } @Override public void setPostDialWait(String callId, String remaining) throws RemoteException { mPostDialById.remove(callId); mPostDialWaitById.put(callId, remaining); } @Override public void onCallAudioStateChanged(CallAudioState audioState) throws RemoteException { mCallAudioState = audioState; } @Override public void onCallEndpointChanged(CallEndpoint callEndpoint) {} @Override public void onAvailableCallEndpointsChanged(List availableCallEndpoints) {} @Override public void onMuteStateChanged(boolean isMuted) {} @Override public void bringToForeground(boolean showDialpad) throws RemoteException { mBringToForeground = true; mShowDialpad = showDialpad; } @Override public void onCanAddCallChanged(boolean canAddCall) throws RemoteException { mCanAddCall = canAddCall; } @Override public void silenceRinger() throws RemoteException { mSilenceRinger = true; } @Override public void onConnectionEvent(String callId, String event, Bundle extras) throws RemoteException { } @Override public void onRttUpgradeRequest(String callId, int id) throws RemoteException { } @Override public void onRttInitiationFailure(String callId, int reason) throws RemoteException { } @Override public IBinder asBinder() { return this; } @Override public IInterface queryLocalInterface(String descriptor) { return this; } @Override public void onHandoverFailed(String callId, int error) {} @Override public void onHandoverComplete(String callId) {} } private IInCallService.Stub mInCallServiceFake = new FakeInCallService(); private IInCallService.Stub mInCallServiceSpy = Mockito.spy(mInCallServiceFake); public InCallServiceFixture() throws Exception { } @Override public IInCallService getTestDouble() { return mInCallServiceSpy; } public ParcelableCall getCall(String id) { return mCallById.get(id); } public IInCallAdapter getInCallAdapter() { return mInCallAdapter; } public void waitForUpdate() { try { mUpdateCallLock.await(5000, TimeUnit.MILLISECONDS); } catch (InterruptedException ie) { return; } mUpdateCallLock = new CountDownLatch(1); } public void waitUntilNumCalls(int numCalls) { if (mCallById.size() == numCalls) { return; } mAddCallLock = new CountDownLatch(1); try { mAddCallLock.await(5000, TimeUnit.MILLISECONDS); } catch (InterruptedException ie) { return; } } }