Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 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_DEFAULT_SOURCE; |
| 23 | |
| 24 | import static junit.framework.Assert.assertTrue; |
Fabian Kozynski | 4e63d9e | 2019-08-29 09:47:07 -0400 | [diff] [blame] | 25 | import static junit.framework.TestCase.assertFalse; |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 26 | |
| 27 | import static org.junit.Assert.assertEquals; |
Fabian Kozynski | 4e63d9e | 2019-08-29 09:47:07 -0400 | [diff] [blame] | 28 | import static org.junit.Assert.assertNotEquals; |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 29 | import static org.mockito.ArgumentMatchers.any; |
| 30 | import static org.mockito.ArgumentMatchers.anyBoolean; |
| 31 | import static org.mockito.ArgumentMatchers.anyInt; |
Fabian Kozynski | 4e63d9e | 2019-08-29 09:47:07 -0400 | [diff] [blame] | 32 | import static org.mockito.Mockito.mock; |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 33 | import static org.mockito.Mockito.never; |
| 34 | import static org.mockito.Mockito.reset; |
| 35 | import static org.mockito.Mockito.verify; |
| 36 | import static org.mockito.Mockito.when; |
| 37 | |
| 38 | import android.content.Context; |
| 39 | import android.net.ConnectivityManager; |
Fabian Kozynski | 4e63d9e | 2019-08-29 09:47:07 -0400 | [diff] [blame] | 40 | import android.net.wifi.WifiInfo; |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 41 | import android.net.wifi.WifiManager; |
| 42 | import android.os.Handler; |
Fabian Kozynski | c3d06f3 | 2019-07-31 14:18:41 -0400 | [diff] [blame] | 43 | import android.provider.Settings; |
Fabian Kozynski | 4e63d9e | 2019-08-29 09:47:07 -0400 | [diff] [blame] | 44 | import android.telephony.ServiceState; |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 45 | import android.telephony.SubscriptionInfo; |
Malcolm Chen | 06ec357 | 2019-04-09 15:55:29 -0700 | [diff] [blame] | 46 | import android.telephony.SubscriptionManager; |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 47 | import android.telephony.TelephonyManager; |
| 48 | import android.test.suitebuilder.annotation.SmallTest; |
| 49 | import android.testing.AndroidTestingRunner; |
| 50 | import android.testing.TestableLooper; |
Fabian Kozynski | 4e63d9e | 2019-08-29 09:47:07 -0400 | [diff] [blame] | 51 | import android.text.TextUtils; |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 52 | |
| 53 | import com.android.internal.telephony.IccCardConstants; |
| 54 | import com.android.systemui.Dependency; |
Sunny Goyal | 87fccf0 | 2019-08-13 17:39:10 -0700 | [diff] [blame] | 55 | import com.android.systemui.R; |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 56 | import com.android.systemui.SysuiTestCase; |
| 57 | import com.android.systemui.keyguard.WakefulnessLifecycle; |
| 58 | |
| 59 | import org.junit.Before; |
| 60 | import org.junit.Test; |
| 61 | import org.junit.runner.RunWith; |
| 62 | import org.mockito.ArgumentCaptor; |
| 63 | import org.mockito.Mock; |
| 64 | import org.mockito.MockitoAnnotations; |
| 65 | |
| 66 | import java.util.ArrayList; |
Bonian Chen | a7e9e8c | 2019-06-02 23:59:31 +0000 | [diff] [blame] | 67 | import java.util.HashMap; |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 68 | import java.util.List; |
| 69 | |
| 70 | @SmallTest |
| 71 | @RunWith(AndroidTestingRunner.class) |
| 72 | @TestableLooper.RunWithLooper |
| 73 | public class CarrierTextControllerTest extends SysuiTestCase { |
| 74 | |
| 75 | private static final CharSequence SEPARATOR = " \u2014 "; |
Fabian Kozynski | c3d06f3 | 2019-07-31 14:18:41 -0400 | [diff] [blame] | 76 | private static final CharSequence INVALID_CARD_TEXT = "Invalid card"; |
| 77 | private static final CharSequence AIRPLANE_MODE_TEXT = "Airplane mode"; |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 78 | private static final String TEST_CARRIER = "TEST_CARRIER"; |
Sooraj Sasindran | 0d45da7 | 2019-04-25 15:12:21 -0700 | [diff] [blame] | 79 | private static final String TEST_CARRIER_2 = "TEST_CARRIER_2"; |
| 80 | private static final String TEST_GROUP_UUID = "59b5c870-fc4c-47a4-a99e-9db826b48b24"; |
| 81 | private static final int TEST_CARRIER_ID = 1; |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 82 | private static final SubscriptionInfo TEST_SUBSCRIPTION = new SubscriptionInfo(0, "", 0, |
| 83 | TEST_CARRIER, TEST_CARRIER, NAME_SOURCE_DEFAULT_SOURCE, 0xFFFFFF, "", |
Sooraj Sasindran | 0d45da7 | 2019-04-25 15:12:21 -0700 | [diff] [blame] | 84 | DATA_ROAMING_DISABLE, null, null, null, null, false, null, "", false, TEST_GROUP_UUID, |
| 85 | TEST_CARRIER_ID, 0); |
| 86 | private static final SubscriptionInfo TEST_SUBSCRIPTION_2 = new SubscriptionInfo(0, "", 0, |
| 87 | TEST_CARRIER, TEST_CARRIER_2, NAME_SOURCE_DEFAULT_SOURCE, 0xFFFFFF, "", |
| 88 | DATA_ROAMING_DISABLE, null, null, null, null, false, null, "", true, TEST_GROUP_UUID, |
| 89 | TEST_CARRIER_ID, 0); |
Fabian Kozynski | 4e63d9e | 2019-08-29 09:47:07 -0400 | [diff] [blame] | 90 | private static final SubscriptionInfo TEST_SUBSCRIPTION_NULL = new SubscriptionInfo(0, "", 0, |
| 91 | TEST_CARRIER, null, NAME_SOURCE_DEFAULT_SOURCE, 0xFFFFFF, "", DATA_ROAMING_DISABLE, |
| 92 | null, null, null, null, false, null, ""); |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 93 | private static final SubscriptionInfo TEST_SUBSCRIPTION_ROAMING = new SubscriptionInfo(0, "", 0, |
| 94 | TEST_CARRIER, TEST_CARRIER, NAME_SOURCE_DEFAULT_SOURCE, 0xFFFFFF, "", |
| 95 | DATA_ROAMING_ENABLE, null, null, null, null, false, null, ""); |
| 96 | @Mock |
| 97 | private WifiManager mWifiManager; |
| 98 | @Mock |
| 99 | private CarrierTextController.CarrierTextCallback mCarrierTextCallback; |
| 100 | @Mock |
| 101 | private KeyguardUpdateMonitor mKeyguardUpdateMonitor; |
| 102 | @Mock |
| 103 | private ConnectivityManager mConnectivityManager; |
| 104 | @Mock |
| 105 | private TelephonyManager mTelephonyManager; |
Malcolm Chen | 06ec357 | 2019-04-09 15:55:29 -0700 | [diff] [blame] | 106 | @Mock |
| 107 | private SubscriptionManager mSubscriptionManager; |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 108 | private CarrierTextController.CarrierTextCallbackInfo mCarrierTextCallbackInfo; |
| 109 | |
| 110 | private CarrierTextController mCarrierTextController; |
| 111 | private TestableLooper mTestableLooper; |
| 112 | |
| 113 | @Before |
| 114 | public void setUp() { |
| 115 | MockitoAnnotations.initMocks(this); |
| 116 | mTestableLooper = TestableLooper.get(this); |
| 117 | |
| 118 | mContext.addMockSystemService(WifiManager.class, mWifiManager); |
| 119 | mContext.addMockSystemService(ConnectivityManager.class, mConnectivityManager); |
| 120 | mContext.addMockSystemService(TelephonyManager.class, mTelephonyManager); |
Malcolm Chen | 06ec357 | 2019-04-09 15:55:29 -0700 | [diff] [blame] | 121 | mContext.addMockSystemService(SubscriptionManager.class, mSubscriptionManager); |
Fabian Kozynski | c3d06f3 | 2019-07-31 14:18:41 -0400 | [diff] [blame] | 122 | mContext.getOrCreateTestableResources().addOverride( |
| 123 | R.string.keyguard_sim_error_message_short, INVALID_CARD_TEXT); |
| 124 | mContext.getOrCreateTestableResources().addOverride( |
| 125 | R.string.airplane_mode, AIRPLANE_MODE_TEXT); |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 126 | mDependency.injectMockDependency(WakefulnessLifecycle.class); |
| 127 | mDependency.injectTestDependency(Dependency.MAIN_HANDLER, |
| 128 | new Handler(mTestableLooper.getLooper())); |
| 129 | |
| 130 | mCarrierTextCallbackInfo = new CarrierTextController.CarrierTextCallbackInfo("", |
| 131 | new CharSequence[]{}, false, new int[]{}); |
Malcolm Chen | 387dc0e | 2019-10-08 18:11:22 -0700 | [diff] [blame] | 132 | when(mTelephonyManager.getSupportedModemCount()).thenReturn(3); |
Malcolm Chen | d384435 | 2019-11-04 16:26:28 -0800 | [diff] [blame] | 133 | when(mTelephonyManager.getActiveModemCount()).thenReturn(3); |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 134 | |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 135 | mCarrierTextController = new TestCarrierTextController(mContext, SEPARATOR, true, true, |
| 136 | mKeyguardUpdateMonitor); |
| 137 | // This should not start listening on any of the real dependencies |
| 138 | mCarrierTextController.setListening(mCarrierTextCallback); |
Sooraj Sasindran | b7d633b | 2019-05-02 13:47:21 -0700 | [diff] [blame] | 139 | mCarrierTextController.updateDisplayOpportunisticSubscriptionCarrierText(false); |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Test |
Fabian Kozynski | c3d06f3 | 2019-07-31 14:18:41 -0400 | [diff] [blame] | 143 | public void testAirplaneMode() { |
| 144 | Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1); |
| 145 | reset(mCarrierTextCallback); |
| 146 | List<SubscriptionInfo> list = new ArrayList<>(); |
| 147 | list.add(TEST_SUBSCRIPTION); |
| 148 | when(mKeyguardUpdateMonitor.getSubscriptionInfo(anyBoolean())).thenReturn(list); |
| 149 | when(mKeyguardUpdateMonitor.getSimState(0)).thenReturn(IccCardConstants.State.READY); |
| 150 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
| 151 | |
| 152 | mCarrierTextController.updateCarrierText(); |
| 153 | |
| 154 | ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor = |
| 155 | ArgumentCaptor.forClass( |
| 156 | CarrierTextController.CarrierTextCallbackInfo.class); |
| 157 | |
| 158 | mTestableLooper.processAllMessages(); |
| 159 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 160 | assertEquals(AIRPLANE_MODE_TEXT, captor.getValue().carrierText); |
| 161 | } |
| 162 | |
| 163 | @Test |
| 164 | public void testCardIOError() { |
| 165 | reset(mCarrierTextCallback); |
| 166 | List<SubscriptionInfo> list = new ArrayList<>(); |
| 167 | list.add(TEST_SUBSCRIPTION); |
| 168 | when(mKeyguardUpdateMonitor.getSubscriptionInfo(anyBoolean())).thenReturn(list); |
| 169 | when(mKeyguardUpdateMonitor.getSimState(0)).thenReturn(IccCardConstants.State.READY); |
| 170 | when(mKeyguardUpdateMonitor.getSimState(1)).thenReturn( |
| 171 | IccCardConstants.State.CARD_IO_ERROR); |
| 172 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
| 173 | |
| 174 | mCarrierTextController.mCallback.onSimStateChanged(3, 1, |
| 175 | IccCardConstants.State.CARD_IO_ERROR); |
| 176 | |
| 177 | ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor = |
| 178 | ArgumentCaptor.forClass( |
| 179 | CarrierTextController.CarrierTextCallbackInfo.class); |
| 180 | |
| 181 | mTestableLooper.processAllMessages(); |
| 182 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 183 | assertEquals("TEST_CARRIER" + SEPARATOR + INVALID_CARD_TEXT, captor.getValue().carrierText); |
| 184 | // There's only one subscription in the list |
| 185 | assertEquals(1, captor.getValue().listOfCarriers.length); |
| 186 | assertEquals(TEST_CARRIER, captor.getValue().listOfCarriers[0]); |
Malcolm Chen | d384435 | 2019-11-04 16:26:28 -0800 | [diff] [blame] | 187 | |
| 188 | // Now it becomes single SIM active mode. |
| 189 | reset(mCarrierTextCallback); |
| 190 | when(mTelephonyManager.getActiveModemCount()).thenReturn(1); |
| 191 | // Update carrier text. It should ignore error state of subId 3 in inactive slotId. |
| 192 | mCarrierTextController.updateCarrierText(); |
| 193 | mTestableLooper.processAllMessages(); |
| 194 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 195 | assertEquals("TEST_CARRIER", captor.getValue().carrierText); |
Fabian Kozynski | c3d06f3 | 2019-07-31 14:18:41 -0400 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | @Test |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 199 | public void testWrongSlots() { |
| 200 | reset(mCarrierTextCallback); |
| 201 | when(mKeyguardUpdateMonitor.getSubscriptionInfo(anyBoolean())).thenReturn( |
| 202 | new ArrayList<>()); |
| 203 | when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn( |
| 204 | IccCardConstants.State.CARD_IO_ERROR); |
| 205 | // This should not produce an out of bounds error, even though there are no subscriptions |
| 206 | mCarrierTextController.mCallback.onSimStateChanged(0, -3, |
| 207 | IccCardConstants.State.CARD_IO_ERROR); |
| 208 | mCarrierTextController.mCallback.onSimStateChanged(0, 3, IccCardConstants.State.READY); |
| 209 | verify(mCarrierTextCallback, never()).updateCarrierInfo(any()); |
| 210 | } |
| 211 | |
| 212 | @Test |
| 213 | public void testMoreSlotsThanSubs() { |
| 214 | reset(mCarrierTextCallback); |
| 215 | when(mKeyguardUpdateMonitor.getSubscriptionInfo(anyBoolean())).thenReturn( |
| 216 | new ArrayList<>()); |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 217 | |
| 218 | // STOPSHIP(b/130246708) This line makes sure that SubscriptionManager provides the |
| 219 | // same answer as KeyguardUpdateMonitor. Remove when this is addressed |
| 220 | when(mSubscriptionManager.getActiveSubscriptionInfoList(anyBoolean())).thenReturn( |
| 221 | new ArrayList<>()); |
| 222 | |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 223 | when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn( |
| 224 | IccCardConstants.State.CARD_IO_ERROR); |
| 225 | // This should not produce an out of bounds error, even though there are no subscriptions |
| 226 | mCarrierTextController.mCallback.onSimStateChanged(0, 1, |
| 227 | IccCardConstants.State.CARD_IO_ERROR); |
| 228 | |
| 229 | mTestableLooper.processAllMessages(); |
| 230 | verify(mCarrierTextCallback).updateCarrierInfo( |
| 231 | any(CarrierTextController.CarrierTextCallbackInfo.class)); |
| 232 | } |
| 233 | |
| 234 | @Test |
| 235 | public void testCallback() { |
| 236 | reset(mCarrierTextCallback); |
| 237 | mCarrierTextController.postToCallback(mCarrierTextCallbackInfo); |
| 238 | mTestableLooper.processAllMessages(); |
| 239 | |
| 240 | ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor = |
| 241 | ArgumentCaptor.forClass( |
| 242 | CarrierTextController.CarrierTextCallbackInfo.class); |
| 243 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 244 | assertEquals(mCarrierTextCallbackInfo, captor.getValue()); |
| 245 | } |
| 246 | |
| 247 | @Test |
| 248 | public void testNullingCallback() { |
| 249 | reset(mCarrierTextCallback); |
| 250 | |
| 251 | mCarrierTextController.postToCallback(mCarrierTextCallbackInfo); |
| 252 | mCarrierTextController.setListening(null); |
| 253 | |
| 254 | // This shouldn't produce NPE |
| 255 | mTestableLooper.processAllMessages(); |
| 256 | verify(mCarrierTextCallback).updateCarrierInfo(any()); |
| 257 | } |
| 258 | |
| 259 | @Test |
| 260 | public void testCreateInfo_OneValidSubscription() { |
| 261 | reset(mCarrierTextCallback); |
| 262 | List<SubscriptionInfo> list = new ArrayList<>(); |
| 263 | list.add(TEST_SUBSCRIPTION); |
| 264 | when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(IccCardConstants.State.READY); |
| 265 | when(mKeyguardUpdateMonitor.getSubscriptionInfo(anyBoolean())).thenReturn(list); |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 266 | |
Bonian Chen | a7e9e8c | 2019-06-02 23:59:31 +0000 | [diff] [blame] | 267 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
| 268 | |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 269 | ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor = |
| 270 | ArgumentCaptor.forClass( |
| 271 | CarrierTextController.CarrierTextCallbackInfo.class); |
| 272 | |
| 273 | mCarrierTextController.updateCarrierText(); |
| 274 | mTestableLooper.processAllMessages(); |
| 275 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 276 | |
| 277 | CarrierTextController.CarrierTextCallbackInfo info = captor.getValue(); |
| 278 | assertEquals(1, info.listOfCarriers.length); |
| 279 | assertEquals(TEST_CARRIER, info.listOfCarriers[0]); |
| 280 | assertEquals(1, info.subscriptionIds.length); |
| 281 | } |
| 282 | |
| 283 | @Test |
| 284 | public void testCreateInfo_OneValidSubscriptionWithRoaming() { |
| 285 | reset(mCarrierTextCallback); |
| 286 | List<SubscriptionInfo> list = new ArrayList<>(); |
| 287 | list.add(TEST_SUBSCRIPTION_ROAMING); |
| 288 | when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(IccCardConstants.State.READY); |
| 289 | when(mKeyguardUpdateMonitor.getSubscriptionInfo(anyBoolean())).thenReturn(list); |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 290 | |
Bonian Chen | a7e9e8c | 2019-06-02 23:59:31 +0000 | [diff] [blame] | 291 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
| 292 | |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 293 | ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor = |
| 294 | ArgumentCaptor.forClass( |
| 295 | CarrierTextController.CarrierTextCallbackInfo.class); |
| 296 | |
| 297 | mCarrierTextController.updateCarrierText(); |
| 298 | mTestableLooper.processAllMessages(); |
| 299 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 300 | |
| 301 | CarrierTextController.CarrierTextCallbackInfo info = captor.getValue(); |
| 302 | assertEquals(1, info.listOfCarriers.length); |
| 303 | assertTrue(info.listOfCarriers[0].toString().contains(TEST_CARRIER)); |
| 304 | assertEquals(1, info.subscriptionIds.length); |
| 305 | } |
| 306 | |
| 307 | @Test |
Fabian Kozynski | 4e63d9e | 2019-08-29 09:47:07 -0400 | [diff] [blame] | 308 | public void testCarrierText_noTextOnReadySimWhenNull() { |
| 309 | reset(mCarrierTextCallback); |
| 310 | List<SubscriptionInfo> list = new ArrayList<>(); |
| 311 | list.add(TEST_SUBSCRIPTION_NULL); |
| 312 | when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(IccCardConstants.State.READY); |
| 313 | when(mKeyguardUpdateMonitor.getSubscriptionInfo(anyBoolean())).thenReturn(list); |
| 314 | |
| 315 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
| 316 | |
| 317 | ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor = |
| 318 | ArgumentCaptor.forClass( |
| 319 | CarrierTextController.CarrierTextCallbackInfo.class); |
| 320 | |
| 321 | mCarrierTextController.updateCarrierText(); |
| 322 | mTestableLooper.processAllMessages(); |
| 323 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 324 | |
| 325 | assertTrue("Carrier text should be empty, instead it's " + captor.getValue().carrierText, |
| 326 | TextUtils.isEmpty(captor.getValue().carrierText)); |
| 327 | assertFalse("No SIM should be available", captor.getValue().anySimReady); |
| 328 | } |
| 329 | |
| 330 | @Test |
| 331 | public void testCarrierText_noTextOnReadySimWhenNull_airplaneMode_wifiOn() { |
| 332 | Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1); |
| 333 | reset(mCarrierTextCallback); |
| 334 | List<SubscriptionInfo> list = new ArrayList<>(); |
| 335 | list.add(TEST_SUBSCRIPTION_NULL); |
| 336 | when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(IccCardConstants.State.READY); |
| 337 | when(mKeyguardUpdateMonitor.getSubscriptionInfo(anyBoolean())).thenReturn(list); |
| 338 | mockWifi(); |
| 339 | |
| 340 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
| 341 | ServiceState ss = mock(ServiceState.class); |
| 342 | when(ss.getDataRegState()).thenReturn(ServiceState.STATE_IN_SERVICE); |
| 343 | mKeyguardUpdateMonitor.mServiceStates.put(TEST_SUBSCRIPTION_NULL.getSubscriptionId(), ss); |
| 344 | |
| 345 | ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor = |
| 346 | ArgumentCaptor.forClass( |
| 347 | CarrierTextController.CarrierTextCallbackInfo.class); |
| 348 | |
| 349 | mCarrierTextController.updateCarrierText(); |
| 350 | mTestableLooper.processAllMessages(); |
| 351 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 352 | |
| 353 | assertFalse("No SIM should be available", captor.getValue().anySimReady); |
| 354 | // There's no airplane mode if at least one SIM is State.READY and there's wifi |
| 355 | assertFalse("Device should not be in airplane mode", captor.getValue().airplaneMode); |
| 356 | assertNotEquals(AIRPLANE_MODE_TEXT, captor.getValue().carrierText); |
| 357 | } |
| 358 | |
| 359 | private void mockWifi() { |
| 360 | when(mWifiManager.isWifiEnabled()).thenReturn(true); |
| 361 | WifiInfo wifiInfo = mock(WifiInfo.class); |
| 362 | when(wifiInfo.getBSSID()).thenReturn(""); |
| 363 | when(mWifiManager.getConnectionInfo()).thenReturn(wifiInfo); |
| 364 | } |
| 365 | |
| 366 | @Test |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 367 | public void testCreateInfo_noSubscriptions() { |
| 368 | reset(mCarrierTextCallback); |
| 369 | when(mKeyguardUpdateMonitor.getSubscriptionInfo(anyBoolean())).thenReturn( |
| 370 | new ArrayList<>()); |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 371 | |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 372 | ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor = |
| 373 | ArgumentCaptor.forClass( |
| 374 | CarrierTextController.CarrierTextCallbackInfo.class); |
| 375 | |
| 376 | mCarrierTextController.updateCarrierText(); |
| 377 | mTestableLooper.processAllMessages(); |
| 378 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 379 | |
| 380 | CarrierTextController.CarrierTextCallbackInfo info = captor.getValue(); |
| 381 | assertEquals(0, info.listOfCarriers.length); |
| 382 | assertEquals(0, info.subscriptionIds.length); |
| 383 | |
| 384 | } |
| 385 | |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 386 | @Test |
| 387 | public void testCarrierText_twoValidSubscriptions() { |
| 388 | reset(mCarrierTextCallback); |
| 389 | List<SubscriptionInfo> list = new ArrayList<>(); |
| 390 | list.add(TEST_SUBSCRIPTION); |
| 391 | list.add(TEST_SUBSCRIPTION); |
| 392 | when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn(IccCardConstants.State.READY); |
| 393 | when(mKeyguardUpdateMonitor.getSubscriptionInfo(anyBoolean())).thenReturn(list); |
| 394 | |
Bonian Chen | a7e9e8c | 2019-06-02 23:59:31 +0000 | [diff] [blame] | 395 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
| 396 | |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 397 | ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor = |
| 398 | ArgumentCaptor.forClass( |
| 399 | CarrierTextController.CarrierTextCallbackInfo.class); |
| 400 | |
| 401 | mCarrierTextController.updateCarrierText(); |
| 402 | mTestableLooper.processAllMessages(); |
| 403 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 404 | |
| 405 | assertEquals(TEST_CARRIER + SEPARATOR + TEST_CARRIER, |
| 406 | captor.getValue().carrierText); |
| 407 | } |
| 408 | |
| 409 | @Test |
| 410 | public void testCarrierText_oneDisabledSub() { |
| 411 | reset(mCarrierTextCallback); |
| 412 | List<SubscriptionInfo> list = new ArrayList<>(); |
| 413 | list.add(TEST_SUBSCRIPTION); |
| 414 | list.add(TEST_SUBSCRIPTION); |
| 415 | when(mKeyguardUpdateMonitor.getSimState(anyInt())) |
| 416 | .thenReturn(IccCardConstants.State.READY) |
| 417 | .thenReturn(IccCardConstants.State.NOT_READY); |
| 418 | when(mKeyguardUpdateMonitor.getSubscriptionInfo(anyBoolean())).thenReturn(list); |
| 419 | |
Bonian Chen | a7e9e8c | 2019-06-02 23:59:31 +0000 | [diff] [blame] | 420 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
| 421 | |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 422 | ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor = |
| 423 | ArgumentCaptor.forClass( |
| 424 | CarrierTextController.CarrierTextCallbackInfo.class); |
| 425 | |
| 426 | mCarrierTextController.updateCarrierText(); |
| 427 | mTestableLooper.processAllMessages(); |
| 428 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 429 | |
| 430 | assertEquals(TEST_CARRIER, |
| 431 | captor.getValue().carrierText); |
| 432 | } |
| 433 | |
| 434 | @Test |
| 435 | public void testCarrierText_firstDisabledSub() { |
| 436 | reset(mCarrierTextCallback); |
| 437 | List<SubscriptionInfo> list = new ArrayList<>(); |
| 438 | list.add(TEST_SUBSCRIPTION); |
| 439 | list.add(TEST_SUBSCRIPTION); |
| 440 | when(mKeyguardUpdateMonitor.getSimState(anyInt())) |
| 441 | .thenReturn(IccCardConstants.State.NOT_READY) |
| 442 | .thenReturn(IccCardConstants.State.READY); |
| 443 | when(mKeyguardUpdateMonitor.getSubscriptionInfo(anyBoolean())).thenReturn(list); |
| 444 | |
Bonian Chen | a7e9e8c | 2019-06-02 23:59:31 +0000 | [diff] [blame] | 445 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
| 446 | |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 447 | ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor = |
| 448 | ArgumentCaptor.forClass( |
| 449 | CarrierTextController.CarrierTextCallbackInfo.class); |
| 450 | |
| 451 | mCarrierTextController.updateCarrierText(); |
| 452 | mTestableLooper.processAllMessages(); |
| 453 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 454 | |
| 455 | assertEquals(TEST_CARRIER, |
| 456 | captor.getValue().carrierText); |
| 457 | } |
| 458 | |
| 459 | @Test |
| 460 | public void testCarrierText_threeSubsMiddleDisabled() { |
| 461 | reset(mCarrierTextCallback); |
| 462 | List<SubscriptionInfo> list = new ArrayList<>(); |
| 463 | list.add(TEST_SUBSCRIPTION); |
| 464 | list.add(TEST_SUBSCRIPTION); |
| 465 | list.add(TEST_SUBSCRIPTION); |
| 466 | when(mKeyguardUpdateMonitor.getSimState(anyInt())) |
| 467 | .thenReturn(IccCardConstants.State.READY) |
| 468 | .thenReturn(IccCardConstants.State.NOT_READY) |
| 469 | .thenReturn(IccCardConstants.State.READY); |
| 470 | when(mKeyguardUpdateMonitor.getSubscriptionInfo(anyBoolean())).thenReturn(list); |
Bonian Chen | a7e9e8c | 2019-06-02 23:59:31 +0000 | [diff] [blame] | 471 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 472 | |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 473 | ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor = |
| 474 | ArgumentCaptor.forClass( |
| 475 | CarrierTextController.CarrierTextCallbackInfo.class); |
| 476 | |
| 477 | mCarrierTextController.updateCarrierText(); |
| 478 | mTestableLooper.processAllMessages(); |
| 479 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 480 | |
| 481 | assertEquals(TEST_CARRIER + SEPARATOR + TEST_CARRIER, |
| 482 | captor.getValue().carrierText); |
| 483 | } |
| 484 | |
Sooraj Sasindran | 0d45da7 | 2019-04-25 15:12:21 -0700 | [diff] [blame] | 485 | @Test |
| 486 | public void testCarrierText_GroupedSubWithOpportunisticCarrierText() { |
| 487 | reset(mCarrierTextCallback); |
| 488 | List<SubscriptionInfo> list = new ArrayList<>(); |
| 489 | list.add(TEST_SUBSCRIPTION); |
| 490 | list.add(TEST_SUBSCRIPTION_2); |
| 491 | when(mKeyguardUpdateMonitor.getSimState(anyInt())) |
| 492 | .thenReturn(IccCardConstants.State.READY); |
Fabian Kozynski | 8d735cc | 2019-04-29 10:44:10 -0400 | [diff] [blame] | 493 | |
Bonian Chen | a7e9e8c | 2019-06-02 23:59:31 +0000 | [diff] [blame] | 494 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
Sooraj Sasindran | b7d633b | 2019-05-02 13:47:21 -0700 | [diff] [blame] | 495 | mCarrierTextController.updateDisplayOpportunisticSubscriptionCarrierText(true); |
Sooraj Sasindran | 0d45da7 | 2019-04-25 15:12:21 -0700 | [diff] [blame] | 496 | when(mSubscriptionManager.getActiveSubscriptionInfoList(anyBoolean())).thenReturn(list); |
| 497 | |
| 498 | ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor = |
| 499 | ArgumentCaptor.forClass( |
| 500 | CarrierTextController.CarrierTextCallbackInfo.class); |
| 501 | |
| 502 | mCarrierTextController.updateCarrierText(); |
| 503 | mTestableLooper.processAllMessages(); |
| 504 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 505 | |
| 506 | assertEquals(TEST_CARRIER_2, captor.getValue().carrierText); |
| 507 | } |
| 508 | |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 509 | public static class TestCarrierTextController extends CarrierTextController { |
| 510 | private KeyguardUpdateMonitor mKUM; |
| 511 | |
| 512 | public TestCarrierTextController(Context context, CharSequence separator, |
| 513 | boolean showAirplaneMode, boolean showMissingSim, KeyguardUpdateMonitor kum) { |
| 514 | super(context, separator, showAirplaneMode, showMissingSim); |
| 515 | mKUM = kum; |
| 516 | } |
| 517 | |
| 518 | @Override |
| 519 | public void setListening(CarrierTextCallback callback) { |
| 520 | super.setListening(callback); |
| 521 | mKeyguardUpdateMonitor = mKUM; |
| 522 | } |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 523 | } |
| 524 | } |