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