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 17 package com.android.incallui; 18 19 import static org.mockito.Mockito.never; 20 import static org.mockito.Mockito.verify; 21 import static org.mockito.Mockito.when; 22 23 import android.content.Context; 24 import android.content.Intent; 25 import android.telecom.PhoneAccountHandle; 26 import android.telephony.TelephonyManager; 27 import android.test.InstrumentationTestCase; 28 import android.test.suitebuilder.annotation.MediumTest; 29 30 import com.android.incallui.InCallPresenter.InCallState; 31 32 import org.mockito.Mock; 33 import org.mockito.Mockito; 34 import org.mockito.MockitoAnnotations; 35 36 @MediumTest 37 public class InCallPresenterTest extends InstrumentationTestCase { 38 private MockCallListWrapper mCallList; 39 @Mock private InCallActivity mInCallActivity; 40 @Mock private AudioModeProvider mAudioModeProvider; 41 @Mock private StatusBarNotifier mStatusBarNotifier; 42 @Mock private ExternalCallNotifier mExternalCallNotifier; 43 @Mock private ContactInfoCache mContactInfoCache; 44 @Mock private ProximitySensor mProximitySensor; 45 46 InCallPresenter mInCallPresenter; 47 @Mock private Context mContext; 48 @Mock private TelephonyManager mTelephonyManager; 49 50 @Override setUp()51 protected void setUp() throws Exception { 52 super.setUp(); 53 System.setProperty("dexmaker.dexcache", 54 getInstrumentation().getTargetContext().getCacheDir().getPath()); 55 MockitoAnnotations.initMocks(this); 56 mCallList = new MockCallListWrapper(); 57 58 when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager); 59 60 mInCallPresenter = InCallPresenter.getInstance(); 61 mInCallPresenter.setUp(mContext, mCallList.getCallList(), new ExternalCallList(), 62 mAudioModeProvider, mStatusBarNotifier, mExternalCallNotifier, mContactInfoCache, 63 mProximitySensor); 64 } 65 66 @Override tearDown()67 protected void tearDown() throws Exception { 68 // The tear down method needs to run in the main thread since there is an explicit check 69 // inside TelecomAdapter.getInstance(). 70 getInstrumentation().runOnMainSync(new Runnable() { 71 @Override 72 public void run() { 73 mInCallPresenter.unsetActivity(mInCallActivity); 74 mInCallPresenter.tearDown(); 75 InCallPresenter.setInstance(null); 76 } 77 }); 78 } 79 testOnActivitySet_finishesActivityWhenNoCalls()80 public void testOnActivitySet_finishesActivityWhenNoCalls() { 81 mInCallPresenter.setActivity(mInCallActivity); 82 83 verify(mInCallActivity).finish(); 84 } 85 testOnCallListChange_sendsNotificationWhenInCall()86 public void testOnCallListChange_sendsNotificationWhenInCall() { 87 mCallList.setHasCall(Call.State.INCOMING, true); 88 89 mInCallPresenter.onCallListChange(mCallList.getCallList()); 90 91 verify(mStatusBarNotifier).updateNotification(InCallState.INCOMING, 92 mCallList.getCallList()); 93 verifyInCallActivityNotStarted(); 94 } 95 96 /** 97 * This behavior is required to ensure that the screen is turned on again by the restarting 98 * activity. 99 */ testOnCallListChange_handlesCallWaitingWhenScreenOffShouldRestartActivity()100 public void testOnCallListChange_handlesCallWaitingWhenScreenOffShouldRestartActivity() { 101 mCallList.setHasCall(Call.State.ACTIVE, true); 102 103 mInCallPresenter.onCallListChange(mCallList.getCallList()); 104 mInCallPresenter.setActivity(mInCallActivity); 105 106 // Pretend that there is a call waiting and the screen is off 107 when(mInCallActivity.isDestroyed()).thenReturn(false); 108 when(mInCallActivity.isFinishing()).thenReturn(false); 109 when(mProximitySensor.isScreenReallyOff()).thenReturn(true); 110 mCallList.setHasCall(Call.State.INCOMING, true); 111 112 mInCallPresenter.onCallListChange(mCallList.getCallList()); 113 verify(mInCallActivity).finish(); 114 } 115 116 /** 117 * Verifies that the PENDING_OUTGOING -> IN_CALL transition brings up InCallActivity so 118 * that it can display an error dialog. 119 */ testOnCallListChange_pendingOutgoingToInCallTransitionShowsUiForErrorDialog()120 public void testOnCallListChange_pendingOutgoingToInCallTransitionShowsUiForErrorDialog() { 121 mCallList.setHasCall(Call.State.CONNECTING, true); 122 123 mInCallPresenter.onCallListChange(mCallList.getCallList()); 124 125 mCallList.setHasCall(Call.State.CONNECTING, false); 126 mCallList.setHasCall(Call.State.ACTIVE, true); 127 128 mInCallPresenter.onCallListChange(mCallList.getCallList()); 129 130 verify(mContext).startActivity(InCallPresenter.getInstance().getInCallIntent(false, false)); 131 verifyIncomingCallNotificationNotSent(); 132 } 133 134 /** 135 * Verifies that if there is a call in the SELECT_PHONE_ACCOUNT state, InCallActivity is displayed 136 * to display the account picker. 137 */ testOnCallListChange_noAccountProvidedForCallShowsUiForAccountPicker()138 public void testOnCallListChange_noAccountProvidedForCallShowsUiForAccountPicker() { 139 mCallList.setHasCall(Call.State.SELECT_PHONE_ACCOUNT, true); 140 mInCallPresenter.onCallListChange(mCallList.getCallList()); 141 142 verify(mContext).startActivity(InCallPresenter.getInstance().getInCallIntent(false, false)); 143 verifyIncomingCallNotificationNotSent(); 144 } 145 146 /** 147 * Verifies that for an expected call state change (e.g. NO_CALLS -> PENDING_OUTGOING), 148 * InCallActivity is not displayed. 149 */ testOnCallListChange_noCallsToPendingOutgoingDoesNotShowUi()150 public void testOnCallListChange_noCallsToPendingOutgoingDoesNotShowUi() { 151 mCallList.setHasCall(Call.State.CONNECTING, true); 152 mInCallPresenter.onCallListChange(mCallList.getCallList()); 153 154 verifyInCallActivityNotStarted(); 155 verifyIncomingCallNotificationNotSent(); 156 } 157 testOnCallListChange_LastCallDisconnectedNoCallsLeftFinishesUi()158 public void testOnCallListChange_LastCallDisconnectedNoCallsLeftFinishesUi() { 159 mCallList.setHasCall(Call.State.DISCONNECTED, true); 160 mInCallPresenter.onCallListChange(mCallList.getCallList()); 161 162 mInCallPresenter.setActivity(mInCallActivity); 163 164 verify(mInCallActivity, never()).finish(); 165 166 // Last remaining disconnected call is removed from CallList, activity should shut down. 167 mCallList.setHasCall(Call.State.DISCONNECTED, false); 168 mInCallPresenter.onCallListChange(mCallList.getCallList()); 169 verify(mInCallActivity).finish(); 170 } 171 172 173 //TODO testCircularReveal_startsCircularRevealForOutgoingCalls()174 public void testCircularReveal_startsCircularRevealForOutgoingCalls() { 175 176 } 177 testCircularReveal_waitTillCircularRevealSentBeforeShowingCallCard()178 public void testCircularReveal_waitTillCircularRevealSentBeforeShowingCallCard() { 179 } 180 testHangupOngoingCall_disconnectsCallCorrectly()181 public void testHangupOngoingCall_disconnectsCallCorrectly() { 182 } 183 testAnswerIncomingCall()184 public void testAnswerIncomingCall() { 185 } 186 testDeclineIncomingCall()187 public void testDeclineIncomingCall() { 188 } 189 verifyInCallActivityNotStarted()190 private void verifyInCallActivityNotStarted() { 191 verify(mContext, never()).startActivity(Mockito.any(Intent.class)); 192 } 193 verifyIncomingCallNotificationNotSent()194 private void verifyIncomingCallNotificationNotSent() { 195 verify(mStatusBarNotifier, never()).updateNotification(Mockito.any(InCallState.class), 196 Mockito.any(CallList.class)); 197 } 198 } 199