blob: 1ae1b9779f427cc0536955ee9a0d6bf6a4906f60 [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;
22import static android.telephony.SubscriptionManager.NAME_SOURCE_DEFAULT_SOURCE;
23
24import static junit.framework.Assert.assertTrue;
25
26import static org.junit.Assert.assertEquals;
27import static org.mockito.ArgumentMatchers.any;
28import static org.mockito.ArgumentMatchers.anyBoolean;
29import static org.mockito.ArgumentMatchers.anyInt;
30import static org.mockito.Mockito.never;
31import static org.mockito.Mockito.reset;
32import static org.mockito.Mockito.verify;
33import static org.mockito.Mockito.when;
34
35import android.content.Context;
36import android.net.ConnectivityManager;
37import android.net.wifi.WifiManager;
38import android.os.Handler;
Fabian Kozynskic3d06f32019-07-31 14:18:41 -040039import android.provider.Settings;
Fabian Kozynskib176f422019-02-05 09:36:59 -050040import android.telephony.SubscriptionInfo;
Malcolm Chen06ec3572019-04-09 15:55:29 -070041import android.telephony.SubscriptionManager;
Fabian Kozynskib176f422019-02-05 09:36:59 -050042import android.telephony.TelephonyManager;
43import android.test.suitebuilder.annotation.SmallTest;
44import android.testing.AndroidTestingRunner;
45import android.testing.TestableLooper;
46
47import com.android.internal.telephony.IccCardConstants;
48import com.android.systemui.Dependency;
49import com.android.systemui.SysuiTestCase;
50import com.android.systemui.keyguard.WakefulnessLifecycle;
51
52import org.junit.Before;
53import org.junit.Test;
54import org.junit.runner.RunWith;
55import org.mockito.ArgumentCaptor;
56import org.mockito.Mock;
57import org.mockito.MockitoAnnotations;
58
59import java.util.ArrayList;
Bonian Chena7e9e8c2019-06-02 23:59:31 +000060import java.util.HashMap;
Fabian Kozynskib176f422019-02-05 09:36:59 -050061import java.util.List;
62
63@SmallTest
64@RunWith(AndroidTestingRunner.class)
65@TestableLooper.RunWithLooper
66public class CarrierTextControllerTest extends SysuiTestCase {
67
68 private static final CharSequence SEPARATOR = " \u2014 ";
Fabian Kozynskic3d06f32019-07-31 14:18:41 -040069 private static final CharSequence INVALID_CARD_TEXT = "Invalid card";
70 private static final CharSequence AIRPLANE_MODE_TEXT = "Airplane mode";
Fabian Kozynskib176f422019-02-05 09:36:59 -050071 private static final String TEST_CARRIER = "TEST_CARRIER";
Sooraj Sasindran0d45da72019-04-25 15:12:21 -070072 private static final String TEST_CARRIER_2 = "TEST_CARRIER_2";
Sooraj Sasindran0d45da72019-04-25 15:12:21 -070073 private static final int TEST_CARRIER_ID = 1;
Fabian Kozynskib176f422019-02-05 09:36:59 -050074 private static final SubscriptionInfo TEST_SUBSCRIPTION = new SubscriptionInfo(0, "", 0,
75 TEST_CARRIER, TEST_CARRIER, NAME_SOURCE_DEFAULT_SOURCE, 0xFFFFFF, "",
Malcolm Chen5c63b512019-08-13 13:24:07 -070076 DATA_ROAMING_DISABLE, null, null, null, null, false, null, "", false, null,
Sooraj Sasindran0d45da72019-04-25 15:12:21 -070077 TEST_CARRIER_ID, 0);
Fabian Kozynskib176f422019-02-05 09:36:59 -050078 private static final SubscriptionInfo TEST_SUBSCRIPTION_ROAMING = new SubscriptionInfo(0, "", 0,
79 TEST_CARRIER, TEST_CARRIER, NAME_SOURCE_DEFAULT_SOURCE, 0xFFFFFF, "",
80 DATA_ROAMING_ENABLE, null, null, null, null, false, null, "");
81 @Mock
82 private WifiManager mWifiManager;
83 @Mock
84 private CarrierTextController.CarrierTextCallback mCarrierTextCallback;
85 @Mock
86 private KeyguardUpdateMonitor mKeyguardUpdateMonitor;
87 @Mock
88 private ConnectivityManager mConnectivityManager;
89 @Mock
90 private TelephonyManager mTelephonyManager;
Malcolm Chen06ec3572019-04-09 15:55:29 -070091 @Mock
92 private SubscriptionManager mSubscriptionManager;
Fabian Kozynskib176f422019-02-05 09:36:59 -050093 private CarrierTextController.CarrierTextCallbackInfo mCarrierTextCallbackInfo;
94
95 private CarrierTextController mCarrierTextController;
96 private TestableLooper mTestableLooper;
97
98 @Before
99 public void setUp() {
100 MockitoAnnotations.initMocks(this);
101 mTestableLooper = TestableLooper.get(this);
102
103 mContext.addMockSystemService(WifiManager.class, mWifiManager);
104 mContext.addMockSystemService(ConnectivityManager.class, mConnectivityManager);
105 mContext.addMockSystemService(TelephonyManager.class, mTelephonyManager);
Malcolm Chen06ec3572019-04-09 15:55:29 -0700106 mContext.addMockSystemService(SubscriptionManager.class, mSubscriptionManager);
Fabian Kozynskic3d06f32019-07-31 14:18:41 -0400107 mContext.getOrCreateTestableResources().addOverride(
108 R.string.keyguard_sim_error_message_short, INVALID_CARD_TEXT);
109 mContext.getOrCreateTestableResources().addOverride(
110 R.string.airplane_mode, AIRPLANE_MODE_TEXT);
Fabian Kozynskib176f422019-02-05 09:36:59 -0500111 mDependency.injectMockDependency(WakefulnessLifecycle.class);
112 mDependency.injectTestDependency(Dependency.MAIN_HANDLER,
113 new Handler(mTestableLooper.getLooper()));
114
115 mCarrierTextCallbackInfo = new CarrierTextController.CarrierTextCallbackInfo("",
116 new CharSequence[]{}, false, new int[]{});
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400117 when(mTelephonyManager.getPhoneCount()).thenReturn(3);
118
Fabian Kozynskib176f422019-02-05 09:36:59 -0500119 mCarrierTextController = new TestCarrierTextController(mContext, SEPARATOR, true, true,
120 mKeyguardUpdateMonitor);
121 // This should not start listening on any of the real dependencies
122 mCarrierTextController.setListening(mCarrierTextCallback);
123 }
124
125 @Test
Fabian Kozynskic3d06f32019-07-31 14:18:41 -0400126 public void testAirplaneMode() {
127 Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
128 reset(mCarrierTextCallback);
129 List<SubscriptionInfo> list = new ArrayList<>();
130 list.add(TEST_SUBSCRIPTION);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700131 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
Fabian Kozynskic3d06f32019-07-31 14:18:41 -0400132 when(mKeyguardUpdateMonitor.getSimState(0)).thenReturn(IccCardConstants.State.READY);
133 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
134
135 mCarrierTextController.updateCarrierText();
136
137 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
138 ArgumentCaptor.forClass(
139 CarrierTextController.CarrierTextCallbackInfo.class);
140
141 mTestableLooper.processAllMessages();
142 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
143 assertEquals(AIRPLANE_MODE_TEXT, captor.getValue().carrierText);
144 }
145
146 @Test
147 public void testCardIOError() {
148 reset(mCarrierTextCallback);
149 List<SubscriptionInfo> list = new ArrayList<>();
150 list.add(TEST_SUBSCRIPTION);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700151 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
Fabian Kozynskic3d06f32019-07-31 14:18:41 -0400152 when(mKeyguardUpdateMonitor.getSimState(0)).thenReturn(IccCardConstants.State.READY);
153 when(mKeyguardUpdateMonitor.getSimState(1)).thenReturn(
154 IccCardConstants.State.CARD_IO_ERROR);
155 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
156
157 mCarrierTextController.mCallback.onSimStateChanged(3, 1,
158 IccCardConstants.State.CARD_IO_ERROR);
159
160 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
161 ArgumentCaptor.forClass(
162 CarrierTextController.CarrierTextCallbackInfo.class);
163
164 mTestableLooper.processAllMessages();
165 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
166 assertEquals("TEST_CARRIER" + SEPARATOR + INVALID_CARD_TEXT, captor.getValue().carrierText);
167 // There's only one subscription in the list
168 assertEquals(1, captor.getValue().listOfCarriers.length);
169 assertEquals(TEST_CARRIER, captor.getValue().listOfCarriers[0]);
170 }
171
172 @Test
Fabian Kozynskib176f422019-02-05 09:36:59 -0500173 public void testWrongSlots() {
174 reset(mCarrierTextCallback);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700175 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(
Fabian Kozynskib176f422019-02-05 09:36:59 -0500176 new ArrayList<>());
177 when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(
178 IccCardConstants.State.CARD_IO_ERROR);
179 // This should not produce an out of bounds error, even though there are no subscriptions
180 mCarrierTextController.mCallback.onSimStateChanged(0, -3,
181 IccCardConstants.State.CARD_IO_ERROR);
182 mCarrierTextController.mCallback.onSimStateChanged(0, 3, IccCardConstants.State.READY);
183 verify(mCarrierTextCallback, never()).updateCarrierInfo(any());
184 }
185
186 @Test
187 public void testMoreSlotsThanSubs() {
188 reset(mCarrierTextCallback);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700189 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(
Fabian Kozynskib176f422019-02-05 09:36:59 -0500190 new ArrayList<>());
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400191
192 // STOPSHIP(b/130246708) This line makes sure that SubscriptionManager provides the
193 // same answer as KeyguardUpdateMonitor. Remove when this is addressed
194 when(mSubscriptionManager.getActiveSubscriptionInfoList(anyBoolean())).thenReturn(
195 new ArrayList<>());
196
Fabian Kozynskib176f422019-02-05 09:36:59 -0500197 when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(
198 IccCardConstants.State.CARD_IO_ERROR);
199 // This should not produce an out of bounds error, even though there are no subscriptions
200 mCarrierTextController.mCallback.onSimStateChanged(0, 1,
201 IccCardConstants.State.CARD_IO_ERROR);
202
203 mTestableLooper.processAllMessages();
204 verify(mCarrierTextCallback).updateCarrierInfo(
205 any(CarrierTextController.CarrierTextCallbackInfo.class));
206 }
207
208 @Test
209 public void testCallback() {
210 reset(mCarrierTextCallback);
211 mCarrierTextController.postToCallback(mCarrierTextCallbackInfo);
212 mTestableLooper.processAllMessages();
213
214 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
215 ArgumentCaptor.forClass(
216 CarrierTextController.CarrierTextCallbackInfo.class);
217 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
218 assertEquals(mCarrierTextCallbackInfo, captor.getValue());
219 }
220
221 @Test
222 public void testNullingCallback() {
223 reset(mCarrierTextCallback);
224
225 mCarrierTextController.postToCallback(mCarrierTextCallbackInfo);
226 mCarrierTextController.setListening(null);
227
228 // This shouldn't produce NPE
229 mTestableLooper.processAllMessages();
230 verify(mCarrierTextCallback).updateCarrierInfo(any());
231 }
232
233 @Test
234 public void testCreateInfo_OneValidSubscription() {
235 reset(mCarrierTextCallback);
236 List<SubscriptionInfo> list = new ArrayList<>();
237 list.add(TEST_SUBSCRIPTION);
238 when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(IccCardConstants.State.READY);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700239 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400240
Bonian Chena7e9e8c2019-06-02 23:59:31 +0000241 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
242
Fabian Kozynskib176f422019-02-05 09:36:59 -0500243 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
244 ArgumentCaptor.forClass(
245 CarrierTextController.CarrierTextCallbackInfo.class);
246
247 mCarrierTextController.updateCarrierText();
248 mTestableLooper.processAllMessages();
249 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
250
251 CarrierTextController.CarrierTextCallbackInfo info = captor.getValue();
252 assertEquals(1, info.listOfCarriers.length);
253 assertEquals(TEST_CARRIER, info.listOfCarriers[0]);
254 assertEquals(1, info.subscriptionIds.length);
255 }
256
257 @Test
258 public void testCreateInfo_OneValidSubscriptionWithRoaming() {
259 reset(mCarrierTextCallback);
260 List<SubscriptionInfo> list = new ArrayList<>();
261 list.add(TEST_SUBSCRIPTION_ROAMING);
262 when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(IccCardConstants.State.READY);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700263 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400264
Bonian Chena7e9e8c2019-06-02 23:59:31 +0000265 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
266
Fabian Kozynskib176f422019-02-05 09:36:59 -0500267 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
268 ArgumentCaptor.forClass(
269 CarrierTextController.CarrierTextCallbackInfo.class);
270
271 mCarrierTextController.updateCarrierText();
272 mTestableLooper.processAllMessages();
273 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
274
275 CarrierTextController.CarrierTextCallbackInfo info = captor.getValue();
276 assertEquals(1, info.listOfCarriers.length);
277 assertTrue(info.listOfCarriers[0].toString().contains(TEST_CARRIER));
278 assertEquals(1, info.subscriptionIds.length);
279 }
280
281 @Test
282 public void testCreateInfo_noSubscriptions() {
283 reset(mCarrierTextCallback);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700284 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(
Fabian Kozynskib176f422019-02-05 09:36:59 -0500285 new ArrayList<>());
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400286
Fabian Kozynskib176f422019-02-05 09:36:59 -0500287 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
288 ArgumentCaptor.forClass(
289 CarrierTextController.CarrierTextCallbackInfo.class);
290
291 mCarrierTextController.updateCarrierText();
292 mTestableLooper.processAllMessages();
293 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
294
295 CarrierTextController.CarrierTextCallbackInfo info = captor.getValue();
296 assertEquals(0, info.listOfCarriers.length);
297 assertEquals(0, info.subscriptionIds.length);
298
299 }
300
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400301 @Test
302 public void testCarrierText_twoValidSubscriptions() {
303 reset(mCarrierTextCallback);
304 List<SubscriptionInfo> list = new ArrayList<>();
305 list.add(TEST_SUBSCRIPTION);
306 list.add(TEST_SUBSCRIPTION);
307 when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(IccCardConstants.State.READY);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700308 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400309
Bonian Chena7e9e8c2019-06-02 23:59:31 +0000310 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
311
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400312 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
313 ArgumentCaptor.forClass(
314 CarrierTextController.CarrierTextCallbackInfo.class);
315
316 mCarrierTextController.updateCarrierText();
317 mTestableLooper.processAllMessages();
318 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
319
320 assertEquals(TEST_CARRIER + SEPARATOR + TEST_CARRIER,
321 captor.getValue().carrierText);
322 }
323
324 @Test
325 public void testCarrierText_oneDisabledSub() {
326 reset(mCarrierTextCallback);
327 List<SubscriptionInfo> list = new ArrayList<>();
328 list.add(TEST_SUBSCRIPTION);
329 list.add(TEST_SUBSCRIPTION);
330 when(mKeyguardUpdateMonitor.getSimState(anyInt()))
331 .thenReturn(IccCardConstants.State.READY)
332 .thenReturn(IccCardConstants.State.NOT_READY);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700333 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400334
Bonian Chena7e9e8c2019-06-02 23:59:31 +0000335 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
336
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400337 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
338 ArgumentCaptor.forClass(
339 CarrierTextController.CarrierTextCallbackInfo.class);
340
341 mCarrierTextController.updateCarrierText();
342 mTestableLooper.processAllMessages();
343 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
344
345 assertEquals(TEST_CARRIER,
346 captor.getValue().carrierText);
347 }
348
349 @Test
350 public void testCarrierText_firstDisabledSub() {
351 reset(mCarrierTextCallback);
352 List<SubscriptionInfo> list = new ArrayList<>();
353 list.add(TEST_SUBSCRIPTION);
354 list.add(TEST_SUBSCRIPTION);
355 when(mKeyguardUpdateMonitor.getSimState(anyInt()))
356 .thenReturn(IccCardConstants.State.NOT_READY)
357 .thenReturn(IccCardConstants.State.READY);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700358 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400359
Bonian Chena7e9e8c2019-06-02 23:59:31 +0000360 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
361
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400362 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
363 ArgumentCaptor.forClass(
364 CarrierTextController.CarrierTextCallbackInfo.class);
365
366 mCarrierTextController.updateCarrierText();
367 mTestableLooper.processAllMessages();
368 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
369
370 assertEquals(TEST_CARRIER,
371 captor.getValue().carrierText);
372 }
373
374 @Test
375 public void testCarrierText_threeSubsMiddleDisabled() {
376 reset(mCarrierTextCallback);
377 List<SubscriptionInfo> list = new ArrayList<>();
378 list.add(TEST_SUBSCRIPTION);
379 list.add(TEST_SUBSCRIPTION);
380 list.add(TEST_SUBSCRIPTION);
381 when(mKeyguardUpdateMonitor.getSimState(anyInt()))
382 .thenReturn(IccCardConstants.State.READY)
383 .thenReturn(IccCardConstants.State.NOT_READY)
384 .thenReturn(IccCardConstants.State.READY);
Malcolm Chen5c63b512019-08-13 13:24:07 -0700385 when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list);
Bonian Chena7e9e8c2019-06-02 23:59:31 +0000386 mKeyguardUpdateMonitor.mServiceStates = new HashMap<>();
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400387
Fabian Kozynski00d02f12019-04-15 09:48:30 -0400388 ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor =
389 ArgumentCaptor.forClass(
390 CarrierTextController.CarrierTextCallbackInfo.class);
391
392 mCarrierTextController.updateCarrierText();
393 mTestableLooper.processAllMessages();
394 verify(mCarrierTextCallback).updateCarrierInfo(captor.capture());
395
396 assertEquals(TEST_CARRIER + SEPARATOR + TEST_CARRIER,
397 captor.getValue().carrierText);
398 }
399
Fabian Kozynskib176f422019-02-05 09:36:59 -0500400 public static class TestCarrierTextController extends CarrierTextController {
401 private KeyguardUpdateMonitor mKUM;
402
403 public TestCarrierTextController(Context context, CharSequence separator,
404 boolean showAirplaneMode, boolean showMissingSim, KeyguardUpdateMonitor kum) {
405 super(context, separator, showAirplaneMode, showMissingSim);
406 mKUM = kum;
407 }
408
409 @Override
410 public void setListening(CarrierTextCallback callback) {
411 super.setListening(callback);
412 mKeyguardUpdateMonitor = mKUM;
413 }
Fabian Kozynskib176f422019-02-05 09:36:59 -0500414 }
415}