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 | 37916d6 | 2019-11-25 16:50:50 -0800 | [diff] [blame] | 22 | import static android.telephony.SubscriptionManager.NAME_SOURCE_DEFAULT; |
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 | 37916d6 | 2019-11-25 16:50:50 -0800 | [diff] [blame] | 86 | TEST_CARRIER, TEST_CARRIER, NAME_SOURCE_DEFAULT, 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 | 37916d6 | 2019-11-25 16:50:50 -0800 | [diff] [blame] | 90 | TEST_CARRIER, null, NAME_SOURCE_DEFAULT, 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 | 37916d6 | 2019-11-25 16:50:50 -0800 | [diff] [blame] | 93 | TEST_CARRIER, TEST_CARRIER, NAME_SOURCE_DEFAULT, 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 | ba46d1c | 2019-10-03 12:01:38 -0400 | [diff] [blame] | 137 | mDependency.injectTestDependency(KeyguardUpdateMonitor.class, mKeyguardUpdateMonitor); |
| 138 | |
| 139 | doAnswer(this::checkMainThread).when(mKeyguardUpdateMonitor) |
| 140 | .registerCallback(any(KeyguardUpdateMonitorCallback.class)); |
| 141 | doAnswer(this::checkMainThread).when(mKeyguardUpdateMonitor) |
| 142 | .removeCallback(any(KeyguardUpdateMonitorCallback.class)); |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 143 | |
| 144 | mCarrierTextCallbackInfo = new CarrierTextController.CarrierTextCallbackInfo("", |
| 145 | new CharSequence[]{}, false, new int[]{}); |
Malcolm Chen | 32f799d | 2019-10-08 18:11:22 -0700 | [diff] [blame] | 146 | when(mTelephonyManager.getSupportedModemCount()).thenReturn(3); |
Malcolm Chen | b908f9c | 2019-11-04 16:26:28 -0800 | [diff] [blame] | 147 | when(mTelephonyManager.getActiveModemCount()).thenReturn(3); |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 148 | |
Fabian Kozynski | ba46d1c | 2019-10-03 12:01:38 -0400 | [diff] [blame] | 149 | mCarrierTextController = new CarrierTextController(mContext, SEPARATOR, true, true); |
| 150 | // This should not start listening on any of the real dependencies but will test that |
| 151 | // callbacks in mKeyguardUpdateMonitor are done in the mTestableLooper thread |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 152 | mCarrierTextController.setListening(mCarrierTextCallback); |
| 153 | } |
| 154 | |
| 155 | @Test |
Fabian Kozynski | ba46d1c | 2019-10-03 12:01:38 -0400 | [diff] [blame] | 156 | public void testKeyguardUpdateMonitorCalledInMainThread() throws Exception { |
| 157 | // This test will run on the main looper (which is not the same as the looper set as MAIN |
| 158 | // for CarrierTextCallback. This will fail if calls to mKeyguardUpdateMonitor are not done |
| 159 | // through the looper set in the set up |
| 160 | HandlerThread thread = new HandlerThread("testThread", |
| 161 | Process.THREAD_PRIORITY_BACKGROUND); |
| 162 | thread.start(); |
| 163 | TestableLooper testableLooper = new TestableLooper(thread.getLooper()); |
| 164 | Handler h = new Handler(testableLooper.getLooper()); |
| 165 | h.post(() -> { |
| 166 | mCarrierTextController.setListening(null); |
| 167 | mCarrierTextController.setListening(mCarrierTextCallback); |
| 168 | }); |
| 169 | testableLooper.processAllMessages(); |
| 170 | mTestableLooper.processAllMessages(); |
| 171 | thread.quitSafely(); |
| 172 | } |
| 173 | |
| 174 | @Test |
Fabian Kozynski | c3d06f3 | 2019-07-31 14:18:41 -0400 | [diff] [blame] | 175 | public void testAirplaneMode() { |
| 176 | Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1); |
| 177 | reset(mCarrierTextCallback); |
| 178 | List<SubscriptionInfo> list = new ArrayList<>(); |
| 179 | list.add(TEST_SUBSCRIPTION); |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 180 | when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list); |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 181 | when(mKeyguardUpdateMonitor.getSimState(0)).thenReturn(TelephonyManager.SIM_STATE_READY); |
Fabian Kozynski | c3d06f3 | 2019-07-31 14:18:41 -0400 | [diff] [blame] | 182 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
| 183 | |
| 184 | mCarrierTextController.updateCarrierText(); |
| 185 | |
| 186 | ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor = |
| 187 | ArgumentCaptor.forClass( |
| 188 | CarrierTextController.CarrierTextCallbackInfo.class); |
| 189 | |
| 190 | mTestableLooper.processAllMessages(); |
| 191 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 192 | assertEquals(AIRPLANE_MODE_TEXT, captor.getValue().carrierText); |
| 193 | } |
| 194 | |
| 195 | @Test |
| 196 | public void testCardIOError() { |
| 197 | reset(mCarrierTextCallback); |
| 198 | List<SubscriptionInfo> list = new ArrayList<>(); |
| 199 | list.add(TEST_SUBSCRIPTION); |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 200 | when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list); |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 201 | when(mKeyguardUpdateMonitor.getSimState(0)).thenReturn(TelephonyManager.SIM_STATE_READY); |
Fabian Kozynski | c3d06f3 | 2019-07-31 14:18:41 -0400 | [diff] [blame] | 202 | when(mKeyguardUpdateMonitor.getSimState(1)).thenReturn( |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 203 | TelephonyManager.SIM_STATE_CARD_IO_ERROR); |
Fabian Kozynski | c3d06f3 | 2019-07-31 14:18:41 -0400 | [diff] [blame] | 204 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
| 205 | |
| 206 | mCarrierTextController.mCallback.onSimStateChanged(3, 1, |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 207 | TelephonyManager.SIM_STATE_CARD_IO_ERROR); |
Fabian Kozynski | c3d06f3 | 2019-07-31 14:18:41 -0400 | [diff] [blame] | 208 | |
| 209 | ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor = |
| 210 | ArgumentCaptor.forClass( |
| 211 | CarrierTextController.CarrierTextCallbackInfo.class); |
| 212 | |
| 213 | mTestableLooper.processAllMessages(); |
| 214 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 215 | assertEquals("TEST_CARRIER" + SEPARATOR + INVALID_CARD_TEXT, captor.getValue().carrierText); |
| 216 | // There's only one subscription in the list |
| 217 | assertEquals(1, captor.getValue().listOfCarriers.length); |
| 218 | assertEquals(TEST_CARRIER, captor.getValue().listOfCarriers[0]); |
Malcolm Chen | b908f9c | 2019-11-04 16:26:28 -0800 | [diff] [blame] | 219 | |
| 220 | // Now it becomes single SIM active mode. |
| 221 | reset(mCarrierTextCallback); |
| 222 | when(mTelephonyManager.getActiveModemCount()).thenReturn(1); |
| 223 | // Update carrier text. It should ignore error state of subId 3 in inactive slotId. |
| 224 | mCarrierTextController.updateCarrierText(); |
| 225 | mTestableLooper.processAllMessages(); |
| 226 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 227 | assertEquals("TEST_CARRIER", captor.getValue().carrierText); |
Fabian Kozynski | c3d06f3 | 2019-07-31 14:18:41 -0400 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | @Test |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 231 | public void testWrongSlots() { |
| 232 | reset(mCarrierTextCallback); |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 233 | when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn( |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 234 | new ArrayList<>()); |
| 235 | when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn( |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 236 | TelephonyManager.SIM_STATE_CARD_IO_ERROR); |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 237 | // This should not produce an out of bounds error, even though there are no subscriptions |
| 238 | mCarrierTextController.mCallback.onSimStateChanged(0, -3, |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 239 | TelephonyManager.SIM_STATE_CARD_IO_ERROR); |
| 240 | mCarrierTextController.mCallback.onSimStateChanged(0, 3, TelephonyManager.SIM_STATE_READY); |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 241 | verify(mCarrierTextCallback, never()).updateCarrierInfo(any()); |
| 242 | } |
| 243 | |
| 244 | @Test |
| 245 | public void testMoreSlotsThanSubs() { |
| 246 | reset(mCarrierTextCallback); |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 247 | when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn( |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 248 | new ArrayList<>()); |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 249 | |
| 250 | // STOPSHIP(b/130246708) This line makes sure that SubscriptionManager provides the |
| 251 | // same answer as KeyguardUpdateMonitor. Remove when this is addressed |
Amit Mahajan | 75eab47 | 2020-01-29 10:47:48 -0800 | [diff] [blame] | 252 | when(mSubscriptionManager.getActiveAndHiddenSubscriptionInfoList()).thenReturn( |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 253 | new ArrayList<>()); |
| 254 | |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 255 | when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn( |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 256 | TelephonyManager.SIM_STATE_CARD_IO_ERROR); |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 257 | // This should not produce an out of bounds error, even though there are no subscriptions |
| 258 | mCarrierTextController.mCallback.onSimStateChanged(0, 1, |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 259 | TelephonyManager.SIM_STATE_CARD_IO_ERROR); |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 260 | |
| 261 | mTestableLooper.processAllMessages(); |
| 262 | verify(mCarrierTextCallback).updateCarrierInfo( |
| 263 | any(CarrierTextController.CarrierTextCallbackInfo.class)); |
| 264 | } |
| 265 | |
| 266 | @Test |
| 267 | public void testCallback() { |
| 268 | reset(mCarrierTextCallback); |
| 269 | mCarrierTextController.postToCallback(mCarrierTextCallbackInfo); |
| 270 | mTestableLooper.processAllMessages(); |
| 271 | |
| 272 | ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor = |
| 273 | ArgumentCaptor.forClass( |
| 274 | CarrierTextController.CarrierTextCallbackInfo.class); |
| 275 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 276 | assertEquals(mCarrierTextCallbackInfo, captor.getValue()); |
| 277 | } |
| 278 | |
| 279 | @Test |
| 280 | public void testNullingCallback() { |
| 281 | reset(mCarrierTextCallback); |
| 282 | |
| 283 | mCarrierTextController.postToCallback(mCarrierTextCallbackInfo); |
| 284 | mCarrierTextController.setListening(null); |
| 285 | |
| 286 | // This shouldn't produce NPE |
| 287 | mTestableLooper.processAllMessages(); |
| 288 | verify(mCarrierTextCallback).updateCarrierInfo(any()); |
| 289 | } |
| 290 | |
| 291 | @Test |
| 292 | public void testCreateInfo_OneValidSubscription() { |
| 293 | reset(mCarrierTextCallback); |
| 294 | List<SubscriptionInfo> list = new ArrayList<>(); |
| 295 | list.add(TEST_SUBSCRIPTION); |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 296 | when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn( |
| 297 | TelephonyManager.SIM_STATE_READY); |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 298 | when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list); |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 299 | |
Bonian Chen | a7e9e8c | 2019-06-02 23:59:31 +0000 | [diff] [blame] | 300 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
| 301 | |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 302 | ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor = |
| 303 | ArgumentCaptor.forClass( |
| 304 | CarrierTextController.CarrierTextCallbackInfo.class); |
| 305 | |
| 306 | mCarrierTextController.updateCarrierText(); |
| 307 | mTestableLooper.processAllMessages(); |
| 308 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 309 | |
| 310 | CarrierTextController.CarrierTextCallbackInfo info = captor.getValue(); |
| 311 | assertEquals(1, info.listOfCarriers.length); |
| 312 | assertEquals(TEST_CARRIER, info.listOfCarriers[0]); |
| 313 | assertEquals(1, info.subscriptionIds.length); |
| 314 | } |
| 315 | |
| 316 | @Test |
| 317 | public void testCreateInfo_OneValidSubscriptionWithRoaming() { |
| 318 | reset(mCarrierTextCallback); |
| 319 | List<SubscriptionInfo> list = new ArrayList<>(); |
| 320 | list.add(TEST_SUBSCRIPTION_ROAMING); |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 321 | when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn( |
| 322 | TelephonyManager.SIM_STATE_READY); |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 323 | when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list); |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 324 | |
Bonian Chen | a7e9e8c | 2019-06-02 23:59:31 +0000 | [diff] [blame] | 325 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
| 326 | |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 327 | ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor = |
| 328 | ArgumentCaptor.forClass( |
| 329 | CarrierTextController.CarrierTextCallbackInfo.class); |
| 330 | |
| 331 | mCarrierTextController.updateCarrierText(); |
| 332 | mTestableLooper.processAllMessages(); |
| 333 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 334 | |
| 335 | CarrierTextController.CarrierTextCallbackInfo info = captor.getValue(); |
| 336 | assertEquals(1, info.listOfCarriers.length); |
| 337 | assertTrue(info.listOfCarriers[0].toString().contains(TEST_CARRIER)); |
| 338 | assertEquals(1, info.subscriptionIds.length); |
| 339 | } |
| 340 | |
| 341 | @Test |
Fabian Kozynski | 0f49f20 | 2019-08-29 09:47:07 -0400 | [diff] [blame] | 342 | public void testCarrierText_noTextOnReadySimWhenNull() { |
| 343 | reset(mCarrierTextCallback); |
| 344 | List<SubscriptionInfo> list = new ArrayList<>(); |
| 345 | list.add(TEST_SUBSCRIPTION_NULL); |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 346 | when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn( |
| 347 | TelephonyManager.SIM_STATE_READY); |
Fabian Kozynski | 0f49f20 | 2019-08-29 09:47:07 -0400 | [diff] [blame] | 348 | when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list); |
| 349 | |
| 350 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
| 351 | |
| 352 | ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor = |
| 353 | ArgumentCaptor.forClass( |
| 354 | CarrierTextController.CarrierTextCallbackInfo.class); |
| 355 | |
| 356 | mCarrierTextController.updateCarrierText(); |
| 357 | mTestableLooper.processAllMessages(); |
| 358 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 359 | |
| 360 | assertTrue("Carrier text should be empty, instead it's " + captor.getValue().carrierText, |
| 361 | TextUtils.isEmpty(captor.getValue().carrierText)); |
| 362 | assertFalse("No SIM should be available", captor.getValue().anySimReady); |
| 363 | } |
| 364 | |
| 365 | @Test |
| 366 | public void testCarrierText_noTextOnReadySimWhenNull_airplaneMode_wifiOn() { |
| 367 | Settings.Global.putInt(mContext.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1); |
| 368 | reset(mCarrierTextCallback); |
| 369 | List<SubscriptionInfo> list = new ArrayList<>(); |
| 370 | list.add(TEST_SUBSCRIPTION_NULL); |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 371 | when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn( |
| 372 | TelephonyManager.SIM_STATE_READY); |
Fabian Kozynski | 0f49f20 | 2019-08-29 09:47:07 -0400 | [diff] [blame] | 373 | when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list); |
| 374 | mockWifi(); |
| 375 | |
| 376 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
| 377 | ServiceState ss = mock(ServiceState.class); |
SongFerngWang | 2670cba | 2019-12-23 19:21:14 +0800 | [diff] [blame] | 378 | when(ss.getDataRegistrationState()).thenReturn(ServiceState.STATE_IN_SERVICE); |
Fabian Kozynski | 0f49f20 | 2019-08-29 09:47:07 -0400 | [diff] [blame] | 379 | mKeyguardUpdateMonitor.mServiceStates.put(TEST_SUBSCRIPTION_NULL.getSubscriptionId(), ss); |
| 380 | |
| 381 | ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor = |
| 382 | ArgumentCaptor.forClass( |
| 383 | CarrierTextController.CarrierTextCallbackInfo.class); |
| 384 | |
| 385 | mCarrierTextController.updateCarrierText(); |
| 386 | mTestableLooper.processAllMessages(); |
| 387 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 388 | |
| 389 | assertFalse("No SIM should be available", captor.getValue().anySimReady); |
| 390 | // There's no airplane mode if at least one SIM is State.READY and there's wifi |
| 391 | assertFalse("Device should not be in airplane mode", captor.getValue().airplaneMode); |
| 392 | assertNotEquals(AIRPLANE_MODE_TEXT, captor.getValue().carrierText); |
| 393 | } |
| 394 | |
| 395 | private void mockWifi() { |
| 396 | when(mWifiManager.isWifiEnabled()).thenReturn(true); |
| 397 | WifiInfo wifiInfo = mock(WifiInfo.class); |
| 398 | when(wifiInfo.getBSSID()).thenReturn(""); |
| 399 | when(mWifiManager.getConnectionInfo()).thenReturn(wifiInfo); |
| 400 | } |
| 401 | |
| 402 | @Test |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 403 | public void testCreateInfo_noSubscriptions() { |
| 404 | reset(mCarrierTextCallback); |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 405 | when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn( |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 406 | new ArrayList<>()); |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 407 | |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 408 | ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor = |
| 409 | ArgumentCaptor.forClass( |
| 410 | CarrierTextController.CarrierTextCallbackInfo.class); |
| 411 | |
| 412 | mCarrierTextController.updateCarrierText(); |
| 413 | mTestableLooper.processAllMessages(); |
| 414 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 415 | |
| 416 | CarrierTextController.CarrierTextCallbackInfo info = captor.getValue(); |
| 417 | assertEquals(0, info.listOfCarriers.length); |
| 418 | assertEquals(0, info.subscriptionIds.length); |
| 419 | |
| 420 | } |
| 421 | |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 422 | @Test |
| 423 | public void testCarrierText_twoValidSubscriptions() { |
| 424 | reset(mCarrierTextCallback); |
| 425 | List<SubscriptionInfo> list = new ArrayList<>(); |
| 426 | list.add(TEST_SUBSCRIPTION); |
| 427 | list.add(TEST_SUBSCRIPTION); |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 428 | when(mKeyguardUpdateMonitor.getSimState(anyInt())).thenReturn( |
| 429 | TelephonyManager.SIM_STATE_READY); |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 430 | when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list); |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 431 | |
Bonian Chen | a7e9e8c | 2019-06-02 23:59:31 +0000 | [diff] [blame] | 432 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
| 433 | |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 434 | ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor = |
| 435 | ArgumentCaptor.forClass( |
| 436 | CarrierTextController.CarrierTextCallbackInfo.class); |
| 437 | |
| 438 | mCarrierTextController.updateCarrierText(); |
| 439 | mTestableLooper.processAllMessages(); |
| 440 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 441 | |
| 442 | assertEquals(TEST_CARRIER + SEPARATOR + TEST_CARRIER, |
| 443 | captor.getValue().carrierText); |
| 444 | } |
| 445 | |
| 446 | @Test |
| 447 | public void testCarrierText_oneDisabledSub() { |
| 448 | reset(mCarrierTextCallback); |
| 449 | List<SubscriptionInfo> list = new ArrayList<>(); |
| 450 | list.add(TEST_SUBSCRIPTION); |
| 451 | list.add(TEST_SUBSCRIPTION); |
| 452 | when(mKeyguardUpdateMonitor.getSimState(anyInt())) |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 453 | .thenReturn(TelephonyManager.SIM_STATE_READY) |
| 454 | .thenReturn(TelephonyManager.SIM_STATE_NOT_READY); |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 455 | when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list); |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 456 | |
Bonian Chen | a7e9e8c | 2019-06-02 23:59:31 +0000 | [diff] [blame] | 457 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
| 458 | |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 459 | ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor = |
| 460 | ArgumentCaptor.forClass( |
| 461 | CarrierTextController.CarrierTextCallbackInfo.class); |
| 462 | |
| 463 | mCarrierTextController.updateCarrierText(); |
| 464 | mTestableLooper.processAllMessages(); |
| 465 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 466 | |
| 467 | assertEquals(TEST_CARRIER, |
| 468 | captor.getValue().carrierText); |
| 469 | } |
| 470 | |
| 471 | @Test |
| 472 | public void testCarrierText_firstDisabledSub() { |
| 473 | reset(mCarrierTextCallback); |
| 474 | List<SubscriptionInfo> list = new ArrayList<>(); |
| 475 | list.add(TEST_SUBSCRIPTION); |
| 476 | list.add(TEST_SUBSCRIPTION); |
| 477 | when(mKeyguardUpdateMonitor.getSimState(anyInt())) |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 478 | .thenReturn(TelephonyManager.SIM_STATE_NOT_READY) |
| 479 | .thenReturn(TelephonyManager.SIM_STATE_READY); |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 480 | when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list); |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 481 | |
Bonian Chen | a7e9e8c | 2019-06-02 23:59:31 +0000 | [diff] [blame] | 482 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
| 483 | |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 484 | ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor = |
| 485 | ArgumentCaptor.forClass( |
| 486 | CarrierTextController.CarrierTextCallbackInfo.class); |
| 487 | |
| 488 | mCarrierTextController.updateCarrierText(); |
| 489 | mTestableLooper.processAllMessages(); |
| 490 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 491 | |
| 492 | assertEquals(TEST_CARRIER, |
| 493 | captor.getValue().carrierText); |
| 494 | } |
| 495 | |
| 496 | @Test |
| 497 | public void testCarrierText_threeSubsMiddleDisabled() { |
| 498 | reset(mCarrierTextCallback); |
| 499 | List<SubscriptionInfo> list = new ArrayList<>(); |
| 500 | list.add(TEST_SUBSCRIPTION); |
| 501 | list.add(TEST_SUBSCRIPTION); |
| 502 | list.add(TEST_SUBSCRIPTION); |
| 503 | when(mKeyguardUpdateMonitor.getSimState(anyInt())) |
Jayachandran C | f5436a6 | 2019-11-08 18:22:45 -0800 | [diff] [blame] | 504 | .thenReturn(TelephonyManager.SIM_STATE_READY) |
| 505 | .thenReturn(TelephonyManager.SIM_STATE_NOT_READY) |
| 506 | .thenReturn(TelephonyManager.SIM_STATE_READY); |
Malcolm Chen | 5c63b51 | 2019-08-13 13:24:07 -0700 | [diff] [blame] | 507 | when(mKeyguardUpdateMonitor.getFilteredSubscriptionInfo(anyBoolean())).thenReturn(list); |
Bonian Chen | a7e9e8c | 2019-06-02 23:59:31 +0000 | [diff] [blame] | 508 | mKeyguardUpdateMonitor.mServiceStates = new HashMap<>(); |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 509 | |
Fabian Kozynski | 00d02f1 | 2019-04-15 09:48:30 -0400 | [diff] [blame] | 510 | ArgumentCaptor<CarrierTextController.CarrierTextCallbackInfo> captor = |
| 511 | ArgumentCaptor.forClass( |
| 512 | CarrierTextController.CarrierTextCallbackInfo.class); |
| 513 | |
| 514 | mCarrierTextController.updateCarrierText(); |
| 515 | mTestableLooper.processAllMessages(); |
| 516 | verify(mCarrierTextCallback).updateCarrierInfo(captor.capture()); |
| 517 | |
| 518 | assertEquals(TEST_CARRIER + SEPARATOR + TEST_CARRIER, |
| 519 | captor.getValue().carrierText); |
| 520 | } |
Fabian Kozynski | b176f42 | 2019-02-05 09:36:59 -0500 | [diff] [blame] | 521 | } |