1 /* 2 * Copyright (C) 2016 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.internal.telephony; 18 19 import static com.android.internal.telephony.TelephonyTestUtils.waitForMs; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.junit.Assert.assertEquals; 24 import static org.junit.Assert.assertFalse; 25 import static org.junit.Assert.assertTrue; 26 import static org.mockito.Matchers.any; 27 import static org.mockito.Matchers.anyLong; 28 import static org.mockito.Matchers.nullable; 29 import static org.mockito.Mockito.anyInt; 30 import static org.mockito.Mockito.anyString; 31 import static org.mockito.Mockito.atLeast; 32 import static org.mockito.Mockito.doReturn; 33 import static org.mockito.Mockito.eq; 34 import static org.mockito.Mockito.mock; 35 import static org.mockito.Mockito.never; 36 import static org.mockito.Mockito.spy; 37 import static org.mockito.Mockito.times; 38 import static org.mockito.Mockito.verify; 39 import static org.mockito.Mockito.when; 40 41 import android.app.IAlarmManager; 42 import android.app.Notification; 43 import android.app.NotificationManager; 44 import android.content.ComponentName; 45 import android.content.Context; 46 import android.content.Intent; 47 import android.content.IntentFilter; 48 import android.content.pm.ApplicationInfo; 49 import android.content.pm.ServiceInfo; 50 import android.content.res.Resources; 51 import android.graphics.drawable.Drawable; 52 import android.os.AsyncResult; 53 import android.os.Bundle; 54 import android.os.Handler; 55 import android.os.HandlerThread; 56 import android.os.Looper; 57 import android.os.Message; 58 import android.os.PersistableBundle; 59 import android.os.Process; 60 import android.os.SystemClock; 61 import android.os.UserHandle; 62 import android.os.WorkSource; 63 import android.telephony.AccessNetworkConstants; 64 import android.telephony.CarrierConfigManager; 65 import android.telephony.CellIdentity; 66 import android.telephony.CellIdentityCdma; 67 import android.telephony.CellIdentityGsm; 68 import android.telephony.CellIdentityLte; 69 import android.telephony.CellInfo; 70 import android.telephony.CellInfoGsm; 71 import android.telephony.CellSignalStrength; 72 import android.telephony.CellSignalStrengthCdma; 73 import android.telephony.CellSignalStrengthGsm; 74 import android.telephony.CellSignalStrengthLte; 75 import android.telephony.CellSignalStrengthNr; 76 import android.telephony.CellSignalStrengthTdscdma; 77 import android.telephony.CellSignalStrengthWcdma; 78 import android.telephony.INetworkService; 79 import android.telephony.LteVopsSupportInfo; 80 import android.telephony.NetworkRegistrationInfo; 81 import android.telephony.NetworkService; 82 import android.telephony.PhysicalChannelConfig; 83 import android.telephony.ServiceState; 84 import android.telephony.SignalStrength; 85 import android.telephony.SubscriptionInfo; 86 import android.telephony.SubscriptionManager; 87 import android.telephony.TelephonyManager; 88 import android.telephony.cdma.CdmaCellLocation; 89 import android.telephony.gsm.GsmCellLocation; 90 import android.test.suitebuilder.annotation.MediumTest; 91 import android.test.suitebuilder.annotation.SmallTest; 92 import android.util.Pair; 93 import android.util.TimestampedValue; 94 95 import androidx.test.filters.FlakyTest; 96 97 import com.android.internal.R; 98 import com.android.internal.telephony.cdma.CdmaSubscriptionSourceManager; 99 import com.android.internal.telephony.test.SimulatedCommands; 100 import com.android.internal.telephony.uicc.IccCardApplicationStatus; 101 import com.android.internal.telephony.uicc.IccRecords; 102 103 import org.junit.After; 104 import org.junit.Before; 105 import org.junit.Ignore; 106 import org.junit.Test; 107 import org.mockito.ArgumentCaptor; 108 import org.mockito.Mock; 109 import org.mockito.Mockito; 110 111 import java.util.ArrayList; 112 import java.util.Arrays; 113 import java.util.HashSet; 114 import java.util.List; 115 116 public class ServiceStateTrackerTest extends TelephonyTest { 117 @Mock 118 private ProxyController mProxyController; 119 @Mock 120 private Handler mTestHandler; 121 @Mock 122 protected IAlarmManager mAlarmManager; 123 124 private CellularNetworkService mCellularNetworkService; 125 126 @Mock 127 private NetworkService mIwlanNetworkService; 128 @Mock 129 private INetworkService.Stub mIwlanNetworkServiceStub; 130 131 @Mock 132 private SubscriptionInfo mSubInfo; 133 134 private ServiceStateTracker sst; 135 private ServiceStateTrackerTestHandler mSSTTestHandler; 136 private PersistableBundle mBundle; 137 138 private static final int EVENT_REGISTERED_TO_NETWORK = 1; 139 private static final int EVENT_SUBSCRIPTION_INFO_READY = 2; 140 private static final int EVENT_DATA_ROAMING_ON = 3; 141 private static final int EVENT_DATA_ROAMING_OFF = 4; 142 private static final int EVENT_DATA_CONNECTION_ATTACHED = 5; 143 private static final int EVENT_DATA_CONNECTION_DETACHED = 6; 144 private static final int EVENT_DATA_RAT_CHANGED = 7; 145 private static final int EVENT_PS_RESTRICT_ENABLED = 8; 146 private static final int EVENT_PS_RESTRICT_DISABLED = 9; 147 private static final int EVENT_VOICE_ROAMING_ON = 10; 148 private static final int EVENT_VOICE_ROAMING_OFF = 11; 149 private static final int EVENT_VOICE_RAT_CHANGED = 12; 150 151 private static final int PHONE_ID = 0; 152 153 private static final String CARRIER_NAME_DISPLAY_NO_SERVICE = "no service"; 154 private static final String CARRIER_NAME_DISPLAY_EMERGENCY_CALL = "emergency call"; 155 private static final String WIFI_CALLING_VOICE_FORMAT = "%s wifi calling"; 156 private static final String WIFI_CALLING_DATA_FORMAT = "%s wifi data"; 157 private static final String WIFI_CALLING_FLIGHT_MODE_FORMAT = "%s flight mode"; 158 159 private static final String[] WIFI_CALLING_FORMATTERS = { 160 WIFI_CALLING_VOICE_FORMAT, 161 WIFI_CALLING_DATA_FORMAT, 162 WIFI_CALLING_FLIGHT_MODE_FORMAT }; 163 164 private static final String HOME_PLMN = "310260"; 165 private static final String PLMN1 = "480123"; 166 private static final String PLMN2 = "586111"; 167 private static final String HOME_PNN = "home pnn"; 168 private static final String[] CARRIER_CONFIG_SPDI = new String[] {HOME_PLMN, PLMN2}; 169 private static final String[] CARRIER_CONFIG_EHPLMN = new String[] {HOME_PLMN, PLMN1}; 170 private static final String[] CARRIER_CONFIG_PNN = new String[] { 171 String.format("%s,%s", HOME_PNN, "short"), "f2,s2" 172 }; 173 174 private class ServiceStateTrackerTestHandler extends HandlerThread { 175 ServiceStateTrackerTestHandler(String name)176 private ServiceStateTrackerTestHandler(String name) { 177 super(name); 178 } 179 180 @Override onLooperPrepared()181 public void onLooperPrepared() { 182 sst = new ServiceStateTracker(mPhone, mSimulatedCommands); 183 setReady(true); 184 } 185 } 186 addNetworkService()187 private void addNetworkService() { 188 mCellularNetworkService = new CellularNetworkService(); 189 ServiceInfo CellularServiceInfo = new ServiceInfo(); 190 CellularServiceInfo.packageName = "com.android.phone"; 191 CellularServiceInfo.name = "CellularNetworkService"; 192 CellularServiceInfo.permission = "android.permission.BIND_TELEPHONY_NETWORK_SERVICE"; 193 IntentFilter cellularIntentfilter = new IntentFilter(); 194 mContextFixture.addService( 195 NetworkService.SERVICE_INTERFACE, 196 new ComponentName("com.android.phone", 197 "com.android.internal.telephony.CellularNetworkService"), 198 "com.android.phone", 199 mCellularNetworkService.mBinder, 200 CellularServiceInfo, 201 cellularIntentfilter); 202 203 ServiceInfo iwlanServiceInfo = new ServiceInfo(); 204 iwlanServiceInfo.packageName = "com.xyz.iwlan.networkservice"; 205 iwlanServiceInfo.name = "IwlanNetworkService"; 206 iwlanServiceInfo.permission = "android.permission.BIND_TELEPHONY_NETWORK_SERVICE"; 207 IntentFilter iwlanIntentFilter = new IntentFilter(); 208 mContextFixture.addService( 209 NetworkService.SERVICE_INTERFACE, 210 new ComponentName("com.xyz.iwlan.networkservice", 211 "com.xyz.iwlan.IwlanNetworkService"), 212 "com.xyz.iwlan.networkservice", 213 mIwlanNetworkServiceStub, 214 iwlanServiceInfo, 215 iwlanIntentFilter); 216 } 217 218 @Before setUp()219 public void setUp() throws Exception { 220 221 logd("ServiceStateTrackerTest +Setup!"); 222 super.setUp("ServiceStateTrackerTest"); 223 224 mContextFixture.putResource(R.string.config_wwan_network_service_package, 225 "com.android.phone"); 226 mContextFixture.putResource(R.string.config_wlan_network_service_package, 227 "com.xyz.iwlan.networkservice"); 228 doReturn(mIwlanNetworkServiceStub).when(mIwlanNetworkServiceStub).asBinder(); 229 addNetworkService(); 230 231 doReturn(true).when(mDcTracker).isDisconnected(); 232 233 doReturn(new ServiceState()).when(mPhone).getServiceState(); 234 235 replaceInstance(ProxyController.class, "sProxyController", null, mProxyController); 236 mBundle = mContextFixture.getCarrierConfigBundle(); 237 mBundle.putStringArray( 238 CarrierConfigManager.KEY_ROAMING_OPERATOR_STRING_ARRAY, new String[]{"123456"}); 239 240 mBundle.putStringArray( 241 CarrierConfigManager.KEY_NON_ROAMING_OPERATOR_STRING_ARRAY, new String[]{"123456"}); 242 243 mBundle.putStringArray(CarrierConfigManager.KEY_RATCHET_RAT_FAMILIES, 244 // UMTS < GPRS < EDGE 245 new String[]{"3,1,2"}); 246 247 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME); 248 mSimulatedCommands.setVoiceRadioTech(ServiceState.RIL_RADIO_TECHNOLOGY_HSPA); 249 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME); 250 mSimulatedCommands.setDataRadioTech(ServiceState.RIL_RADIO_TECHNOLOGY_HSPA); 251 252 doReturn(PHONE_ID).when(mPhone).getPhoneId(); 253 254 int dds = SubscriptionManager.getDefaultDataSubscriptionId(); 255 doReturn(dds).when(mPhone).getSubId(); 256 doReturn(true).when(mPhone).areAllDataDisconnected(); 257 258 mSSTTestHandler = new ServiceStateTrackerTestHandler(getClass().getSimpleName()); 259 mSSTTestHandler.start(); 260 waitUntilReady(); 261 waitForMs(600); 262 263 Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED); 264 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, 0); 265 mContext.sendBroadcast(intent); 266 waitForMs(100); 267 268 // Override SPN related resource 269 mContextFixture.putResource( 270 com.android.internal.R.string.lockscreen_carrier_default, 271 CARRIER_NAME_DISPLAY_NO_SERVICE); 272 mContextFixture.putResource( 273 com.android.internal.R.string.emergency_calls_only, 274 CARRIER_NAME_DISPLAY_EMERGENCY_CALL); 275 mContextFixture.putStringArrayResource( 276 com.android.internal.R.array.wfcSpnFormats, 277 WIFI_CALLING_FORMATTERS); 278 279 mBundle.putBoolean( 280 CarrierConfigManager.KEY_ENABLE_CARRIER_DISPLAY_NAME_RESOLVER_BOOL, true); 281 mBundle.putInt(CarrierConfigManager.KEY_WFC_SPN_FORMAT_IDX_INT, 0); 282 mBundle.putInt(CarrierConfigManager.KEY_WFC_DATA_SPN_FORMAT_IDX_INT, 1); 283 mBundle.putInt(CarrierConfigManager.KEY_WFC_FLIGHT_MODE_SPN_FORMAT_IDX_INT, 2); 284 285 // Show SPN is required when roaming 286 // Show PLMN is required when non-roaming. 287 doReturn(IccRecords.CARRIER_NAME_DISPLAY_CONDITION_BITMASK_PLMN 288 | IccRecords.CARRIER_NAME_DISPLAY_CONDITION_BITMASK_SPN) 289 .when(mSimRecords).getCarrierNameDisplayCondition(); 290 291 mBundle.putString(CarrierConfigManager.KEY_CARRIER_NAME_STRING, "SPN from carrier config"); 292 mBundle.putInt(CarrierConfigManager.KEY_SPN_DISPLAY_CONDITION_OVERRIDE_INT, 293 IccRecords.CARRIER_NAME_DISPLAY_CONDITION_BITMASK_PLMN 294 | IccRecords.CARRIER_NAME_DISPLAY_CONDITION_BITMASK_SPN); 295 mBundle.putStringArray(CarrierConfigManager.KEY_SPDI_OVERRIDE_STRING_ARRAY, 296 CARRIER_CONFIG_SPDI); 297 mBundle.putStringArray(CarrierConfigManager.KEY_EHPLMN_OVERRIDE_STRING_ARRAY, 298 CARRIER_CONFIG_EHPLMN); 299 mBundle.putStringArray(CarrierConfigManager.KEY_PNN_OVERRIDE_STRING_ARRAY, 300 CARRIER_CONFIG_PNN); 301 302 // Do not force display "No service" when sim is not ready 303 mContextFixture.putBooleanResource( 304 com.android.internal.R.bool.config_display_no_service_when_sim_unready, false); 305 306 logd("ServiceStateTrackerTest -Setup!"); 307 } 308 309 @After tearDown()310 public void tearDown() throws Exception { 311 sst = null; 312 mSSTTestHandler.quit(); 313 mSSTTestHandler.join(); 314 super.tearDown(); 315 } 316 317 @Test 318 @MediumTest testSetRadioPower()319 public void testSetRadioPower() { 320 boolean oldState = (mSimulatedCommands.getRadioState() == TelephonyManager.RADIO_POWER_ON); 321 sst.setRadioPower(!oldState); 322 waitForMs(100); 323 assertTrue(oldState 324 != (mSimulatedCommands.getRadioState() == TelephonyManager.RADIO_POWER_ON)); 325 } 326 327 @Test 328 @MediumTest testSetRadioPowerFromCarrier()329 public void testSetRadioPowerFromCarrier() { 330 // Carrier disable radio power 331 sst.setRadioPowerFromCarrier(false); 332 waitForMs(100); 333 assertFalse(mSimulatedCommands.getRadioState() 334 == TelephonyManager.RADIO_POWER_ON); 335 assertTrue(sst.getDesiredPowerState()); 336 assertFalse(sst.getPowerStateFromCarrier()); 337 338 // User toggle radio power will not overrides carrier settings 339 sst.setRadioPower(true); 340 waitForMs(100); 341 assertFalse(mSimulatedCommands.getRadioState() 342 == TelephonyManager.RADIO_POWER_ON); 343 assertTrue(sst.getDesiredPowerState()); 344 assertFalse(sst.getPowerStateFromCarrier()); 345 346 // Carrier re-enable radio power 347 sst.setRadioPowerFromCarrier(true); 348 waitForMs(100); 349 assertTrue(mSimulatedCommands.getRadioState() == TelephonyManager.RADIO_POWER_ON); 350 assertTrue(sst.getDesiredPowerState()); 351 assertTrue(sst.getPowerStateFromCarrier()); 352 353 // User toggle radio power off (airplane mode) and set carrier on 354 sst.setRadioPower(false); 355 sst.setRadioPowerFromCarrier(true); 356 waitForMs(100); 357 assertFalse(mSimulatedCommands.getRadioState() 358 == TelephonyManager.RADIO_POWER_ON); 359 assertFalse(sst.getDesiredPowerState()); 360 assertTrue(sst.getPowerStateFromCarrier()); 361 } 362 363 @Test 364 @MediumTest testRilTrafficAfterSetRadioPower()365 public void testRilTrafficAfterSetRadioPower() { 366 sst.setRadioPower(true); 367 final int getOperatorCallCount = mSimulatedCommands.getGetOperatorCallCount(); 368 final int getDataRegistrationStateCallCount = 369 mSimulatedCommands.getGetDataRegistrationStateCallCount(); 370 final int getVoiceRegistrationStateCallCount = 371 mSimulatedCommands.getGetVoiceRegistrationStateCallCount(); 372 final int getNetworkSelectionModeCallCount = 373 mSimulatedCommands.getGetNetworkSelectionModeCallCount(); 374 sst.setRadioPower(false); 375 376 waitForMs(500); 377 sst.pollState(); 378 waitForMs(250); 379 380 // This test was meant to be for *no* ril traffic. However, RADIO_STATE_CHANGED is 381 // considered a modem triggered action and that causes a pollState() to be done 382 assertEquals(getOperatorCallCount + 1, mSimulatedCommands.getGetOperatorCallCount()); 383 assertEquals(getDataRegistrationStateCallCount + 1, 384 mSimulatedCommands.getGetDataRegistrationStateCallCount()); 385 assertEquals(getVoiceRegistrationStateCallCount + 1, 386 mSimulatedCommands.getGetVoiceRegistrationStateCallCount()); 387 assertEquals(getNetworkSelectionModeCallCount + 1, 388 mSimulatedCommands.getGetNetworkSelectionModeCallCount()); 389 390 // Note that if the poll is triggered by a network change notification 391 // and the modem is supposed to be off, we should still do the poll 392 mSimulatedCommands.notifyNetworkStateChanged(); 393 waitForMs(250); 394 395 assertEquals(getOperatorCallCount + 2 , mSimulatedCommands.getGetOperatorCallCount()); 396 assertEquals(getDataRegistrationStateCallCount + 2, 397 mSimulatedCommands.getGetDataRegistrationStateCallCount()); 398 assertEquals(getVoiceRegistrationStateCallCount + 2, 399 mSimulatedCommands.getGetVoiceRegistrationStateCallCount()); 400 assertEquals(getNetworkSelectionModeCallCount + 2, 401 mSimulatedCommands.getGetNetworkSelectionModeCallCount()); 402 } 403 404 @FlakyTest 405 @Ignore 406 @Test 407 @MediumTest testSpnUpdateShowPlmnOnly()408 public void testSpnUpdateShowPlmnOnly() { 409 doReturn(0).when(mSimRecords).getCarrierNameDisplayCondition(); 410 doReturn(IccCardApplicationStatus.AppState.APPSTATE_UNKNOWN). 411 when(mUiccCardApplication3gpp).getState(); 412 413 sst.sendMessage(sst.obtainMessage(ServiceStateTracker.EVENT_NETWORK_STATE_CHANGED, null)); 414 415 waitForMs(750); 416 417 ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class); 418 verify(mContextFixture.getTestDouble(), times(3)) 419 .sendStickyBroadcastAsUser(intentArgumentCaptor.capture(), eq(UserHandle.ALL)); 420 421 // We only want to verify the intent SPN_STRINGS_UPDATED_ACTION. 422 List<Intent> intents = intentArgumentCaptor.getAllValues(); 423 logd("Total " + intents.size() + " intents"); 424 for (Intent intent : intents) { 425 logd(" " + intent.getAction()); 426 } 427 Intent intent = intents.get(2); 428 assertEquals(TelephonyIntents.SPN_STRINGS_UPDATED_ACTION, intent.getAction()); 429 430 Bundle b = intent.getExtras(); 431 432 // For boolean we need to make sure the key exists first 433 assertTrue(b.containsKey(TelephonyIntents.EXTRA_SHOW_SPN)); 434 assertFalse(b.getBoolean(TelephonyIntents.EXTRA_SHOW_SPN)); 435 436 assertEquals(null, b.getString(TelephonyIntents.EXTRA_SPN)); 437 assertEquals(null, b.getString(TelephonyIntents.EXTRA_DATA_SPN)); 438 439 // For boolean we need to make sure the key exists first 440 assertTrue(b.containsKey(TelephonyIntents.EXTRA_SHOW_PLMN)); 441 assertTrue(b.getBoolean(TelephonyIntents.EXTRA_SHOW_PLMN)); 442 443 assertEquals(SimulatedCommands.FAKE_LONG_NAME, b.getString(TelephonyIntents.EXTRA_PLMN)); 444 445 ArgumentCaptor<Integer> intArgumentCaptor = ArgumentCaptor.forClass(Integer.class); 446 verify(mTelephonyManager).setDataNetworkTypeForPhone(anyInt(), intArgumentCaptor.capture()); 447 assertEquals(ServiceState.RIL_RADIO_TECHNOLOGY_HSPA, 448 intArgumentCaptor.getValue().intValue()); 449 } 450 getCellInfoGsm()451 private CellInfoGsm getCellInfoGsm() { 452 CellInfoGsm tmp = new CellInfoGsm(); 453 tmp.setCellIdentity(new CellIdentityGsm(0, 1, 900, 5, "001", "01", "test", "tst")); 454 tmp.setCellSignalStrength(new CellSignalStrengthGsm(-85, 2, 3)); 455 return tmp; 456 } 457 458 @Test 459 @MediumTest testCachedCellInfoList()460 public void testCachedCellInfoList() { 461 ArrayList<CellInfo> list = new ArrayList(); 462 list.add(getCellInfoGsm()); 463 mSimulatedCommands.setCellInfoList(list); 464 465 WorkSource workSource = new WorkSource(Process.myUid(), 466 mContext.getPackageName()); 467 468 // null worksource and no response message will update the writethrough cache 469 sst.requestAllCellInfo(null, null); 470 waitForMs(200); 471 assertEquals(sst.getAllCellInfo(), list); 472 } 473 474 private static class CellInfoHandler extends Handler { 475 // Need to define this here so that it's accessible 476 public List<CellInfo> cellInfoResult; 477 CellInfoHandler(Looper l)478 CellInfoHandler(Looper l) { 479 super(l); 480 } 481 482 @Override handleMessage(Message msg)483 public void handleMessage(Message msg) { 484 synchronized (msg) { 485 assertTrue("handler received null message", msg.obj != null); 486 AsyncResult ar = (AsyncResult) msg.obj; 487 cellInfoResult = (List<CellInfo>) ar.result; 488 msg.notifyAll(); 489 } 490 } 491 } 492 493 @Test 494 @MediumTest testGetCellInfoResponse()495 public void testGetCellInfoResponse() throws InterruptedException { 496 mSimulatedCommands.setCellInfoListBehavior(true); 497 ArrayList<CellInfo> list = new ArrayList(); 498 list.add(getCellInfoGsm()); 499 mSimulatedCommands.setCellInfoList(list); 500 CellInfoHandler cih = new CellInfoHandler(mSSTTestHandler.getLooper()); 501 502 Message rsp = cih.obtainMessage(0x7357); 503 504 sst.requestAllCellInfo(null, rsp); 505 506 synchronized (rsp) { 507 if (cih.cellInfoResult == null) rsp.wait(5000); 508 } 509 510 AsyncResult ar = (AsyncResult) rsp.obj; 511 assertTrue("CellInfo Response Not Received", cih.cellInfoResult != null); 512 assertEquals(getCellInfoGsm(), cih.cellInfoResult.get(0)); 513 } 514 515 @Test 516 @MediumTest testGetCellInfoResponseTimeout()517 public void testGetCellInfoResponseTimeout() throws InterruptedException { 518 mSimulatedCommands.setCellInfoListBehavior(false); 519 CellInfoHandler cih = new CellInfoHandler(mSSTTestHandler.getLooper()); 520 521 Message rsp = cih.obtainMessage(0x7357); 522 523 sst.requestAllCellInfo(null, rsp); 524 525 synchronized (rsp) { 526 if (cih.cellInfoResult == null) rsp.wait(5000); 527 } 528 529 assertTrue("Spurious CellInfo Response Received", cih.cellInfoResult == null); 530 } 531 532 @Test 533 @MediumTest testImsRegState()534 public void testImsRegState() { 535 // Simulate IMS registered 536 mSimulatedCommands.setImsRegistrationState(new int[]{1, PhoneConstants.PHONE_TYPE_GSM}); 537 538 sst.sendMessage(sst.obtainMessage(ServiceStateTracker.EVENT_IMS_STATE_CHANGED, null)); 539 waitForMs(200); 540 541 assertTrue(sst.isImsRegistered()); 542 543 // Simulate IMS unregistered 544 mSimulatedCommands.setImsRegistrationState(new int[]{0, PhoneConstants.PHONE_TYPE_GSM}); 545 546 sst.sendMessage(sst.obtainMessage(ServiceStateTracker.EVENT_IMS_STATE_CHANGED, null)); 547 waitForMs(200); 548 549 assertFalse(sst.isImsRegistered()); 550 } 551 552 @Test testOnImsServiceStateChanged()553 public void testOnImsServiceStateChanged() { 554 // The service state of GsmCdmaPhone is STATE_OUT_OF_SERVICE, and IMS is unregistered. 555 ServiceState ss = new ServiceState(); 556 ss.setVoiceRegState(ServiceState.STATE_OUT_OF_SERVICE); 557 558 sst.mSS = ss; 559 sst.sendMessage(sst.obtainMessage(ServiceStateTracker.EVENT_IMS_SERVICE_STATE_CHANGED)); 560 waitForMs(200); 561 562 // The listener will be notified that the service state was changed. 563 verify(mPhone).notifyServiceStateChanged(any(ServiceState.class)); 564 565 // The service state of GsmCdmaPhone is STATE_IN_SERVICE, and IMS is registered. 566 ss = new ServiceState(); 567 ss.setVoiceRegState(ServiceState.STATE_IN_SERVICE); 568 sst.mSS = ss; 569 570 sst.sendMessage(sst.obtainMessage(ServiceStateTracker.EVENT_IMS_SERVICE_STATE_CHANGED)); 571 waitForMs(200); 572 573 // Nothing happened because the IMS service state was not affected the merged service state. 574 verify(mPhone, times(1)).notifyServiceStateChanged(any(ServiceState.class)); 575 } 576 sendSignalStrength(SignalStrength ss)577 private void sendSignalStrength(SignalStrength ss) { 578 mSimulatedCommands.setSignalStrength(ss); 579 mSimulatedCommands.notifySignalStrength(); 580 waitForMs(300); 581 } 582 583 @Test 584 @MediumTest testSignalStrength()585 public void testSignalStrength() { 586 // Send in GSM Signal Strength Info and expect isGsm == true 587 SignalStrength ss = new SignalStrength( 588 new CellSignalStrengthCdma(), 589 new CellSignalStrengthGsm(-53, 0, SignalStrength.INVALID), 590 new CellSignalStrengthWcdma(), 591 new CellSignalStrengthTdscdma(), 592 new CellSignalStrengthLte(), 593 new CellSignalStrengthNr()); 594 595 sendSignalStrength(ss); 596 assertEquals(sst.getSignalStrength(), ss); 597 assertEquals(sst.getSignalStrength().isGsm(), true); 598 599 // Send in CDMA+LTE Signal Strength Info and expect isGsm == true 600 ss = new SignalStrength( 601 new CellSignalStrengthCdma(-90, -12, 602 SignalStrength.INVALID, SignalStrength.INVALID, SignalStrength.INVALID), 603 new CellSignalStrengthGsm(), 604 new CellSignalStrengthWcdma(), 605 new CellSignalStrengthTdscdma(), 606 new CellSignalStrengthLte( 607 -110, -114, -5, 0, SignalStrength.INVALID, SignalStrength.INVALID), 608 new CellSignalStrengthNr()); 609 610 sendSignalStrength(ss); 611 assertEquals(sst.getSignalStrength(), ss); 612 assertEquals(sst.getSignalStrength().isGsm(), true); 613 614 // Send in CDMA-only Signal Strength Info and expect isGsm == false 615 ss = new SignalStrength( 616 new CellSignalStrengthCdma(-90, -12, 617 SignalStrength.INVALID, SignalStrength.INVALID, SignalStrength.INVALID), 618 new CellSignalStrengthGsm(), 619 new CellSignalStrengthWcdma(), 620 new CellSignalStrengthTdscdma(), 621 new CellSignalStrengthLte(), 622 new CellSignalStrengthNr()); 623 624 sendSignalStrength(ss); 625 assertEquals(sst.getSignalStrength(), ss); 626 assertEquals(sst.getSignalStrength().isGsm(), false); 627 } 628 sendCarrierConfigUpdate()629 private void sendCarrierConfigUpdate() { 630 CarrierConfigManager mockConfigManager = Mockito.mock(CarrierConfigManager.class); 631 when(mContext.getSystemService(Context.CARRIER_CONFIG_SERVICE)) 632 .thenReturn(mockConfigManager); 633 when(mockConfigManager.getConfigForSubId(anyInt())).thenReturn(mBundle); 634 635 Intent intent = new Intent().setAction(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED); 636 intent.putExtra(CarrierConfigManager.EXTRA_SLOT_INDEX, PHONE_ID); 637 mContext.sendBroadcast(intent); 638 waitForMs(300); 639 } 640 641 @Test testLteSignalStrengthReportingCriteria()642 public void testLteSignalStrengthReportingCriteria() { 643 SignalStrength ss = new SignalStrength( 644 new CellSignalStrengthCdma(), 645 new CellSignalStrengthGsm(), 646 new CellSignalStrengthWcdma(), 647 new CellSignalStrengthTdscdma(), 648 new CellSignalStrengthLte( 649 -110, /* rssi */ 650 -114, /* rsrp */ 651 -5, /* rsrq */ 652 0, /* rssnr */ 653 SignalStrength.INVALID, /* cqi */ 654 SignalStrength.INVALID /* ta */), 655 new CellSignalStrengthNr()); 656 657 mBundle.putBoolean(CarrierConfigManager.KEY_USE_ONLY_RSRP_FOR_LTE_SIGNAL_BAR_BOOL, 658 true); 659 660 sendCarrierConfigUpdate(); 661 662 mSimulatedCommands.setSignalStrength(ss); 663 mSimulatedCommands.notifySignalStrength(); 664 waitForMs(300); 665 // Default thresholds are POOR=-115 MODERATE=-105 GOOD=-95 GREAT=-85 666 assertEquals(CellSignalStrength.SIGNAL_STRENGTH_POOR, sst.getSignalStrength().getLevel()); 667 668 int[] lteThresholds = { 669 -130, // SIGNAL_STRENGTH_POOR 670 -120, // SIGNAL_STRENGTH_MODERATE 671 -110, // SIGNAL_STRENGTH_GOOD 672 -100, // SIGNAL_STRENGTH_GREAT 673 }; 674 mBundle.putIntArray(CarrierConfigManager.KEY_LTE_RSRP_THRESHOLDS_INT_ARRAY, 675 lteThresholds); 676 sendCarrierConfigUpdate(); 677 678 mSimulatedCommands.setSignalStrength(ss); 679 mSimulatedCommands.notifySignalStrength(); 680 waitForMs(300); 681 assertEquals(sst.getSignalStrength().getLevel(), 682 CellSignalStrength.SIGNAL_STRENGTH_MODERATE); 683 } 684 685 @Test testWcdmaSignalStrengthReportingCriteria()686 public void testWcdmaSignalStrengthReportingCriteria() { 687 SignalStrength ss = new SignalStrength( 688 new CellSignalStrengthCdma(), 689 new CellSignalStrengthGsm(), 690 new CellSignalStrengthWcdma(-79, 0, -85, -5), 691 new CellSignalStrengthTdscdma(), 692 new CellSignalStrengthLte(), 693 new CellSignalStrengthNr()); 694 695 mSimulatedCommands.setSignalStrength(ss); 696 mSimulatedCommands.notifySignalStrength(); 697 waitForMs(300); 698 assertEquals(sst.getSignalStrength().getLevel(), CellSignalStrength.SIGNAL_STRENGTH_GOOD); 699 700 int[] wcdmaThresholds = { 701 -110, // SIGNAL_STRENGTH_POOR 702 -100, // SIGNAL_STRENGTH_MODERATE 703 -90, // SIGNAL_STRENGTH_GOOD 704 -80 // SIGNAL_STRENGTH_GREAT 705 }; 706 mBundle.putIntArray(CarrierConfigManager.KEY_WCDMA_RSCP_THRESHOLDS_INT_ARRAY, 707 wcdmaThresholds); 708 mBundle.putString( 709 CarrierConfigManager.KEY_WCDMA_DEFAULT_SIGNAL_STRENGTH_MEASUREMENT_STRING, 710 "rscp"); 711 sendCarrierConfigUpdate(); 712 mSimulatedCommands.setSignalStrength(ss); 713 mSimulatedCommands.notifySignalStrength(); 714 waitForMs(300); 715 assertEquals(sst.getSignalStrength().getLevel(), CellSignalStrength.SIGNAL_STRENGTH_GOOD); 716 } 717 718 @Test 719 @MediumTest 720 // TODO(nharold): we probably should remove support for this procedure (GET_LOC) testGsmCellLocation()721 public void testGsmCellLocation() { 722 CellIdentityGsm cellIdentityGsm = new CellIdentityGsm( 723 2, 3, 900, 5, "001", "01", "test", "tst"); 724 725 NetworkRegistrationInfo result = new NetworkRegistrationInfo.Builder() 726 .setDomain(NetworkRegistrationInfo.DOMAIN_CS) 727 .setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WWAN) 728 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME) 729 .setCellIdentity(cellIdentityGsm) 730 .build(); 731 732 sst.sendMessage(sst.obtainMessage(ServiceStateTracker.EVENT_GET_LOC_DONE, 733 new AsyncResult(null, result, null))); 734 735 waitForMs(200); 736 WorkSource workSource = new WorkSource(Process.myUid(), mContext.getPackageName()); 737 GsmCellLocation cl = (GsmCellLocation) sst.getCellLocation(); 738 assertEquals(2, cl.getLac()); 739 assertEquals(3, cl.getCid()); 740 } 741 742 @FlakyTest /* flakes 0.86% of the time */ 743 @Test 744 @MediumTest 745 // TODO(nharold): we probably should remove support for this procedure (GET_LOC) testCdmaCellLocation()746 public void testCdmaCellLocation() { 747 CellIdentityCdma cellIdentityCdma = new CellIdentityCdma(1, 2, 3, 4, 5, "test", "tst"); 748 749 NetworkRegistrationInfo result = new NetworkRegistrationInfo.Builder() 750 .setDomain(NetworkRegistrationInfo.DOMAIN_CS) 751 .setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WWAN) 752 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME) 753 .setCellIdentity(cellIdentityCdma) 754 .build(); 755 756 sst.sendMessage(sst.obtainMessage(ServiceStateTracker.EVENT_GET_LOC_DONE, 757 new AsyncResult(null, result, null))); 758 759 waitForMs(200); 760 WorkSource workSource = new WorkSource(Process.myUid(), mContext.getPackageName()); 761 CdmaCellLocation cl = (CdmaCellLocation) sst.getCellLocation(); 762 assertEquals(5, cl.getBaseStationLatitude()); 763 assertEquals(4, cl.getBaseStationLongitude()); 764 } 765 766 @Test 767 @MediumTest testUpdatePhoneType()768 public void testUpdatePhoneType() { 769 doReturn(false).when(mPhone).isPhoneTypeGsm(); 770 doReturn(true).when(mPhone).isPhoneTypeCdmaLte(); 771 doReturn(CdmaSubscriptionSourceManager.SUBSCRIPTION_FROM_RUIM).when(mCdmaSSM). 772 getCdmaSubscriptionSource(); 773 774 logd("Calling updatePhoneType"); 775 // switch to CDMA 776 sst.updatePhoneType(); 777 778 ArgumentCaptor<Integer> integerArgumentCaptor = ArgumentCaptor.forClass(Integer.class); 779 verify(mRuimRecords).registerForRecordsLoaded(eq(sst), integerArgumentCaptor.capture(), 780 nullable(Object.class)); 781 782 // response for mRuimRecords.registerForRecordsLoaded() 783 Message msg = Message.obtain(); 784 msg.what = integerArgumentCaptor.getValue(); 785 msg.obj = new AsyncResult(null, null, null); 786 sst.sendMessage(msg); 787 waitForMs(100); 788 789 // on RUIM_RECORDS_LOADED, sst is expected to call following apis 790 verify(mRuimRecords, times(1)).isProvisioned(); 791 792 // switch back to GSM 793 doReturn(true).when(mPhone).isPhoneTypeGsm(); 794 doReturn(false).when(mPhone).isPhoneTypeCdmaLte(); 795 796 // response for mRuimRecords.registerForRecordsLoaded() can be sent after switching to GSM 797 msg = Message.obtain(); 798 msg.what = integerArgumentCaptor.getValue(); 799 msg.obj = new AsyncResult(null, null, null); 800 sst.sendMessage(msg); 801 802 // There's no easy way to check if the msg was handled or discarded. Wait to make sure sst 803 // did not crash, and then verify that the functions called records loaded are not called 804 // again 805 waitForMs(200); 806 807 verify(mRuimRecords, times(1)).isProvisioned(); 808 } 809 810 @Test 811 @MediumTest testRegAndUnregForVoiceRoamingOn()812 public void testRegAndUnregForVoiceRoamingOn() throws Exception { 813 sst.registerForVoiceRoamingOn(mTestHandler, EVENT_DATA_ROAMING_ON, null); 814 815 // Enable roaming and trigger events to notify handler registered 816 doReturn(true).when(mPhone).isPhoneTypeGsm(); 817 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 818 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 819 mSimulatedCommands.notifyNetworkStateChanged(); 820 821 waitForMs(200); 822 823 // verify if registered handler has message posted to it 824 ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); 825 verify(mTestHandler).sendMessageAtTime(messageArgumentCaptor.capture(), anyLong()); 826 assertEquals(EVENT_DATA_ROAMING_ON, messageArgumentCaptor.getValue().what); 827 828 // Disable roaming 829 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME); 830 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME); 831 mSimulatedCommands.notifyNetworkStateChanged(); 832 833 waitForMs(100); 834 835 // Unregister registrant 836 sst.unregisterForVoiceRoamingOn(mTestHandler); 837 838 // Enable roaming 839 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 840 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 841 mSimulatedCommands.notifyNetworkStateChanged(); 842 843 waitForMs(200); 844 845 // verify that no new message posted to handler 846 verify(mTestHandler, times(1)).sendMessageAtTime(any(Message.class), anyLong()); 847 } 848 849 @Test 850 @MediumTest testRegAndUnregForVoiceRoamingOff()851 public void testRegAndUnregForVoiceRoamingOff() throws Exception { 852 // Enable roaming 853 doReturn(true).when(mPhone).isPhoneTypeGsm(); 854 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 855 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 856 mSimulatedCommands.notifyNetworkStateChanged(); 857 858 waitForMs(100); 859 860 sst.registerForVoiceRoamingOff(mTestHandler, EVENT_DATA_ROAMING_OFF, null); 861 862 // Disable roaming 863 doReturn(true).when(mPhone).isPhoneTypeGsm(); 864 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME); 865 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME); 866 mSimulatedCommands.notifyNetworkStateChanged(); 867 868 waitForMs(200); 869 870 // verify if registered handler has message posted to it 871 ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); 872 verify(mTestHandler).sendMessageAtTime(messageArgumentCaptor.capture(), anyLong()); 873 assertEquals(EVENT_DATA_ROAMING_OFF, messageArgumentCaptor.getValue().what); 874 875 // Enable roaming 876 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 877 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 878 mSimulatedCommands.notifyNetworkStateChanged(); 879 880 waitForMs(100); 881 882 // Unregister registrant 883 sst.unregisterForVoiceRoamingOff(mTestHandler); 884 885 // Disable roaming 886 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME); 887 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME); 888 mSimulatedCommands.notifyNetworkStateChanged(); 889 890 waitForMs(100); 891 892 // verify that no new message posted to handler 893 verify(mTestHandler, times(1)).sendMessageAtTime(any(Message.class), anyLong()); 894 } 895 896 @Test 897 @MediumTest testRegAndUnregForDataRoamingOn()898 public void testRegAndUnregForDataRoamingOn() throws Exception { 899 sst.registerForDataRoamingOn(mTestHandler, EVENT_DATA_ROAMING_ON, null); 900 901 // Enable roaming and trigger events to notify handler registered 902 doReturn(true).when(mPhone).isPhoneTypeGsm(); 903 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 904 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 905 mSimulatedCommands.notifyNetworkStateChanged(); 906 907 waitForMs(200); 908 909 // verify if registered handler has message posted to it 910 ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); 911 verify(mTestHandler).sendMessageAtTime(messageArgumentCaptor.capture(), anyLong()); 912 assertEquals(EVENT_DATA_ROAMING_ON, messageArgumentCaptor.getValue().what); 913 914 // Disable roaming 915 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME); 916 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME); 917 mSimulatedCommands.notifyNetworkStateChanged(); 918 919 waitForMs(100); 920 921 // Unregister registrant 922 sst.unregisterForDataRoamingOn(mTestHandler); 923 924 // Enable roaming 925 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 926 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 927 mSimulatedCommands.notifyNetworkStateChanged(); 928 929 waitForMs(200); 930 931 // verify that no new message posted to handler 932 verify(mTestHandler, times(1)).sendMessageAtTime(any(Message.class), anyLong()); 933 } 934 935 @Test 936 @MediumTest testRegAndUnregForDataRoamingOff()937 public void testRegAndUnregForDataRoamingOff() throws Exception { 938 // Enable roaming 939 doReturn(true).when(mPhone).isPhoneTypeGsm(); 940 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 941 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 942 mSimulatedCommands.notifyNetworkStateChanged(); 943 944 waitForMs(100); 945 946 sst.registerForDataRoamingOff(mTestHandler, EVENT_DATA_ROAMING_OFF, null, true); 947 948 // Disable roaming 949 doReturn(true).when(mPhone).isPhoneTypeGsm(); 950 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME); 951 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME); 952 mSimulatedCommands.notifyNetworkStateChanged(); 953 954 waitForMs(100); 955 956 // verify if registered handler has message posted to it 957 ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); 958 verify(mTestHandler).sendMessageAtTime(messageArgumentCaptor.capture(), anyLong()); 959 assertEquals(EVENT_DATA_ROAMING_OFF, messageArgumentCaptor.getValue().what); 960 961 // Enable roaming 962 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 963 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 964 mSimulatedCommands.notifyNetworkStateChanged(); 965 966 waitForMs(100); 967 968 // Unregister registrant 969 sst.unregisterForDataRoamingOff(mTestHandler); 970 971 // Disable roaming 972 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME); 973 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME); 974 mSimulatedCommands.notifyNetworkStateChanged(); 975 976 waitForMs(100); 977 978 // verify that no new message posted to handler 979 verify(mTestHandler, times(1)).sendMessageAtTime(any(Message.class), anyLong()); 980 } 981 982 @Test 983 @MediumTest testRegAndInvalidregForDataConnAttach()984 public void testRegAndInvalidregForDataConnAttach() throws Exception { 985 // Initially set service state out of service 986 doReturn(true).when(mPhone).isPhoneTypeGsm(); 987 mSimulatedCommands.setVoiceRegState(23); 988 mSimulatedCommands.setDataRegState(23); 989 mSimulatedCommands.notifyNetworkStateChanged(); 990 991 waitForMs(100); 992 993 sst.registerForDataConnectionAttached(AccessNetworkConstants.TRANSPORT_TYPE_WWAN, 994 mTestHandler, EVENT_DATA_CONNECTION_ATTACHED, null); 995 996 // set service state in service and trigger events to post message on handler 997 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 998 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 999 mSimulatedCommands.notifyNetworkStateChanged(); 1000 1001 waitForMs(200); 1002 1003 // verify if registered handler has message posted to it 1004 ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); 1005 verify(mTestHandler).sendMessageAtTime(messageArgumentCaptor.capture(), anyLong()); 1006 assertEquals(EVENT_DATA_CONNECTION_ATTACHED, messageArgumentCaptor.getValue().what); 1007 1008 // set service state out of service 1009 mSimulatedCommands.setVoiceRegState(-1); 1010 mSimulatedCommands.setDataRegState(-1); 1011 mSimulatedCommands.notifyNetworkStateChanged(); 1012 1013 waitForMs(100); 1014 1015 // Unregister registrant 1016 sst.unregisterForDataConnectionAttached(AccessNetworkConstants.TRANSPORT_TYPE_WWAN, 1017 mTestHandler); 1018 1019 // set service state in service 1020 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 1021 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 1022 mSimulatedCommands.notifyNetworkStateChanged(); 1023 1024 waitForMs(100); 1025 1026 // verify that no new message posted to handler 1027 verify(mTestHandler, times(1)).sendMessageAtTime(any(Message.class), anyLong()); 1028 } 1029 1030 @Test 1031 @MediumTest testRegAndUnregForDataConnAttach()1032 public void testRegAndUnregForDataConnAttach() throws Exception { 1033 // Initially set service state out of service 1034 doReturn(true).when(mPhone).isPhoneTypeGsm(); 1035 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_UNKNOWN); 1036 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_UNKNOWN); 1037 mSimulatedCommands.notifyNetworkStateChanged(); 1038 1039 waitForMs(100); 1040 1041 sst.registerForDataConnectionAttached(AccessNetworkConstants.TRANSPORT_TYPE_WWAN, 1042 mTestHandler, EVENT_DATA_CONNECTION_ATTACHED, null); 1043 1044 // set service state in service and trigger events to post message on handler 1045 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 1046 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 1047 mSimulatedCommands.notifyNetworkStateChanged(); 1048 1049 waitForMs(200); 1050 1051 // verify if registered handler has message posted to it 1052 ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); 1053 verify(mTestHandler).sendMessageAtTime(messageArgumentCaptor.capture(), anyLong()); 1054 assertEquals(EVENT_DATA_CONNECTION_ATTACHED, messageArgumentCaptor.getValue().what); 1055 1056 // set service state out of service 1057 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_UNKNOWN); 1058 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_UNKNOWN); 1059 mSimulatedCommands.notifyNetworkStateChanged(); 1060 1061 waitForMs(100); 1062 1063 // Unregister registrant 1064 sst.unregisterForDataConnectionAttached(AccessNetworkConstants.TRANSPORT_TYPE_WWAN, 1065 mTestHandler); 1066 1067 // set service state in service 1068 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 1069 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 1070 mSimulatedCommands.notifyNetworkStateChanged(); 1071 1072 waitForMs(100); 1073 1074 // verify that no new message posted to handler 1075 verify(mTestHandler, times(1)).sendMessageAtTime(any(Message.class), anyLong()); 1076 } 1077 1078 @Test 1079 @MediumTest testRegAndUnregForDataConnDetach()1080 public void testRegAndUnregForDataConnDetach() throws Exception { 1081 // Initially set service state in service 1082 doReturn(true).when(mPhone).isPhoneTypeGsm(); 1083 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 1084 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 1085 mSimulatedCommands.notifyNetworkStateChanged(); 1086 1087 sst.registerForDataConnectionDetached(AccessNetworkConstants.TRANSPORT_TYPE_WWAN, 1088 mTestHandler, EVENT_DATA_CONNECTION_DETACHED, null); 1089 1090 // set service state out of service and trigger events to post message on handler 1091 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_UNKNOWN); 1092 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_UNKNOWN); 1093 mSimulatedCommands.notifyNetworkStateChanged(); 1094 1095 waitForMs(200); 1096 1097 // verify if registered handler has message posted to it 1098 ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); 1099 verify(mTestHandler).sendMessageAtTime(messageArgumentCaptor.capture(), anyLong()); 1100 assertEquals(EVENT_DATA_CONNECTION_DETACHED, messageArgumentCaptor.getValue().what); 1101 1102 // set service state in service 1103 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 1104 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 1105 mSimulatedCommands.notifyNetworkStateChanged(); 1106 1107 waitForMs(100); 1108 1109 // Unregister registrant 1110 sst.unregisterForDataConnectionDetached(AccessNetworkConstants.TRANSPORT_TYPE_WWAN, 1111 mTestHandler); 1112 1113 // set service state out of service 1114 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_UNKNOWN); 1115 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_UNKNOWN); 1116 mSimulatedCommands.notifyNetworkStateChanged(); 1117 1118 waitForMs(100); 1119 1120 // verify that no new message posted to handler 1121 verify(mTestHandler, times(1)).sendMessageAtTime(any(Message.class), anyLong()); 1122 } 1123 1124 @Test 1125 @MediumTest testRegisterForVoiceRegStateOrRatChange()1126 public void testRegisterForVoiceRegStateOrRatChange() { 1127 NetworkRegistrationInfo nri = new NetworkRegistrationInfo.Builder() 1128 .setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WWAN) 1129 .setDomain(NetworkRegistrationInfo.DOMAIN_CS) 1130 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_LTE) 1131 .build(); 1132 sst.mSS.addNetworkRegistrationInfo(nri); 1133 1134 sst.registerForVoiceRegStateOrRatChanged(mTestHandler, EVENT_VOICE_RAT_CHANGED, null); 1135 1136 waitForMs(100); 1137 1138 // Verify if message was posted to handler and value of result 1139 ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); 1140 verify(mTestHandler).sendMessageAtTime(messageArgumentCaptor.capture(), anyLong()); 1141 assertEquals(EVENT_VOICE_RAT_CHANGED, messageArgumentCaptor.getValue().what); 1142 assertEquals(new Pair<Integer, Integer>(ServiceState.STATE_IN_SERVICE, 1143 ServiceState.RIL_RADIO_TECHNOLOGY_LTE), 1144 ((AsyncResult)messageArgumentCaptor.getValue().obj).result); 1145 } 1146 1147 @Test 1148 @MediumTest testRegisterForDataRegStateOrRatChange()1149 public void testRegisterForDataRegStateOrRatChange() { 1150 NetworkRegistrationInfo nrs = new NetworkRegistrationInfo.Builder() 1151 .setDomain(NetworkRegistrationInfo.DOMAIN_PS) 1152 .setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WWAN) 1153 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_LTE) 1154 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME) 1155 .build(); 1156 sst.mSS.addNetworkRegistrationInfo(nrs); 1157 sst.registerForDataRegStateOrRatChanged(AccessNetworkConstants.TRANSPORT_TYPE_WWAN, 1158 mTestHandler, EVENT_DATA_RAT_CHANGED, null); 1159 1160 waitForMs(100); 1161 1162 // Verify if message was posted to handler and value of result 1163 ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); 1164 verify(mTestHandler).sendMessageAtTime(messageArgumentCaptor.capture(), anyLong()); 1165 assertEquals(EVENT_DATA_RAT_CHANGED, messageArgumentCaptor.getValue().what); 1166 assertEquals(new Pair<Integer, Integer>(ServiceState.STATE_IN_SERVICE, 1167 ServiceState.RIL_RADIO_TECHNOLOGY_LTE), 1168 ((AsyncResult)messageArgumentCaptor.getValue().obj).result); 1169 } 1170 1171 @FlakyTest /* flakes 0.43% of the time */ 1172 @Test 1173 @MediumTest testRegAndUnregForNetworkAttached()1174 public void testRegAndUnregForNetworkAttached() throws Exception { 1175 // Initially set service state out of service 1176 doReturn(true).when(mPhone).isPhoneTypeGsm(); 1177 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_UNKNOWN); 1178 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_UNKNOWN); 1179 mSimulatedCommands.notifyNetworkStateChanged(); 1180 1181 waitForMs(100); 1182 1183 sst.registerForNetworkAttached(mTestHandler, EVENT_REGISTERED_TO_NETWORK, null); 1184 1185 // set service state in service and trigger events to post message on handler 1186 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 1187 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 1188 mSimulatedCommands.notifyNetworkStateChanged(); 1189 1190 waitForMs(100); 1191 1192 // verify if registered handler has message posted to it 1193 ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); 1194 verify(mTestHandler).sendMessageAtTime(messageArgumentCaptor.capture(), anyLong()); 1195 assertEquals(EVENT_REGISTERED_TO_NETWORK, messageArgumentCaptor.getValue().what); 1196 1197 // set service state out of service 1198 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_UNKNOWN); 1199 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_UNKNOWN); 1200 mSimulatedCommands.notifyNetworkStateChanged(); 1201 1202 waitForMs(100); 1203 1204 // Unregister registrant 1205 sst.unregisterForNetworkAttached(mTestHandler); 1206 1207 // set service state in service 1208 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 1209 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 1210 mSimulatedCommands.notifyNetworkStateChanged(); 1211 1212 waitForMs(100); 1213 1214 // verify that no new message posted to handler 1215 verify(mTestHandler, times(1)).sendMessageAtTime(any(Message.class), anyLong()); 1216 } 1217 1218 @Test 1219 @MediumTest testRegAndInvalidRegForNetworkAttached()1220 public void testRegAndInvalidRegForNetworkAttached() throws Exception { 1221 // Initially set service state out of service 1222 doReturn(true).when(mPhone).isPhoneTypeGsm(); 1223 mSimulatedCommands.setVoiceRegState(23); 1224 mSimulatedCommands.setDataRegState(23); 1225 mSimulatedCommands.notifyNetworkStateChanged(); 1226 1227 waitForMs(100); 1228 1229 sst.registerForNetworkAttached(mTestHandler, EVENT_REGISTERED_TO_NETWORK, null); 1230 1231 // set service state in service and trigger events to post message on handler 1232 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 1233 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 1234 mSimulatedCommands.notifyNetworkStateChanged(); 1235 1236 waitForMs(100); 1237 1238 // verify if registered handler has message posted to it 1239 ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); 1240 verify(mTestHandler).sendMessageAtTime(messageArgumentCaptor.capture(), anyLong()); 1241 assertEquals(EVENT_REGISTERED_TO_NETWORK, messageArgumentCaptor.getValue().what); 1242 1243 // set service state out of service 1244 mSimulatedCommands.setVoiceRegState(-1); 1245 mSimulatedCommands.setDataRegState(-1); 1246 mSimulatedCommands.notifyNetworkStateChanged(); 1247 1248 waitForMs(100); 1249 1250 // Unregister registrant 1251 sst.unregisterForNetworkAttached(mTestHandler); 1252 1253 1254 waitForMs(100); 1255 1256 sst.registerForNetworkAttached(mTestHandler, EVENT_REGISTERED_TO_NETWORK, null); 1257 1258 // set service state in service 1259 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 1260 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 1261 mSimulatedCommands.notifyNetworkStateChanged(); 1262 1263 waitForMs(100); 1264 1265 // verify if registered handler has message posted to it 1266 messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); 1267 verify(mTestHandler, times(2)).sendMessageAtTime(messageArgumentCaptor.capture(), 1268 anyLong()); 1269 assertEquals(EVENT_REGISTERED_TO_NETWORK, messageArgumentCaptor.getValue().what); 1270 } 1271 1272 @Test 1273 @MediumTest testRegisterForPsRestrictedEnabled()1274 public void testRegisterForPsRestrictedEnabled() throws Exception { 1275 sst.mRestrictedState.setPsRestricted(true); 1276 // Since PsRestricted is set to true, registerForPsRestrictedEnabled will 1277 // also post message to handler 1278 sst.registerForPsRestrictedEnabled(mTestHandler, EVENT_PS_RESTRICT_ENABLED, null); 1279 1280 waitForMs(100); 1281 1282 // verify posted message 1283 ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); 1284 verify(mTestHandler).sendMessageAtTime(messageArgumentCaptor.capture(), anyLong()); 1285 assertEquals(EVENT_PS_RESTRICT_ENABLED, messageArgumentCaptor.getValue().what); 1286 } 1287 1288 @Test 1289 @MediumTest testRegisterForPsRestrictedDisabled()1290 public void testRegisterForPsRestrictedDisabled() throws Exception { 1291 sst.mRestrictedState.setPsRestricted(true); 1292 // Since PsRestricted is set to true, registerForPsRestrictedDisabled will 1293 // also post message to handler 1294 sst.registerForPsRestrictedDisabled(mTestHandler, EVENT_PS_RESTRICT_DISABLED, null); 1295 1296 waitForMs(100); 1297 1298 // verify posted message 1299 ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); 1300 verify(mTestHandler).sendMessageAtTime(messageArgumentCaptor.capture(), anyLong()); 1301 assertEquals(EVENT_PS_RESTRICT_DISABLED, messageArgumentCaptor.getValue().what); 1302 } 1303 1304 @Test 1305 @MediumTest testOnRestrictedStateChanged()1306 public void testOnRestrictedStateChanged() throws Exception { 1307 ServiceStateTracker spySst = spy(sst); 1308 doReturn(true).when(mPhone).isPhoneTypeGsm(); 1309 doReturn(IccCardApplicationStatus.AppState.APPSTATE_READY).when( 1310 mUiccCardApplication3gpp).getState(); 1311 1312 ArgumentCaptor<Integer> intArgumentCaptor = ArgumentCaptor.forClass(Integer.class); 1313 verify(mSimulatedCommandsVerifier).setOnRestrictedStateChanged(any(Handler.class), 1314 intArgumentCaptor.capture(), eq(null)); 1315 // Since spy() creates a copy of sst object we need to call 1316 // setOnRestrictedStateChanged() explicitly. 1317 mSimulatedCommands.setOnRestrictedStateChanged(spySst, 1318 intArgumentCaptor.getValue().intValue(), null); 1319 1320 // Combination of restricted state and expected notification type. 1321 final int CS_ALL[] = {RILConstants.RIL_RESTRICTED_STATE_CS_ALL, 1322 ServiceStateTracker.CS_ENABLED}; 1323 final int CS_NOR[] = {RILConstants.RIL_RESTRICTED_STATE_CS_NORMAL, 1324 ServiceStateTracker.CS_NORMAL_ENABLED}; 1325 final int CS_EME[] = {RILConstants.RIL_RESTRICTED_STATE_CS_EMERGENCY, 1326 ServiceStateTracker.CS_EMERGENCY_ENABLED}; 1327 final int CS_NON[] = {RILConstants.RIL_RESTRICTED_STATE_NONE, 1328 ServiceStateTracker.CS_DISABLED}; 1329 final int PS_ALL[] = {RILConstants.RIL_RESTRICTED_STATE_PS_ALL, 1330 ServiceStateTracker.PS_ENABLED}; 1331 final int PS_NON[] = {RILConstants.RIL_RESTRICTED_STATE_NONE, 1332 ServiceStateTracker.PS_DISABLED}; 1333 1334 int notifyCount = 0; 1335 // cs not restricted -> cs emergency/normal restricted 1336 internalCheckForRestrictedStateChange(spySst, ++notifyCount, CS_ALL); 1337 // cs emergency/normal restricted -> cs normal restricted 1338 internalCheckForRestrictedStateChange(spySst, ++notifyCount, CS_NOR); 1339 // cs normal restricted -> cs emergency restricted 1340 internalCheckForRestrictedStateChange(spySst, ++notifyCount, CS_EME); 1341 // cs emergency restricted -> cs not restricted 1342 internalCheckForRestrictedStateChange(spySst, ++notifyCount, CS_NON); 1343 // cs not restricted -> cs normal restricted 1344 internalCheckForRestrictedStateChange(spySst, ++notifyCount, CS_NOR); 1345 // cs normal restricted -> cs emergency/normal restricted 1346 internalCheckForRestrictedStateChange(spySst, ++notifyCount, CS_ALL); 1347 // cs emergency/normal restricted -> cs emergency restricted 1348 internalCheckForRestrictedStateChange(spySst, ++notifyCount, CS_EME); 1349 // cs emergency restricted -> cs emergency/normal restricted 1350 internalCheckForRestrictedStateChange(spySst, ++notifyCount, CS_ALL); 1351 // cs emergency/normal restricted -> cs not restricted 1352 internalCheckForRestrictedStateChange(spySst, ++notifyCount, CS_NON); 1353 // cs not restricted -> cs emergency restricted 1354 internalCheckForRestrictedStateChange(spySst, ++notifyCount, CS_EME); 1355 // cs emergency restricted -> cs normal restricted 1356 internalCheckForRestrictedStateChange(spySst, ++notifyCount, CS_NOR); 1357 // cs normal restricted -> cs not restricted 1358 internalCheckForRestrictedStateChange(spySst, ++notifyCount, CS_NON); 1359 1360 // ps not restricted -> ps restricted 1361 internalCheckForRestrictedStateChange(spySst, ++notifyCount, PS_ALL); 1362 // ps restricted -> ps not restricted 1363 internalCheckForRestrictedStateChange(spySst, ++notifyCount, PS_NON); 1364 } 1365 internalCheckForRestrictedStateChange(ServiceStateTracker serviceStateTracker, int times, int[] restrictedState)1366 private void internalCheckForRestrictedStateChange(ServiceStateTracker serviceStateTracker, 1367 int times, int[] restrictedState) { 1368 mSimulatedCommands.triggerRestrictedStateChanged(restrictedState[0]); 1369 waitForMs(100); 1370 ArgumentCaptor<Integer> intArgumentCaptor = ArgumentCaptor.forClass(Integer.class); 1371 verify(serviceStateTracker, times(times)).setNotification(intArgumentCaptor.capture()); 1372 assertEquals(intArgumentCaptor.getValue().intValue(), restrictedState[1]); 1373 } 1374 notificationHasTitleSet(Notification n)1375 private boolean notificationHasTitleSet(Notification n) { 1376 // Notification has no methods to check the actual title, but #toString() includes the 1377 // word "tick" if the title is set so we check this as a workaround 1378 return n.toString().contains("tick"); 1379 } 1380 getNotificationTitle(Notification n)1381 private String getNotificationTitle(Notification n) { 1382 return n.extras.getString(Notification.EXTRA_TITLE); 1383 } 1384 1385 @Test 1386 @SmallTest testSetPsNotifications()1387 public void testSetPsNotifications() { 1388 int subId = 1; 1389 sst.mSubId = subId; 1390 doReturn(subId).when(mSubInfo).getSubscriptionId(); 1391 1392 doReturn(mSubInfo).when(mSubscriptionController).getActiveSubscriptionInfo( 1393 anyInt(), anyString()); 1394 1395 final NotificationManager nm = (NotificationManager) 1396 mContext.getSystemService(Context.NOTIFICATION_SERVICE); 1397 mContextFixture.putBooleanResource( 1398 R.bool.config_user_notification_of_restrictied_mobile_access, true); 1399 doReturn(new ApplicationInfo()).when(mContext).getApplicationInfo(); 1400 Drawable mockDrawable = mock(Drawable.class); 1401 Resources mockResources = mContext.getResources(); 1402 when(mockResources.getDrawable(anyInt(), any())).thenReturn(mockDrawable); 1403 1404 mContextFixture.putResource(com.android.internal.R.string.RestrictedOnDataTitle, "test1"); 1405 sst.setNotification(ServiceStateTracker.PS_ENABLED); 1406 ArgumentCaptor<Notification> notificationArgumentCaptor = 1407 ArgumentCaptor.forClass(Notification.class); 1408 verify(nm).notify(anyString(), anyInt(), notificationArgumentCaptor.capture()); 1409 // if the postedNotification has title set then it must have been the correct notification 1410 Notification postedNotification = notificationArgumentCaptor.getValue(); 1411 assertTrue(notificationHasTitleSet(postedNotification)); 1412 assertEquals("test1", getNotificationTitle(postedNotification)); 1413 1414 sst.setNotification(ServiceStateTracker.PS_DISABLED); 1415 verify(nm).cancel(Integer.toString(sst.mSubId), ServiceStateTracker.PS_NOTIFICATION); 1416 } 1417 1418 @Test 1419 @SmallTest testSetCsNotifications()1420 public void testSetCsNotifications() { 1421 int subId = 1; 1422 sst.mSubId = subId; 1423 doReturn(subId).when(mSubInfo).getSubscriptionId(); 1424 doReturn(mSubInfo).when(mSubscriptionController) 1425 .getActiveSubscriptionInfo(anyInt(), anyString()); 1426 1427 final NotificationManager nm = (NotificationManager) 1428 mContext.getSystemService(Context.NOTIFICATION_SERVICE); 1429 mContextFixture.putBooleanResource( 1430 R.bool.config_user_notification_of_restrictied_mobile_access, true); 1431 doReturn(new ApplicationInfo()).when(mContext).getApplicationInfo(); 1432 Drawable mockDrawable = mock(Drawable.class); 1433 Resources mockResources = mContext.getResources(); 1434 when(mockResources.getDrawable(anyInt(), any())).thenReturn(mockDrawable); 1435 1436 mContextFixture.putResource(com.android.internal.R.string.RestrictedOnAllVoiceTitle, 1437 "test2"); 1438 sst.setNotification(ServiceStateTracker.CS_ENABLED); 1439 ArgumentCaptor<Notification> notificationArgumentCaptor = 1440 ArgumentCaptor.forClass(Notification.class); 1441 verify(nm).notify(anyString(), anyInt(), notificationArgumentCaptor.capture()); 1442 // if the postedNotification has title set then it must have been the correct notification 1443 Notification postedNotification = notificationArgumentCaptor.getValue(); 1444 assertTrue(notificationHasTitleSet(postedNotification)); 1445 assertEquals("test2", getNotificationTitle(postedNotification)); 1446 1447 sst.setNotification(ServiceStateTracker.CS_DISABLED); 1448 verify(nm).cancel(Integer.toString(sst.mSubId), ServiceStateTracker.CS_NOTIFICATION); 1449 } 1450 1451 @Test 1452 @SmallTest testSetCsNormalNotifications()1453 public void testSetCsNormalNotifications() { 1454 int subId = 1; 1455 sst.mSubId = subId; 1456 doReturn(subId).when(mSubInfo).getSubscriptionId(); 1457 doReturn(mSubInfo).when(mSubscriptionController) 1458 .getActiveSubscriptionInfo(anyInt(), anyString()); 1459 1460 final NotificationManager nm = (NotificationManager) 1461 mContext.getSystemService(Context.NOTIFICATION_SERVICE); 1462 mContextFixture.putBooleanResource( 1463 R.bool.config_user_notification_of_restrictied_mobile_access, true); 1464 doReturn(new ApplicationInfo()).when(mContext).getApplicationInfo(); 1465 Drawable mockDrawable = mock(Drawable.class); 1466 Resources mockResources = mContext.getResources(); 1467 when(mockResources.getDrawable(anyInt(), any())).thenReturn(mockDrawable); 1468 1469 mContextFixture.putResource(com.android.internal.R.string.RestrictedOnNormalTitle, "test3"); 1470 sst.setNotification(ServiceStateTracker.CS_NORMAL_ENABLED); 1471 ArgumentCaptor<Notification> notificationArgumentCaptor = 1472 ArgumentCaptor.forClass(Notification.class); 1473 verify(nm).notify(anyString(), anyInt(), notificationArgumentCaptor.capture()); 1474 // if the postedNotification has title set then it must have been the correct notification 1475 Notification postedNotification = notificationArgumentCaptor.getValue(); 1476 assertTrue(notificationHasTitleSet(postedNotification)); 1477 assertEquals("test3", getNotificationTitle(postedNotification)); 1478 1479 sst.setNotification(ServiceStateTracker.CS_DISABLED); 1480 verify(nm).cancel(Integer.toString(sst.mSubId), ServiceStateTracker.CS_NOTIFICATION); 1481 } 1482 1483 @Test 1484 @SmallTest testSetCsEmergencyNotifications()1485 public void testSetCsEmergencyNotifications() { 1486 int subId = 1; 1487 sst.mSubId = subId; 1488 doReturn(subId).when(mSubInfo).getSubscriptionId(); 1489 doReturn(mSubInfo).when(mSubscriptionController) 1490 .getActiveSubscriptionInfo(anyInt(), anyString()); 1491 1492 final NotificationManager nm = (NotificationManager) 1493 mContext.getSystemService(Context.NOTIFICATION_SERVICE); 1494 mContextFixture.putBooleanResource( 1495 R.bool.config_user_notification_of_restrictied_mobile_access, true); 1496 doReturn(new ApplicationInfo()).when(mContext).getApplicationInfo(); 1497 Drawable mockDrawable = mock(Drawable.class); 1498 Resources mockResources = mContext.getResources(); 1499 when(mockResources.getDrawable(anyInt(), any())).thenReturn(mockDrawable); 1500 1501 mContextFixture.putResource(com.android.internal.R.string.RestrictedOnEmergencyTitle, 1502 "test4"); 1503 sst.setNotification(ServiceStateTracker.CS_EMERGENCY_ENABLED); 1504 ArgumentCaptor<Notification> notificationArgumentCaptor = 1505 ArgumentCaptor.forClass(Notification.class); 1506 verify(nm).notify(anyString(), anyInt(), notificationArgumentCaptor.capture()); 1507 // if the postedNotification has title set then it must have been the correct notification 1508 Notification postedNotification = notificationArgumentCaptor.getValue(); 1509 assertTrue(notificationHasTitleSet(postedNotification)); 1510 assertEquals("test4", getNotificationTitle(postedNotification)); 1511 1512 sst.setNotification(ServiceStateTracker.CS_DISABLED); 1513 verify(nm).cancel(Integer.toString(sst.mSubId), ServiceStateTracker.CS_NOTIFICATION); 1514 sst.setNotification(ServiceStateTracker.CS_REJECT_CAUSE_ENABLED); 1515 } 1516 1517 @Test 1518 @SmallTest testSetNotificationsForGroupedSubs()1519 public void testSetNotificationsForGroupedSubs() { 1520 //if subscription is grouped, no notification should be set whatsoever 1521 int subId = 1; 1522 int otherSubId = 2; 1523 sst.mSubId = otherSubId; 1524 doReturn(subId).when(mSubInfo).getSubscriptionId(); 1525 1526 final NotificationManager nm = (NotificationManager) 1527 mContext.getSystemService(Context.NOTIFICATION_SERVICE); 1528 mContextFixture.putBooleanResource( 1529 R.bool.config_user_notification_of_restrictied_mobile_access, true); 1530 doReturn(new ApplicationInfo()).when(mContext).getApplicationInfo(); 1531 Drawable mockDrawable = mock(Drawable.class); 1532 Resources mockResources = mContext.getResources(); 1533 when(mockResources.getDrawable(anyInt(), any())).thenReturn(mockDrawable); 1534 1535 mContextFixture.putResource(com.android.internal.R.string.RestrictedOnDataTitle, "test1"); 1536 1537 sst.setNotification(ServiceStateTracker.EVENT_NETWORK_STATE_CHANGED); 1538 ArgumentCaptor<Notification> notificationArgumentCaptor = 1539 ArgumentCaptor.forClass(Notification.class); 1540 verify(nm, never()).notify(anyString(), anyInt(), notificationArgumentCaptor.capture()); 1541 1542 sst.setNotification(ServiceStateTracker.PS_DISABLED); 1543 verify(nm, never()).cancel(Integer.toString(sst.mSubId), 1544 ServiceStateTracker.PS_NOTIFICATION); 1545 } 1546 1547 @Test 1548 @MediumTest testRegisterForSubscriptionInfoReady()1549 public void testRegisterForSubscriptionInfoReady() { 1550 sst.registerForSubscriptionInfoReady(mTestHandler, EVENT_SUBSCRIPTION_INFO_READY, null); 1551 1552 // Call functions which would trigger posting of message on test handler 1553 doReturn(false).when(mPhone).isPhoneTypeGsm(); 1554 sst.updatePhoneType(); 1555 mSimulatedCommands.notifyOtaProvisionStatusChanged(); 1556 1557 waitForMs(200); 1558 1559 // verify posted message 1560 ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); 1561 verify(mTestHandler).sendMessageAtTime(messageArgumentCaptor.capture(), anyLong()); 1562 assertEquals(EVENT_SUBSCRIPTION_INFO_READY, messageArgumentCaptor.getValue().what); 1563 } 1564 1565 @Test 1566 @MediumTest testRoamingPhoneTypeSwitch()1567 public void testRoamingPhoneTypeSwitch() { 1568 // Enable roaming 1569 doReturn(true).when(mPhone).isPhoneTypeGsm(); 1570 1571 mSimulatedCommands.setVoiceRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 1572 mSimulatedCommands.setDataRegState(NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING); 1573 mSimulatedCommands.notifyNetworkStateChanged(); 1574 1575 waitForMs(200); 1576 1577 sst.registerForDataRoamingOff(mTestHandler, EVENT_DATA_ROAMING_OFF, null, true); 1578 sst.registerForVoiceRoamingOff(mTestHandler, EVENT_VOICE_ROAMING_OFF, null); 1579 sst.registerForDataConnectionDetached(AccessNetworkConstants.TRANSPORT_TYPE_WWAN, 1580 mTestHandler, EVENT_DATA_CONNECTION_DETACHED, null); 1581 1582 // Call functions which would trigger posting of message on test handler 1583 doReturn(false).when(mPhone).isPhoneTypeGsm(); 1584 sst.updatePhoneType(); 1585 1586 // verify if registered handler has message posted to it 1587 ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class); 1588 verify(mTestHandler, atLeast(3)).sendMessageAtTime( 1589 messageArgumentCaptor.capture(), anyLong()); 1590 HashSet<Integer> messageSet = new HashSet<>(); 1591 for (Message m : messageArgumentCaptor.getAllValues()) { 1592 messageSet.add(m.what); 1593 } 1594 1595 assertTrue(messageSet.contains(EVENT_DATA_ROAMING_OFF)); 1596 assertTrue(messageSet.contains(EVENT_VOICE_ROAMING_OFF)); 1597 assertTrue(messageSet.contains(EVENT_DATA_CONNECTION_DETACHED)); 1598 } 1599 1600 @Test 1601 @SmallTest testGetDesiredPowerState()1602 public void testGetDesiredPowerState() { 1603 sst.setRadioPower(true); 1604 assertEquals(sst.getDesiredPowerState(), true); 1605 } 1606 1607 @Test 1608 @MediumTest testEnableLocationUpdates()1609 public void testEnableLocationUpdates() throws Exception { 1610 sst.enableLocationUpdates(); 1611 verify(mSimulatedCommandsVerifier, times(1)).setLocationUpdates(eq(true), 1612 any(Message.class)); 1613 } 1614 1615 @Test 1616 @SmallTest testDisableLocationUpdates()1617 public void testDisableLocationUpdates() throws Exception { 1618 sst.disableLocationUpdates(); 1619 verify(mSimulatedCommandsVerifier, times(1)).setLocationUpdates(eq(false), 1620 nullable(Message.class)); 1621 } 1622 1623 @Test 1624 @SmallTest testGetCurrentDataRegState()1625 public void testGetCurrentDataRegState() throws Exception { 1626 sst.mSS.setDataRegState(ServiceState.STATE_OUT_OF_SERVICE); 1627 assertEquals(sst.getCurrentDataConnectionState(), ServiceState.STATE_OUT_OF_SERVICE); 1628 } 1629 1630 @Test 1631 @SmallTest testIsConcurrentVoiceAndDataAllowed()1632 public void testIsConcurrentVoiceAndDataAllowed() { 1633 doReturn(false).when(mPhone).isPhoneTypeGsm(); 1634 sst.mSS.setCssIndicator(1); 1635 assertEquals(true, sst.isConcurrentVoiceAndDataAllowed()); 1636 sst.mSS.setCssIndicator(0); 1637 assertEquals(false, sst.isConcurrentVoiceAndDataAllowed()); 1638 1639 doReturn(true).when(mPhone).isPhoneTypeGsm(); 1640 NetworkRegistrationInfo nri = new NetworkRegistrationInfo.Builder() 1641 .setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WWAN) 1642 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_HSPA) 1643 .setDomain(NetworkRegistrationInfo.DOMAIN_PS) 1644 .build(); 1645 sst.mSS.addNetworkRegistrationInfo(nri); 1646 assertEquals(true, sst.isConcurrentVoiceAndDataAllowed()); 1647 nri = new NetworkRegistrationInfo.Builder() 1648 .setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WWAN) 1649 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_GPRS) 1650 .setDomain(NetworkRegistrationInfo.DOMAIN_PS) 1651 .build(); 1652 sst.mSS.addNetworkRegistrationInfo(nri); 1653 assertEquals(false, sst.isConcurrentVoiceAndDataAllowed()); 1654 sst.mSS.setCssIndicator(1); 1655 assertEquals(true, sst.isConcurrentVoiceAndDataAllowed()); 1656 } 1657 1658 @Test 1659 @MediumTest testIsImsRegistered()1660 public void testIsImsRegistered() throws Exception { 1661 mSimulatedCommands.setImsRegistrationState(new int[]{1, PhoneConstants.PHONE_TYPE_GSM}); 1662 mSimulatedCommands.notifyImsNetworkStateChanged(); 1663 waitForMs(200); 1664 assertEquals(sst.isImsRegistered(), true); 1665 } 1666 1667 @Test 1668 @SmallTest testIsDeviceShuttingDown()1669 public void testIsDeviceShuttingDown() throws Exception { 1670 sst.requestShutdown(); 1671 assertEquals(true, sst.isDeviceShuttingDown()); 1672 } 1673 1674 @Test 1675 @SmallTest testShuttingDownRequest()1676 public void testShuttingDownRequest() throws Exception { 1677 sst.setRadioPower(true); 1678 waitForMs(100); 1679 1680 sst.requestShutdown(); 1681 waitForMs(100); 1682 assertFalse(mSimulatedCommands.getRadioState() 1683 != TelephonyManager.RADIO_POWER_UNAVAILABLE); 1684 } 1685 1686 @Test 1687 @SmallTest testShuttingDownRequestWithRadioPowerFailResponse()1688 public void testShuttingDownRequestWithRadioPowerFailResponse() throws Exception { 1689 sst.setRadioPower(true); 1690 waitForMs(100); 1691 1692 // Simulate RIL fails the radio power settings. 1693 mSimulatedCommands.setRadioPowerFailResponse(true); 1694 sst.setRadioPower(false); 1695 waitForMs(100); 1696 assertTrue(mSimulatedCommands.getRadioState() == TelephonyManager.RADIO_POWER_ON); 1697 sst.requestShutdown(); 1698 waitForMs(100); 1699 assertFalse(mSimulatedCommands.getRadioState() 1700 != TelephonyManager.RADIO_POWER_UNAVAILABLE); 1701 } 1702 1703 @Test 1704 @SmallTest testSetTimeFromNITZStr()1705 public void testSetTimeFromNITZStr() throws Exception { 1706 { 1707 // Mock sending incorrect nitz str from RIL 1708 mSimulatedCommands.triggerNITZupdate("38/06/20,00:00:00+0"); 1709 waitForMs(200); 1710 verify(mNitzStateMachine, times(0)).handleNitzReceived(any()); 1711 } 1712 { 1713 // Mock sending correct nitz str from RIL 1714 String nitzStr = "15/06/20,00:00:00+0"; 1715 NitzData expectedNitzData = NitzData.parse(nitzStr); 1716 mSimulatedCommands.triggerNITZupdate(nitzStr); 1717 waitForMs(200); 1718 1719 ArgumentCaptor<TimestampedValue<NitzData>> argumentsCaptor = 1720 ArgumentCaptor.forClass(TimestampedValue.class); 1721 verify(mNitzStateMachine, times(1)) 1722 .handleNitzReceived(argumentsCaptor.capture()); 1723 1724 // Confirm the argument was what we expected. 1725 TimestampedValue<NitzData> actualNitzSignal = argumentsCaptor.getValue(); 1726 assertEquals(expectedNitzData, actualNitzSignal.getValue()); 1727 assertTrue(actualNitzSignal.getReferenceTimeMillis() <= SystemClock.elapsedRealtime()); 1728 } 1729 } 1730 changeRegState(int state, CellIdentity cid, int rat)1731 private void changeRegState(int state, CellIdentity cid, int rat) { 1732 changeRegState(state, cid, rat, rat); 1733 } 1734 changeRegState(int state, CellIdentity cid, int voiceRat, int dataRat)1735 private void changeRegState(int state, CellIdentity cid, int voiceRat, int dataRat) { 1736 LteVopsSupportInfo lteVopsSupportInfo = 1737 new LteVopsSupportInfo(LteVopsSupportInfo.LTE_STATUS_NOT_AVAILABLE, 1738 LteVopsSupportInfo.LTE_STATUS_NOT_AVAILABLE); 1739 NetworkRegistrationInfo dataResult = new NetworkRegistrationInfo( 1740 NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN, 1741 state, dataRat, 0, false, 1742 null, cid, 1, false, false, false, lteVopsSupportInfo, false); 1743 sst.mPollingContext[0] = 2; 1744 // update data reg state to be in service 1745 sst.sendMessage(sst.obtainMessage( 1746 ServiceStateTracker.EVENT_POLL_STATE_PS_CELLULAR_REGISTRATION, 1747 new AsyncResult(sst.mPollingContext, dataResult, null))); 1748 waitForMs(200); 1749 NetworkRegistrationInfo voiceResult = new NetworkRegistrationInfo( 1750 NetworkRegistrationInfo.DOMAIN_CS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN, 1751 state, voiceRat, 0, false, 1752 null, cid, false, 0, 0, 0); 1753 sst.sendMessage(sst.obtainMessage( 1754 ServiceStateTracker.EVENT_POLL_STATE_CS_CELLULAR_REGISTRATION, 1755 new AsyncResult(sst.mPollingContext, voiceResult, null))); 1756 waitForMs(200); 1757 } 1758 1759 // Edge and GPRS are grouped under the same family and Edge has higher rate than GPRS. 1760 // Expect no rat update when move from E to G. 1761 @Test testRatRatchet()1762 public void testRatRatchet() throws Exception { 1763 CellIdentityGsm cellIdentity = 1764 new CellIdentityGsm(0, 1, 900, 5, "001", "01", "test", "tst"); 1765 // start on GPRS 1766 changeRegState(1, cellIdentity, 16, 1); 1767 assertEquals(ServiceState.STATE_IN_SERVICE, sst.getCurrentDataConnectionState()); 1768 assertEquals(ServiceState.RIL_RADIO_TECHNOLOGY_GPRS, sst.mSS.getRilDataRadioTechnology()); 1769 // upgrade to EDGE 1770 changeRegState(1, cellIdentity, 16, 2); 1771 assertEquals(ServiceState.RIL_RADIO_TECHNOLOGY_EDGE, sst.mSS.getRilDataRadioTechnology()); 1772 // drop back to GPRS and expect a ratchet 1773 changeRegState(1, cellIdentity, 16, 1); 1774 assertEquals(ServiceState.RIL_RADIO_TECHNOLOGY_EDGE, sst.mSS.getRilDataRadioTechnology()); 1775 } 1776 1777 // Edge and GPRS are grouped under the same family and Edge has higher rate than GPRS. 1778 // Bypass rat rachet when cell id changed. Expect rat update from E to G 1779 @Test testRatRatchetWithCellChange()1780 public void testRatRatchetWithCellChange() throws Exception { 1781 CellIdentityGsm cellIdentity = 1782 new CellIdentityGsm(0, 1, 900, 5, "001", "01", "test", "tst"); 1783 // update data reg state to be in service 1784 changeRegState(1, cellIdentity, 16, 2); 1785 assertEquals(ServiceState.STATE_IN_SERVICE, sst.getCurrentDataConnectionState()); 1786 assertEquals(ServiceState.RIL_RADIO_TECHNOLOGY_GSM, sst.mSS.getRilVoiceRadioTechnology()); 1787 assertEquals(ServiceState.RIL_RADIO_TECHNOLOGY_EDGE, sst.mSS.getRilDataRadioTechnology()); 1788 // RAT: EDGE -> GPRS cell ID: 1 -> 2 1789 cellIdentity = new CellIdentityGsm(0, 2, 900, 5, "001", "01", "test", "tst"); 1790 changeRegState(1, cellIdentity, 16, 1); 1791 assertEquals(ServiceState.RIL_RADIO_TECHNOLOGY_GPRS, sst.mSS.getRilDataRadioTechnology()); 1792 1793 } 1794 1795 // TODO(nharold): This actually seems like broken behavior; rather than preserve it, we should 1796 // probably remove it. 1797 // GSM, Edge, GPRS are grouped under the same family where Edge > GPRS > GSM. 1798 // Expect no rat update from E to G immediately following cell id change. 1799 // Expect ratratchet (from G to E) for the following rat update within the cell location. 1800 @Test testRatRatchetWithCellChangeBeforeRatChange()1801 public void testRatRatchetWithCellChangeBeforeRatChange() throws Exception { 1802 // cell ID update 1803 CellIdentityGsm cellIdentity = 1804 new CellIdentityGsm(0, 1, 900, 5, "001", "01", "test", "tst"); 1805 changeRegState(1, cellIdentity, 16, 2); 1806 assertEquals(ServiceState.STATE_IN_SERVICE, sst.getCurrentDataConnectionState()); 1807 assertEquals(ServiceState.RIL_RADIO_TECHNOLOGY_EDGE, sst.mSS.getRilDataRadioTechnology()); 1808 1809 // RAT: EDGE -> GPRS, cell ID unchanged. Expect no rat ratchet following cell Id change. 1810 changeRegState(1, cellIdentity, 16, 1); 1811 assertEquals(ServiceState.RIL_RADIO_TECHNOLOGY_GPRS, sst.mSS.getRilDataRadioTechnology()); 1812 1813 // RAT: GPRS -> EDGE should ratchet. 1814 changeRegState(1, cellIdentity, 16, 2); 1815 assertEquals(ServiceState.RIL_RADIO_TECHNOLOGY_EDGE, sst.mSS.getRilDataRadioTechnology()); 1816 } 1817 sendPhyChanConfigChange(int[] bandwidths)1818 private void sendPhyChanConfigChange(int[] bandwidths) { 1819 ArrayList<PhysicalChannelConfig> pc = new ArrayList<>(); 1820 int ssType = PhysicalChannelConfig.CONNECTION_PRIMARY_SERVING; 1821 for (int bw : bandwidths) { 1822 pc.add(new PhysicalChannelConfig.Builder() 1823 .setCellConnectionStatus(ssType) 1824 .setCellBandwidthDownlinkKhz(bw) 1825 .build()); 1826 1827 // All cells after the first are secondary serving cells. 1828 ssType = PhysicalChannelConfig.CONNECTION_SECONDARY_SERVING; 1829 } 1830 sst.sendMessage(sst.obtainMessage(ServiceStateTracker.EVENT_PHYSICAL_CHANNEL_CONFIG, 1831 new AsyncResult(null, pc, null))); 1832 waitForMs(100); 1833 } 1834 sendRegStateUpdateForLteCellId(CellIdentityLte cellId)1835 private void sendRegStateUpdateForLteCellId(CellIdentityLte cellId) { 1836 LteVopsSupportInfo lteVopsSupportInfo = 1837 new LteVopsSupportInfo(LteVopsSupportInfo.LTE_STATUS_NOT_AVAILABLE, 1838 LteVopsSupportInfo.LTE_STATUS_NOT_AVAILABLE); 1839 NetworkRegistrationInfo dataResult = new NetworkRegistrationInfo( 1840 NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN, 1841 NetworkRegistrationInfo.REGISTRATION_STATE_HOME, TelephonyManager.NETWORK_TYPE_LTE, 1842 0, false, null, cellId, 1, false, false, false, lteVopsSupportInfo, false); 1843 NetworkRegistrationInfo voiceResult = new NetworkRegistrationInfo( 1844 NetworkRegistrationInfo.DOMAIN_CS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN, 1845 NetworkRegistrationInfo.REGISTRATION_STATE_HOME, TelephonyManager.NETWORK_TYPE_LTE, 1846 0, false, null, cellId, false, 0, 0, 0); 1847 sst.mPollingContext[0] = 2; 1848 // update data reg state to be in service 1849 sst.sendMessage(sst.obtainMessage( 1850 ServiceStateTracker.EVENT_POLL_STATE_PS_CELLULAR_REGISTRATION, 1851 new AsyncResult(sst.mPollingContext, dataResult, null))); 1852 waitForMs(200); 1853 sst.sendMessage(sst.obtainMessage( 1854 ServiceStateTracker.EVENT_POLL_STATE_CS_CELLULAR_REGISTRATION, 1855 new AsyncResult(sst.mPollingContext, voiceResult, null))); 1856 waitForMs(200); 1857 } 1858 1859 @Test testPhyChanBandwidthUpdatedOnDataRegState()1860 public void testPhyChanBandwidthUpdatedOnDataRegState() throws Exception { 1861 // Cell ID change should trigger hasLocationChanged. 1862 CellIdentityLte cellIdentity5 = 1863 new CellIdentityLte(1, 1, 5, 1, 5000, "001", "01", "test", "tst"); 1864 1865 sendPhyChanConfigChange(new int[] {10000}); 1866 sendRegStateUpdateForLteCellId(cellIdentity5); 1867 assertTrue(Arrays.equals(new int[] {5000}, sst.mSS.getCellBandwidths())); 1868 } 1869 1870 @Test testPhyChanBandwidthNotUpdatedWhenInvalidInCellIdentity()1871 public void testPhyChanBandwidthNotUpdatedWhenInvalidInCellIdentity() throws Exception { 1872 // Cell ID change should trigger hasLocationChanged. 1873 CellIdentityLte cellIdentityInv = 1874 new CellIdentityLte(1, 1, 5, 1, 12345, "001", "01", "test", "tst"); 1875 1876 sendPhyChanConfigChange(new int[] {10000}); 1877 sendRegStateUpdateForLteCellId(cellIdentityInv); 1878 assertTrue(Arrays.equals(new int[] {10000}, sst.mSS.getCellBandwidths())); 1879 } 1880 1881 @Test testPhyChanBandwidthPrefersCarrierAggregationReport()1882 public void testPhyChanBandwidthPrefersCarrierAggregationReport() throws Exception { 1883 // Cell ID change should trigger hasLocationChanged. 1884 CellIdentityLte cellIdentity10 = 1885 new CellIdentityLte(1, 1, 5, 1, 10000, "001", "01", "test", "tst"); 1886 1887 sendPhyChanConfigChange(new int[] {10000, 5000}); 1888 sendRegStateUpdateForLteCellId(cellIdentity10); 1889 assertTrue(Arrays.equals(new int[] {10000, 5000}, sst.mSS.getCellBandwidths())); 1890 } 1891 1892 @Test testPhyChanBandwidthRatchetedOnPhyChanBandwidth()1893 public void testPhyChanBandwidthRatchetedOnPhyChanBandwidth() throws Exception { 1894 // LTE Cell with bandwidth = 10000 1895 CellIdentityLte cellIdentity10 = 1896 new CellIdentityLte(1, 1, 1, 1, 10000, "1", "1", "test", "tst"); 1897 1898 sendRegStateUpdateForLteCellId(cellIdentity10); 1899 assertTrue(Arrays.equals(new int[] {10000}, sst.mSS.getCellBandwidths())); 1900 sendPhyChanConfigChange(new int[] {10000, 5000}); 1901 assertTrue(Arrays.equals(new int[] {10000, 5000}, sst.mSS.getCellBandwidths())); 1902 } 1903 1904 @Test testPhyChanBandwidthResetsOnOos()1905 public void testPhyChanBandwidthResetsOnOos() throws Exception { 1906 testPhyChanBandwidthRatchetedOnPhyChanBandwidth(); 1907 LteVopsSupportInfo lteVopsSupportInfo = 1908 new LteVopsSupportInfo(LteVopsSupportInfo.LTE_STATUS_NOT_AVAILABLE, 1909 LteVopsSupportInfo.LTE_STATUS_NOT_AVAILABLE); 1910 NetworkRegistrationInfo dataResult = new NetworkRegistrationInfo( 1911 NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN, 1912 NetworkRegistrationInfo.REGISTRATION_STATE_NOT_REGISTERED_OR_SEARCHING, 1913 TelephonyManager.NETWORK_TYPE_UNKNOWN, 0, false, null, null, 1, false, false, 1914 false, lteVopsSupportInfo, false); 1915 NetworkRegistrationInfo voiceResult = new NetworkRegistrationInfo( 1916 NetworkRegistrationInfo.DOMAIN_CS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN, 1917 NetworkRegistrationInfo.REGISTRATION_STATE_NOT_REGISTERED_OR_SEARCHING, 1918 TelephonyManager.NETWORK_TYPE_UNKNOWN, 0, false, null, null, false, 0, 0, 0); 1919 sst.mPollingContext[0] = 2; 1920 sst.sendMessage(sst.obtainMessage( 1921 ServiceStateTracker.EVENT_POLL_STATE_PS_CELLULAR_REGISTRATION, 1922 new AsyncResult(sst.mPollingContext, dataResult, null))); 1923 waitForMs(200); 1924 sst.sendMessage(sst.obtainMessage( 1925 ServiceStateTracker.EVENT_POLL_STATE_CS_CELLULAR_REGISTRATION, 1926 new AsyncResult(sst.mPollingContext, voiceResult, null))); 1927 waitForMs(200); 1928 assertTrue(Arrays.equals(new int[0], sst.mSS.getCellBandwidths())); 1929 } 1930 1931 @Test testGetServiceProviderNameWithBrandOverride()1932 public void testGetServiceProviderNameWithBrandOverride() { 1933 String brandOverride = "spn from brand override"; 1934 doReturn(brandOverride).when(mUiccProfile).getOperatorBrandOverride(); 1935 1936 assertThat(sst.getServiceProviderName()).isEqualTo(brandOverride); 1937 } 1938 1939 @Test testGetServiceProviderNameWithCarrierConfigOverride()1940 public void testGetServiceProviderNameWithCarrierConfigOverride() { 1941 String carrierOverride = "spn from carrier override"; 1942 mBundle.putBoolean(CarrierConfigManager.KEY_CARRIER_NAME_OVERRIDE_BOOL, true); 1943 mBundle.putString(CarrierConfigManager.KEY_CARRIER_NAME_STRING, carrierOverride); 1944 1945 assertThat(sst.getServiceProviderName()).isEqualTo(carrierOverride); 1946 } 1947 1948 @Test testGetServiceProviderNameWithSimRecord()1949 public void testGetServiceProviderNameWithSimRecord() { 1950 String spn = "spn from sim record"; 1951 doReturn(spn).when(mSimRecords).getServiceProviderName(); 1952 1953 assertThat(sst.getServiceProviderName()).isEqualTo(spn); 1954 } 1955 1956 @Test testGetServiceProviderNameWithAllSource()1957 public void testGetServiceProviderNameWithAllSource() { 1958 String brandOverride = "spn from brand override"; 1959 doReturn(brandOverride).when(mUiccProfile).getOperatorBrandOverride(); 1960 1961 String carrierOverride = "spn from carrier override"; 1962 mBundle.putBoolean(CarrierConfigManager.KEY_CARRIER_NAME_OVERRIDE_BOOL, true); 1963 mBundle.putString(CarrierConfigManager.KEY_CARRIER_NAME_STRING, carrierOverride); 1964 1965 String spn = "spn from sim record"; 1966 doReturn(spn).when(mSimRecords).getServiceProviderName(); 1967 1968 // Operator brand override has highest priority 1969 assertThat(sst.getServiceProviderName()).isEqualTo(brandOverride); 1970 1971 // Remove the brand override 1972 doReturn(null).when(mUiccProfile).getOperatorBrandOverride(); 1973 1974 // Carrier config override has 2nd priority 1975 assertThat(sst.getServiceProviderName()).isEqualTo(carrierOverride); 1976 1977 // Remove the carrier config override 1978 mBundle.putBoolean(CarrierConfigManager.KEY_CARRIER_NAME_OVERRIDE_BOOL, false); 1979 1980 // SPN from sim has lowest priority 1981 assertThat(sst.getServiceProviderName()).isEqualTo(spn); 1982 } 1983 1984 @Test testGetCarrierNameDisplayConditionWithBrandOverride()1985 public void testGetCarrierNameDisplayConditionWithBrandOverride() { 1986 String brandOverride = "spn from brand override"; 1987 doReturn(brandOverride).when(mUiccProfile).getOperatorBrandOverride(); 1988 1989 // Only show spn because all PLMNs will be considered HOME PLMNs. 1990 assertThat(sst.getCarrierNameDisplayBitmask(new ServiceState())).isEqualTo( 1991 ServiceStateTracker.CARRIER_NAME_DISPLAY_BITMASK_SHOW_SPN); 1992 } 1993 1994 @Test 1995 @SmallTest testGetMdn()1996 public void testGetMdn() throws Exception { 1997 doReturn(false).when(mPhone).isPhoneTypeGsm(); 1998 doReturn(false).when(mPhone).isPhoneTypeCdma(); 1999 doReturn(true).when(mPhone).isPhoneTypeCdmaLte(); 2000 doReturn(CdmaSubscriptionSourceManager.SUBSCRIPTION_FROM_RUIM).when(mCdmaSSM) 2001 .getCdmaSubscriptionSource(); 2002 2003 logd("Calling updatePhoneType"); 2004 // switch to CDMA 2005 sst.updatePhoneType(); 2006 2007 // trigger RUIM_RECORDS_LOADED 2008 ArgumentCaptor<Integer> integerArgumentCaptor = ArgumentCaptor.forClass(Integer.class); 2009 verify(mRuimRecords).registerForRecordsLoaded(eq(sst), integerArgumentCaptor.capture(), 2010 nullable(Object.class)); 2011 2012 // response for mRuimRecords.registerForRecordsLoaded() 2013 Message msg = Message.obtain(); 2014 msg.what = integerArgumentCaptor.getValue(); 2015 msg.obj = new AsyncResult(null, null, null); 2016 sst.sendMessage(msg); 2017 2018 // wait for RUIM_RECORDS_LOADED to be handled 2019 waitForHandlerAction(sst, 5000); 2020 2021 // mdn should be null as nothing populated it 2022 assertEquals(null, sst.getMdnNumber()); 2023 2024 // if ruim is provisioned, mdn should still be null 2025 doReturn(true).when(mRuimRecords).isProvisioned(); 2026 assertEquals(null, sst.getMdnNumber()); 2027 2028 // if ruim is not provisioned, and mdn is non null, sst should still return the correct 2029 // value 2030 doReturn(false).when(mRuimRecords).isProvisioned(); 2031 String mockMdn = "mockMdn"; 2032 doReturn(mockMdn).when(mRuimRecords).getMdn(); 2033 2034 // trigger RUIM_RECORDS_LOADED 2035 Message msg1 = Message.obtain(); 2036 msg1.what = integerArgumentCaptor.getValue(); 2037 msg1.obj = new AsyncResult(null, null, null); 2038 sst.sendMessage(msg1); 2039 2040 // wait for RUIM_RECORDS_LOADED to be handled 2041 waitForHandlerAction(sst, 5000); 2042 2043 assertEquals(mockMdn, sst.getMdnNumber()); 2044 } 2045 2046 @Test 2047 @SmallTest testOnVopsInfoChanged()2048 public void testOnVopsInfoChanged() { 2049 ServiceState ss = new ServiceState(); 2050 ss.setVoiceRegState(ServiceState.STATE_IN_SERVICE); 2051 ss.setDataRegState(ServiceState.STATE_IN_SERVICE); 2052 sst.mSS = ss; 2053 2054 CellIdentityLte cellId = 2055 new CellIdentityLte(1, 1, 5, 1, 5000, "001", "01", "test", "tst"); 2056 LteVopsSupportInfo lteVopsSupportInfo = 2057 new LteVopsSupportInfo(LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED, 2058 LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED); 2059 2060 NetworkRegistrationInfo dataResult = new NetworkRegistrationInfo( 2061 NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN, 2062 NetworkRegistrationInfo.REGISTRATION_STATE_HOME, TelephonyManager.NETWORK_TYPE_LTE, 2063 0, false, null, cellId, 1, false, false, false, lteVopsSupportInfo, false); 2064 sst.mPollingContext[0] = 2; 2065 2066 sst.sendMessage(sst.obtainMessage( 2067 ServiceStateTracker.EVENT_POLL_STATE_PS_CELLULAR_REGISTRATION, 2068 new AsyncResult(sst.mPollingContext, dataResult, null))); 2069 NetworkRegistrationInfo voiceResult = new NetworkRegistrationInfo( 2070 NetworkRegistrationInfo.DOMAIN_CS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN, 2071 NetworkRegistrationInfo.REGISTRATION_STATE_HOME, 2072 TelephonyManager.NETWORK_TYPE_LTE, 0, 2073 false, null, cellId, false, 0, 0, 0); 2074 sst.sendMessage(sst.obtainMessage( 2075 ServiceStateTracker.EVENT_POLL_STATE_CS_CELLULAR_REGISTRATION, 2076 new AsyncResult(sst.mPollingContext, voiceResult, null))); 2077 2078 waitForMs(200); 2079 assertEquals(ServiceState.STATE_IN_SERVICE, sst.getCurrentDataConnectionState()); 2080 NetworkRegistrationInfo sSnetworkRegistrationInfo = 2081 sst.mSS.getNetworkRegistrationInfo(NetworkRegistrationInfo.DOMAIN_PS, 2082 AccessNetworkConstants.TRANSPORT_TYPE_WWAN); 2083 assertEquals(lteVopsSupportInfo, 2084 sSnetworkRegistrationInfo.getDataSpecificInfo().getLteVopsSupportInfo()); 2085 2086 lteVopsSupportInfo = 2087 new LteVopsSupportInfo(LteVopsSupportInfo.LTE_STATUS_SUPPORTED, 2088 LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED); 2089 dataResult = new NetworkRegistrationInfo(NetworkRegistrationInfo.DOMAIN_PS, 2090 AccessNetworkConstants.TRANSPORT_TYPE_WWAN, 2091 NetworkRegistrationInfo.REGISTRATION_STATE_HOME, 2092 TelephonyManager.NETWORK_TYPE_LTE, 0, false, null, cellId, 1, false, false, false, 2093 lteVopsSupportInfo, false); 2094 sst.mPollingContext[0] = 1; 2095 sst.sendMessage(sst.obtainMessage( 2096 ServiceStateTracker.EVENT_POLL_STATE_PS_CELLULAR_REGISTRATION, 2097 new AsyncResult(sst.mPollingContext, dataResult, null))); 2098 waitForMs(200); 2099 2100 sSnetworkRegistrationInfo = 2101 sst.mSS.getNetworkRegistrationInfo(2, 1); 2102 assertEquals(lteVopsSupportInfo, 2103 sSnetworkRegistrationInfo.getDataSpecificInfo().getLteVopsSupportInfo()); 2104 } 2105 2106 @Test 2107 @SmallTest testEriLoading()2108 public void testEriLoading() { 2109 sst.obtainMessage(GsmCdmaPhone.EVENT_CARRIER_CONFIG_CHANGED, null).sendToTarget(); 2110 waitForMs(100); 2111 verify(mEriManager, times(1)).loadEriFile(); 2112 } 2113 enableCdnr()2114 private void enableCdnr() { 2115 mBundle.putBoolean( 2116 CarrierConfigManager.KEY_ENABLE_CARRIER_DISPLAY_NAME_RESOLVER_BOOL, true); 2117 sendCarrierConfigUpdate(); 2118 } 2119 2120 @Test testUpdateSpnDisplay_noService_displayEmergencyCallOnly()2121 public void testUpdateSpnDisplay_noService_displayEmergencyCallOnly() { 2122 enableCdnr(); 2123 2124 // GSM phone 2125 doReturn(true).when(mPhone).isPhoneTypeGsm(); 2126 2127 // Emergency call only 2128 ServiceState ss = new ServiceState(); 2129 ss.setVoiceRegState(ServiceState.STATE_EMERGENCY_ONLY); 2130 ss.setDataRegState(ServiceState.STATE_OUT_OF_SERVICE); 2131 ss.setEmergencyOnly(true); 2132 doReturn(ss).when(mSST).getServiceState(); 2133 2134 // update the spn 2135 sst.updateSpnDisplay(); 2136 2137 // Plmn should be shown, and the string is "Emergency call only" 2138 Bundle b = getExtrasFromLastSpnUpdateIntent(); 2139 assertThat(b.getString(TelephonyIntents.EXTRA_PLMN)) 2140 .isEqualTo(CARRIER_NAME_DISPLAY_EMERGENCY_CALL); 2141 assertThat(b.getBoolean(TelephonyIntents.EXTRA_SHOW_PLMN)).isTrue(); 2142 } 2143 2144 @Test testUpdateSpnDisplay_noServiceAndEmergencyCallNotAvailable_displayOOS()2145 public void testUpdateSpnDisplay_noServiceAndEmergencyCallNotAvailable_displayOOS() { 2146 enableCdnr(); 2147 2148 // GSM phone 2149 doReturn(true).when(mPhone).isPhoneTypeGsm(); 2150 2151 // Completely out of service 2152 ServiceState ss = new ServiceState(); 2153 ss.setVoiceRegState(ServiceState.STATE_OUT_OF_SERVICE); 2154 ss.setDataRegState(ServiceState.STATE_OUT_OF_SERVICE); 2155 ss.setEmergencyOnly(false); 2156 doReturn(ss).when(mSST).getServiceState(); 2157 2158 // update the spn 2159 sst.updateSpnDisplay(); 2160 2161 // Plmn should be shown, and the string is "No service" 2162 Bundle b = getExtrasFromLastSpnUpdateIntent(); 2163 assertThat(b.getString(TelephonyIntents.EXTRA_PLMN)) 2164 .isEqualTo(CARRIER_NAME_DISPLAY_NO_SERVICE); 2165 assertThat(b.getBoolean(TelephonyIntents.EXTRA_SHOW_PLMN)).isTrue(); 2166 } 2167 2168 @Test testUpdateSpnDisplay_flightMode_displayOOS()2169 public void testUpdateSpnDisplay_flightMode_displayOOS() { 2170 enableCdnr(); 2171 2172 // GSM phone 2173 doReturn(true).when(mPhone).isPhoneTypeGsm(); 2174 2175 // Flight mode 2176 ServiceState ss = new ServiceState(); 2177 ss.setVoiceRegState(ServiceState.STATE_POWER_OFF); 2178 ss.setDataRegState(ServiceState.STATE_POWER_OFF); 2179 doReturn(ss).when(mSST).getServiceState(); 2180 2181 // update the spn 2182 sst.updateSpnDisplay(); 2183 2184 // Plmn should be shown, and the string is "No service" 2185 Bundle b = getExtrasFromLastSpnUpdateIntent(); 2186 assertThat(b.getString(TelephonyIntents.EXTRA_PLMN)) 2187 .isEqualTo(CARRIER_NAME_DISPLAY_NO_SERVICE); 2188 assertThat(b.getBoolean(TelephonyIntents.EXTRA_SHOW_PLMN)).isTrue(); 2189 } 2190 2191 @Test testUpdateSpnDisplay_spnNotEmptyAndWifiCallingEnabled_showSpnOnly()2192 public void testUpdateSpnDisplay_spnNotEmptyAndWifiCallingEnabled_showSpnOnly() { 2193 enableCdnr(); 2194 2195 // GSM phone 2196 doReturn(true).when(mPhone).isPhoneTypeGsm(); 2197 2198 // In Service 2199 ServiceState ss = new ServiceState(); 2200 ss.setVoiceRegState(ServiceState.STATE_IN_SERVICE); 2201 ss.setDataRegState(ServiceState.STATE_IN_SERVICE); 2202 sst.mSS = ss; 2203 2204 // wifi-calling is enabled 2205 doReturn(true).when(mPhone).isWifiCallingEnabled(); 2206 2207 // update the spn 2208 sst.updateSpnDisplay(); 2209 2210 // Only spn should be shown 2211 String spn = mBundle.getString(CarrierConfigManager.KEY_CARRIER_NAME_STRING); 2212 Bundle b = getExtrasFromLastSpnUpdateIntent(); 2213 assertThat(b.getString(TelephonyIntents.EXTRA_SPN)) 2214 .isEqualTo(String.format(WIFI_CALLING_VOICE_FORMAT, spn)); 2215 assertThat(b.getBoolean(TelephonyIntents.EXTRA_SHOW_SPN)).isTrue(); 2216 assertThat(b.getString(TelephonyIntents.EXTRA_DATA_SPN)) 2217 .isEqualTo(String.format(WIFI_CALLING_DATA_FORMAT, spn)); 2218 assertThat(b.getBoolean(TelephonyIntents.EXTRA_SHOW_PLMN)).isFalse(); 2219 } 2220 2221 @Test testUpdateSpnDisplay_spnEmptyAndWifiCallingEnabled_showPlmnOnly()2222 public void testUpdateSpnDisplay_spnEmptyAndWifiCallingEnabled_showPlmnOnly() { 2223 // set empty service provider name 2224 mBundle.putString(CarrierConfigManager.KEY_CARRIER_NAME_STRING, ""); 2225 2226 enableCdnr(); 2227 2228 // GSM phone 2229 doReturn(true).when(mPhone).isPhoneTypeGsm(); 2230 2231 // In Service 2232 ServiceState ss = new ServiceState(); 2233 ss.setVoiceRegState(ServiceState.STATE_IN_SERVICE); 2234 ss.setDataRegState(ServiceState.STATE_IN_SERVICE); 2235 sst.mSS = ss; 2236 2237 // wifi-calling is enabled 2238 doReturn(true).when(mPhone).isWifiCallingEnabled(); 2239 2240 // update the spn 2241 sst.updateSpnDisplay(); 2242 2243 // Only plmn should be shown 2244 String plmn = mBundle.getStringArray(CarrierConfigManager.KEY_PNN_OVERRIDE_STRING_ARRAY)[0]; 2245 plmn = plmn.split("\\s*,\\s*")[0]; 2246 Bundle b = getExtrasFromLastSpnUpdateIntent(); 2247 assertThat(b.getString(TelephonyIntents.EXTRA_PLMN)) 2248 .isEqualTo(String.format(WIFI_CALLING_VOICE_FORMAT, plmn)); 2249 assertThat(b.getBoolean(TelephonyIntents.EXTRA_SHOW_PLMN)).isTrue(); 2250 assertThat(b.getBoolean(TelephonyIntents.EXTRA_SHOW_SPN)).isFalse(); 2251 } 2252 2253 @Test testUpdateSpnDisplay_inServiceNoWifiCalling_showSpnAndPlmn()2254 public void testUpdateSpnDisplay_inServiceNoWifiCalling_showSpnAndPlmn() { 2255 enableCdnr(); 2256 2257 // GSM phone 2258 doReturn(true).when(mPhone).isPhoneTypeGsm(); 2259 2260 ServiceState ss = new ServiceState(); 2261 ss.setVoiceRegState(ServiceState.STATE_IN_SERVICE); 2262 ss.setDataRegState(ServiceState.STATE_IN_SERVICE); 2263 sst.mSS = ss; 2264 2265 // wifi-calling is disable 2266 doReturn(false).when(mPhone).isWifiCallingEnabled(); 2267 2268 // update the spn 2269 sst.updateSpnDisplay(); 2270 2271 // Show both spn & plmn 2272 String spn = mBundle.getString(CarrierConfigManager.KEY_CARRIER_NAME_STRING); 2273 String plmn = mBundle.getStringArray(CarrierConfigManager.KEY_PNN_OVERRIDE_STRING_ARRAY)[0]; 2274 plmn = plmn.split("\\s*,\\s*")[0]; 2275 Bundle b = getExtrasFromLastSpnUpdateIntent(); 2276 assertThat(b.getString(TelephonyIntents.EXTRA_SPN)).isEqualTo(spn); 2277 assertThat(b.getBoolean(TelephonyIntents.EXTRA_SHOW_SPN)).isTrue(); 2278 assertThat(b.getString(TelephonyIntents.EXTRA_PLMN)).isEqualTo(plmn); 2279 assertThat(b.getBoolean(TelephonyIntents.EXTRA_SHOW_PLMN)).isTrue(); 2280 } 2281 getExtrasFromLastSpnUpdateIntent()2282 private Bundle getExtrasFromLastSpnUpdateIntent() { 2283 // Verify the spn update notification was sent 2284 ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class); 2285 verify(mContextFixture.getTestDouble(), atLeast(1)) 2286 .sendStickyBroadcastAsUser(intentArgumentCaptor.capture(), eq(UserHandle.ALL)); 2287 2288 List<Intent> intents = intentArgumentCaptor.getAllValues(); 2289 return intents.get(intents.size() - 1).getExtras(); 2290 } 2291 } 2292