1/* 2 * Copyright (C) 2016 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 */ 16package com.android.internal.telephony; 17 18import org.junit.After; 19import org.junit.Before; 20import org.junit.Test; 21import org.mockito.Mock; 22 23import java.util.List; 24import java.util.ArrayList; 25 26import static org.mockito.Matchers.anyString; 27import static org.mockito.Matchers.eq; 28import static org.mockito.Mockito.doReturn; 29import static org.junit.Assert.assertEquals; 30import static org.mockito.Mockito.verify; 31import static org.mockito.Mockito.anyInt; 32import static org.mockito.Mockito.times; 33import org.mockito.ArgumentCaptor; 34import android.telephony.CellInfo; 35import android.telephony.DisconnectCause; 36import android.telephony.PreciseCallState; 37import android.telephony.PreciseDisconnectCause; 38import android.telephony.SignalStrength; 39import android.telephony.TelephonyManager; 40import android.telephony.VoLteServiceState; 41import android.telephony.gsm.GsmCellLocation; 42import android.os.Bundle; 43import android.test.suitebuilder.annotation.SmallTest; 44 45public class DefaultPhoneNotifierTest extends TelephonyTest { 46 47 private DefaultPhoneNotifier mDefaultPhoneNotifierUT; 48 @Mock 49 ITelephonyRegistry.Stub mTelephonyRegisteryMock; 50 @Mock 51 SignalStrength mSignalStrength; 52 @Mock 53 CellInfo mCellInfo; 54 @Mock 55 GsmCdmaCall mForeGroundCall; 56 @Mock 57 GsmCdmaCall mBackGroundCall; 58 @Mock 59 GsmCdmaCall mRingingCall; 60 61 @Before 62 public void setUp() throws Exception { 63 super.setUp(getClass().getSimpleName()); 64 mServiceManagerMockedServices.put("telephony.registry", mTelephonyRegisteryMock); 65 doReturn(mTelephonyRegisteryMock).when(mTelephonyRegisteryMock) 66 .queryLocalInterface(anyString()); 67 68 mDefaultPhoneNotifierUT = new DefaultPhoneNotifier(); 69 } 70 71 @After 72 public void tearDown() throws Exception { 73 super.tearDown(); 74 } 75 76 @Test @SmallTest 77 public void testNotifyCallForwarding() throws Exception { 78 mDefaultPhoneNotifierUT.notifyCallForwardingChanged(mPhone); 79 verify(mTelephonyRegisteryMock).notifyCallForwardingChangedForSubscriber(eq(0), eq(false)); 80 81 doReturn(true).when(mPhone).getCallForwardingIndicator(); 82 doReturn(1).when(mPhone).getSubId(); 83 mDefaultPhoneNotifierUT.notifyCallForwardingChanged(mPhone); 84 verify(mTelephonyRegisteryMock).notifyCallForwardingChangedForSubscriber(eq(1), eq(true)); 85 } 86 87 @Test @SmallTest 88 public void testNotifyDataActivity() throws Exception { 89 //mock data activity state 90 doReturn(Phone.DataActivityState.NONE).when(mPhone).getDataActivityState(); 91 mDefaultPhoneNotifierUT.notifyDataActivity(mPhone); 92 verify(mTelephonyRegisteryMock).notifyDataActivityForSubscriber(eq(0), 93 eq(TelephonyManager.DATA_ACTIVITY_NONE)); 94 95 doReturn(1).when(mPhone).getSubId(); 96 doReturn(Phone.DataActivityState.DATAIN).when(mPhone).getDataActivityState(); 97 mDefaultPhoneNotifierUT.notifyDataActivity(mPhone); 98 verify(mTelephonyRegisteryMock).notifyDataActivityForSubscriber(eq(1), 99 eq(TelephonyManager.DATA_ACTIVITY_IN)); 100 } 101 102 @Test @SmallTest 103 public void testNotifySignalStrength() throws Exception { 104 //mock signal strength value 105 doReturn(99).when(mSignalStrength).getGsmSignalStrength(); 106 doReturn(mSignalStrength).when(mPhone).getSignalStrength(); 107 ArgumentCaptor<SignalStrength> signalStrengthArgumentCaptor = 108 ArgumentCaptor.forClass(SignalStrength.class); 109 110 mDefaultPhoneNotifierUT.notifySignalStrength(mPhone); 111 verify(mTelephonyRegisteryMock).notifySignalStrengthForPhoneId(eq(0), eq(0), 112 signalStrengthArgumentCaptor.capture()); 113 assertEquals(99, signalStrengthArgumentCaptor.getValue().getGsmSignalStrength()); 114 115 doReturn(1).when(mPhone).getSubId(); 116 doReturn(2).when(mPhone).getPhoneId(); 117 mDefaultPhoneNotifierUT.notifySignalStrength(mPhone); 118 verify(mTelephonyRegisteryMock).notifySignalStrengthForPhoneId(eq(2), eq(1), 119 signalStrengthArgumentCaptor.capture()); 120 assertEquals(99, signalStrengthArgumentCaptor.getValue().getGsmSignalStrength()); 121 } 122 123 @Test @SmallTest 124 public void testNotifyCellInfo() throws Exception { 125 //mock cellinfo 126 List<CellInfo> mCellInfoList = new ArrayList<>(); 127 mCellInfoList.add(mCellInfo); 128 ArgumentCaptor<List> cellInfoArgumentCaptor = ArgumentCaptor.forClass(List.class); 129 130 mDefaultPhoneNotifierUT.notifyCellInfo(mPhone, mCellInfoList); 131 132 verify(mTelephonyRegisteryMock).notifyCellInfoForSubscriber(eq(0), 133 cellInfoArgumentCaptor.capture()); 134 assertEquals(mCellInfo, cellInfoArgumentCaptor.getValue().get(0)); 135 } 136 137 @Test @SmallTest 138 public void testNotifyMessageWaiting() throws Exception { 139 doReturn(1).when(mPhone).getPhoneId(); 140 mDefaultPhoneNotifierUT.notifyMessageWaitingChanged(mPhone); 141 verify(mTelephonyRegisteryMock).notifyMessageWaitingChangedForPhoneId(1, 0, false); 142 143 doReturn(2).when(mPhone).getPhoneId(); 144 mDefaultPhoneNotifierUT.notifyMessageWaitingChanged(mPhone); 145 verify(mTelephonyRegisteryMock).notifyMessageWaitingChangedForPhoneId(2, 0, false); 146 147 doReturn(1).when(mPhone).getSubId(); 148 mDefaultPhoneNotifierUT.notifyMessageWaitingChanged(mPhone); 149 verify(mTelephonyRegisteryMock).notifyMessageWaitingChangedForPhoneId(2, 1, false); 150 151 doReturn(true).when(mPhone).getMessageWaitingIndicator(); 152 mDefaultPhoneNotifierUT.notifyMessageWaitingChanged(mPhone); 153 verify(mTelephonyRegisteryMock).notifyMessageWaitingChangedForPhoneId(2, 1, true); 154 } 155 156 @Test @SmallTest 157 public void testNotifyDisconnectCause() throws Exception { 158 mDefaultPhoneNotifierUT.notifyDisconnectCause(DisconnectCause.NOT_VALID, 159 PreciseDisconnectCause.FDN_BLOCKED); 160 verify(mTelephonyRegisteryMock).notifyDisconnectCause(DisconnectCause.NOT_VALID, 161 PreciseDisconnectCause.FDN_BLOCKED); 162 163 mDefaultPhoneNotifierUT.notifyDisconnectCause(DisconnectCause.LOCAL, 164 PreciseDisconnectCause.CHANNEL_NOT_AVAIL); 165 verify(mTelephonyRegisteryMock).notifyDisconnectCause(DisconnectCause.LOCAL, 166 PreciseDisconnectCause.CHANNEL_NOT_AVAIL); 167 } 168 169 @Test @SmallTest 170 public void testNotifyDataConnectionFailed() throws Exception { 171 mDefaultPhoneNotifierUT.notifyDataConnectionFailed(mPhone, "BUSY", "APN_0"); 172 verify(mTelephonyRegisteryMock).notifyDataConnectionFailedForSubscriber(0, "BUSY", "APN_0"); 173 174 mDefaultPhoneNotifierUT.notifyDataConnectionFailed(mPhone, "LOCAL", "APN_0"); 175 verify(mTelephonyRegisteryMock).notifyDataConnectionFailedForSubscriber(0, "LOCAL", 176 "APN_0"); 177 178 mDefaultPhoneNotifierUT.notifyDataConnectionFailed(mPhone, "LOCAL", "APN_1"); 179 verify(mTelephonyRegisteryMock).notifyDataConnectionFailedForSubscriber(0, "LOCAL", 180 "APN_1"); 181 182 doReturn(1).when(mPhone).getSubId(); 183 mDefaultPhoneNotifierUT.notifyDataConnectionFailed(mPhone, "LOCAL", "APN_1"); 184 verify(mTelephonyRegisteryMock).notifyDataConnectionFailedForSubscriber(1, "LOCAL", 185 "APN_1"); 186 } 187 188 @Test @SmallTest 189 public void testNotifyPreciseCallState() throws Exception { 190 191 //mock forground/background/ringing call and call state 192 doReturn(Call.State.IDLE).when(mForeGroundCall).getState(); 193 doReturn(Call.State.IDLE).when(mBackGroundCall).getState(); 194 doReturn(Call.State.IDLE).when(mRingingCall).getState(); 195 196 mDefaultPhoneNotifierUT.notifyPreciseCallState(mPhone); 197 verify(mTelephonyRegisteryMock, times(0)).notifyPreciseCallState(anyInt(), anyInt(), 198 anyInt()); 199 200 doReturn(mForeGroundCall).when(mPhone).getForegroundCall(); 201 mDefaultPhoneNotifierUT.notifyPreciseCallState(mPhone); 202 verify(mTelephonyRegisteryMock, times(0)).notifyPreciseCallState(anyInt(), anyInt(), 203 anyInt()); 204 205 doReturn(mBackGroundCall).when(mPhone).getBackgroundCall(); 206 mDefaultPhoneNotifierUT.notifyPreciseCallState(mPhone); 207 verify(mTelephonyRegisteryMock, times(0)).notifyPreciseCallState(anyInt(), anyInt(), 208 anyInt()); 209 210 doReturn(mRingingCall).when(mPhone).getRingingCall(); 211 mDefaultPhoneNotifierUT.notifyPreciseCallState(mPhone); 212 verify(mTelephonyRegisteryMock, times(1)).notifyPreciseCallState( 213 PreciseCallState.PRECISE_CALL_STATE_IDLE, 214 PreciseCallState.PRECISE_CALL_STATE_IDLE, 215 PreciseCallState.PRECISE_CALL_STATE_IDLE); 216 217 doReturn(Call.State.ACTIVE).when(mForeGroundCall).getState(); 218 mDefaultPhoneNotifierUT.notifyPreciseCallState(mPhone); 219 verify(mTelephonyRegisteryMock, times(1)).notifyPreciseCallState( 220 PreciseCallState.PRECISE_CALL_STATE_IDLE, 221 PreciseCallState.PRECISE_CALL_STATE_ACTIVE, 222 PreciseCallState.PRECISE_CALL_STATE_IDLE); 223 224 doReturn(Call.State.HOLDING).when(mBackGroundCall).getState(); 225 mDefaultPhoneNotifierUT.notifyPreciseCallState(mPhone); 226 verify(mTelephonyRegisteryMock, times(1)).notifyPreciseCallState( 227 PreciseCallState.PRECISE_CALL_STATE_IDLE, 228 PreciseCallState.PRECISE_CALL_STATE_ACTIVE, 229 PreciseCallState.PRECISE_CALL_STATE_HOLDING); 230 231 doReturn(Call.State.ALERTING).when(mRingingCall).getState(); 232 mDefaultPhoneNotifierUT.notifyPreciseCallState(mPhone); 233 verify(mTelephonyRegisteryMock, times(1)).notifyPreciseCallState( 234 PreciseCallState.PRECISE_CALL_STATE_ALERTING, 235 PreciseCallState.PRECISE_CALL_STATE_ACTIVE, 236 PreciseCallState.PRECISE_CALL_STATE_HOLDING); 237 } 238 239 @Test @SmallTest 240 public void testNotifyCellLocation() throws Exception { 241 // mock gsm cell location 242 GsmCellLocation mGsmCellLocation = new GsmCellLocation(); 243 mGsmCellLocation.setLacAndCid(2, 3); 244 doReturn(mGsmCellLocation).when(mPhone).getCellLocation(); 245 ArgumentCaptor<Bundle> cellLocationCapture = 246 ArgumentCaptor.forClass(Bundle.class); 247 248 mDefaultPhoneNotifierUT.notifyCellLocation(mPhone); 249 verify(mTelephonyRegisteryMock).notifyCellLocationForSubscriber(eq(0), 250 cellLocationCapture.capture()); 251 assertEquals(2, cellLocationCapture.getValue().getInt("lac")); 252 assertEquals(3, cellLocationCapture.getValue().getInt("cid")); 253 assertEquals(-1, cellLocationCapture.getValue().getInt("psc")); 254 255 doReturn(1).when(mPhone).getSubId(); 256 mGsmCellLocation.setPsc(5); 257 mDefaultPhoneNotifierUT.notifyCellLocation(mPhone); 258 verify(mTelephonyRegisteryMock).notifyCellLocationForSubscriber(eq(1), 259 cellLocationCapture.capture()); 260 assertEquals(2, cellLocationCapture.getValue().getInt("lac")); 261 assertEquals(3, cellLocationCapture.getValue().getInt("cid")); 262 assertEquals(5, cellLocationCapture.getValue().getInt("psc")); 263 } 264 265 @Test @SmallTest 266 public void testNotifyOtaspChanged() throws Exception { 267 mDefaultPhoneNotifierUT.notifyOtaspChanged(mPhone, ServiceStateTracker.OTASP_NEEDED); 268 verify(mTelephonyRegisteryMock).notifyOtaspChanged(ServiceStateTracker.OTASP_NEEDED); 269 270 mDefaultPhoneNotifierUT.notifyOtaspChanged(mPhone, ServiceStateTracker.OTASP_UNKNOWN); 271 verify(mTelephonyRegisteryMock).notifyOtaspChanged(ServiceStateTracker.OTASP_UNKNOWN); 272 } 273 274 @Test @SmallTest 275 public void testNotifyVoLteServiceStateChanged() throws Exception { 276 VoLteServiceState state = new VoLteServiceState(VoLteServiceState.NOT_SUPPORTED); 277 mDefaultPhoneNotifierUT.notifyVoLteServiceStateChanged(mPhone, state); 278 verify(mTelephonyRegisteryMock).notifyVoLteServiceStateChanged(state); 279 280 state = new VoLteServiceState(VoLteServiceState.HANDOVER_COMPLETED); 281 mDefaultPhoneNotifierUT.notifyVoLteServiceStateChanged(mPhone, state); 282 verify(mTelephonyRegisteryMock).notifyVoLteServiceStateChanged(state); 283 } 284} 285