From f204078965a59e2440adde040b90ef556919aeda Mon Sep 17 00:00:00 2001 From: Yorke Lee Date: Thu, 2 Apr 2015 17:12:59 -0700 Subject: Refactor mock CallList functionality MockCallListWrapper provides an instance of a mock call list and provides functionality to easily add/remove calls from the mock call list. Change-Id: I4f7651284d35bdac61574fd804afa33dbf43d6b0 --- InCallUI/src/com/android/incallui/CallList.java | 7 +- .../com/android/incallui/InCallPresenterTest.java | 73 +++++++------------- .../com/android/incallui/MockCallListWrapper.java | 80 ++++++++++++++++++++++ 3 files changed, 110 insertions(+), 50 deletions(-) create mode 100644 InCallUI/tests/src/com/android/incallui/MockCallListWrapper.java diff --git a/InCallUI/src/com/android/incallui/CallList.java b/InCallUI/src/com/android/incallui/CallList.java index 111d48d2e..1ede89d8b 100644 --- a/InCallUI/src/com/android/incallui/CallList.java +++ b/InCallUI/src/com/android/incallui/CallList.java @@ -16,6 +16,7 @@ package com.android.incallui; +import com.android.contacts.common.testing.NeededForTesting; import com.google.common.collect.Maps; import com.google.common.base.Preconditions; @@ -94,9 +95,11 @@ public class CallList implements InCallPhoneListener { }; /** - * Private constructor. Instance should only be acquired through getInstance(). + * USED ONLY FOR TESTING + * Testing-only constructor. Instance should only be acquired through getInstance(). */ - private CallList() { + @NeededForTesting + CallList() { } @Override diff --git a/InCallUI/tests/src/com/android/incallui/InCallPresenterTest.java b/InCallUI/tests/src/com/android/incallui/InCallPresenterTest.java index 55ab1e1dd..3345c6bb0 100644 --- a/InCallUI/tests/src/com/android/incallui/InCallPresenterTest.java +++ b/InCallUI/tests/src/com/android/incallui/InCallPresenterTest.java @@ -32,15 +32,9 @@ import org.mockito.Mockito; import org.mockito.MockitoAnnotations; public class InCallPresenterTest extends InstrumentationTestCase { - private Call mIncomingCall; - private Call mActiveCall; - private Call mConnectingCall; - private Call mWaitingForAccountsCall; - private Call mDisconnectedCall; - + private MockCallListWrapper mCallList; @Mock private InCallActivity mInCallActivity; @Mock private AudioModeProvider mAudioModeProvider; - @Mock private CallList mCallList; @Mock private StatusBarNotifier mStatusBarNotifier; @Mock private ContactInfoCache mContactInfoCache; @Mock private ProximitySensor mProximitySensor; @@ -54,16 +48,11 @@ public class InCallPresenterTest extends InstrumentationTestCase { System.setProperty("dexmaker.dexcache", getInstrumentation().getTargetContext().getCacheDir().getPath()); MockitoAnnotations.initMocks(this); - - mIncomingCall = getMockCall(Call.State.INCOMING); - mActiveCall = getMockCall(Call.State.ACTIVE); - mConnectingCall = getMockCall(Call.State.CONNECTING); - mWaitingForAccountsCall = getMockCall(Call.State.PRE_DIAL_WAIT, false); - mDisconnectedCall = getMockCall(Call.State.DISCONNECTED); + mCallList = new MockCallListWrapper(); mInCallPresenter = InCallPresenter.getInstance(); - mInCallPresenter.setUp(mContext, mCallList, mAudioModeProvider, mStatusBarNotifier, - mContactInfoCache, mProximitySensor); + mInCallPresenter.setUp(mContext, mCallList.getCallList(), mAudioModeProvider, + mStatusBarNotifier, mContactInfoCache, mProximitySensor); } @Override @@ -87,11 +76,12 @@ public class InCallPresenterTest extends InstrumentationTestCase { } public void testOnCallListChange_sendsNotificationWhenInCall() { - when(mCallList.getIncomingCall()).thenReturn(mIncomingCall); + mCallList.setHasCall(Call.State.INCOMING, true); - mInCallPresenter.onCallListChange(mCallList); + mInCallPresenter.onCallListChange(mCallList.getCallList()); - verify(mStatusBarNotifier).updateNotification(InCallState.INCOMING, mCallList); + verify(mStatusBarNotifier).updateNotification(InCallState.INCOMING, + mCallList.getCallList()); verifyInCallActivityNotStarted(); } @@ -100,18 +90,18 @@ public class InCallPresenterTest extends InstrumentationTestCase { * activity. */ public void testOnCallListChange_handlesCallWaitingWhenScreenOffShouldRestartActivity() { - when(mCallList.getActiveCall()).thenReturn(mActiveCall); + mCallList.setHasCall(Call.State.ACTIVE, true); - mInCallPresenter.onCallListChange(mCallList); + mInCallPresenter.onCallListChange(mCallList.getCallList()); mInCallPresenter.setActivity(mInCallActivity); // Pretend that there is a call waiting and the screen is off when(mInCallActivity.isDestroyed()).thenReturn(false); when(mInCallActivity.isFinishing()).thenReturn(false); when(mProximitySensor.isScreenReallyOff()).thenReturn(true); - when(mCallList.getIncomingCall()).thenReturn(mIncomingCall); + mCallList.setHasCall(Call.State.INCOMING, true); - mInCallPresenter.onCallListChange(mCallList); + mInCallPresenter.onCallListChange(mCallList.getCallList()); verify(mInCallActivity).finish(); } @@ -120,14 +110,14 @@ public class InCallPresenterTest extends InstrumentationTestCase { * that it can display an error dialog. */ public void testOnCallListChange_pendingOutgoingToInCallTransitionShowsUiForErrorDialog() { - when(mCallList.getPendingOutgoingCall()).thenReturn(mConnectingCall); + mCallList.setHasCall(Call.State.CONNECTING, true); - mInCallPresenter.onCallListChange(mCallList); + mInCallPresenter.onCallListChange(mCallList.getCallList()); - when(mCallList.getPendingOutgoingCall()).thenReturn(null); - when(mCallList.getActiveCall()).thenReturn(mActiveCall); + mCallList.setHasCall(Call.State.CONNECTING, false); + mCallList.setHasCall(Call.State.ACTIVE, true); - mInCallPresenter.onCallListChange(mCallList); + mInCallPresenter.onCallListChange(mCallList.getCallList()); verify(mContext).startActivity(InCallPresenter.getInstance().getInCallIntent(false, false)); verifyIncomingCallNotificationNotSent(); @@ -138,8 +128,8 @@ public class InCallPresenterTest extends InstrumentationTestCase { * to display the account picker. */ public void testOnCallListChange_noAccountProvidedForCallShowsUiForAccountPicker() { - when(mCallList.getWaitingForAccountCall()).thenReturn(mWaitingForAccountsCall); - mInCallPresenter.onCallListChange(mCallList); + mCallList.setHasCall(Call.State.PRE_DIAL_WAIT, true); + mInCallPresenter.onCallListChange(mCallList.getCallList()); verify(mContext).startActivity(InCallPresenter.getInstance().getInCallIntent(false, false)); verifyIncomingCallNotificationNotSent(); @@ -150,24 +140,24 @@ public class InCallPresenterTest extends InstrumentationTestCase { * InCallActivity is not displayed. */ public void testOnCallListChange_noCallsToPendingOutgoingDoesNotShowUi() { - when(mCallList.getPendingOutgoingCall()).thenReturn(mConnectingCall); - mInCallPresenter.onCallListChange(mCallList); + mCallList.setHasCall(Call.State.CONNECTING, true); + mInCallPresenter.onCallListChange(mCallList.getCallList()); verifyInCallActivityNotStarted(); verifyIncomingCallNotificationNotSent(); } public void testOnCallListChange_LastCallDisconnectedNoCallsLeftFinishesUi() { - when(mCallList.getDisconnectedCall()).thenReturn(mDisconnectedCall); - mInCallPresenter.onCallListChange(mCallList); + mCallList.setHasCall(Call.State.DISCONNECTED, true); + mInCallPresenter.onCallListChange(mCallList.getCallList()); mInCallPresenter.setActivity(mInCallActivity); verify(mInCallActivity, never()).finish(); // Last remaining disconnected call is removed from CallList, activity should shut down. - when(mCallList.getDisconnectedCall()).thenReturn(null); - mInCallPresenter.onCallListChange(mCallList); + mCallList.setHasCall(Call.State.DISCONNECTED, false); + mInCallPresenter.onCallListChange(mCallList.getCallList()); verify(mInCallActivity).finish(); } @@ -197,17 +187,4 @@ public class InCallPresenterTest extends InstrumentationTestCase { verify(mStatusBarNotifier, never()).updateNotification(Mockito.any(InCallState.class), Mockito.any(CallList.class)); } - - private static Call getMockCall(int state) { - return getMockCall(state, true); - } - - private static Call getMockCall(int state, boolean hasAccountHandle) { - final Call call = Mockito.mock(Call.class); - when(call.getState()).thenReturn(Integer.valueOf(state)); - if (hasAccountHandle) { - when(call.getAccountHandle()).thenReturn(new PhoneAccountHandle(null, null)); - } - return call; - } } diff --git a/InCallUI/tests/src/com/android/incallui/MockCallListWrapper.java b/InCallUI/tests/src/com/android/incallui/MockCallListWrapper.java new file mode 100644 index 000000000..5b574825c --- /dev/null +++ b/InCallUI/tests/src/com/android/incallui/MockCallListWrapper.java @@ -0,0 +1,80 @@ +/* + * 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.incallui; + +import static org.mockito.Mockito.anyInt; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import android.telecom.PhoneAccountHandle; + +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +import java.util.HashSet; + +/** + * Provides an instance of a mock CallList, and provides utility methods to put the CallList into + * various states (e.g. incoming call, active call, call waiting). + */ +public class MockCallListWrapper { + private CallList mCallList; + private HashSet mCallSet = new HashSet<>(); + + public MockCallListWrapper() { + mCallList = Mockito.mock(CallList.class); + mCallList = spy(new CallList()); + when(mCallList.getFirstCallWithState(anyInt())).thenAnswer(new Answer() { + @Override + public Call answer(InvocationOnMock i) throws Throwable { + Object[] args = i.getArguments(); + final int state = (int) args[0]; + if (mCallSet.contains(state)) { + return getMockCall(state); + } else { + return null; + } + } + }); + } + + public CallList getCallList() { + return mCallList; + } + + public void setHasCall(int state, boolean hasCall) { + if (hasCall) { + mCallSet.add(state); + } else { + mCallSet.remove(state); + } + } + + private static Call getMockCall(int state) { + return getMockCall(state, state != Call.State.PRE_DIAL_WAIT); + } + + private static Call getMockCall(int state, boolean hasAccountHandle) { + final Call call = Mockito.mock(Call.class); + when(call.getState()).thenReturn(Integer.valueOf(state)); + if (hasAccountHandle) { + when(call.getAccountHandle()).thenReturn(new PhoneAccountHandle(null, null)); + } + return call; + } +} -- cgit v1.2.3