blob: aa4122fd190a63c86313985af5e5b8b1c1a30e20 [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 Wangff7f25f2020-01-31 18:46:26 -080022import static android.telephony.SubscriptionManager.NAME_SOURCE_CARRIER_ID;
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 Wangff7f25f2020-01-31 18:46:26 -080086 TEST_CARRIER, TEST_CARRIER, NAME_SOURCE_CARRIER_ID, 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 Wangff7f25f2020-01-31 18:46:26 -080090 TEST_CARRIER, null, NAME_SOURCE_CARRIER_ID, 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 Wangff7f25f2020-01-31 18:46:26 -080093 TEST_CARRIER, TEST_CARRIER, NAME_SOURCE_CARRIER_ID, 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 Kozynski49325ea2020-05-12 12:02:40 -0400137 mDependency.injectTestDependency(Dependency.BG_LOOPER, mTestableLooper.getLooper());
Fabian Kozynskiba46d1c2019-10-03 12:01:38 -0400138 mDependency.injectTestDependency(KeyguardUpdateMonitor.class, mKeyguardUpdateMonitor);
139
140 doAnswer(this::checkMainThread).when(mKeyguardUpdateMonitor)
141 .registerCallback(any(KeyguardUpdateMonitorCallback.class));
142 doAnswer(this::checkMainThread).when(mKeyguardUpdateMonitor)
143 .removeCallback(any(KeyguardUpdateMonitorCallback.class));
Fabian Kozynskib176f422019-02-05 09:36:59 -0500144
145 mCarrierTextCallbackInfo = new CarrierTextController.CarrierTextCallbackInfo("",
146 new CharSequence[]{}, false, new int[]{});
Malcolm Chen32f799d2019-10-08 18:11:22 -0700147 when(mTelephonyManager.getSupportedModemCount()).thenReturn(3);
Malcolm Chenb908f9c2019-11-04 16:26:28 -0800148 when(mTelephonyManager.getActiveModemCount()).thenReturn(3);
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400149
Fabian Kozynskiba46d1c2019-10-03 12:01:38 -0400150 mCarrierTextController = new CarrierTextController(mContext, SEPARATOR, true, true);
151 // This should not start listening on any of the real dependencies but will test that
152 // callbacks in mKeyguardUpdateMonitor are done in the mTestableLooper thread
Fabian Kozynskib176f422019-02-05 09:36:59 -0500153 mCarrierTextController.setListening(mCarrierTextCallback);
Fabian Kozynski49325ea2020-05-12 12:02:40 -0400154 mTestableLooper.processAllMessages();
Fabian Kozynskib176f422019-02-05 09:36:59 -0500155 }
156
157 @Test
Fabian Kozynskiba46d1c2019-10-03 12:01:38 -0400158 public void testKeyguardUpdateMonitorCalledInMainThread() throws Exception {
159 // This test will run on the main looper (which is not the same as the looper set as MAIN
160 // for CarrierTextCallback. This will fail if calls to mKeyguardUpdateMonitor are not done
161 // through the looper set in the set up
162 HandlerThread thread = new HandlerThread("testThread",
163 Process.THREAD_PRIORITY_BACKGROUND);
164 thread.start();
165 TestableLooper testableLooper = new TestableLooper(thread.getLooper());
166 Handler h = new Handler(testableLooper.getLooper());
167 h.post(() -> {
168 mCarrierTextController.setListening(null);
169 mCarrierTextController.setListening(mCarrierTextCallback);
170 });
171 testableLooper.processAllMessages();
172 mTestableLooper.processAllMessages();
173 thread.quitSafely();
174 }
175
176 @Test
Fabian Kozynskic3d06f32019-07-31 14:18:41 -0400177 public void testAirplaneMode() {
178 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
179 reset(mCarrierTextCallback);
180 List<SubscriptionInfo> list = new ArrayList<>();
181 list.add(TEST_SUBSCRIPTION);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700182 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
Jayachandran Cf5436a62019-11-08 18:22:45 -0800183 when(mKeyguardUpdateMonitor.getSimState(0)).thenReturn(TelephonyManager.SIM_STATE_READY);
Fabian Kozynskic3d06f32019-07-31 14:18:41 -0400184 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
185
186 mCarrierTextController.updateCarrierText();
187
188 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
189 ArgumentCaptor.forClass(
190 CarrierTextController.CarrierTextCallbackInfo.class);
191
192 mTestableLooper.processAllMessages();
193 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
194 assertEquals(AIRPLANE_MODE_TEXT, captor.getValue().carrierText);
195 }
196
197 @Test
198 public void testCardIOError() {
199 reset(mCarrierTextCallback);
200 List<SubscriptionInfo> list = new ArrayList<>();
201 list.add(TEST_SUBSCRIPTION);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700202 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
Jayachandran Cf5436a62019-11-08 18:22:45 -0800203 when(mKeyguardUpdateMonitor.getSimState(0)).thenReturn(TelephonyManager.SIM_STATE_READY);
Fabian Kozynskic3d06f32019-07-31 14:18:41 -0400204 when(mKeyguardUpdateMonitor.getSimState(1)).thenReturn(
Jayachandran Cf5436a62019-11-08 18:22:45 -0800205 TelephonyManager.SIM_STATE_CARD_IO_ERROR);
Fabian Kozynskic3d06f32019-07-31 14:18:41 -0400206 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
207
208 mCarrierTextController.mCallback.onSimStateChanged(3, 1,
Jayachandran Cf5436a62019-11-08 18:22:45 -0800209 TelephonyManager.SIM_STATE_CARD_IO_ERROR);
Fabian Kozynskic3d06f32019-07-31 14:18:41 -0400210
211 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
212 ArgumentCaptor.forClass(
213 CarrierTextController.CarrierTextCallbackInfo.class);
214
215 mTestableLooper.processAllMessages();
216 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
217 assertEquals("TEST_CARRIER" + SEPARATOR + INVALID_CARD_TEXT, captor.getValue().carrierText);
218 // There's only one subscription in the list
219 assertEquals(1, captor.getValue().listOfCarriers.length);
220 assertEquals(TEST_CARRIER, captor.getValue().listOfCarriers[0]);
Malcolm Chenb908f9c2019-11-04 16:26:28 -0800221
222 // Now it becomes single SIM active mode.
223 reset(mCarrierTextCallback);
224 when(mTelephonyManager.getActiveModemCount()).thenReturn(1);
225 // Update carrier text. It should ignore error state of subId 3 in inactive slotId.
226 mCarrierTextController.updateCarrierText();
227 mTestableLooper.processAllMessages();
228 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
229 assertEquals("TEST_CARRIER", captor.getValue().carrierText);
Fabian Kozynskic3d06f32019-07-31 14:18:41 -0400230 }
231
232 @Test
Fabian Kozynskib176f422019-02-05 09:36:59 -0500233 public void testWrongSlots() {
234 reset(mCarrierTextCallback);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700235 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(
Fabian Kozynskib176f422019-02-05 09:36:59 -0500236 new ArrayList<>());
237 when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(
Jayachandran Cf5436a62019-11-08 18:22:45 -0800238 TelephonyManager.SIM_STATE_CARD_IO_ERROR);
Fabian Kozynskib176f422019-02-05 09:36:59 -0500239 // This should not produce an out of bounds error, even though there are no subscriptions
240 mCarrierTextController.mCallback.onSimStateChanged(0, -3,
Jayachandran Cf5436a62019-11-08 18:22:45 -0800241 TelephonyManager.SIM_STATE_CARD_IO_ERROR);
242 mCarrierTextController.mCallback.onSimStateChanged(0, 3, TelephonyManager.SIM_STATE_READY);
Fabian Kozynskib176f422019-02-05 09:36:59 -0500243 verify(mCarrierTextCallback, never()).updateCarrierInfo(any());
244 }
245
246 @Test
247 public void testMoreSlotsThanSubs() {
248 reset(mCarrierTextCallback);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700249 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(
Fabian Kozynskib176f422019-02-05 09:36:59 -0500250 new ArrayList<>());
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400251
252 // STOPSHIP(b/130246708) This line makes sure that SubscriptionManager provides the
253 // same answer as KeyguardUpdateMonitor. Remove when this is addressed
Sooraj Sasindran40468062020-01-06 19:10:50 -0800254 when(mSubscriptionManager.getCompleteActiveSubscriptionInfoList()).thenReturn(
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400255 new ArrayList<>());
256
Fabian Kozynskib176f422019-02-05 09:36:59 -0500257 when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(
Jayachandran Cf5436a62019-11-08 18:22:45 -0800258 TelephonyManager.SIM_STATE_CARD_IO_ERROR);
Fabian Kozynskib176f422019-02-05 09:36:59 -0500259 // This should not produce an out of bounds error, even though there are no subscriptions
260 mCarrierTextController.mCallback.onSimStateChanged(0, 1,
Jayachandran Cf5436a62019-11-08 18:22:45 -0800261 TelephonyManager.SIM_STATE_CARD_IO_ERROR);
Fabian Kozynskib176f422019-02-05 09:36:59 -0500262
263 mTestableLooper.processAllMessages();
264 verify(mCarrierTextCallback).updateCarrierInfo(
265 any(CarrierTextController.CarrierTextCallbackInfo.class));
266 }
267
268 @Test
269 public void testCallback() {
270 reset(mCarrierTextCallback);
271 mCarrierTextController.postToCallback(mCarrierTextCallbackInfo);
272 mTestableLooper.processAllMessages();
273
274 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
275 ArgumentCaptor.forClass(
276 CarrierTextController.CarrierTextCallbackInfo.class);
277 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
278 assertEquals(mCarrierTextCallbackInfo, captor.getValue());
279 }
280
281 @Test
282 public void testNullingCallback() {
283 reset(mCarrierTextCallback);
284
285 mCarrierTextController.postToCallback(mCarrierTextCallbackInfo);
286 mCarrierTextController.setListening(null);
287
288 // This shouldn't produce NPE
289 mTestableLooper.processAllMessages();
290 verify(mCarrierTextCallback).updateCarrierInfo(any());
291 }
292
293 @Test
294 public void testCreateInfo_OneValidSubscription() {
295 reset(mCarrierTextCallback);
296 List<SubscriptionInfo> list = new ArrayList<>();
297 list.add(TEST_SUBSCRIPTION);
Jayachandran Cf5436a62019-11-08 18:22:45 -0800298 when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(
299 TelephonyManager.SIM_STATE_READY);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700300 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400301
Bonian Chena7e9e8c2019-06-02 23:59:31 +0000302 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
303
Fabian Kozynskib176f422019-02-05 09:36:59 -0500304 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
305 ArgumentCaptor.forClass(
306 CarrierTextController.CarrierTextCallbackInfo.class);
307
308 mCarrierTextController.updateCarrierText();
309 mTestableLooper.processAllMessages();
310 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
311
312 CarrierTextController.CarrierTextCallbackInfo info = captor.getValue();
313 assertEquals(1, info.listOfCarriers.length);
314 assertEquals(TEST_CARRIER, info.listOfCarriers[0]);
315 assertEquals(1, info.subscriptionIds.length);
316 }
317
318 @Test
319 public void testCreateInfo_OneValidSubscriptionWithRoaming() {
320 reset(mCarrierTextCallback);
321 List<SubscriptionInfo> list = new ArrayList<>();
322 list.add(TEST_SUBSCRIPTION_ROAMING);
Jayachandran Cf5436a62019-11-08 18:22:45 -0800323 when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(
324 TelephonyManager.SIM_STATE_READY);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700325 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400326
Bonian Chena7e9e8c2019-06-02 23:59:31 +0000327 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
328
Fabian Kozynskib176f422019-02-05 09:36:59 -0500329 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
330 ArgumentCaptor.forClass(
331 CarrierTextController.CarrierTextCallbackInfo.class);
332
333 mCarrierTextController.updateCarrierText();
334 mTestableLooper.processAllMessages();
335 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
336
337 CarrierTextController.CarrierTextCallbackInfo info = captor.getValue();
338 assertEquals(1, info.listOfCarriers.length);
339 assertTrue(info.listOfCarriers[0].toString().contains(TEST_CARRIER));
340 assertEquals(1, info.subscriptionIds.length);
341 }
342
343 @Test
Fabian Kozynski0f49f202019-08-29 09:47:07 -0400344 public void testCarrierText_noTextOnReadySimWhenNull() {
345 reset(mCarrierTextCallback);
346 List<SubscriptionInfo> list = new ArrayList<>();
347 list.add(TEST_SUBSCRIPTION_NULL);
Jayachandran Cf5436a62019-11-08 18:22:45 -0800348 when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(
349 TelephonyManager.SIM_STATE_READY);
Fabian Kozynski0f49f202019-08-29 09:47:07 -0400350 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
351
352 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
353
354 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
355 ArgumentCaptor.forClass(
356 CarrierTextController.CarrierTextCallbackInfo.class);
357
358 mCarrierTextController.updateCarrierText();
359 mTestableLooper.processAllMessages();
360 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
361
362 assertTrue("Carrier text should be empty, instead it's " + captor.getValue().carrierText,
363 TextUtils.isEmpty(captor.getValue().carrierText));
364 assertFalse("No SIM should be available", captor.getValue().anySimReady);
365 }
366
367 @Test
368 public void testCarrierText_noTextOnReadySimWhenNull_airplaneMode_wifiOn() {
369 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
370 reset(mCarrierTextCallback);
371 List<SubscriptionInfo> list = new ArrayList<>();
372 list.add(TEST_SUBSCRIPTION_NULL);
Jayachandran Cf5436a62019-11-08 18:22:45 -0800373 when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(
374 TelephonyManager.SIM_STATE_READY);
Fabian Kozynski0f49f202019-08-29 09:47:07 -0400375 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
376 mockWifi();
377
378 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
379 ServiceState ss = mock(ServiceState.class);
SongFerngWang2670cba2019-12-23 19:21:14 +0800380 when(ss.getDataRegistrationState()).thenReturn(ServiceState.STATE_IN_SERVICE);
Fabian Kozynski0f49f202019-08-29 09:47:07 -0400381 mKeyguardUpdateMonitor.mServiceStates.put(TEST_SUBSCRIPTION_NULL.getSubscriptionId(), ss);
382
383 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
384 ArgumentCaptor.forClass(
385 CarrierTextController.CarrierTextCallbackInfo.class);
386
387 mCarrierTextController.updateCarrierText();
388 mTestableLooper.processAllMessages();
389 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
390
391 assertFalse("No SIM should be available", captor.getValue().anySimReady);
392 // There's no airplane mode if at least one SIM is State.READY and there's wifi
393 assertFalse("Device should not be in airplane mode", captor.getValue().airplaneMode);
394 assertNotEquals(AIRPLANE_MODE_TEXT, captor.getValue().carrierText);
395 }
396
397 private void mockWifi() {
398 when(mWifiManager.isWifiEnabled()).thenReturn(true);
399 WifiInfo wifiInfo = mock(WifiInfo.class);
400 when(wifiInfo.getBSSID()).thenReturn("");
401 when(mWifiManager.getConnectionInfo()).thenReturn(wifiInfo);
402 }
403
404 @Test
Fabian Kozynskib176f422019-02-05 09:36:59 -0500405 public void testCreateInfo_noSubscriptions() {
406 reset(mCarrierTextCallback);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700407 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(
Fabian Kozynskib176f422019-02-05 09:36:59 -0500408 new ArrayList<>());
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400409
Fabian Kozynskib176f422019-02-05 09:36:59 -0500410 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
411 ArgumentCaptor.forClass(
412 CarrierTextController.CarrierTextCallbackInfo.class);
413
414 mCarrierTextController.updateCarrierText();
415 mTestableLooper.processAllMessages();
416 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
417
418 CarrierTextController.CarrierTextCallbackInfo info = captor.getValue();
419 assertEquals(0, info.listOfCarriers.length);
420 assertEquals(0, info.subscriptionIds.length);
421
422 }
423
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400424 @Test
425 public void testCarrierText_twoValidSubscriptions() {
426 reset(mCarrierTextCallback);
427 List<SubscriptionInfo> list = new ArrayList<>();
428 list.add(TEST_SUBSCRIPTION);
429 list.add(TEST_SUBSCRIPTION);
Jayachandran Cf5436a62019-11-08 18:22:45 -0800430 when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(
431 TelephonyManager.SIM_STATE_READY);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700432 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400433
Bonian Chena7e9e8c2019-06-02 23:59:31 +0000434 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
435
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400436 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
437 ArgumentCaptor.forClass(
438 CarrierTextController.CarrierTextCallbackInfo.class);
439
440 mCarrierTextController.updateCarrierText();
441 mTestableLooper.processAllMessages();
442 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
443
444 assertEquals(TEST_CARRIER + SEPARATOR + TEST_CARRIER,
445 captor.getValue().carrierText);
446 }
447
448 @Test
449 public void testCarrierText_oneDisabledSub() {
450 reset(mCarrierTextCallback);
451 List<SubscriptionInfo> list = new ArrayList<>();
452 list.add(TEST_SUBSCRIPTION);
453 list.add(TEST_SUBSCRIPTION);
454 when(mKeyguardUpdateMonitor.getSimState(anyInt()))
Jayachandran Cf5436a62019-11-08 18:22:45 -0800455 .thenReturn(TelephonyManager.SIM_STATE_READY)
456 .thenReturn(TelephonyManager.SIM_STATE_NOT_READY);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700457 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400458
Bonian Chena7e9e8c2019-06-02 23:59:31 +0000459 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
460
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400461 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
462 ArgumentCaptor.forClass(
463 CarrierTextController.CarrierTextCallbackInfo.class);
464
465 mCarrierTextController.updateCarrierText();
466 mTestableLooper.processAllMessages();
467 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
468
469 assertEquals(TEST_CARRIER,
470 captor.getValue().carrierText);
471 }
472
473 @Test
474 public void testCarrierText_firstDisabledSub() {
475 reset(mCarrierTextCallback);
476 List<SubscriptionInfo> list = new ArrayList<>();
477 list.add(TEST_SUBSCRIPTION);
478 list.add(TEST_SUBSCRIPTION);
479 when(mKeyguardUpdateMonitor.getSimState(anyInt()))
Jayachandran Cf5436a62019-11-08 18:22:45 -0800480 .thenReturn(TelephonyManager.SIM_STATE_NOT_READY)
481 .thenReturn(TelephonyManager.SIM_STATE_READY);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700482 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400483
Bonian Chena7e9e8c2019-06-02 23:59:31 +0000484 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
485
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400486 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
487 ArgumentCaptor.forClass(
488 CarrierTextController.CarrierTextCallbackInfo.class);
489
490 mCarrierTextController.updateCarrierText();
491 mTestableLooper.processAllMessages();
492 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
493
494 assertEquals(TEST_CARRIER,
495 captor.getValue().carrierText);
496 }
497
498 @Test
499 public void testCarrierText_threeSubsMiddleDisabled() {
500 reset(mCarrierTextCallback);
501 List<SubscriptionInfo> list = new ArrayList<>();
502 list.add(TEST_SUBSCRIPTION);
503 list.add(TEST_SUBSCRIPTION);
504 list.add(TEST_SUBSCRIPTION);
505 when(mKeyguardUpdateMonitor.getSimState(anyInt()))
Jayachandran Cf5436a62019-11-08 18:22:45 -0800506 .thenReturn(TelephonyManager.SIM_STATE_READY)
507 .thenReturn(TelephonyManager.SIM_STATE_NOT_READY)
508 .thenReturn(TelephonyManager.SIM_STATE_READY);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700509 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
Bonian Chena7e9e8c2019-06-02 23:59:31 +0000510 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400511
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400512 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
513 ArgumentCaptor.forClass(
514 CarrierTextController.CarrierTextCallbackInfo.class);
515
516 mCarrierTextController.updateCarrierText();
517 mTestableLooper.processAllMessages();
518 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
519
520 assertEquals(TEST_CARRIER + SEPARATOR + TEST_CARRIER,
521 captor.getValue().carrierText);
522 }
Fabian Kozynskib176f422019-02-05 09:36:59 -0500523}