1 /*
2  * Copyright (C) 2019 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.keyguard;
18 
19 
20 import static android.telephony.SubscriptionManager.DATA_ROAMING_DISABLE;
21 import static android.telephony.SubscriptionManager.DATA_ROAMING_ENABLE;
22 import static android.telephony.SubscriptionManager.NAME_SOURCE_CARRIER_ID;
23 
24 import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
25 
26 import static com.google.common.truth.Truth.assertThat;
27 
28 import static junit.framework.Assert.assertTrue;
29 import static junit.framework.TestCase.assertFalse;
30 
31 import static org.junit.Assert.assertEquals;
32 import static org.junit.Assert.assertNotEquals;
33 import static org.mockito.ArgumentMatchers.any;
34 import static org.mockito.ArgumentMatchers.anyInt;
35 import static org.mockito.ArgumentMatchers.eq;
36 import static org.mockito.Mockito.doAnswer;
37 import static org.mockito.Mockito.mock;
38 import static org.mockito.Mockito.never;
39 import static org.mockito.Mockito.reset;
40 import static org.mockito.Mockito.spy;
41 import static org.mockito.Mockito.verify;
42 import static org.mockito.Mockito.when;
43 
44 import android.content.Context;
45 import android.content.Intent;
46 import android.content.IntentFilter;
47 import android.content.pm.PackageManager;
48 import android.provider.Settings;
49 import android.telephony.ServiceState;
50 import android.telephony.SubscriptionInfo;
51 import android.telephony.SubscriptionManager;
52 import android.telephony.TelephonyManager;
53 import android.test.suitebuilder.annotation.SmallTest;
54 import android.testing.AndroidTestingRunner;
55 import android.text.TextUtils;
56 
57 import com.android.keyguard.logging.CarrierTextManagerLogger;
58 import com.android.systemui.R;
59 import com.android.systemui.SysuiTestCase;
60 import com.android.systemui.dump.LogBufferHelperKt;
61 import com.android.systemui.keyguard.WakefulnessLifecycle;
62 import com.android.systemui.statusbar.pipeline.wifi.data.repository.FakeWifiRepository;
63 import com.android.systemui.statusbar.pipeline.wifi.shared.model.WifiNetworkModel;
64 import com.android.systemui.telephony.TelephonyListenerManager;
65 import com.android.systemui.util.concurrency.FakeExecutor;
66 import com.android.systemui.util.time.FakeSystemClock;
67 
68 import org.junit.Before;
69 import org.junit.Test;
70 import org.junit.runner.RunWith;
71 import org.mockito.ArgumentCaptor;
72 import org.mockito.Mock;
73 import org.mockito.MockitoAnnotations;
74 import org.mockito.invocation.InvocationOnMock;
75 
76 import java.util.ArrayList;
77 import java.util.HashMap;
78 import java.util.List;
79 
80 @SmallTest
81 @RunWith(AndroidTestingRunner.class)
82 public class CarrierTextManagerTest extends SysuiTestCase {
83 
84     private static final CharSequence SEPARATOR = " \u2014 ";
85     private static final CharSequence INVALID_CARD_TEXT = "Invalid card";
86     private static final CharSequence AIRPLANE_MODE_TEXT = "Airplane mode";
87     private static final String TEST_CARRIER = "TEST_CARRIER";
88     private static final String TEST_CARRIER_2 = "TEST_CARRIER_2";
89     private static final int TEST_CARRIER_ID = 1;
90     private static final SubscriptionInfo TEST_SUBSCRIPTION = new SubscriptionInfo(0, "", 0,
91             TEST_CARRIER, TEST_CARRIER, NAME_SOURCE_CARRIER_ID, 0xFFFFFF, "",
92             DATA_ROAMING_DISABLE, null, null, null, null, false, null, "", false, null,
93             TEST_CARRIER_ID, 0);
94     private static final SubscriptionInfo TEST_SUBSCRIPTION_NULL = new SubscriptionInfo(0, "", 0,
95             TEST_CARRIER, null, NAME_SOURCE_CARRIER_ID, 0xFFFFFF, "", DATA_ROAMING_DISABLE,
96             null, null, null, null, false, null, "");
97     private static final SubscriptionInfo TEST_SUBSCRIPTION_ROAMING = new SubscriptionInfo(0, "", 0,
98             TEST_CARRIER, TEST_CARRIER, NAME_SOURCE_CARRIER_ID, 0xFFFFFF, "",
99             DATA_ROAMING_ENABLE, null, null, null, null, false, null, "");
100     private FakeWifiRepository mWifiRepository = new FakeWifiRepository();
101     @Mock
102     private WakefulnessLifecycle mWakefulnessLifecycle;
103     @Mock
104     private CarrierTextManager.CarrierTextCallback mCarrierTextCallback;
105     @Mock
106     private KeyguardUpdateMonitor mKeyguardUpdateMonitor;
107     @Mock
108     private PackageManager mPackageManager;
109     @Mock
110     private TelephonyManager mTelephonyManager;
111     @Mock
112     private TelephonyListenerManager mTelephonyListenerManager;
113     private FakeSystemClock mFakeSystemClock = new FakeSystemClock();
114     private FakeExecutor mMainExecutor = new FakeExecutor(mFakeSystemClock);
115     private FakeExecutor mBgExecutor = new FakeExecutor(mFakeSystemClock);
116     @Mock
117     private SubscriptionManager mSubscriptionManager;
118     private CarrierTextManager.CarrierTextCallbackInfo mCarrierTextCallbackInfo;
119 
120     private CarrierTextManager mCarrierTextManager;
121 
122     private CarrierTextManagerLogger mLogger =
123             new CarrierTextManagerLogger(
124                     LogBufferHelperKt.logcatLogBuffer("CarrierTextManagerLog"));
125 
checkMainThread(InvocationOnMock inv)126     private Void checkMainThread(InvocationOnMock inv) {
127         assertThat(mMainExecutor.isExecuting()).isTrue();
128         assertThat(mBgExecutor.isExecuting()).isFalse();
129         return null;
130     }
131 
132     @Before
setUp()133     public void setUp() {
134         MockitoAnnotations.initMocks(this);
135 
136         mContext.addMockSystemService(PackageManager.class, mPackageManager);
137         when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)).thenReturn(true);
138         mContext.addMockSystemService(TelephonyManager.class, mTelephonyManager);
139         mContext.addMockSystemService(SubscriptionManager.class, mSubscriptionManager);
140         mContext.getOrCreateTestableResources().addOverride(
141                 R.string.keyguard_sim_error_message_short, INVALID_CARD_TEXT);
142         mContext.getOrCreateTestableResources().addOverride(
143                 R.string.airplane_mode, AIRPLANE_MODE_TEXT);
144         mDependency.injectMockDependency(WakefulnessLifecycle.class);
145         mDependency.injectTestDependency(KeyguardUpdateMonitor.class, mKeyguardUpdateMonitor);
146 
147         doAnswer(this::checkMainThread).when(mKeyguardUpdateMonitor)
148                 .registerCallback(any(KeyguardUpdateMonitorCallback.class));
149         doAnswer(this::checkMainThread).when(mKeyguardUpdateMonitor)
150                 .removeCallback(any(KeyguardUpdateMonitorCallback.class));
151 
152         mCarrierTextCallbackInfo = new CarrierTextManager.CarrierTextCallbackInfo("",
153                 new CharSequence[]{}, false, new int[]{});
154         when(mTelephonyManager.getSupportedModemCount()).thenReturn(3);
155         when(mTelephonyManager.getActiveModemCount()).thenReturn(3);
156 
157         mCarrierTextManager = new CarrierTextManager.Builder(
158                 mContext, mContext.getResources(), mWifiRepository,
159                 mTelephonyManager, mTelephonyListenerManager, mWakefulnessLifecycle, mMainExecutor,
160                 mBgExecutor, mKeyguardUpdateMonitor, mLogger)
161                 .setShowAirplaneMode(true)
162                 .setShowMissingSim(true)
163                 .build();
164 
165         // This should not start listening on any of the real dependencies but will test that
166         // callbacks in mKeyguardUpdateMonitor are done in the mTestableLooper thread
167         mCarrierTextManager.setListening(mCarrierTextCallback);
168         FakeExecutor.exhaustExecutors(mMainExecutor, mBgExecutor);
169     }
170 
171     @Test
testKeyguardUpdateMonitorCalledInMainThread()172     public void testKeyguardUpdateMonitorCalledInMainThread() throws Exception {
173         mCarrierTextManager.setListening(null);
174         mCarrierTextManager.setListening(mCarrierTextCallback);
175         FakeExecutor.exhaustExecutors(mMainExecutor, mBgExecutor);
176     }
177 
178     @Test
testAirplaneMode()179     public void testAirplaneMode() {
180         Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
181         reset(mCarrierTextCallback);
182         List<SubscriptionInfo> list = new ArrayList<>();
183         list.add(TEST_SUBSCRIPTION);
184         when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo()).thenReturn(list);
185         when(mKeyguardUpdateMonitor.getSimState(0)).thenReturn(TelephonyManager.SIM_STATE_READY);
186         mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
187 
188         mCarrierTextManager.updateCarrierText();
189 
190         ArgumentCaptor<CarrierTextManager.CarrierTextCallbackInfo> captor =
191                 ArgumentCaptor.forClass(
192                         CarrierTextManager.CarrierTextCallbackInfo.class);
193 
194         FakeExecutor.exhaustExecutors(mMainExecutor, mBgExecutor);
195         verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
196         assertEquals(AIRPLANE_MODE_TEXT, captor.getValue().carrierText);
197     }
198 
199     /** regression test for b/281706473, caused by sending NULL plmn / spn to the logger */
200     @Test
testAirplaneMode_noSim_nullPlmn_nullSpn_doesNotCrash()201     public void testAirplaneMode_noSim_nullPlmn_nullSpn_doesNotCrash() {
202         // GIVEN - sticy broadcast that returns a null PLMN and null SPN
203         Intent stickyIntent = new Intent(TelephonyManager.ACTION_SERVICE_PROVIDERS_UPDATED);
204         stickyIntent.putExtra(TelephonyManager.EXTRA_SHOW_PLMN, true);
205         stickyIntent.removeExtra(TelephonyManager.EXTRA_PLMN);
206         stickyIntent.putExtra(TelephonyManager.EXTRA_SHOW_SPN, true);
207         stickyIntent.removeExtra(TelephonyManager.EXTRA_SPN);
208 
209         mCarrierTextManager = new CarrierTextManager.Builder(
210                 getContextSpyForStickyBroadcast(stickyIntent),
211                 mContext.getResources(),
212                 mWifiRepository,
213                 mTelephonyManager,
214                 mTelephonyListenerManager,
215                 mWakefulnessLifecycle,
216                 mMainExecutor,
217                 mBgExecutor,
218                 mKeyguardUpdateMonitor,
219                 mLogger
220         )
221                 .setShowAirplaneMode(true)
222                 .setShowMissingSim(true)
223                 .build();
224 
225         // GIVEN - airplane mode is off (causing CTM to fetch the sticky broadcast)
226         Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
227         reset(mCarrierTextCallback);
228         List<SubscriptionInfo> list = new ArrayList<>();
229         when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo()).thenReturn(list);
230         when(mKeyguardUpdateMonitor.getSimState(0))
231                 .thenReturn(TelephonyManager.SIM_STATE_NOT_READY);
232         mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
233 
234         // WHEN CTM fetches the broadcast and attempts to log the result, no crash results
235         mCarrierTextManager.updateCarrierText();
236 
237         // No assert, this test should not crash
238     }
239 
240     @Test
testCardIOError()241     public void testCardIOError() {
242         reset(mCarrierTextCallback);
243         List<SubscriptionInfo> list = new ArrayList<>();
244         list.add(TEST_SUBSCRIPTION);
245         when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo()).thenReturn(list);
246         when(mKeyguardUpdateMonitor.getSimState(0)).thenReturn(TelephonyManager.SIM_STATE_READY);
247         when(mKeyguardUpdateMonitor.getSimState(1)).thenReturn(
248                 TelephonyManager.SIM_STATE_CARD_IO_ERROR);
249         mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
250 
251         mCarrierTextManager.mCallback.onSimStateChanged(3, 1,
252                 TelephonyManager.SIM_STATE_CARD_IO_ERROR);
253 
254         ArgumentCaptor<CarrierTextManager.CarrierTextCallbackInfo> captor =
255                 ArgumentCaptor.forClass(
256                         CarrierTextManager.CarrierTextCallbackInfo.class);
257 
258         FakeExecutor.exhaustExecutors(mMainExecutor, mBgExecutor);
259         verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
260         assertEquals("TEST_CARRIER" + SEPARATOR + INVALID_CARD_TEXT, captor.getValue().carrierText);
261         // There's only one subscription in the list
262         assertEquals(1, captor.getValue().listOfCarriers.length);
263         assertEquals(TEST_CARRIER, captor.getValue().listOfCarriers[0]);
264 
265         // Now it becomes single SIM active mode.
266         reset(mCarrierTextCallback);
267         when(mTelephonyManager.getActiveModemCount()).thenReturn(1);
268         // Update carrier text. It should ignore error state of subId 3 in inactive slotId.
269         mCarrierTextManager.updateCarrierText();
270         FakeExecutor.exhaustExecutors(mMainExecutor, mBgExecutor);
271         verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
272         assertEquals("TEST_CARRIER", captor.getValue().carrierText);
273     }
274 
275     @Test
testWrongSlots()276     public void testWrongSlots() {
277         reset(mCarrierTextCallback);
278         when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo()).thenReturn(
279                 new ArrayList<>());
280         when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(
281                 TelephonyManager.SIM_STATE_CARD_IO_ERROR);
282         // This should not produce an out of bounds error, even though there are no subscriptions
283         mCarrierTextManager.mCallback.onSimStateChanged(0, -3,
284                 TelephonyManager.SIM_STATE_CARD_IO_ERROR);
285         mCarrierTextManager.mCallback.onSimStateChanged(0, 3, TelephonyManager.SIM_STATE_READY);
286         verify(mCarrierTextCallback, never()).updateCarrierInfo(any());
287     }
288 
289     @Test
testMoreSlotsThanSubs()290     public void testMoreSlotsThanSubs() {
291         reset(mCarrierTextCallback);
292         when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo()).thenReturn(
293                 new ArrayList<>());
294 
295         // STOPSHIP(b/130246708) This line makes sure that SubscriptionManager provides the
296         // same answer as KeyguardUpdateMonitor. Remove when this is addressed
297         when(mSubscriptionManager.getCompleteActiveSubscriptionInfoList()).thenReturn(
298                 new ArrayList<>());
299 
300         when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(
301                 TelephonyManager.SIM_STATE_CARD_IO_ERROR);
302         // This should not produce an out of bounds error, even though there are no subscriptions
303         mCarrierTextManager.mCallback.onSimStateChanged(0, 1,
304                 TelephonyManager.SIM_STATE_CARD_IO_ERROR);
305 
306         FakeExecutor.exhaustExecutors(mMainExecutor, mBgExecutor);
307         verify(mCarrierTextCallback).updateCarrierInfo(
308                 any(CarrierTextManager.CarrierTextCallbackInfo.class));
309     }
310 
311     @Test
testCallback()312     public void testCallback() {
313         reset(mCarrierTextCallback);
314         mCarrierTextManager.postToCallback(mCarrierTextCallbackInfo);
315         FakeExecutor.exhaustExecutors(mMainExecutor, mBgExecutor);
316 
317         ArgumentCaptor<CarrierTextManager.CarrierTextCallbackInfo> captor =
318                 ArgumentCaptor.forClass(
319                         CarrierTextManager.CarrierTextCallbackInfo.class);
320         verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
321         assertEquals(mCarrierTextCallbackInfo, captor.getValue());
322     }
323 
324     @Test
testNullingCallback()325     public void testNullingCallback() {
326         reset(mCarrierTextCallback);
327 
328         mCarrierTextManager.postToCallback(mCarrierTextCallbackInfo);
329         mCarrierTextManager.setListening(null);
330 
331         // This shouldn't produce NPE
332         FakeExecutor.exhaustExecutors(mMainExecutor, mBgExecutor);
333         verify(mCarrierTextCallback).updateCarrierInfo(any());
334     }
335 
336     @Test
testCreateInfo_OneValidSubscription()337     public void testCreateInfo_OneValidSubscription() {
338         reset(mCarrierTextCallback);
339         List<SubscriptionInfo> list = new ArrayList<>();
340         list.add(TEST_SUBSCRIPTION);
341         when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(
342                 TelephonyManager.SIM_STATE_READY);
343         when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo()).thenReturn(list);
344 
345         mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
346 
347         ArgumentCaptor<CarrierTextManager.CarrierTextCallbackInfo> captor =
348                 ArgumentCaptor.forClass(
349                         CarrierTextManager.CarrierTextCallbackInfo.class);
350 
351         mCarrierTextManager.updateCarrierText();
352         FakeExecutor.exhaustExecutors(mMainExecutor, mBgExecutor);
353         verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
354 
355         CarrierTextManager.CarrierTextCallbackInfo info = captor.getValue();
356         assertEquals(1, info.listOfCarriers.length);
357         assertEquals(TEST_CARRIER, info.listOfCarriers[0]);
358         assertEquals(1, info.subscriptionIds.length);
359     }
360 
361     @Test
testCreateInfo_OneValidSubscriptionWithRoaming()362     public void testCreateInfo_OneValidSubscriptionWithRoaming() {
363         reset(mCarrierTextCallback);
364         List<SubscriptionInfo> list = new ArrayList<>();
365         list.add(TEST_SUBSCRIPTION_ROAMING);
366         when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(
367                 TelephonyManager.SIM_STATE_READY);
368         when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo()).thenReturn(list);
369 
370         mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
371 
372         ArgumentCaptor<CarrierTextManager.CarrierTextCallbackInfo> captor =
373                 ArgumentCaptor.forClass(
374                         CarrierTextManager.CarrierTextCallbackInfo.class);
375 
376         mCarrierTextManager.updateCarrierText();
377         FakeExecutor.exhaustExecutors(mMainExecutor, mBgExecutor);
378         verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
379 
380         CarrierTextManager.CarrierTextCallbackInfo info = captor.getValue();
381         assertEquals(1, info.listOfCarriers.length);
382         assertTrue(info.listOfCarriers[0].toString().contains(TEST_CARRIER));
383         assertEquals(1, info.subscriptionIds.length);
384     }
385 
386     @Test
testCarrierText_noTextOnReadySimWhenNull()387     public void testCarrierText_noTextOnReadySimWhenNull() {
388         reset(mCarrierTextCallback);
389         List<SubscriptionInfo> list = new ArrayList<>();
390         list.add(TEST_SUBSCRIPTION_NULL);
391         when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(
392                 TelephonyManager.SIM_STATE_READY);
393         when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo()).thenReturn(list);
394 
395         mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
396 
397         ArgumentCaptor<CarrierTextManager.CarrierTextCallbackInfo> captor =
398                 ArgumentCaptor.forClass(
399                         CarrierTextManager.CarrierTextCallbackInfo.class);
400 
401         mCarrierTextManager.updateCarrierText();
402         FakeExecutor.exhaustExecutors(mMainExecutor, mBgExecutor);
403         verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
404 
405         assertTrue("Carrier text should be empty, instead it's " + captor.getValue().carrierText,
406                 TextUtils.isEmpty(captor.getValue().carrierText));
407         assertFalse("No SIM should be available", captor.getValue().anySimReady);
408     }
409 
410     @Test
testCarrierText_noTextOnReadySimWhenNull_airplaneMode_wifiOn()411     public void testCarrierText_noTextOnReadySimWhenNull_airplaneMode_wifiOn() {
412         Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
413         reset(mCarrierTextCallback);
414         List<SubscriptionInfo> list = new ArrayList<>();
415         list.add(TEST_SUBSCRIPTION_NULL);
416         when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(
417                 TelephonyManager.SIM_STATE_READY);
418         when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo()).thenReturn(list);
419 
420         assertFalse(mWifiRepository.isWifiConnectedWithValidSsid());
421         mWifiRepository.setWifiNetwork(
422                 new WifiNetworkModel.Active(
423                         /* networkId= */ 0,
424                         /* isValidated= */ false,
425                         /* level= */ 0,
426                         /* ssid= */ "",
427                         /* hotspotDeviceType= */ WifiNetworkModel.HotspotDeviceType.NONE,
428                         /* isPasspointAccessPoint= */ false,
429                         /* isOnlineSignUpForPasspointAccessPoint= */ false,
430                         /* passpointProviderFriendlyName= */ null));
431         assertTrue(mWifiRepository.isWifiConnectedWithValidSsid());
432 
433         mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
434         ServiceState ss = mock(ServiceState.class);
435         when(ss.getDataRegistrationState()).thenReturn(ServiceState.STATE_IN_SERVICE);
436         mKeyguardUpdateMonitor.mServiceStates.put(TEST_SUBSCRIPTION_NULL.getSubscriptionId(), ss);
437 
438         ArgumentCaptor<CarrierTextManager.CarrierTextCallbackInfo> captor =
439                 ArgumentCaptor.forClass(
440                         CarrierTextManager.CarrierTextCallbackInfo.class);
441 
442         mCarrierTextManager.updateCarrierText();
443         FakeExecutor.exhaustExecutors(mMainExecutor, mBgExecutor);
444         verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
445 
446         assertFalse("No SIM should be available", captor.getValue().anySimReady);
447         // There's no airplane mode if at least one SIM is State.READY and there's wifi
448         assertFalse("Device should not be in airplane mode", captor.getValue().airplaneMode);
449         assertNotEquals(AIRPLANE_MODE_TEXT, captor.getValue().carrierText);
450     }
451 
452     @Test
testCreateInfo_noSubscriptions()453     public void testCreateInfo_noSubscriptions() {
454         reset(mCarrierTextCallback);
455         when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo()).thenReturn(
456                 new ArrayList<>());
457 
458         ArgumentCaptor<CarrierTextManager.CarrierTextCallbackInfo> captor =
459                 ArgumentCaptor.forClass(
460                         CarrierTextManager.CarrierTextCallbackInfo.class);
461 
462         mCarrierTextManager.updateCarrierText();
463         FakeExecutor.exhaustExecutors(mMainExecutor, mBgExecutor);
464         verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
465 
466         CarrierTextManager.CarrierTextCallbackInfo info = captor.getValue();
467         assertEquals(0, info.listOfCarriers.length);
468         assertEquals(0, info.subscriptionIds.length);
469 
470     }
471 
472     @Test
testCarrierText_twoValidSubscriptions()473     public void testCarrierText_twoValidSubscriptions() {
474         reset(mCarrierTextCallback);
475         List<SubscriptionInfo> list = new ArrayList<>();
476         list.add(TEST_SUBSCRIPTION);
477         list.add(TEST_SUBSCRIPTION);
478         when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(
479                 TelephonyManager.SIM_STATE_READY);
480         when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo()).thenReturn(list);
481 
482         mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
483 
484         ArgumentCaptor<CarrierTextManager.CarrierTextCallbackInfo> captor =
485                 ArgumentCaptor.forClass(
486                         CarrierTextManager.CarrierTextCallbackInfo.class);
487 
488         mCarrierTextManager.updateCarrierText();
489         FakeExecutor.exhaustExecutors(mMainExecutor, mBgExecutor);
490         verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
491 
492         assertEquals(TEST_CARRIER + SEPARATOR + TEST_CARRIER,
493                 captor.getValue().carrierText);
494     }
495 
496     @Test
testCarrierText_oneDisabledSub()497     public void testCarrierText_oneDisabledSub() {
498         reset(mCarrierTextCallback);
499         List<SubscriptionInfo> list = new ArrayList<>();
500         list.add(TEST_SUBSCRIPTION);
501         list.add(TEST_SUBSCRIPTION);
502         when(mKeyguardUpdateMonitor.getSimState(anyInt()))
503                 .thenReturn(TelephonyManager.SIM_STATE_READY)
504                 .thenReturn(TelephonyManager.SIM_STATE_NOT_READY);
505         when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo()).thenReturn(list);
506 
507         mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
508 
509         ArgumentCaptor<CarrierTextManager.CarrierTextCallbackInfo> captor =
510                 ArgumentCaptor.forClass(
511                         CarrierTextManager.CarrierTextCallbackInfo.class);
512 
513         mCarrierTextManager.updateCarrierText();
514         FakeExecutor.exhaustExecutors(mMainExecutor, mBgExecutor);
515         verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
516 
517         assertEquals(TEST_CARRIER,
518                 captor.getValue().carrierText);
519     }
520 
521     @Test
testCarrierText_firstDisabledSub()522     public void testCarrierText_firstDisabledSub() {
523         reset(mCarrierTextCallback);
524         List<SubscriptionInfo> list = new ArrayList<>();
525         list.add(TEST_SUBSCRIPTION);
526         list.add(TEST_SUBSCRIPTION);
527         when(mKeyguardUpdateMonitor.getSimState(anyInt()))
528                 .thenReturn(TelephonyManager.SIM_STATE_NOT_READY)
529                 .thenReturn(TelephonyManager.SIM_STATE_READY);
530         when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo()).thenReturn(list);
531 
532         mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
533 
534         ArgumentCaptor<CarrierTextManager.CarrierTextCallbackInfo> captor =
535                 ArgumentCaptor.forClass(
536                         CarrierTextManager.CarrierTextCallbackInfo.class);
537 
538         mCarrierTextManager.updateCarrierText();
539         FakeExecutor.exhaustExecutors(mMainExecutor, mBgExecutor);
540         verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
541 
542         assertEquals(TEST_CARRIER,
543                 captor.getValue().carrierText);
544     }
545 
546     @Test
testCarrierText_threeSubsMiddleDisabled()547     public void testCarrierText_threeSubsMiddleDisabled() {
548         reset(mCarrierTextCallback);
549         List<SubscriptionInfo> list = new ArrayList<>();
550         list.add(TEST_SUBSCRIPTION);
551         list.add(TEST_SUBSCRIPTION);
552         list.add(TEST_SUBSCRIPTION);
553         when(mKeyguardUpdateMonitor.getSimState(anyInt()))
554                 .thenReturn(TelephonyManager.SIM_STATE_READY)
555                 .thenReturn(TelephonyManager.SIM_STATE_NOT_READY)
556                 .thenReturn(TelephonyManager.SIM_STATE_READY);
557         when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo()).thenReturn(list);
558         mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
559 
560         ArgumentCaptor<CarrierTextManager.CarrierTextCallbackInfo> captor =
561                 ArgumentCaptor.forClass(
562                         CarrierTextManager.CarrierTextCallbackInfo.class);
563 
564         mCarrierTextManager.updateCarrierText();
565         FakeExecutor.exhaustExecutors(mMainExecutor, mBgExecutor);
566         verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
567 
568         assertEquals(TEST_CARRIER + SEPARATOR + TEST_CARRIER,
569                 captor.getValue().carrierText);
570     }
571 
572     @Test
testGetStatusForIccState()573     public void testGetStatusForIccState() {
574         when(mKeyguardUpdateMonitor.isDeviceProvisioned()).thenReturn(false);
575         assertEquals(CarrierTextManager.StatusMode.SimMissingLocked,
576                 mCarrierTextManager.getStatusForIccState(TelephonyManager.SIM_STATE_ABSENT));
577         assertEquals(CarrierTextManager.StatusMode.NetworkLocked,
578                 mCarrierTextManager.getStatusForIccState(
579                         TelephonyManager.SIM_STATE_NETWORK_LOCKED));
580         assertEquals(CarrierTextManager.StatusMode.SimNotReady,
581                 mCarrierTextManager.getStatusForIccState(TelephonyManager.SIM_STATE_NOT_READY));
582         assertEquals(CarrierTextManager.StatusMode.SimLocked,
583                 mCarrierTextManager.getStatusForIccState(
584                         TelephonyManager.SIM_STATE_PIN_REQUIRED));
585         assertEquals(CarrierTextManager.StatusMode.SimPukLocked,
586                 mCarrierTextManager.getStatusForIccState(
587                         TelephonyManager.SIM_STATE_PUK_REQUIRED));
588         assertEquals(CarrierTextManager.StatusMode.Normal,
589                 mCarrierTextManager.getStatusForIccState(TelephonyManager.SIM_STATE_READY));
590         assertEquals(CarrierTextManager.StatusMode.SimMissingLocked,
591                 mCarrierTextManager.getStatusForIccState(
592                         TelephonyManager.SIM_STATE_PERM_DISABLED));
593         assertEquals(CarrierTextManager.StatusMode.SimUnknown,
594                 mCarrierTextManager.getStatusForIccState(TelephonyManager.SIM_STATE_UNKNOWN));
595         assertEquals(CarrierTextManager.StatusMode.SimIoError,
596                 mCarrierTextManager.getStatusForIccState(
597                         TelephonyManager.SIM_STATE_CARD_IO_ERROR));
598         assertEquals(CarrierTextManager.StatusMode.SimRestricted,
599                 mCarrierTextManager.getStatusForIccState(
600                         TelephonyManager.SIM_STATE_CARD_RESTRICTED));
601 
602         when(mKeyguardUpdateMonitor.isDeviceProvisioned()).thenReturn(true);
603         assertEquals(CarrierTextManager.StatusMode.SimMissing,
604                 mCarrierTextManager.getStatusForIccState(TelephonyManager.SIM_STATE_ABSENT));
605         assertEquals(CarrierTextManager.StatusMode.NetworkLocked,
606                 mCarrierTextManager.getStatusForIccState(
607                         TelephonyManager.SIM_STATE_NETWORK_LOCKED));
608         assertEquals(CarrierTextManager.StatusMode.SimNotReady,
609                 mCarrierTextManager.getStatusForIccState(
610                         TelephonyManager.SIM_STATE_NOT_READY));
611         assertEquals(CarrierTextManager.StatusMode.SimLocked,
612                 mCarrierTextManager.getStatusForIccState(
613                         TelephonyManager.SIM_STATE_PIN_REQUIRED));
614         assertEquals(CarrierTextManager.StatusMode.SimPukLocked,
615                 mCarrierTextManager.getStatusForIccState(
616                         TelephonyManager.SIM_STATE_PUK_REQUIRED));
617         assertEquals(CarrierTextManager.StatusMode.Normal,
618                 mCarrierTextManager.getStatusForIccState(TelephonyManager.SIM_STATE_READY));
619         assertEquals(CarrierTextManager.StatusMode.SimPermDisabled,
620                 mCarrierTextManager.getStatusForIccState(
621                         TelephonyManager.SIM_STATE_PERM_DISABLED));
622         assertEquals(CarrierTextManager.StatusMode.SimUnknown,
623                 mCarrierTextManager.getStatusForIccState(TelephonyManager.SIM_STATE_UNKNOWN));
624         assertEquals(CarrierTextManager.StatusMode.SimIoError,
625                 mCarrierTextManager.getStatusForIccState(
626                         TelephonyManager.SIM_STATE_CARD_IO_ERROR));
627         assertEquals(CarrierTextManager.StatusMode.SimRestricted,
628                 mCarrierTextManager.getStatusForIccState(
629                         TelephonyManager.SIM_STATE_CARD_RESTRICTED));
630     }
631 
getContextSpyForStickyBroadcast(Intent returnVal)632     private Context getContextSpyForStickyBroadcast(Intent returnVal) {
633         Context contextSpy = spy(mContext);
634         doReturn(returnVal).when(contextSpy).registerReceiver(eq(null), any(IntentFilter.class));
635         return contextSpy;
636     }
637 }
638