blob: eccf09633f169ceeebfaa3955a8847561476701a [file] [log] [blame]
Fabian Kozynskib176f422019-02-05 09:36:59 -05001/*
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
17package com.android.keyguard;
18
19
20import static android.telephony.SubscriptionManager.DATA_ROAMING_DISABLE;
21import static android.telephony.SubscriptionManager.DATA_ROAMING_ENABLE;
Peter Wang37916d62019-11-25 16:50:50 -080022import static android.telephony.SubscriptionManager.NAME_SOURCE_DEFAULT;
Fabian Kozynskib176f422019-02-05 09:36:59 -050023
24import static junit.framework.Assert.assertTrue;
Fabian Kozynski0f49f202019-08-29 09:47:07 -040025import static junit.framework.TestCase.assertFalse;
Fabian Kozynskib176f422019-02-05 09:36:59 -050026
27import static org.junit.Assert.assertEquals;
Fabian Kozynski0f49f202019-08-29 09:47:07 -040028import static org.junit.Assert.assertNotEquals;
Fabian Kozynskiba46d1c2019-10-03 12:01:38 -040029import static org.junit.Assert.fail;
Fabian Kozynskib176f422019-02-05 09:36:59 -050030import static org.mockito.ArgumentMatchers.any;
31import static org.mockito.ArgumentMatchers.anyBoolean;
32import static org.mockito.ArgumentMatchers.anyInt;
Fabian Kozynskiba46d1c2019-10-03 12:01:38 -040033import static org.mockito.Mockito.doAnswer;
Fabian Kozynski0f49f202019-08-29 09:47:07 -040034import static org.mockito.Mockito.mock;
Fabian Kozynskib176f422019-02-05 09:36:59 -050035import static org.mockito.Mockito.never;
36import static org.mockito.Mockito.reset;
37import static org.mockito.Mockito.verify;
38import static org.mockito.Mockito.when;
39
Fabian Kozynskib176f422019-02-05 09:36:59 -050040import android.net.ConnectivityManager;
Fabian Kozynski0f49f202019-08-29 09:47:07 -040041import android.net.wifi.WifiInfo;
Fabian Kozynskib176f422019-02-05 09:36:59 -050042import android.net.wifi.WifiManager;
43import android.os.Handler;
Fabian Kozynskiba46d1c2019-10-03 12:01:38 -040044import android.os.HandlerThread;
45import android.os.Looper;
46import android.os.Process;
Fabian Kozynskic3d06f32019-07-31 14:18:41 -040047import android.provider.Settings;
Fabian Kozynski0f49f202019-08-29 09:47:07 -040048import android.telephony.ServiceState;
Fabian Kozynskib176f422019-02-05 09:36:59 -050049import android.telephony.SubscriptionInfo;
Malcolm Chen06ec3572019-04-09 15:55:29 -070050import android.telephony.SubscriptionManager;
Fabian Kozynskib176f422019-02-05 09:36:59 -050051import android.telephony.TelephonyManager;
52import android.test.suitebuilder.annotation.SmallTest;
53import android.testing.AndroidTestingRunner;
54import android.testing.TestableLooper;
Fabian Kozynski0f49f202019-08-29 09:47:07 -040055import android.text.TextUtils;
Fabian Kozynskib176f422019-02-05 09:36:59 -050056
Fabian Kozynskib176f422019-02-05 09:36:59 -050057import com.android.systemui.Dependency;
Hyunyoung Song8f9d34c2019-08-30 14:47:43 -070058import com.android.systemui.R;
Fabian Kozynskib176f422019-02-05 09:36:59 -050059import com.android.systemui.SysuiTestCase;
60import com.android.systemui.keyguard.WakefulnessLifecycle;
61
62import org.junit.Before;
63import org.junit.Test;
64import org.junit.runner.RunWith;
65import org.mockito.ArgumentCaptor;
66import org.mockito.Mock;
67import org.mockito.MockitoAnnotations;
Fabian Kozynskiba46d1c2019-10-03 12:01:38 -040068import org.mockito.invocation.InvocationOnMock;
Fabian Kozynskib176f422019-02-05 09:36:59 -050069
70import java.util.ArrayList;
Bonian Chena7e9e8c2019-06-02 23:59:31 +000071import java.util.HashMap;
Fabian Kozynskib176f422019-02-05 09:36:59 -050072import java.util.List;
73
74@SmallTest
75@RunWith(AndroidTestingRunner.class)
76@TestableLooper.RunWithLooper
77public class CarrierTextControllerTest extends SysuiTestCase {
78
79 private static final CharSequence SEPARATOR = " \u2014 ";
Fabian Kozynskic3d06f32019-07-31 14:18:41 -040080 private static final CharSequence INVALID_CARD_TEXT = "Invalid card";
81 private static final CharSequence AIRPLANE_MODE_TEXT = "Airplane mode";
Fabian Kozynskib176f422019-02-05 09:36:59 -050082 private static final String TEST_CARRIER = "TEST_CARRIER";
Sooraj Sasindran0d45da72019-04-25 15:12:21 -070083 private static final String TEST_CARRIER_2 = "TEST_CARRIER_2";
Sooraj Sasindran0d45da72019-04-25 15:12:21 -070084 private static final int TEST_CARRIER_ID = 1;
Fabian Kozynskib176f422019-02-05 09:36:59 -050085 private static final SubscriptionInfo TEST_SUBSCRIPTION = new SubscriptionInfo(0, "", 0,
Peter Wang37916d62019-11-25 16:50:50 -080086 TEST_CARRIER, TEST_CARRIER, NAME_SOURCE_DEFAULT, 0xFFFFFF, "",
Malcolm Chen5c63b512019-08-13 13:24:07 -070087 DATA_ROAMING_DISABLE, null, null, null, null, false, null, "", false, null,
Sooraj Sasindran0d45da72019-04-25 15:12:21 -070088 TEST_CARRIER_ID, 0);
Fabian Kozynski0f49f202019-08-29 09:47:07 -040089 private static final SubscriptionInfo TEST_SUBSCRIPTION_NULL = new SubscriptionInfo(0, "", 0,
Peter Wang37916d62019-11-25 16:50:50 -080090 TEST_CARRIER, null, NAME_SOURCE_DEFAULT, 0xFFFFFF, "", DATA_ROAMING_DISABLE,
Fabian Kozynski0f49f202019-08-29 09:47:07 -040091 null, null, null, null, false, null, "");
Fabian Kozynskib176f422019-02-05 09:36:59 -050092 private static final SubscriptionInfo TEST_SUBSCRIPTION_ROAMING = new SubscriptionInfo(0, "", 0,
Peter Wang37916d62019-11-25 16:50:50 -080093 TEST_CARRIER, TEST_CARRIER, NAME_SOURCE_DEFAULT, 0xFFFFFF, "",
Fabian Kozynskib176f422019-02-05 09:36:59 -050094 DATA_ROAMING_ENABLE, null, null, null, null, false, null, "");
95 @Mock
96 private WifiManager mWifiManager;
97 @Mock
98 private CarrierTextController.CarrierTextCallback mCarrierTextCallback;
99 @Mock
100 private KeyguardUpdateMonitor mKeyguardUpdateMonitor;
101 @Mock
102 private ConnectivityManager mConnectivityManager;
103 @Mock
104 private TelephonyManager mTelephonyManager;
Malcolm Chen06ec3572019-04-09 15:55:29 -0700105 @Mock
106 private SubscriptionManager mSubscriptionManager;
Fabian Kozynskib176f422019-02-05 09:36:59 -0500107 private CarrierTextController.CarrierTextCallbackInfo mCarrierTextCallbackInfo;
108
109 private CarrierTextController mCarrierTextController;
110 private TestableLooper mTestableLooper;
111
Fabian Kozynskiba46d1c2019-10-03 12:01:38 -0400112 private Void checkMainThread(InvocationOnMock inv) {
113 Looper mainLooper = Dependency.get(Dependency.MAIN_HANDLER).getLooper();
114 if (!mainLooper.isCurrentThread()) {
115 fail("This call should be done from the main thread");
116 }
117 return null;
118 }
119
Fabian Kozynskib176f422019-02-05 09:36:59 -0500120 @Before
121 public void setUp() {
122 MockitoAnnotations.initMocks(this);
123 mTestableLooper = TestableLooper.get(this);
124
125 mContext.addMockSystemService(WifiManager.class, mWifiManager);
126 mContext.addMockSystemService(ConnectivityManager.class, mConnectivityManager);
Fabian Kozynskiba46d1c2019-10-03 12:01:38 -0400127 when(mConnectivityManager.isNetworkSupported(anyInt())).thenReturn(true);
Fabian Kozynskib176f422019-02-05 09:36:59 -0500128 mContext.addMockSystemService(TelephonyManager.class, mTelephonyManager);
Malcolm Chen06ec3572019-04-09 15:55:29 -0700129 mContext.addMockSystemService(SubscriptionManager.class, mSubscriptionManager);
Fabian Kozynskic3d06f32019-07-31 14:18:41 -0400130 mContext.getOrCreateTestableResources().addOverride(
131 R.string.keyguard_sim_error_message_short, INVALID_CARD_TEXT);
132 mContext.getOrCreateTestableResources().addOverride(
133 R.string.airplane_mode, AIRPLANE_MODE_TEXT);
Fabian Kozynskib176f422019-02-05 09:36:59 -0500134 mDependency.injectMockDependency(WakefulnessLifecycle.class);
135 mDependency.injectTestDependency(Dependency.MAIN_HANDLER,
136 new Handler(mTestableLooper.getLooper()));
Fabian Kozynskiba46d1c2019-10-03 12:01:38 -0400137 mDependency.injectTestDependency(KeyguardUpdateMonitor.class, mKeyguardUpdateMonitor);
138
139 doAnswer(this::checkMainThread).when(mKeyguardUpdateMonitor)
140 .registerCallback(any(KeyguardUpdateMonitorCallback.class));
141 doAnswer(this::checkMainThread).when(mKeyguardUpdateMonitor)
142 .removeCallback(any(KeyguardUpdateMonitorCallback.class));
Fabian Kozynskib176f422019-02-05 09:36:59 -0500143
144 mCarrierTextCallbackInfo = new CarrierTextController.CarrierTextCallbackInfo("",
145 new CharSequence[]{}, false, new int[]{});
Malcolm Chen32f799d2019-10-08 18:11:22 -0700146 when(mTelephonyManager.getSupportedModemCount()).thenReturn(3);
Malcolm Chenb908f9c2019-11-04 16:26:28 -0800147 when(mTelephonyManager.getActiveModemCount()).thenReturn(3);
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400148
Fabian Kozynskiba46d1c2019-10-03 12:01:38 -0400149 mCarrierTextController = new CarrierTextController(mContext, SEPARATOR, true, true);
150 // This should not start listening on any of the real dependencies but will test that
151 // callbacks in mKeyguardUpdateMonitor are done in the mTestableLooper thread
Fabian Kozynskib176f422019-02-05 09:36:59 -0500152 mCarrierTextController.setListening(mCarrierTextCallback);
153 }
154
155 @Test
Fabian Kozynskiba46d1c2019-10-03 12:01:38 -0400156 public void testKeyguardUpdateMonitorCalledInMainThread() throws Exception {
157 // This test will run on the main looper (which is not the same as the looper set as MAIN
158 // for CarrierTextCallback. This will fail if calls to mKeyguardUpdateMonitor are not done
159 // through the looper set in the set up
160 HandlerThread thread = new HandlerThread("testThread",
161 Process.THREAD_PRIORITY_BACKGROUND);
162 thread.start();
163 TestableLooper testableLooper = new TestableLooper(thread.getLooper());
164 Handler h = new Handler(testableLooper.getLooper());
165 h.post(() -> {
166 mCarrierTextController.setListening(null);
167 mCarrierTextController.setListening(mCarrierTextCallback);
168 });
169 testableLooper.processAllMessages();
170 mTestableLooper.processAllMessages();
171 thread.quitSafely();
172 }
173
174 @Test
Fabian Kozynskic3d06f32019-07-31 14:18:41 -0400175 public void testAirplaneMode() {
176 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
177 reset(mCarrierTextCallback);
178 List<SubscriptionInfo> list = new ArrayList<>();
179 list.add(TEST_SUBSCRIPTION);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700180 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
Jayachandran Cf5436a62019-11-08 18:22:45 -0800181 when(mKeyguardUpdateMonitor.getSimState(0)).thenReturn(TelephonyManager.SIM_STATE_READY);
Fabian Kozynskic3d06f32019-07-31 14:18:41 -0400182 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
183
184 mCarrierTextController.updateCarrierText();
185
186 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
187 ArgumentCaptor.forClass(
188 CarrierTextController.CarrierTextCallbackInfo.class);
189
190 mTestableLooper.processAllMessages();
191 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
192 assertEquals(AIRPLANE_MODE_TEXT, captor.getValue().carrierText);
193 }
194
195 @Test
196 public void testCardIOError() {
197 reset(mCarrierTextCallback);
198 List<SubscriptionInfo> list = new ArrayList<>();
199 list.add(TEST_SUBSCRIPTION);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700200 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
Jayachandran Cf5436a62019-11-08 18:22:45 -0800201 when(mKeyguardUpdateMonitor.getSimState(0)).thenReturn(TelephonyManager.SIM_STATE_READY);
Fabian Kozynskic3d06f32019-07-31 14:18:41 -0400202 when(mKeyguardUpdateMonitor.getSimState(1)).thenReturn(
Jayachandran Cf5436a62019-11-08 18:22:45 -0800203 TelephonyManager.SIM_STATE_CARD_IO_ERROR);
Fabian Kozynskic3d06f32019-07-31 14:18:41 -0400204 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
205
206 mCarrierTextController.mCallback.onSimStateChanged(3, 1,
Jayachandran Cf5436a62019-11-08 18:22:45 -0800207 TelephonyManager.SIM_STATE_CARD_IO_ERROR);
Fabian Kozynskic3d06f32019-07-31 14:18:41 -0400208
209 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
210 ArgumentCaptor.forClass(
211 CarrierTextController.CarrierTextCallbackInfo.class);
212
213 mTestableLooper.processAllMessages();
214 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
215 assertEquals("TEST_CARRIER" + SEPARATOR + INVALID_CARD_TEXT, captor.getValue().carrierText);
216 // There's only one subscription in the list
217 assertEquals(1, captor.getValue().listOfCarriers.length);
218 assertEquals(TEST_CARRIER, captor.getValue().listOfCarriers[0]);
Malcolm Chenb908f9c2019-11-04 16:26:28 -0800219
220 // Now it becomes single SIM active mode.
221 reset(mCarrierTextCallback);
222 when(mTelephonyManager.getActiveModemCount()).thenReturn(1);
223 // Update carrier text. It should ignore error state of subId 3 in inactive slotId.
224 mCarrierTextController.updateCarrierText();
225 mTestableLooper.processAllMessages();
226 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
227 assertEquals("TEST_CARRIER", captor.getValue().carrierText);
Fabian Kozynskic3d06f32019-07-31 14:18:41 -0400228 }
229
230 @Test
Fabian Kozynskib176f422019-02-05 09:36:59 -0500231 public void testWrongSlots() {
232 reset(mCarrierTextCallback);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700233 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(
Fabian Kozynskib176f422019-02-05 09:36:59 -0500234 new ArrayList<>());
235 when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(
Jayachandran Cf5436a62019-11-08 18:22:45 -0800236 TelephonyManager.SIM_STATE_CARD_IO_ERROR);
Fabian Kozynskib176f422019-02-05 09:36:59 -0500237 // This should not produce an out of bounds error, even though there are no subscriptions
238 mCarrierTextController.mCallback.onSimStateChanged(0, -3,
Jayachandran Cf5436a62019-11-08 18:22:45 -0800239 TelephonyManager.SIM_STATE_CARD_IO_ERROR);
240 mCarrierTextController.mCallback.onSimStateChanged(0, 3, TelephonyManager.SIM_STATE_READY);
Fabian Kozynskib176f422019-02-05 09:36:59 -0500241 verify(mCarrierTextCallback, never()).updateCarrierInfo(any());
242 }
243
244 @Test
245 public void testMoreSlotsThanSubs() {
246 reset(mCarrierTextCallback);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700247 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(
Fabian Kozynskib176f422019-02-05 09:36:59 -0500248 new ArrayList<>());
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400249
250 // STOPSHIP(b/130246708) This line makes sure that SubscriptionManager provides the
251 // same answer as KeyguardUpdateMonitor. Remove when this is addressed
252 when(mSubscriptionManager.getActiveSubscriptionInfoList(anyBoolean())).thenReturn(
253 new ArrayList<>());
254
Fabian Kozynskib176f422019-02-05 09:36:59 -0500255 when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(
Jayachandran Cf5436a62019-11-08 18:22:45 -0800256 TelephonyManager.SIM_STATE_CARD_IO_ERROR);
Fabian Kozynskib176f422019-02-05 09:36:59 -0500257 // This should not produce an out of bounds error, even though there are no subscriptions
258 mCarrierTextController.mCallback.onSimStateChanged(0, 1,
Jayachandran Cf5436a62019-11-08 18:22:45 -0800259 TelephonyManager.SIM_STATE_CARD_IO_ERROR);
Fabian Kozynskib176f422019-02-05 09:36:59 -0500260
261 mTestableLooper.processAllMessages();
262 verify(mCarrierTextCallback).updateCarrierInfo(
263 any(CarrierTextController.CarrierTextCallbackInfo.class));
264 }
265
266 @Test
267 public void testCallback() {
268 reset(mCarrierTextCallback);
269 mCarrierTextController.postToCallback(mCarrierTextCallbackInfo);
270 mTestableLooper.processAllMessages();
271
272 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
273 ArgumentCaptor.forClass(
274 CarrierTextController.CarrierTextCallbackInfo.class);
275 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
276 assertEquals(mCarrierTextCallbackInfo, captor.getValue());
277 }
278
279 @Test
280 public void testNullingCallback() {
281 reset(mCarrierTextCallback);
282
283 mCarrierTextController.postToCallback(mCarrierTextCallbackInfo);
284 mCarrierTextController.setListening(null);
285
286 // This shouldn't produce NPE
287 mTestableLooper.processAllMessages();
288 verify(mCarrierTextCallback).updateCarrierInfo(any());
289 }
290
291 @Test
292 public void testCreateInfo_OneValidSubscription() {
293 reset(mCarrierTextCallback);
294 List<SubscriptionInfo> list = new ArrayList<>();
295 list.add(TEST_SUBSCRIPTION);
Jayachandran Cf5436a62019-11-08 18:22:45 -0800296 when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(
297 TelephonyManager.SIM_STATE_READY);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700298 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400299
Bonian Chena7e9e8c2019-06-02 23:59:31 +0000300 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
301
Fabian Kozynskib176f422019-02-05 09:36:59 -0500302 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
303 ArgumentCaptor.forClass(
304 CarrierTextController.CarrierTextCallbackInfo.class);
305
306 mCarrierTextController.updateCarrierText();
307 mTestableLooper.processAllMessages();
308 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
309
310 CarrierTextController.CarrierTextCallbackInfo info = captor.getValue();
311 assertEquals(1, info.listOfCarriers.length);
312 assertEquals(TEST_CARRIER, info.listOfCarriers[0]);
313 assertEquals(1, info.subscriptionIds.length);
314 }
315
316 @Test
317 public void testCreateInfo_OneValidSubscriptionWithRoaming() {
318 reset(mCarrierTextCallback);
319 List<SubscriptionInfo> list = new ArrayList<>();
320 list.add(TEST_SUBSCRIPTION_ROAMING);
Jayachandran Cf5436a62019-11-08 18:22:45 -0800321 when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(
322 TelephonyManager.SIM_STATE_READY);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700323 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400324
Bonian Chena7e9e8c2019-06-02 23:59:31 +0000325 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
326
Fabian Kozynskib176f422019-02-05 09:36:59 -0500327 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
328 ArgumentCaptor.forClass(
329 CarrierTextController.CarrierTextCallbackInfo.class);
330
331 mCarrierTextController.updateCarrierText();
332 mTestableLooper.processAllMessages();
333 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
334
335 CarrierTextController.CarrierTextCallbackInfo info = captor.getValue();
336 assertEquals(1, info.listOfCarriers.length);
337 assertTrue(info.listOfCarriers[0].toString().contains(TEST_CARRIER));
338 assertEquals(1, info.subscriptionIds.length);
339 }
340
341 @Test
Fabian Kozynski0f49f202019-08-29 09:47:07 -0400342 public void testCarrierText_noTextOnReadySimWhenNull() {
343 reset(mCarrierTextCallback);
344 List<SubscriptionInfo> list = new ArrayList<>();
345 list.add(TEST_SUBSCRIPTION_NULL);
Jayachandran Cf5436a62019-11-08 18:22:45 -0800346 when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(
347 TelephonyManager.SIM_STATE_READY);
Fabian Kozynski0f49f202019-08-29 09:47:07 -0400348 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
349
350 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
351
352 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
353 ArgumentCaptor.forClass(
354 CarrierTextController.CarrierTextCallbackInfo.class);
355
356 mCarrierTextController.updateCarrierText();
357 mTestableLooper.processAllMessages();
358 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
359
360 assertTrue("Carrier text should be empty, instead it's " + captor.getValue().carrierText,
361 TextUtils.isEmpty(captor.getValue().carrierText));
362 assertFalse("No SIM should be available", captor.getValue().anySimReady);
363 }
364
365 @Test
366 public void testCarrierText_noTextOnReadySimWhenNull_airplaneMode_wifiOn() {
367 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
368 reset(mCarrierTextCallback);
369 List<SubscriptionInfo> list = new ArrayList<>();
370 list.add(TEST_SUBSCRIPTION_NULL);
Jayachandran Cf5436a62019-11-08 18:22:45 -0800371 when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(
372 TelephonyManager.SIM_STATE_READY);
Fabian Kozynski0f49f202019-08-29 09:47:07 -0400373 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
374 mockWifi();
375
376 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
377 ServiceState ss = mock(ServiceState.class);
SongFerngWang2670cba2019-12-23 19:21:14 +0800378 when(ss.getDataRegistrationState()).thenReturn(ServiceState.STATE_IN_SERVICE);
Fabian Kozynski0f49f202019-08-29 09:47:07 -0400379 mKeyguardUpdateMonitor.mServiceStates.put(TEST_SUBSCRIPTION_NULL.getSubscriptionId(), ss);
380
381 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
382 ArgumentCaptor.forClass(
383 CarrierTextController.CarrierTextCallbackInfo.class);
384
385 mCarrierTextController.updateCarrierText();
386 mTestableLooper.processAllMessages();
387 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
388
389 assertFalse("No SIM should be available", captor.getValue().anySimReady);
390 // There's no airplane mode if at least one SIM is State.READY and there's wifi
391 assertFalse("Device should not be in airplane mode", captor.getValue().airplaneMode);
392 assertNotEquals(AIRPLANE_MODE_TEXT, captor.getValue().carrierText);
393 }
394
395 private void mockWifi() {
396 when(mWifiManager.isWifiEnabled()).thenReturn(true);
397 WifiInfo wifiInfo = mock(WifiInfo.class);
398 when(wifiInfo.getBSSID()).thenReturn("");
399 when(mWifiManager.getConnectionInfo()).thenReturn(wifiInfo);
400 }
401
402 @Test
Fabian Kozynskib176f422019-02-05 09:36:59 -0500403 public void testCreateInfo_noSubscriptions() {
404 reset(mCarrierTextCallback);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700405 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(
Fabian Kozynskib176f422019-02-05 09:36:59 -0500406 new ArrayList<>());
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400407
Fabian Kozynskib176f422019-02-05 09:36:59 -0500408 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
409 ArgumentCaptor.forClass(
410 CarrierTextController.CarrierTextCallbackInfo.class);
411
412 mCarrierTextController.updateCarrierText();
413 mTestableLooper.processAllMessages();
414 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
415
416 CarrierTextController.CarrierTextCallbackInfo info = captor.getValue();
417 assertEquals(0, info.listOfCarriers.length);
418 assertEquals(0, info.subscriptionIds.length);
419
420 }
421
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400422 @Test
423 public void testCarrierText_twoValidSubscriptions() {
424 reset(mCarrierTextCallback);
425 List<SubscriptionInfo> list = new ArrayList<>();
426 list.add(TEST_SUBSCRIPTION);
427 list.add(TEST_SUBSCRIPTION);
Jayachandran Cf5436a62019-11-08 18:22:45 -0800428 when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(
429 TelephonyManager.SIM_STATE_READY);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700430 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400431
Bonian Chena7e9e8c2019-06-02 23:59:31 +0000432 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
433
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400434 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
435 ArgumentCaptor.forClass(
436 CarrierTextController.CarrierTextCallbackInfo.class);
437
438 mCarrierTextController.updateCarrierText();
439 mTestableLooper.processAllMessages();
440 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
441
442 assertEquals(TEST_CARRIER + SEPARATOR + TEST_CARRIER,
443 captor.getValue().carrierText);
444 }
445
446 @Test
447 public void testCarrierText_oneDisabledSub() {
448 reset(mCarrierTextCallback);
449 List<SubscriptionInfo> list = new ArrayList<>();
450 list.add(TEST_SUBSCRIPTION);
451 list.add(TEST_SUBSCRIPTION);
452 when(mKeyguardUpdateMonitor.getSimState(anyInt()))
Jayachandran Cf5436a62019-11-08 18:22:45 -0800453 .thenReturn(TelephonyManager.SIM_STATE_READY)
454 .thenReturn(TelephonyManager.SIM_STATE_NOT_READY);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700455 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400456
Bonian Chena7e9e8c2019-06-02 23:59:31 +0000457 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
458
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400459 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
460 ArgumentCaptor.forClass(
461 CarrierTextController.CarrierTextCallbackInfo.class);
462
463 mCarrierTextController.updateCarrierText();
464 mTestableLooper.processAllMessages();
465 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
466
467 assertEquals(TEST_CARRIER,
468 captor.getValue().carrierText);
469 }
470
471 @Test
472 public void testCarrierText_firstDisabledSub() {
473 reset(mCarrierTextCallback);
474 List<SubscriptionInfo> list = new ArrayList<>();
475 list.add(TEST_SUBSCRIPTION);
476 list.add(TEST_SUBSCRIPTION);
477 when(mKeyguardUpdateMonitor.getSimState(anyInt()))
Jayachandran Cf5436a62019-11-08 18:22:45 -0800478 .thenReturn(TelephonyManager.SIM_STATE_NOT_READY)
479 .thenReturn(TelephonyManager.SIM_STATE_READY);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700480 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400481
Bonian Chena7e9e8c2019-06-02 23:59:31 +0000482 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
483
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400484 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
485 ArgumentCaptor.forClass(
486 CarrierTextController.CarrierTextCallbackInfo.class);
487
488 mCarrierTextController.updateCarrierText();
489 mTestableLooper.processAllMessages();
490 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
491
492 assertEquals(TEST_CARRIER,
493 captor.getValue().carrierText);
494 }
495
496 @Test
497 public void testCarrierText_threeSubsMiddleDisabled() {
498 reset(mCarrierTextCallback);
499 List<SubscriptionInfo> list = new ArrayList<>();
500 list.add(TEST_SUBSCRIPTION);
501 list.add(TEST_SUBSCRIPTION);
502 list.add(TEST_SUBSCRIPTION);
503 when(mKeyguardUpdateMonitor.getSimState(anyInt()))
Jayachandran Cf5436a62019-11-08 18:22:45 -0800504 .thenReturn(TelephonyManager.SIM_STATE_READY)
505 .thenReturn(TelephonyManager.SIM_STATE_NOT_READY)
506 .thenReturn(TelephonyManager.SIM_STATE_READY);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700507 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
Bonian Chena7e9e8c2019-06-02 23:59:31 +0000508 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400509
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400510 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
511 ArgumentCaptor.forClass(
512 CarrierTextController.CarrierTextCallbackInfo.class);
513
514 mCarrierTextController.updateCarrierText();
515 mTestableLooper.processAllMessages();
516 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
517
518 assertEquals(TEST_CARRIER + SEPARATOR + TEST_CARRIER,
519 captor.getValue().carrierText);
520 }
Fabian Kozynskib176f422019-02-05 09:36:59 -0500521}