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; |
Peter Wang | ff7f25f | 2020-01-31 18:46:26 -0800 | [diff] [blame] | 22 | import static android.telephony.SubscriptionManager.NAME_SOURCE_CARRIER_ID; |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 23 | |
| 24 | import static junit.framework.Assert.assertTrue; |
Fabian Kozynski | 0f49f20 | 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 | 0f49f20 | 2019-08-29 09:47:07 -0400 | [diff] [blame] | 28 | import static org.junit.Assert.assertNotEquals; |
Fabian Kozynski | ba46d1c | 2019-10-03 12:01:38 -0400 | [diff] [blame] | 29 | import static org.junit.Assert.fail; |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 30 | import static org.mockito.ArgumentMatchers.any; |
| 31 | import static org.mockito.ArgumentMatchers.anyBoolean; |
| 32 | import static org.mockito.ArgumentMatchers.anyInt; |
Fabian Kozynski | ba46d1c | 2019-10-03 12:01:38 -0400 | [diff] [blame] | 33 | import static org.mockito.Mockito.doAnswer; |
Fabian Kozynski | 0f49f20 | 2019-08-29 09:47:07 -0400 | [diff] [blame] | 34 | import static org.mockito.Mockito.mock; |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 35 | import static org.mockito.Mockito.never; |
| 36 | import static org.mockito.Mockito.reset; |
| 37 | import static org.mockito.Mockito.verify; |
| 38 | import static org.mockito.Mockito.when; |
| 39 | |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 40 | import android.net.ConnectivityManager; |
Fabian Kozynski | 0f49f20 | 2019-08-29 09:47:07 -0400 | [diff] [blame] | 41 | import android.net.wifi.WifiInfo; |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 42 | import android.net.wifi.WifiManager; |
| 43 | import android.os.Handler; |
Fabian Kozynski | ba46d1c | 2019-10-03 12:01:38 -0400 | [diff] [blame] | 44 | import android.os.HandlerThread; |
| 45 | import android.os.Looper; |
| 46 | import android.os.Process; |
Fabian Kozynski | c3d06f3 | 2019-07-31 14:18:41 -0400 | [diff] [blame] | 47 | import android.provider.Settings; |
Fabian Kozynski | 0f49f20 | 2019-08-29 09:47:07 -0400 | [diff] [blame] | 48 | import android.telephony.ServiceState; |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 49 | import android.telephony.SubscriptionInfo; |
Malcolm Chen | 06ec357 | 2019-04-09 15:55:29 -0700 | [diff] [blame] | 50 | import android.telephony.SubscriptionManager; |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 51 | import android.telephony.TelephonyManager; |
| 52 | import android.test.suitebuilder.annotation.SmallTest; |
| 53 | import android.testing.AndroidTestingRunner; |
| 54 | import android.testing.TestableLooper; |
Fabian Kozynski | 0f49f20 | 2019-08-29 09:47:07 -0400 | [diff] [blame] | 55 | import android.text.TextUtils; |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 56 | |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 57 | import com.android.systemui.Dependency; |
Hyunyoung Song | 8f9d34c | 2019-08-30 14:47:43 -0700 | [diff] [blame] | 58 | import com.android.systemui.R; |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 59 | import com.android.systemui.SysuiTestCase; |
| 60 | import com.android.systemui.keyguard.WakefulnessLifecycle; |
| 61 | |
| 62 | import org.junit.Before; |
| 63 | import org.junit.Test; |
| 64 | import org.junit.runner.RunWith; |
| 65 | import org.mockito.ArgumentCaptor; |
| 66 | import org.mockito.Mock; |
| 67 | import org.mockito.MockitoAnnotations; |
Fabian Kozynski | ba46d1c | 2019-10-03 12:01:38 -0400 | [diff] [blame] | 68 | import org.mockito.invocation.InvocationOnMock; |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 69 | |
| 70 | import java.util.ArrayList; |
Bonian Chen | a7e9e8c | 2019-06-02 23:59:31 +0000 | [diff] [blame] | 71 | import java.util.HashMap; |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 72 | import java.util.List; |
| 73 | |
| 74 | @SmallTest |
| 75 | @RunWith(AndroidTestingRunner.class) |
| 76 | @TestableLooper.RunWithLooper |
| 77 | public class CarrierTextControllerTest extends SysuiTestCase { |
| 78 | |
| 79 | private static final CharSequence SEPARATOR = " \u2014 "; |
Fabian Kozynski | c3d06f3 | 2019-07-31 14:18:41 -0400 | [diff] [blame] | 80 | private static final CharSequence INVALID_CARD_TEXT = "Invalid card"; |
| 81 | private static final CharSequence AIRPLANE_MODE_TEXT = "Airplane mode"; |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 82 | private static final String TEST_CARRIER = "TEST_CARRIER"; |
Sooraj Sasindran | 0d45da7 | 2019-04-25 15:12:21 -0700 | [diff] [blame] | 83 | private static final String TEST_CARRIER_2 = "TEST_CARRIER_2"; |
Sooraj Sasindran | 0d45da7 | 2019-04-25 15:12:21 -0700 | [diff] [blame] | 84 | private static final int TEST_CARRIER_ID = 1; |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 85 | private static final SubscriptionInfo TEST_SUBSCRIPTION = new SubscriptionInfo(0, "", 0, |
Peter Wang | ff7f25f | 2020-01-31 18:46:26 -0800 | [diff] [blame] | 86 | TEST_CARRIER, TEST_CARRIER, NAME_SOURCE_CARRIER_ID, 0xFFFFFF, "", |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 87 | DATA_ROAMING_DISABLE, null, null, null, null, false, null, "", false, null, |
Sooraj Sasindran | 0d45da7 | 2019-04-25 15:12:21 -0700 | [diff] [blame] | 88 | TEST_CARRIER_ID, 0); |
Fabian Kozynski | 0f49f20 | 2019-08-29 09:47:07 -0400 | [diff] [blame] | 89 | private static final SubscriptionInfo TEST_SUBSCRIPTION_NULL = new SubscriptionInfo(0, "", 0, |
Peter Wang | ff7f25f | 2020-01-31 18:46:26 -0800 | [diff] [blame] | 90 | TEST_CARRIER, null, NAME_SOURCE_CARRIER_ID, 0xFFFFFF, "", DATA_ROAMING_DISABLE, |
Fabian Kozynski | 0f49f20 | 2019-08-29 09:47:07 -0400 | [diff] [blame] | 91 | null, null, null, null, false, null, ""); |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 92 | private static final SubscriptionInfo TEST_SUBSCRIPTION_ROAMING = new SubscriptionInfo(0, "", 0, |
Peter Wang | ff7f25f | 2020-01-31 18:46:26 -0800 | [diff] [blame] | 93 | TEST_CARRIER, TEST_CARRIER, NAME_SOURCE_CARRIER_ID, 0xFFFFFF, "", |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 94 | 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 Chen | 06ec357 | 2019-04-09 15:55:29 -0700 | [diff] [blame] | 105 | @Mock |
| 106 | private SubscriptionManager mSubscriptionManager; |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 107 | private CarrierTextController.CarrierTextCallbackInfo mCarrierTextCallbackInfo; |
| 108 | |
| 109 | private CarrierTextController mCarrierTextController; |
| 110 | private TestableLooper mTestableLooper; |
| 111 | |
Fabian Kozynski | ba46d1c | 2019-10-03 12:01:38 -0400 | [diff] [blame] | 112 | 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 Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 120 | @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 Kozynski | ba46d1c | 2019-10-03 12:01:38 -0400 | [diff] [blame] | 127 | when(mConnectivityManager.isNetworkSupported(anyInt())).thenReturn(true); |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 128 | mContext.addMockSystemService(TelephonyManager.class, mTelephonyManager); |
Malcolm Chen | 06ec357 | 2019-04-09 15:55:29 -0700 | [diff] [blame] | 129 | mContext.addMockSystemService(SubscriptionManager.class, mSubscriptionManager); |
Fabian Kozynski | c3d06f3 | 2019-07-31 14:18:41 -0400 | [diff] [blame] | 130 | 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 Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 134 | mDependency.injectMockDependency(WakefulnessLifecycle.class); |
| 135 | mDependency.injectTestDependency(Dependency.MAIN_HANDLER, |
| 136 | new Handler(mTestableLooper.getLooper())); |
Fabian Kozynski | 49325ea | 2020-05-12 12:02:40 -0400 | [diff] [blame] | 137 | mDependency.injectTestDependency(Dependency.BG_LOOPER, mTestableLooper.getLooper()); |
Fabian Kozynski | ba46d1c | 2019-10-03 12:01:38 -0400 | [diff] [blame] | 138 | 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 Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 144 | |
| 145 | mCarrierTextCallbackInfo = new CarrierTextController.CarrierTextCallbackInfo("", |
| 146 | new CharSequence[]{}, false, new int[]{}); |
Malcolm Chen | 32f799d | 2019-10-08 18:11:22 -0700 | [diff] [blame] | 147 | when(mTelephonyManager.getSupportedModemCount()).thenReturn(3); |
Malcolm Chen | b908f9c | 2019-11-04 16:26:28 -0800 | [diff] [blame] | 148 | when(mTelephonyManager.getActiveModemCount()).thenReturn(3); |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 149 | |
Fabian Kozynski | ba46d1c | 2019-10-03 12:01:38 -0400 | [diff] [blame] | 150 | 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 Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 153 | mCarrierTextController.setListening(mCarrierTextCallback); |
Fabian Kozynski | 49325ea | 2020-05-12 12:02:40 -0400 | [diff] [blame] | 154 | mTestableLooper.processAllMessages(); |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Test |
Fabian Kozynski | ba46d1c | 2019-10-03 12:01:38 -0400 | [diff] [blame] | 158 | 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 Kozynski | c3d06f3 | 2019-07-31 14:18:41 -0400 | [diff] [blame] | 177 | 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 Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 182 | when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list); |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 183 | when(mKeyguardUpdateMonitor.getSimState(0)).thenReturn(TelephonyManager.SIM_STATE_READY); |
Fabian Kozynski | c3d06f3 | 2019-07-31 14:18:41 -0400 | [diff] [blame] | 184 | 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 Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 202 | when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list); |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 203 | when(mKeyguardUpdateMonitor.getSimState(0)).thenReturn(TelephonyManager.SIM_STATE_READY); |
Fabian Kozynski | c3d06f3 | 2019-07-31 14:18:41 -0400 | [diff] [blame] | 204 | when(mKeyguardUpdateMonitor.getSimState(1)).thenReturn( |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 205 | TelephonyManager.SIM_STATE_CARD_IO_ERROR); |
Fabian Kozynski | c3d06f3 | 2019-07-31 14:18:41 -0400 | [diff] [blame] | 206 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
| 207 | |
| 208 | mCarrierTextController.mCallback.onSimStateChanged(3, 1, |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 209 | TelephonyManager.SIM_STATE_CARD_IO_ERROR); |
Fabian Kozynski | c3d06f3 | 2019-07-31 14:18:41 -0400 | [diff] [blame] | 210 | |
| 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 Chen | b908f9c | 2019-11-04 16:26:28 -0800 | [diff] [blame] | 221 | |
| 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 Kozynski | c3d06f3 | 2019-07-31 14:18:41 -0400 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | @Test |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 233 | public void testWrongSlots() { |
| 234 | reset(mCarrierTextCallback); |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 235 | when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn( |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 236 | new ArrayList<>()); |
| 237 | when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn( |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 238 | TelephonyManager.SIM_STATE_CARD_IO_ERROR); |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 239 | // This should not produce an out of bounds error, even though there are no subscriptions |
| 240 | mCarrierTextController.mCallback.onSimStateChanged(0, -3, |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 241 | TelephonyManager.SIM_STATE_CARD_IO_ERROR); |
| 242 | mCarrierTextController.mCallback.onSimStateChanged(0, 3, TelephonyManager.SIM_STATE_READY); |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 243 | verify(mCarrierTextCallback, never()).updateCarrierInfo(any()); |
| 244 | } |
| 245 | |
| 246 | @Test |
| 247 | public void testMoreSlotsThanSubs() { |
| 248 | reset(mCarrierTextCallback); |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 249 | when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn( |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 250 | new ArrayList<>()); |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 251 | |
| 252 | // STOPSHIP(b/130246708) This line makes sure that SubscriptionManager provides the |
| 253 | // same answer as KeyguardUpdateMonitor. Remove when this is addressed |
Sooraj Sasindran | 4046806 | 2020-01-06 19:10:50 -0800 | [diff] [blame] | 254 | when(mSubscriptionManager.getCompleteActiveSubscriptionInfoList()).thenReturn( |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 255 | new ArrayList<>()); |
| 256 | |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 257 | when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn( |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 258 | TelephonyManager.SIM_STATE_CARD_IO_ERROR); |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 259 | // This should not produce an out of bounds error, even though there are no subscriptions |
| 260 | mCarrierTextController.mCallback.onSimStateChanged(0, 1, |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 261 | TelephonyManager.SIM_STATE_CARD_IO_ERROR); |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 262 | |
| 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 C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 298 | when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn( |
| 299 | TelephonyManager.SIM_STATE_READY); |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 300 | when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list); |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 301 | |
Bonian Chen | a7e9e8c | 2019-06-02 23:59:31 +0000 | [diff] [blame] | 302 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
| 303 | |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 304 | 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 C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 323 | when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn( |
| 324 | TelephonyManager.SIM_STATE_READY); |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 325 | when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list); |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 326 | |
Bonian Chen | a7e9e8c | 2019-06-02 23:59:31 +0000 | [diff] [blame] | 327 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
| 328 | |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 329 | 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 Kozynski | 0f49f20 | 2019-08-29 09:47:07 -0400 | [diff] [blame] | 344 | public void testCarrierText_noTextOnReadySimWhenNull() { |
| 345 | reset(mCarrierTextCallback); |
| 346 | List<SubscriptionInfo> list = new ArrayList<>(); |
| 347 | list.add(TEST_SUBSCRIPTION_NULL); |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 348 | when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn( |
| 349 | TelephonyManager.SIM_STATE_READY); |
Fabian Kozynski | 0f49f20 | 2019-08-29 09:47:07 -0400 | [diff] [blame] | 350 | 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 C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 373 | when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn( |
| 374 | TelephonyManager.SIM_STATE_READY); |
Fabian Kozynski | 0f49f20 | 2019-08-29 09:47:07 -0400 | [diff] [blame] | 375 | when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list); |
| 376 | mockWifi(); |
| 377 | |
| 378 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
| 379 | ServiceState ss = mock(ServiceState.class); |
SongFerngWang | 2670cba | 2019-12-23 19:21:14 +0800 | [diff] [blame] | 380 | when(ss.getDataRegistrationState()).thenReturn(ServiceState.STATE_IN_SERVICE); |
Fabian Kozynski | 0f49f20 | 2019-08-29 09:47:07 -0400 | [diff] [blame] | 381 | 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 Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 405 | public void testCreateInfo_noSubscriptions() { |
| 406 | reset(mCarrierTextCallback); |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 407 | when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn( |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 408 | new ArrayList<>()); |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 409 | |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 410 | 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 Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 424 | @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 C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 430 | when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn( |
| 431 | TelephonyManager.SIM_STATE_READY); |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 432 | when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list); |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 433 | |
Bonian Chen | a7e9e8c | 2019-06-02 23:59:31 +0000 | [diff] [blame] | 434 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
| 435 | |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 436 | 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 C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 455 | .thenReturn(TelephonyManager.SIM_STATE_READY) |
| 456 | .thenReturn(TelephonyManager.SIM_STATE_NOT_READY); |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 457 | when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list); |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 458 | |
Bonian Chen | a7e9e8c | 2019-06-02 23:59:31 +0000 | [diff] [blame] | 459 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
| 460 | |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 461 | 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 C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 480 | .thenReturn(TelephonyManager.SIM_STATE_NOT_READY) |
| 481 | .thenReturn(TelephonyManager.SIM_STATE_READY); |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 482 | when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list); |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 483 | |
Bonian Chen | a7e9e8c | 2019-06-02 23:59:31 +0000 | [diff] [blame] | 484 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
| 485 | |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 486 | 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 C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 506 | .thenReturn(TelephonyManager.SIM_STATE_READY) |
| 507 | .thenReturn(TelephonyManager.SIM_STATE_NOT_READY) |
| 508 | .thenReturn(TelephonyManager.SIM_STATE_READY); |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 509 | when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list); |
Bonian Chen | a7e9e8c | 2019-06-02 23:59:31 +0000 | [diff] [blame] | 510 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 511 | |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 512 | 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 Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 523 | } |