1 /* 2 * Copyright (C) 2022 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.phone; 18 19 import static android.telephony.RadioAccessFamily.RAF_1xRTT; 20 import static android.telephony.RadioAccessFamily.RAF_EDGE; 21 import static android.telephony.RadioAccessFamily.RAF_EHRPD; 22 import static android.telephony.RadioAccessFamily.RAF_EVDO_0; 23 import static android.telephony.RadioAccessFamily.RAF_EVDO_A; 24 import static android.telephony.RadioAccessFamily.RAF_EVDO_B; 25 import static android.telephony.RadioAccessFamily.RAF_GPRS; 26 import static android.telephony.RadioAccessFamily.RAF_GSM; 27 import static android.telephony.RadioAccessFamily.RAF_HSDPA; 28 import static android.telephony.RadioAccessFamily.RAF_HSPA; 29 import static android.telephony.RadioAccessFamily.RAF_HSPAP; 30 import static android.telephony.RadioAccessFamily.RAF_HSUPA; 31 import static android.telephony.RadioAccessFamily.RAF_IS95A; 32 import static android.telephony.RadioAccessFamily.RAF_IS95B; 33 import static android.telephony.RadioAccessFamily.RAF_LTE; 34 import static android.telephony.RadioAccessFamily.RAF_LTE_CA; 35 import static android.telephony.RadioAccessFamily.RAF_TD_SCDMA; 36 import static android.telephony.RadioAccessFamily.RAF_UMTS; 37 import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID; 38 39 import static com.android.phone.NotificationMgr.DATA_ROAMING_NOTIFICATION; 40 import static com.android.phone.NotificationMgr.LIMITED_SIM_FUNCTION_NOTIFICATION; 41 42 import static com.google.common.truth.Truth.assertThat; 43 44 import static org.mockito.Mockito.any; 45 import static org.mockito.Mockito.anyInt; 46 import static org.mockito.Mockito.anyString; 47 import static org.mockito.Mockito.doReturn; 48 import static org.mockito.Mockito.eq; 49 import static org.mockito.Mockito.never; 50 import static org.mockito.Mockito.spy; 51 import static org.mockito.Mockito.verify; 52 import static org.mockito.Mockito.when; 53 54 import android.app.Notification; 55 import android.app.NotificationManager; 56 import android.app.StatusBarManager; 57 import android.content.Context; 58 import android.content.SharedPreferences; 59 import android.content.pm.ApplicationInfo; 60 import android.content.res.Resources; 61 import android.os.Build; 62 import android.os.PersistableBundle; 63 import android.os.SystemClock; 64 import android.os.UserHandle; 65 import android.os.UserManager; 66 import android.telecom.TelecomManager; 67 import android.telephony.CarrierConfigManager; 68 import android.telephony.NetworkRegistrationInfo; 69 import android.telephony.ServiceState; 70 import android.telephony.SubscriptionInfo; 71 import android.telephony.SubscriptionManager; 72 import android.telephony.TelephonyManager; 73 import android.testing.AndroidTestingRunner; 74 import android.testing.TestableLooper; 75 76 import com.android.TelephonyTestBase; 77 import com.android.internal.telephony.Phone; 78 import com.android.internal.telephony.PhoneConstants; 79 import com.android.internal.telephony.PhoneFactory; 80 import com.android.internal.telephony.ServiceStateTracker; 81 import com.android.internal.telephony.SignalStrengthController; 82 import com.android.internal.telephony.data.DataConfigManager; 83 import com.android.internal.telephony.data.DataNetworkController; 84 import com.android.internal.telephony.data.DataSettingsManager; 85 import com.android.internal.telephony.util.NotificationChannelController; 86 87 import org.junit.Before; 88 import org.junit.Test; 89 import org.junit.runner.RunWith; 90 import org.mockito.ArgumentCaptor; 91 import org.mockito.Mock; 92 import org.mockito.MockitoAnnotations; 93 94 import java.util.Collections; 95 import java.util.concurrent.TimeUnit; 96 97 /** 98 * Unit Test for NotificationMgr 99 */ 100 @RunWith(AndroidTestingRunner.class) 101 @TestableLooper.RunWithLooper 102 public class NotificationMgrTest extends TelephonyTestBase { 103 private static final int TEST_SUB_ID = 1; 104 private static final long SERIAL_NUMBER_OF_USER = 1234567L; 105 private static final String TEST_LABEL_CF = "test_call_forwarding"; 106 private static final String TEST_SUB_INFO_DISPLAY_NAME = "display_name"; 107 private static final String TEST_PACKAGE_NAME = "com.android.phone"; 108 private static final String TEST_SELECTED_NETWORK_OPERATOR_NAME = "TheOperator"; 109 private static final String MOBILE_NETWORK_SELECTION_PACKAGE = "com.android.phone"; 110 private static final String MOBILE_NETWORK_SELECTION_CLASS = ".testClass"; 111 private static final String CARRIER_NAME = "CoolCarrier"; 112 113 @Mock PhoneGlobals mApp; 114 @Mock StatusBarManager mStatusBarManager; 115 @Mock UserManager mUserManager; 116 @Mock SubscriptionManager mSubscriptionManager; 117 @Mock TelecomManager mTelecomManager; 118 @Mock TelephonyManager mTelephonyManager; 119 @Mock Phone mPhone; 120 @Mock SharedPreferences mSharedPreferences; 121 @Mock NotificationManager mNotificationManager; 122 @Mock SubscriptionInfo mSubscriptionInfo; 123 @Mock Resources mResources; 124 @Mock Context mMockedContext; 125 @Mock ServiceStateTracker mServiceStateTracker; 126 @Mock ServiceState mServiceState; 127 @Mock CarrierConfigManager mCarrierConfigManager; 128 @Mock DataNetworkController mDataNetworkController; 129 @Mock DataSettingsManager mDataSettingsManager; 130 @Mock DataConfigManager mDataConfigManager; 131 @Mock SignalStrengthController mSignalStrengthController; 132 133 private Phone[] mPhones; 134 private NotificationMgr mNotificationMgr; 135 private TestableLooper mTestableLooper; 136 137 @Before setUp()138 public void setUp() throws Exception { 139 MockitoAnnotations.initMocks(this); 140 mPhones = new Phone[]{mPhone}; 141 replaceInstance(PhoneFactory.class, "sPhones", null, mPhones); 142 when(mPhone.getPhoneType()).thenReturn(PhoneConstants.PHONE_TYPE_GSM); 143 when(mPhone.getContext()).thenReturn(mMockedContext); 144 when(mMockedContext.getResources()).thenReturn(mResources); 145 when(mPhone.getServiceState()).thenReturn(mServiceState); 146 when(mServiceState.getNetworkRegistrationInfo(anyInt(), anyInt())).thenReturn( 147 new NetworkRegistrationInfo.Builder() 148 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_LTE) 149 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME) 150 .build()); 151 when(mPhone.getServiceStateTracker()).thenReturn(mServiceStateTracker); 152 mServiceStateTracker.mSS = mServiceState; 153 when(mPhone.getSignalStrengthController()).thenReturn(mSignalStrengthController); 154 when(mPhone.getDataNetworkController()).thenReturn(mDataNetworkController); 155 when(mDataNetworkController.getInternetDataDisallowedReasons()).thenReturn( 156 Collections.emptyList()); 157 when(mDataNetworkController.getDataConfigManager()).thenReturn(mDataConfigManager); 158 when(mPhone.getDataSettingsManager()).thenReturn(mDataSettingsManager); 159 when(mDataSettingsManager.isDataEnabledForReason(anyInt())).thenReturn(true); 160 when(mApp.getSharedPreferences(anyString(), anyInt())).thenReturn(mSharedPreferences); 161 162 when(mApp.getPackageName()).thenReturn(TEST_PACKAGE_NAME); 163 when(mApp.getSystemService(Context.STATUS_BAR_SERVICE)).thenReturn(mStatusBarManager); 164 when(mApp.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 165 when(mApp.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE)).thenReturn( 166 mSubscriptionManager); 167 when(mApp.getSystemServiceName(TelecomManager.class)).thenReturn(Context.TELECOM_SERVICE); 168 when(mApp.getSystemService(TelecomManager.class)).thenReturn(mTelecomManager); 169 when(mApp.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager); 170 when(mApp.getSystemServiceName(CarrierConfigManager.class)).thenReturn( 171 Context.CARRIER_CONFIG_SERVICE); 172 when(mApp.getSystemService(CarrierConfigManager.class)).thenReturn(mCarrierConfigManager); 173 when(mApp.getSystemServiceName(CarrierConfigManager.class)).thenReturn( 174 Context.CARRIER_CONFIG_SERVICE); 175 when(mApp.getSystemService(CarrierConfigManager.class)).thenReturn(mCarrierConfigManager); 176 177 when(mApp.createPackageContextAsUser(any(), eq(0), any())).thenReturn(mApp); 178 when(mApp.getSystemService(Context.NOTIFICATION_SERVICE)).thenReturn(mNotificationManager); 179 when(mUserManager.getSerialNumbersOfUsers(true)).thenReturn( 180 new long[]{SERIAL_NUMBER_OF_USER}); 181 when(mUserManager.getUserForSerialNumber(eq(SERIAL_NUMBER_OF_USER))).thenReturn( 182 UserHandle.SYSTEM); 183 when(mApp.getResources()).thenReturn(mResources); 184 when(mResources.getString(R.string.labelCF)).thenReturn(TEST_LABEL_CF); 185 ApplicationInfo appWithSdkS = buildApplicationInfo(Build.VERSION_CODES.S); 186 when(mApp.getApplicationInfo()).thenReturn(appWithSdkS); 187 when(mTelephonyManager.createForSubscriptionId(anyInt())).thenReturn(mTelephonyManager); 188 when(mTelephonyManager.getServiceState()).thenReturn(mServiceState); 189 190 mTestableLooper = TestableLooper.get(this); 191 // Spy it only to avoid sleep for SystemClock.elapsedRealtime() 192 mNotificationMgr = spy(new NotificationMgr(mApp)); 193 mTestableLooper.processAllMessages(); 194 } 195 196 @Test testUpdateCfi_visible_noActiveSubscription_notificationNeverSent()197 public void testUpdateCfi_visible_noActiveSubscription_notificationNeverSent() { 198 // Given no active subscription available 199 when(mSubscriptionManager.getActiveSubscriptionInfo(eq(TEST_SUB_ID))).thenReturn(null); 200 201 // When updateCfi method is called 202 mNotificationMgr.updateCfi(TEST_SUB_ID, /*visible=*/true, /*isFresh=*/false); 203 204 // Then the notification should never be sent 205 verify(mNotificationManager, never()).notify(any(), anyInt(), any()); 206 } 207 208 @Test testUpdateCfi_visible_hasActiveSub_singleSIM_notificationSent()209 public void testUpdateCfi_visible_hasActiveSub_singleSIM_notificationSent() { 210 when(mTelephonyManager.getPhoneCount()).thenReturn(1); 211 when(mSubscriptionManager.getActiveSubscriptionInfo(eq(TEST_SUB_ID))).thenReturn( 212 mSubscriptionInfo); 213 214 mNotificationMgr.updateCfi(TEST_SUB_ID, /*visible=*/true, /*isFresh=*/false); 215 216 verifyNotificationSentWithChannelId(NotificationChannelController.CHANNEL_ID_CALL_FORWARD); 217 } 218 219 @Test testUpdateCfi_visible_hasActiveSub_multiSIM_notificationSentWithoutDisplayName()220 public void testUpdateCfi_visible_hasActiveSub_multiSIM_notificationSentWithoutDisplayName() { 221 when(mTelephonyManager.getPhoneCount()).thenReturn(2); 222 when(mSubscriptionManager.getActiveSubscriptionInfo(eq(TEST_SUB_ID))).thenReturn( 223 mSubscriptionInfo); 224 when(mSubscriptionInfo.getDisplayName()).thenReturn(null); 225 226 mNotificationMgr.updateCfi(TEST_SUB_ID, /*visible=*/true, /*isFresh=*/false); 227 228 verifyNotificationSentWithChannelId(NotificationChannelController.CHANNEL_ID_CALL_FORWARD); 229 } 230 231 @Test testUpdateCfi_visible_hasActiveSub_multiSIM_notificationSentWithDisplayName()232 public void testUpdateCfi_visible_hasActiveSub_multiSIM_notificationSentWithDisplayName() { 233 when(mTelephonyManager.getPhoneCount()).thenReturn(2); 234 when(mSubscriptionManager.getActiveSubscriptionInfo(eq(TEST_SUB_ID))).thenReturn( 235 mSubscriptionInfo); 236 when(mSubscriptionInfo.getDisplayName()).thenReturn(TEST_SUB_INFO_DISPLAY_NAME); 237 238 mNotificationMgr.updateCfi(TEST_SUB_ID, /*visible=*/true, /*isFresh=*/false); 239 240 verifyNotificationSentWithChannelId(NotificationChannelController.CHANNEL_ID_CALL_FORWARD); 241 } 242 243 @Test testUpdateCfi_invisible_hasUnmanagedProfile_notificationCanceled()244 public void testUpdateCfi_invisible_hasUnmanagedProfile_notificationCanceled() { 245 when(mUserManager.isManagedProfile(anyInt())).thenReturn(false); 246 247 mNotificationMgr.updateCfi(TEST_SUB_ID, /*visible=*/false, /*isFresh=*/false); 248 249 verify(mNotificationManager).cancel(any(), anyInt()); 250 } 251 252 @Test testUpdateCfi_invisible_allProfilesAreManaged_notificationNeverCanceled()253 public void testUpdateCfi_invisible_allProfilesAreManaged_notificationNeverCanceled() { 254 when(mUserManager.isManagedProfile(anyInt())).thenReturn(true); 255 256 mNotificationMgr.updateCfi(TEST_SUB_ID, /*visible=*/false, /*isFresh=*/false); 257 258 verify(mNotificationManager, never()).cancel(any(), anyInt()); 259 } 260 261 @Test testShowDataRoamingNotification_roamingOn()262 public void testShowDataRoamingNotification_roamingOn() { 263 mNotificationMgr.showDataRoamingNotification(TEST_SUB_ID, /*roamingOn=*/true); 264 265 verifyNotificationSentWithChannelId( 266 NotificationChannelController.CHANNEL_ID_MOBILE_DATA_STATUS); 267 } 268 269 @Test testShowDataRoamingNotification_roamingOff()270 public void testShowDataRoamingNotification_roamingOff() { 271 mNotificationMgr.showDataRoamingNotification(TEST_SUB_ID, /*roamingOn=*/false); 272 273 verifyNotificationSentWithChannelId( 274 NotificationChannelController.CHANNEL_ID_MOBILE_DATA_STATUS); 275 } 276 277 @Test testHideDataRoamingNotification()278 public void testHideDataRoamingNotification() { 279 mNotificationMgr.hideDataRoamingNotification(); 280 281 verify(mNotificationManager).cancel(any(), eq(DATA_ROAMING_NOTIFICATION)); 282 } 283 284 @Test testUpdateNetworkSelection_justOutOfService_notificationNeverSent()285 public void testUpdateNetworkSelection_justOutOfService_notificationNeverSent() { 286 prepareResourcesForNetworkSelection(); 287 288 mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID); 289 moveTimeForward(2 /* seconds */); 290 mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID); 291 292 verify(mNotificationManager, never()).notify(any(), anyInt(), any()); 293 } 294 295 @Test testUpdateNetworkSelection_oosEnoughTime_selectionVisibleToUser_notificationSent()296 public void testUpdateNetworkSelection_oosEnoughTime_selectionVisibleToUser_notificationSent() { 297 prepareResourcesForNetworkSelection(); 298 when(mTelephonyManager.isManualNetworkSelectionAllowed()).thenReturn(true); 299 PersistableBundle config = new PersistableBundle(); 300 config.putBoolean(CarrierConfigManager.KEY_OPERATOR_SELECTION_EXPAND_BOOL, true); 301 config.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL, false); 302 config.putBoolean(CarrierConfigManager.KEY_CSP_ENABLED_BOOL, false); 303 config.putBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL, true); 304 when(mCarrierConfigManager.getConfigForSubId(TEST_SUB_ID)).thenReturn(config); 305 306 // update to OOS as base state 307 mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID); 308 // 10 seconds later 309 moveTimeForward(10 /* seconds */); 310 // verify the behavior on new request 311 mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID); 312 313 verifyNotificationSentWithChannelId(NotificationChannelController.CHANNEL_ID_ALERT); 314 } 315 316 @Test testUpdateNetworkSelection_invalidSubscription_notificationNotSent()317 public void testUpdateNetworkSelection_invalidSubscription_notificationNotSent() { 318 prepareResourcesForNetworkSelection(); 319 when(mTelephonyManager.isManualNetworkSelectionAllowed()).thenReturn(true); 320 PersistableBundle config = new PersistableBundle(); 321 config.putBoolean(CarrierConfigManager.KEY_OPERATOR_SELECTION_EXPAND_BOOL, true); 322 config.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL, false); 323 config.putBoolean(CarrierConfigManager.KEY_CSP_ENABLED_BOOL, false); 324 config.putBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL, true); 325 when(mCarrierConfigManager.getConfigForSubId(TEST_SUB_ID)).thenReturn(config); 326 327 mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, 328 INVALID_SUBSCRIPTION_ID); 329 moveTimeForward(10 /* seconds */); 330 mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, 331 INVALID_SUBSCRIPTION_ID); 332 333 verify(mNotificationManager, never()).notify(any(), anyInt(), any()); 334 } 335 336 @Test testUpdateNetworkSelection_nullCarrierConfig_notificationNotSent()337 public void testUpdateNetworkSelection_nullCarrierConfig_notificationNotSent() { 338 prepareResourcesForNetworkSelection(); 339 340 when(mCarrierConfigManager.getConfigForSubId(TEST_SUB_ID)).thenReturn(null); 341 342 mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID); 343 moveTimeForward(10 /* seconds */); 344 mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID); 345 346 verify(mNotificationManager, never()).notify(any(), anyInt(), any()); 347 } 348 349 @Test testUpdateNetworkSelection_userNotAllowedToChooseOperator_notificationNotSent()350 public void testUpdateNetworkSelection_userNotAllowedToChooseOperator_notificationNotSent() { 351 prepareResourcesForNetworkSelection(); 352 353 PersistableBundle config = new PersistableBundle(); 354 // User is NOT allowed to choose operator 355 config.putBoolean(CarrierConfigManager.KEY_OPERATOR_SELECTION_EXPAND_BOOL, false); 356 config.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL, false); 357 config.putBoolean(CarrierConfigManager.KEY_CSP_ENABLED_BOOL, false); 358 when(mTelephonyManager.isManualNetworkSelectionAllowed()).thenReturn(false); 359 config.putBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL, true); 360 when(mCarrierConfigManager.getConfigForSubId(TEST_SUB_ID)).thenReturn(config); 361 362 mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID); 363 moveTimeForward(10 /* seconds */); 364 mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID); 365 366 verify(mNotificationManager, never()).notify(any(), anyInt(), any()); 367 } 368 369 @Test 370 public void testUpdateNetworkSelection_OverrideHideCarrierNetworkSelection_notificationNotSent()371 testUpdateNetworkSelection_OverrideHideCarrierNetworkSelection_notificationNotSent() { 372 prepareResourcesForNetworkSelection(); 373 374 PersistableBundle config = new PersistableBundle(); 375 // Hide network selection menu 376 config.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL, true); 377 config.putBoolean(CarrierConfigManager.KEY_OPERATOR_SELECTION_EXPAND_BOOL, true); 378 config.putBoolean(CarrierConfigManager.KEY_CSP_ENABLED_BOOL, false); 379 when(mTelephonyManager.isManualNetworkSelectionAllowed()).thenReturn(false); 380 config.putBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL, true); 381 when(mCarrierConfigManager.getConfigForSubId(TEST_SUB_ID)).thenReturn(config); 382 383 mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID); 384 moveTimeForward(10 /* seconds */); 385 mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID); 386 387 verify(mNotificationManager, never()).notify(any(), anyInt(), any()); 388 } 389 390 @Test testUpdateNetworkSelection_simPreventManualSelection_notificationNotSent()391 public void testUpdateNetworkSelection_simPreventManualSelection_notificationNotSent() 392 throws Exception { 393 prepareResourcesForNetworkSelection(); 394 395 PersistableBundle config = new PersistableBundle(); 396 config.putBoolean(CarrierConfigManager.KEY_OPERATOR_SELECTION_EXPAND_BOOL, true); 397 config.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL, false); 398 // SIM card can prevent manual network selection which is forbidden 399 config.putBoolean(CarrierConfigManager.KEY_CSP_ENABLED_BOOL, true); 400 when(mTelephonyManager.isManualNetworkSelectionAllowed()).thenReturn(false); 401 config.putBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL, true); 402 when(mCarrierConfigManager.getConfigForSubId(TEST_SUB_ID)).thenReturn(config); 403 404 mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID); 405 moveTimeForward(10 /* seconds */); 406 mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID); 407 408 verify(mNotificationManager, never()).notify(any(), anyInt(), any()); 409 } 410 411 @Test testUpdateNetworkSelection_worldMode_userSetLTE_notificationNotSent()412 public void testUpdateNetworkSelection_worldMode_userSetLTE_notificationNotSent() { 413 prepareResourcesForNetworkSelection(); 414 415 PersistableBundle config = new PersistableBundle(); 416 config.putBoolean(CarrierConfigManager.KEY_OPERATOR_SELECTION_EXPAND_BOOL, true); 417 config.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL, false); 418 config.putBoolean(CarrierConfigManager.KEY_CSP_ENABLED_BOOL, false); 419 config.putBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL, true); 420 421 // World mode is on 422 config.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true); 423 // User set Network mode as LTE 424 when(mTelephonyManager.getAllowedNetworkTypesForReason( 425 TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER)).thenReturn( 426 (long) (RAF_LTE | RAF_LTE_CA | RAF_IS95A | RAF_IS95B | RAF_1xRTT | RAF_EVDO_0 427 | RAF_EVDO_A | RAF_EVDO_B | RAF_EHRPD)); 428 when(mCarrierConfigManager.getConfigForSubId(TEST_SUB_ID)).thenReturn(config); 429 430 mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID); 431 moveTimeForward(10 /* seconds */); 432 mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID); 433 434 verify(mNotificationManager, never()).notify(any(), anyInt(), any()); 435 } 436 437 @Test testUpdateNetworkSelection_worldMode_userSetTDSCDMA_notSupported_notifNotSent()438 public void testUpdateNetworkSelection_worldMode_userSetTDSCDMA_notSupported_notifNotSent() { 439 prepareResourcesForNetworkSelection(); 440 441 PersistableBundle config = new PersistableBundle(); 442 config.putBoolean(CarrierConfigManager.KEY_OPERATOR_SELECTION_EXPAND_BOOL, true); 443 config.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL, false); 444 config.putBoolean(CarrierConfigManager.KEY_CSP_ENABLED_BOOL, false); 445 config.putBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL, true); 446 447 // World mode is on 448 config.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true); 449 // User set Network mode as NETWORK_MODE_LTE_TDSCDMA_GSM 450 when(mTelephonyManager.getAllowedNetworkTypesForReason( 451 TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER)).thenReturn( 452 (long) (RAF_LTE | RAF_LTE_CA | RAF_TD_SCDMA | RAF_GSM | RAF_GPRS | RAF_EDGE)); 453 // But TDSCDMA is NOT supported 454 config.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, false); 455 when(mCarrierConfigManager.getConfigForSubId(TEST_SUB_ID)).thenReturn(config); 456 457 mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID); 458 moveTimeForward(10 /* seconds */); 459 mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID); 460 461 verify(mNotificationManager, never()).notify(any(), anyInt(), any()); 462 } 463 464 @Test testUpdateNetworkSelection_worldMode_userSetWCDMA_notificationSent()465 public void testUpdateNetworkSelection_worldMode_userSetWCDMA_notificationSent() { 466 prepareResourcesForNetworkSelection(); 467 468 PersistableBundle config = new PersistableBundle(); 469 config.putBoolean(CarrierConfigManager.KEY_OPERATOR_SELECTION_EXPAND_BOOL, true); 470 config.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL, false); 471 config.putBoolean(CarrierConfigManager.KEY_CSP_ENABLED_BOOL, false); 472 config.putBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL, true); 473 474 // World mode is on 475 config.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, true); 476 // User set Network mode as NETWORK_MODE_LTE_TDSCDMA_GSM 477 when(mTelephonyManager.getAllowedNetworkTypesForReason( 478 TelephonyManager.ALLOWED_NETWORK_TYPES_REASON_USER)).thenReturn( 479 (long) (RAF_LTE | RAF_LTE_CA | RAF_GSM | RAF_GPRS | RAF_EDGE | RAF_HSUPA | RAF_HSDPA 480 | RAF_HSPA | RAF_HSPAP | RAF_UMTS)); 481 // But TDSCDMA is NOT supported 482 config.putBoolean(CarrierConfigManager.KEY_SUPPORT_TDSCDMA_BOOL, false); 483 when(mCarrierConfigManager.getConfigForSubId(TEST_SUB_ID)).thenReturn(config); 484 485 mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID); 486 moveTimeForward(10 /* seconds */); 487 mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID); 488 489 verifyNotificationSentWithChannelId(NotificationChannelController.CHANNEL_ID_ALERT); 490 } 491 492 @Test testUpdateNetworkSelection_worldPhone_networkSelectionNotHide_notificationSent()493 public void testUpdateNetworkSelection_worldPhone_networkSelectionNotHide_notificationSent() { 494 prepareResourcesForNetworkSelection(); 495 496 PersistableBundle config = new PersistableBundle(); 497 config.putBoolean(CarrierConfigManager.KEY_OPERATOR_SELECTION_EXPAND_BOOL, true); 498 config.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL, false); 499 config.putBoolean(CarrierConfigManager.KEY_CSP_ENABLED_BOOL, false); 500 // World mode is off 501 config.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, false); 502 // World phone is on 503 config.putBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL, true); 504 when(mCarrierConfigManager.getConfigForSubId(TEST_SUB_ID)).thenReturn(config); 505 506 mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID); 507 moveTimeForward(10 /* seconds */); 508 mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID); 509 510 verifyNotificationSentWithChannelId(NotificationChannelController.CHANNEL_ID_ALERT); 511 } 512 513 @Test testUpdateNetworkSelection_gsmBasicOptionOn_notificationSent()514 public void testUpdateNetworkSelection_gsmBasicOptionOn_notificationSent() { 515 prepareResourcesForNetworkSelection(); 516 517 PersistableBundle config = new PersistableBundle(); 518 config.putBoolean(CarrierConfigManager.KEY_OPERATOR_SELECTION_EXPAND_BOOL, true); 519 config.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL, false); 520 config.putBoolean(CarrierConfigManager.KEY_CSP_ENABLED_BOOL, false); 521 // World phone is on 522 config.putBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL, true); 523 // World mode is off 524 config.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, false); 525 when(mTelephonyManager.getPhoneType()).thenReturn(TelephonyManager.PHONE_TYPE_GSM); 526 when(mCarrierConfigManager.getConfigForSubId(TEST_SUB_ID)).thenReturn(config); 527 528 mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID); 529 moveTimeForward(10 /* seconds */); 530 mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID); 531 532 verifyNotificationSentWithChannelId(NotificationChannelController.CHANNEL_ID_ALERT); 533 } 534 535 @Test testUpdateNetworkSelection_gsmBasicOptionOff_notificationNotSent()536 public void testUpdateNetworkSelection_gsmBasicOptionOff_notificationNotSent() { 537 prepareResourcesForNetworkSelection(); 538 539 PersistableBundle config = new PersistableBundle(); 540 config.putBoolean(CarrierConfigManager.KEY_OPERATOR_SELECTION_EXPAND_BOOL, true); 541 config.putBoolean(CarrierConfigManager.KEY_HIDE_CARRIER_NETWORK_SETTINGS_BOOL, false); 542 config.putBoolean(CarrierConfigManager.KEY_CSP_ENABLED_BOOL, false); 543 // World mode is off 544 config.putBoolean(CarrierConfigManager.KEY_WORLD_MODE_ENABLED_BOOL, false); 545 when(mCarrierConfigManager.getConfigForSubId(TEST_SUB_ID)).thenReturn(config); 546 when(mTelephonyManager.getPhoneType()).thenReturn(TelephonyManager.PHONE_TYPE_CDMA); 547 548 mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID); 549 moveTimeForward(10 /* seconds */); 550 mNotificationMgr.updateNetworkSelection(ServiceState.STATE_OUT_OF_SERVICE, TEST_SUB_ID); 551 552 verify(mNotificationManager, never()).notify(any(), anyInt(), any()); 553 } 554 555 @Test testShowLimitedSimFunctionWarningNotification_forTheFirstTime_notificationSent()556 public void testShowLimitedSimFunctionWarningNotification_forTheFirstTime_notificationSent() { 557 when(mResources.getText(R.string.limited_sim_function_notification_message)).thenReturn( 558 CARRIER_NAME); 559 when(mResources.getText( 560 R.string.limited_sim_function_with_phone_num_notification_message)).thenReturn( 561 "123"); 562 563 mNotificationMgr.showLimitedSimFunctionWarningNotification(TEST_SUB_ID, CARRIER_NAME); 564 565 verifyNotificationSentWithChannelId( 566 NotificationChannelController.CHANNEL_ID_SIM_HIGH_PRIORITY); 567 } 568 569 @Test 570 public void testShowLimitedSimFunctionWarningNotification_consecutiveCall_notificationSentOnce()571 testShowLimitedSimFunctionWarningNotification_consecutiveCall_notificationSentOnce() { 572 when(mResources.getText(R.string.limited_sim_function_notification_message)).thenReturn( 573 CARRIER_NAME); 574 when(mResources.getText( 575 R.string.limited_sim_function_with_phone_num_notification_message)).thenReturn( 576 "123"); 577 578 // Call the method TWICE with the same subscription 579 mNotificationMgr.showLimitedSimFunctionWarningNotification(TEST_SUB_ID, CARRIER_NAME); 580 mNotificationMgr.showLimitedSimFunctionWarningNotification(TEST_SUB_ID, CARRIER_NAME); 581 582 // Verify the notification is only sent ONCE 583 verifyNotificationSentWithChannelId( 584 NotificationChannelController.CHANNEL_ID_SIM_HIGH_PRIORITY); 585 } 586 587 @Test testDismissLimitedSimFunctionWarningNotification_noShowCalledBefore_noCancelSent()588 public void testDismissLimitedSimFunctionWarningNotification_noShowCalledBefore_noCancelSent() { 589 // showLimitedSimFunctionWarningNotification was never called before 590 591 mNotificationMgr.dismissLimitedSimFunctionWarningNotification(TEST_SUB_ID); 592 593 verify(mNotificationManager, never()).cancel(any(), anyInt()); 594 } 595 596 @Test testDismissLimitedSimFunctionWarningNotification_showCalledBefore_cancelSent()597 public void testDismissLimitedSimFunctionWarningNotification_showCalledBefore_cancelSent() { 598 when(mResources.getText(R.string.limited_sim_function_notification_message)).thenReturn( 599 CARRIER_NAME); 600 when(mResources.getText( 601 R.string.limited_sim_function_with_phone_num_notification_message)).thenReturn( 602 "123"); 603 mNotificationMgr.showLimitedSimFunctionWarningNotification(TEST_SUB_ID, CARRIER_NAME); 604 605 mNotificationMgr.dismissLimitedSimFunctionWarningNotification(TEST_SUB_ID); 606 607 verify(mNotificationManager).cancel(any(), eq(LIMITED_SIM_FUNCTION_NOTIFICATION)); 608 } 609 buildApplicationInfo(int targetSdkVersion)610 private ApplicationInfo buildApplicationInfo(int targetSdkVersion) { 611 ApplicationInfo applicationInfo = new ApplicationInfo(); 612 applicationInfo.targetSdkVersion = targetSdkVersion; 613 return applicationInfo; 614 } 615 verifyNotificationSentWithChannelId(String expectedNotificationChannelId)616 private void verifyNotificationSentWithChannelId(String expectedNotificationChannelId) { 617 ArgumentCaptor<Notification> notificationArgumentCaptor = ArgumentCaptor.forClass( 618 Notification.class); 619 verify(mNotificationManager).notify(any(), anyInt(), notificationArgumentCaptor.capture()); 620 Notification capturedNotification = notificationArgumentCaptor.getAllValues().get(0); 621 assertThat(capturedNotification.getChannelId()).isEqualTo(expectedNotificationChannelId); 622 } 623 prepareResourcesForNetworkSelection()624 private void prepareResourcesForNetworkSelection() { 625 when(mSharedPreferences.getString(Phone.NETWORK_SELECTION_NAME_KEY + TEST_SUB_ID, 626 "")).thenReturn(TEST_SELECTED_NETWORK_OPERATOR_NAME); 627 when(mResources.getBoolean( 628 com.android.internal.R.bool.skip_restoring_network_selection)).thenReturn(false); 629 when(mServiceState.getState()).thenReturn(ServiceState.STATE_OUT_OF_SERVICE); 630 when(mApp.getString(R.string.mobile_network_settings_package)).thenReturn( 631 MOBILE_NETWORK_SELECTION_PACKAGE); 632 when(mApp.getString(R.string.mobile_network_settings_class)).thenReturn( 633 MOBILE_NETWORK_SELECTION_CLASS); 634 when(mSubscriptionManager.isActiveSubId(anyInt())).thenReturn(true); 635 } 636 moveTimeForward(long seconds)637 private void moveTimeForward(long seconds) { 638 final long millis = TimeUnit.SECONDS.toMillis(seconds); 639 mTestableLooper.moveTimeForward(millis); 640 mTestableLooper.processAllMessages(); 641 doReturn(SystemClock.elapsedRealtime() + millis).when(mNotificationMgr).getTimeStamp(); 642 } 643 } 644