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.slice; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertNotEquals; 22 import static org.junit.Assert.assertTrue; 23 import static org.junit.Assert.fail; 24 import static org.mockito.Mockito.any; 25 import static org.mockito.Mockito.anyBoolean; 26 import static org.mockito.Mockito.anyInt; 27 import static org.mockito.Mockito.anyString; 28 import static org.mockito.Mockito.clearInvocations; 29 import static org.mockito.Mockito.doAnswer; 30 import static org.mockito.Mockito.doCallRealMethod; 31 import static org.mockito.Mockito.doReturn; 32 import static org.mockito.Mockito.eq; 33 import static org.mockito.Mockito.never; 34 import static org.mockito.Mockito.spy; 35 import static org.mockito.Mockito.verify; 36 37 import android.annotation.NonNull; 38 import android.content.BroadcastReceiver; 39 import android.content.Context; 40 import android.content.Intent; 41 import android.content.IntentFilter; 42 import android.content.SharedPreferences; 43 import android.os.AsyncResult; 44 import android.os.Handler; 45 import android.os.HandlerThread; 46 import android.os.Message; 47 import android.os.PersistableBundle; 48 import android.telephony.CarrierConfigManager; 49 import android.telephony.ServiceState; 50 import android.telephony.SubscriptionManager; 51 import android.telephony.TelephonyManager; 52 import android.telephony.data.NetworkSliceInfo; 53 import android.telephony.data.NetworkSlicingConfig; 54 import android.telephony.data.RouteSelectionDescriptor; 55 import android.telephony.data.TrafficDescriptor; 56 import android.telephony.data.UrspRule; 57 import android.testing.TestableLooper; 58 59 import androidx.test.ext.junit.runners.AndroidJUnit4; 60 61 import com.android.TelephonyTestBase; 62 import com.android.internal.telephony.CommandsInterface; 63 import com.android.internal.telephony.Phone; 64 import com.android.internal.telephony.data.DataSettingsManager; 65 import com.android.internal.telephony.flags.FeatureFlags; 66 67 import org.junit.Before; 68 import org.junit.Test; 69 import org.junit.runner.RunWith; 70 import org.mockito.Mock; 71 import org.mockito.Mockito; 72 73 import java.time.LocalDate; 74 import java.util.ArrayList; 75 import java.util.Collections; 76 import java.util.List; 77 import java.util.Map; 78 79 @RunWith(AndroidJUnit4.class) 80 public class SlicePurchaseControllerTest extends TelephonyTestBase { 81 private static final String CARRIER = "Some Carrier"; 82 private static final String DAILY_NOTIFICATION_COUNT_KEY = "daily_notification_count0"; 83 private static final String MONTHLY_NOTIFICATION_COUNT_KEY = "monthly_notification_count0"; 84 private static final int YEAR = 2000; 85 private static final int MONTH = 6; 86 private static final int DATE = 1; 87 private static final int PHONE_ID = 0; 88 private static final int DAILY_NOTIFICATION_MAX = 3; 89 private static final int MONTHLY_NOTIFICATION_MAX = 5; 90 private static final long NOTIFICATION_TIMEOUT = 1000; 91 private static final long PURCHASE_CONDITION_TIMEOUT = 2000; 92 private static final long NETWORK_SETUP_TIMEOUT = 3000; 93 private static final long THROTTLE_TIMEOUT = 4000; 94 95 @Mock Phone mPhone; 96 @Mock FeatureFlags mFeatureFlags; 97 @Mock CarrierConfigManager mCarrierConfigManager; 98 @Mock CommandsInterface mCommandsInterface; 99 @Mock ServiceState mServiceState; 100 @Mock DataSettingsManager mDataSettingsManager; 101 @Mock PremiumNetworkEntitlementApi mPremiumNetworkEntitlementApi; 102 @Mock SharedPreferences mSharedPreferences; 103 @Mock SharedPreferences.Editor mEditor; 104 105 private SlicePurchaseController mSlicePurchaseController; 106 private PersistableBundle mBundle; 107 private PremiumNetworkEntitlementResponse mEntitlementResponse; 108 private Handler mHandler; 109 private TestableLooper mTestableLooper; 110 @TelephonyManager.PurchasePremiumCapabilityResult private int mResult; 111 112 @Before setUp()113 public void setUp() throws Exception { 114 super.setUp(); 115 HandlerThread handlerThread = new HandlerThread("SlicePurchaseControllerTest"); 116 handlerThread.start(); 117 mHandler = new Handler(handlerThread.getLooper()) { 118 @Override 119 public void handleMessage(Message msg) { 120 AsyncResult ar = (AsyncResult) msg.obj; 121 mResult = (int) ar.result; 122 } 123 }; 124 mTestableLooper = new TestableLooper(mHandler.getLooper()); 125 126 doReturn(PHONE_ID).when(mPhone).getPhoneId(); 127 doReturn(mContext).when(mPhone).getContext(); 128 doReturn(mServiceState).when(mPhone).getServiceState(); 129 doReturn(mDataSettingsManager).when(mPhone).getDataSettingsManager(); 130 mPhone.mCi = mCommandsInterface; 131 132 doReturn(mCarrierConfigManager).when(mContext) 133 .getSystemService(Context.CARRIER_CONFIG_SERVICE); 134 mBundle = new PersistableBundle(); 135 mBundle.putInt( 136 CarrierConfigManager.KEY_PREMIUM_CAPABILITY_MAXIMUM_DAILY_NOTIFICATION_COUNT_INT, 137 DAILY_NOTIFICATION_MAX); 138 mBundle.putInt( 139 CarrierConfigManager.KEY_PREMIUM_CAPABILITY_MAXIMUM_MONTHLY_NOTIFICATION_COUNT_INT, 140 MONTHLY_NOTIFICATION_MAX); 141 doReturn(mBundle).when(mCarrierConfigManager).getConfigForSubId(anyInt()); 142 143 doReturn(mSharedPreferences).when(mContext).getSharedPreferences(anyString(), anyInt()); 144 doReturn(mEditor).when(mSharedPreferences).edit(); 145 doAnswer(invocation -> { 146 doReturn(invocation.getArgument(1)).when(mSharedPreferences) 147 .getInt(eq(invocation.getArgument(0)), anyInt()); 148 return null; 149 }).when(mEditor).putInt(anyString(), anyInt()); 150 doAnswer(invocation -> { 151 doReturn(invocation.getArgument(1)).when(mSharedPreferences) 152 .getString(eq(invocation.getArgument(0)), anyString()); 153 return null; 154 }).when(mEditor).putString(anyString(), anyString()); 155 156 // create a spy to mock final PendingIntent methods 157 SlicePurchaseController slicePurchaseController = 158 new SlicePurchaseController(mPhone, mFeatureFlags, mHandler.getLooper()); 159 mSlicePurchaseController = spy(slicePurchaseController); 160 doReturn(null).when(mSlicePurchaseController).createPendingIntent( 161 anyString(), anyInt(), anyBoolean()); 162 doReturn(CARRIER).when(mSlicePurchaseController).getSimOperator(); 163 replaceInstance(SlicePurchaseController.class, "sInstances", mSlicePurchaseController, 164 Map.of(PHONE_ID, mSlicePurchaseController)); 165 replaceInstance(SlicePurchaseController.class, "mIsSlicingUpsellEnabled", 166 mSlicePurchaseController, true); 167 mEntitlementResponse = new PremiumNetworkEntitlementResponse(); 168 doReturn(mPremiumNetworkEntitlementApi).when(mSlicePurchaseController) 169 .getPremiumNetworkEntitlementApi(); 170 doReturn(mEntitlementResponse).when(mPremiumNetworkEntitlementApi) 171 .checkEntitlementStatus(anyInt()); 172 } 173 174 @Test testCreatePendingIntent()175 public void testCreatePendingIntent() { 176 doCallRealMethod().when(mSlicePurchaseController).createPendingIntent( 177 anyString(), anyInt(), anyBoolean()); 178 try { 179 mSlicePurchaseController.createPendingIntent( 180 "com.android.phone.slice.action.SLICE_PURCHASE_APP_RESPONSE_CANCELED", 181 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, 182 true); 183 } catch (Exception expected) { 184 return; 185 } 186 fail("Expected createPendingIntent to throw an exception"); 187 } 188 189 @Test testIsPremiumCapabilityAvailableForPurchase()190 public void testIsPremiumCapabilityAvailableForPurchase() { 191 assertFalse(mSlicePurchaseController.isPremiumCapabilityAvailableForPurchase( 192 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY)); 193 194 // all conditions met 195 doReturn((int) TelephonyManager.NETWORK_TYPE_BITMASK_NR).when(mPhone) 196 .getCachedAllowedNetworkTypesBitmask(); 197 mBundle.putIntArray(CarrierConfigManager.KEY_SUPPORTED_PREMIUM_CAPABILITIES_INT_ARRAY, 198 new int[]{TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY}); 199 mBundle.putString(CarrierConfigManager.KEY_PREMIUM_CAPABILITY_PURCHASE_URL_STRING, 200 SlicePurchaseController.SLICE_PURCHASE_TEST_FILE); 201 doReturn(SubscriptionManager.getDefaultDataSubscriptionId()).when(mPhone).getSubId(); 202 203 // retry to verify available 204 assertTrue(mSlicePurchaseController.isPremiumCapabilityAvailableForPurchase( 205 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY)); 206 } 207 208 @Test testGetPurchaseURL()209 public void testGetPurchaseURL() { 210 mEntitlementResponse.mServiceFlowURL = SlicePurchaseController.SLICE_PURCHASE_TEST_FILE; 211 String purchaseUrl = mSlicePurchaseController.getPurchaseUrl(mEntitlementResponse); 212 assertEquals(purchaseUrl, SlicePurchaseController.SLICE_PURCHASE_TEST_FILE); 213 214 mEntitlementResponse.mServiceFlowURL = null; 215 mBundle.putString(CarrierConfigManager.KEY_PREMIUM_CAPABILITY_PURCHASE_URL_STRING, 216 SlicePurchaseController.SLICE_PURCHASE_TEST_FILE); 217 purchaseUrl = mSlicePurchaseController.getPurchaseUrl(mEntitlementResponse); 218 assertEquals(purchaseUrl, SlicePurchaseController.SLICE_PURCHASE_TEST_FILE); 219 220 String[] invalidUrls = new String[] { 221 null, 222 "", 223 "www.google.com", 224 "htt://www.google.com", 225 "http//www.google.com", 226 "http:/www.google.com", 227 "file:///android_asset/", 228 "file:///android_asset/slice_store_test.html" 229 }; 230 for (String url : invalidUrls) { 231 mBundle.putString(CarrierConfigManager.KEY_PREMIUM_CAPABILITY_PURCHASE_URL_STRING, url); 232 assertEquals("", mSlicePurchaseController.getPurchaseUrl(mEntitlementResponse)); 233 } 234 } 235 236 @Test testUpdateNotificationCounts()237 public void testUpdateNotificationCounts() { 238 mSlicePurchaseController.setLocalDate(LocalDate.of(YEAR, MONTH, DATE)); 239 mSlicePurchaseController.updateNotificationCounts(); 240 241 // change only date, month and year remain the same 242 Mockito.clearInvocations(mEditor); 243 mSlicePurchaseController.setLocalDate(LocalDate.of(YEAR, MONTH, DATE + 1)); 244 mSlicePurchaseController.updateNotificationCounts(); 245 verify(mEditor).putInt(eq(DAILY_NOTIFICATION_COUNT_KEY), eq(0)); 246 verify(mEditor, never()).putInt(eq(MONTHLY_NOTIFICATION_COUNT_KEY), eq(0)); 247 248 // change only month, date and year remain the same 249 Mockito.clearInvocations(mEditor); 250 mSlicePurchaseController.setLocalDate(LocalDate.of(YEAR, MONTH + 1, DATE + 1)); 251 mSlicePurchaseController.updateNotificationCounts(); 252 verify(mEditor).putInt(eq(DAILY_NOTIFICATION_COUNT_KEY), eq(0)); 253 verify(mEditor).putInt(eq(MONTHLY_NOTIFICATION_COUNT_KEY), eq(0)); 254 255 // change only year, date and month remain the same 256 Mockito.clearInvocations(mEditor); 257 mSlicePurchaseController.setLocalDate(LocalDate.of(YEAR + 1, MONTH + 1, DATE + 1)); 258 mSlicePurchaseController.updateNotificationCounts(); 259 verify(mEditor).putInt(eq(DAILY_NOTIFICATION_COUNT_KEY), eq(0)); 260 verify(mEditor).putInt(eq(MONTHLY_NOTIFICATION_COUNT_KEY), eq(0)); 261 262 // change only month and year, date remains the same 263 Mockito.clearInvocations(mEditor); 264 mSlicePurchaseController.setLocalDate(LocalDate.of(YEAR + 2, MONTH + 2, DATE + 1)); 265 mSlicePurchaseController.updateNotificationCounts(); 266 verify(mEditor).putInt(eq(DAILY_NOTIFICATION_COUNT_KEY), eq(0)); 267 verify(mEditor).putInt(eq(MONTHLY_NOTIFICATION_COUNT_KEY), eq(0)); 268 269 // change only date and year, month remains the same 270 Mockito.clearInvocations(mEditor); 271 mSlicePurchaseController.setLocalDate(LocalDate.of(YEAR + 3, MONTH + 2, DATE + 2)); 272 mSlicePurchaseController.updateNotificationCounts(); 273 verify(mEditor).putInt(eq(DAILY_NOTIFICATION_COUNT_KEY), eq(0)); 274 verify(mEditor).putInt(eq(MONTHLY_NOTIFICATION_COUNT_KEY), eq(0)); 275 276 // change only date and month, year remains the same 277 Mockito.clearInvocations(mEditor); 278 mSlicePurchaseController.setLocalDate(LocalDate.of(YEAR + 3, MONTH + 3, DATE + 3)); 279 mSlicePurchaseController.updateNotificationCounts(); 280 verify(mEditor).putInt(eq(DAILY_NOTIFICATION_COUNT_KEY), eq(0)); 281 verify(mEditor).putInt(eq(MONTHLY_NOTIFICATION_COUNT_KEY), eq(0)); 282 } 283 284 @Test testPurchasePremiumCapabilityResultFeatureNotSupported()285 public void testPurchasePremiumCapabilityResultFeatureNotSupported() { 286 mSlicePurchaseController.purchasePremiumCapability( 287 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, mHandler.obtainMessage()); 288 mTestableLooper.processAllMessages(); 289 assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_FEATURE_NOT_SUPPORTED, 290 mResult); 291 292 // retry after enabling feature 293 doReturn((int) TelephonyManager.NETWORK_TYPE_BITMASK_NR).when(mPhone) 294 .getCachedAllowedNetworkTypesBitmask(); 295 296 mSlicePurchaseController.purchasePremiumCapability( 297 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, mHandler.obtainMessage()); 298 mTestableLooper.processAllMessages(); 299 assertNotEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_FEATURE_NOT_SUPPORTED, 300 mResult); 301 } 302 303 @Test testPurchasePremiumCapabilityResultCarrierDisabled()304 public void testPurchasePremiumCapabilityResultCarrierDisabled() { 305 doReturn((int) TelephonyManager.NETWORK_TYPE_BITMASK_NR).when(mPhone) 306 .getCachedAllowedNetworkTypesBitmask(); 307 308 mSlicePurchaseController.purchasePremiumCapability( 309 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, mHandler.obtainMessage()); 310 mTestableLooper.processAllMessages(); 311 assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_CARRIER_DISABLED, mResult); 312 313 // retry after enabling carrier configs 314 mBundle.putIntArray(CarrierConfigManager.KEY_SUPPORTED_PREMIUM_CAPABILITIES_INT_ARRAY, 315 new int[]{TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY}); 316 mBundle.putString(CarrierConfigManager.KEY_PREMIUM_CAPABILITY_PURCHASE_URL_STRING, 317 SlicePurchaseController.SLICE_PURCHASE_TEST_FILE); 318 319 mSlicePurchaseController.purchasePremiumCapability( 320 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, mHandler.obtainMessage()); 321 mTestableLooper.processAllMessages(); 322 assertNotEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_CARRIER_DISABLED, 323 mResult); 324 } 325 326 @Test testPurchasePremiumCapabilityResultNotDefaultDataSubscription()327 public void testPurchasePremiumCapabilityResultNotDefaultDataSubscription() { 328 doReturn((int) TelephonyManager.NETWORK_TYPE_BITMASK_NR).when(mPhone) 329 .getCachedAllowedNetworkTypesBitmask(); 330 mBundle.putIntArray(CarrierConfigManager.KEY_SUPPORTED_PREMIUM_CAPABILITIES_INT_ARRAY, 331 new int[]{TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY}); 332 mBundle.putString(CarrierConfigManager.KEY_PREMIUM_CAPABILITY_PURCHASE_URL_STRING, 333 SlicePurchaseController.SLICE_PURCHASE_TEST_FILE); 334 335 mSlicePurchaseController.purchasePremiumCapability( 336 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, mHandler.obtainMessage()); 337 mTestableLooper.processAllMessages(); 338 assertEquals( 339 TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_NOT_DEFAULT_DATA_SUBSCRIPTION, 340 mResult); 341 342 // retry on default data subscription 343 doReturn(SubscriptionManager.getDefaultDataSubscriptionId()).when(mPhone).getSubId(); 344 345 mSlicePurchaseController.purchasePremiumCapability( 346 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, mHandler.obtainMessage()); 347 mTestableLooper.processAllMessages(); 348 assertNotEquals( 349 TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_NOT_DEFAULT_DATA_SUBSCRIPTION, 350 mResult); 351 } 352 353 @Test testPurchasePremiumCapabilityResultNetworkNotAvailable()354 public void testPurchasePremiumCapabilityResultNetworkNotAvailable() { 355 doReturn((int) TelephonyManager.NETWORK_TYPE_BITMASK_NR).when(mPhone) 356 .getCachedAllowedNetworkTypesBitmask(); 357 mBundle.putIntArray(CarrierConfigManager.KEY_SUPPORTED_PREMIUM_CAPABILITIES_INT_ARRAY, 358 new int[]{TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY}); 359 mBundle.putString(CarrierConfigManager.KEY_PREMIUM_CAPABILITY_PURCHASE_URL_STRING, 360 SlicePurchaseController.SLICE_PURCHASE_TEST_FILE); 361 doReturn(SubscriptionManager.getDefaultDataSubscriptionId()).when(mPhone).getSubId(); 362 363 mSlicePurchaseController.purchasePremiumCapability( 364 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, mHandler.obtainMessage()); 365 mTestableLooper.processAllMessages(); 366 assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_NETWORK_NOT_AVAILABLE, 367 mResult); 368 369 // retry with valid network 370 doReturn(TelephonyManager.NETWORK_TYPE_NR).when(mServiceState).getDataNetworkType(); 371 doReturn(true).when(mDataSettingsManager).isDataEnabledForReason(anyInt()); 372 373 mSlicePurchaseController.purchasePremiumCapability( 374 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, mHandler.obtainMessage()); 375 mTestableLooper.processAllMessages(); 376 assertNotEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_NETWORK_NOT_AVAILABLE, 377 mResult); 378 } 379 380 @Test testPurchasePremiumCapabilityResultEntitlementCheckFailed()381 public void testPurchasePremiumCapabilityResultEntitlementCheckFailed() { 382 doReturn((int) TelephonyManager.NETWORK_TYPE_BITMASK_NR).when(mPhone) 383 .getCachedAllowedNetworkTypesBitmask(); 384 mBundle.putIntArray(CarrierConfigManager.KEY_SUPPORTED_PREMIUM_CAPABILITIES_INT_ARRAY, 385 new int[]{TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY}); 386 mBundle.putString(CarrierConfigManager.KEY_PREMIUM_CAPABILITY_PURCHASE_URL_STRING, 387 SlicePurchaseController.SLICE_PURCHASE_TEST_FILE); 388 doReturn(SubscriptionManager.getDefaultDataSubscriptionId()).when(mPhone).getSubId(); 389 doReturn(TelephonyManager.NETWORK_TYPE_NR).when(mServiceState).getDataNetworkType(); 390 doReturn(true).when(mDataSettingsManager).isDataEnabledForReason(anyInt()); 391 doReturn(null).when(mPremiumNetworkEntitlementApi).checkEntitlementStatus(anyInt()); 392 393 mSlicePurchaseController.purchasePremiumCapability( 394 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, mHandler.obtainMessage()); 395 mTestableLooper.processAllMessages(); 396 assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_CARRIER_ERROR, mResult); 397 398 // retry with provisioned response 399 mEntitlementResponse.mEntitlementStatus = 400 PremiumNetworkEntitlementResponse.PREMIUM_NETWORK_ENTITLEMENT_STATUS_INCLUDED; 401 mEntitlementResponse.mProvisionStatus = 402 PremiumNetworkEntitlementResponse.PREMIUM_NETWORK_PROVISION_STATUS_PROVISIONED; 403 doReturn(mEntitlementResponse).when(mPremiumNetworkEntitlementApi) 404 .checkEntitlementStatus(anyInt()); 405 406 mSlicePurchaseController.purchasePremiumCapability( 407 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, mHandler.obtainMessage()); 408 mTestableLooper.processAllMessages(); 409 assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_ALREADY_PURCHASED, 410 mResult); 411 412 // retry with provisioning response 413 mEntitlementResponse.mEntitlementStatus = 414 PremiumNetworkEntitlementResponse.PREMIUM_NETWORK_ENTITLEMENT_STATUS_PROVISIONING; 415 mEntitlementResponse.mProvisionStatus = 416 PremiumNetworkEntitlementResponse.PREMIUM_NETWORK_PROVISION_STATUS_IN_PROGRESS; 417 418 mSlicePurchaseController.purchasePremiumCapability( 419 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, mHandler.obtainMessage()); 420 mTestableLooper.processAllMessages(); 421 assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_ALREADY_IN_PROGRESS, 422 mResult); 423 424 // retry with disallowed response and throttling 425 mEntitlementResponse.mProvisionStatus = 426 PremiumNetworkEntitlementResponse.PREMIUM_NETWORK_PROVISION_STATUS_NOT_PROVISIONED; 427 mEntitlementResponse.mEntitlementStatus = 428 PremiumNetworkEntitlementResponse.PREMIUM_NETWORK_ENTITLEMENT_STATUS_INCOMPATIBLE; 429 mBundle.putLong(CarrierConfigManager 430 .KEY_PREMIUM_CAPABILITY_PURCHASE_CONDITION_BACKOFF_HYSTERESIS_TIME_MILLIS_LONG, 431 PURCHASE_CONDITION_TIMEOUT); 432 433 mSlicePurchaseController.purchasePremiumCapability( 434 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, mHandler.obtainMessage()); 435 mTestableLooper.processAllMessages(); 436 assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_ENTITLEMENT_CHECK_FAILED, 437 mResult); 438 439 // retry to verify throttled 440 mSlicePurchaseController.purchasePremiumCapability( 441 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, mHandler.obtainMessage()); 442 mTestableLooper.processAllMessages(); 443 assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_THROTTLED, mResult); 444 445 // retry with valid entitlement check to verify unthrottled 446 mTestableLooper.moveTimeForward(PURCHASE_CONDITION_TIMEOUT); 447 mTestableLooper.processAllMessages(); 448 449 testPurchasePremiumCapabilityResultSuccess(); 450 } 451 452 @Test testPurchasePremiumCapabilityResultAlreadyInProgress()453 public void testPurchasePremiumCapabilityResultAlreadyInProgress() { 454 sendValidPurchaseRequest(); 455 456 mSlicePurchaseController.purchasePremiumCapability( 457 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, mHandler.obtainMessage()); 458 mTestableLooper.processAllMessages(); 459 assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_ALREADY_IN_PROGRESS, 460 mResult); 461 462 // retry to verify same result 463 mSlicePurchaseController.purchasePremiumCapability( 464 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, mHandler.obtainMessage()); 465 mTestableLooper.processAllMessages(); 466 assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_ALREADY_IN_PROGRESS, 467 mResult); 468 } 469 470 @Test testPurchasePremiumCapabilityResultSuccess()471 public void testPurchasePremiumCapabilityResultSuccess() { 472 sendValidPurchaseRequest(); 473 474 // broadcast SUCCESS response from slice purchase application 475 Intent intent = new Intent(); 476 intent.setAction("com.android.phone.slice.action.SLICE_PURCHASE_APP_RESPONSE_SUCCESS"); 477 intent.putExtra(SlicePurchaseController.EXTRA_PHONE_ID, PHONE_ID); 478 intent.putExtra(SlicePurchaseController.EXTRA_PREMIUM_CAPABILITY, 479 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY); 480 mContext.getBroadcastReceiver().onReceive(mContext, intent); 481 mTestableLooper.processAllMessages(); 482 assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_SUCCESS, mResult); 483 484 // retry tested in testPurchasePremiumCapabilityResultPendingNetworkSetup 485 } 486 487 @Test testPurchasePremiumCapabilityResultPendingNetworkSetup()488 public void testPurchasePremiumCapabilityResultPendingNetworkSetup() { 489 testPurchasePremiumCapabilityResultSuccess(); 490 491 mSlicePurchaseController.purchasePremiumCapability( 492 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, mHandler.obtainMessage()); 493 mTestableLooper.processAllMessages(); 494 assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_PENDING_NETWORK_SETUP, 495 mResult); 496 497 // retry to verify unthrottled 498 mTestableLooper.moveTimeForward(NETWORK_SETUP_TIMEOUT); 499 mTestableLooper.processAllMessages(); 500 501 testPurchasePremiumCapabilityResultSuccess(); 502 } 503 504 @Test testPurchasePremiumCapabilityResultAlreadyPurchased()505 public void testPurchasePremiumCapabilityResultAlreadyPurchased() { 506 testPurchasePremiumCapabilityResultSuccess(); 507 508 sendNetworkSlicingConfig(TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, true); 509 510 mSlicePurchaseController.purchasePremiumCapability( 511 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, mHandler.obtainMessage()); 512 mTestableLooper.processAllMessages(); 513 assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_ALREADY_PURCHASED, 514 mResult); 515 516 // retry to verify same result 517 mSlicePurchaseController.purchasePremiumCapability( 518 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, mHandler.obtainMessage()); 519 mTestableLooper.processAllMessages(); 520 assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_ALREADY_PURCHASED, 521 mResult); 522 523 // retry to verify purchase expired 524 sendNetworkSlicingConfig(TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, false); 525 526 testPurchasePremiumCapabilityResultSuccess(); 527 } 528 529 @Test testPurchasePremiumCapabilityResultTimeout()530 public void testPurchasePremiumCapabilityResultTimeout() { 531 sendValidPurchaseRequest(); 532 533 mTestableLooper.moveTimeForward(NOTIFICATION_TIMEOUT); 534 mTestableLooper.processAllMessages(); 535 assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_TIMEOUT, mResult); 536 537 // retry to verify throttled 538 mSlicePurchaseController.purchasePremiumCapability( 539 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, mHandler.obtainMessage()); 540 mTestableLooper.processAllMessages(); 541 assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_THROTTLED, mResult); 542 543 // retry to verify unthrottled 544 mTestableLooper.moveTimeForward(THROTTLE_TIMEOUT); 545 mTestableLooper.processAllMessages(); 546 547 testPurchasePremiumCapabilityResultSuccess(); 548 } 549 550 @Test testPurchasePremiumCapabilityResultUserCanceled()551 public void testPurchasePremiumCapabilityResultUserCanceled() { 552 sendValidPurchaseRequest(); 553 554 // broadcast CANCELED response from slice purchase application 555 Intent intent = new Intent(); 556 intent.setAction("com.android.phone.slice.action.SLICE_PURCHASE_APP_RESPONSE_CANCELED"); 557 intent.putExtra(SlicePurchaseController.EXTRA_PHONE_ID, PHONE_ID); 558 intent.putExtra(SlicePurchaseController.EXTRA_PREMIUM_CAPABILITY, 559 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY); 560 mContext.getBroadcastReceiver().onReceive(mContext, intent); 561 mTestableLooper.processAllMessages(); 562 assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_USER_CANCELED, mResult); 563 564 // retry to verify throttled 565 mSlicePurchaseController.purchasePremiumCapability( 566 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, mHandler.obtainMessage()); 567 mTestableLooper.processAllMessages(); 568 assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_THROTTLED, mResult); 569 570 // retry to verify unthrottled 571 mTestableLooper.moveTimeForward(THROTTLE_TIMEOUT); 572 mTestableLooper.processAllMessages(); 573 574 testPurchasePremiumCapabilityResultSuccess(); 575 } 576 577 @Test testPurchasePremiumCapabilityResultCarrierError()578 public void testPurchasePremiumCapabilityResultCarrierError() { 579 sendValidPurchaseRequest(); 580 581 // broadcast CARRIER_ERROR response from slice purchase application 582 Intent intent = new Intent(); 583 intent.setAction( 584 "com.android.phone.slice.action.SLICE_PURCHASE_APP_RESPONSE_CARRIER_ERROR"); 585 intent.putExtra(SlicePurchaseController.EXTRA_PHONE_ID, PHONE_ID); 586 intent.putExtra(SlicePurchaseController.EXTRA_PREMIUM_CAPABILITY, 587 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY); 588 intent.putExtra(SlicePurchaseController.EXTRA_FAILURE_CODE, 589 SlicePurchaseController.FAILURE_CODE_CARRIER_URL_UNAVAILABLE); 590 mContext.getBroadcastReceiver().onReceive(mContext, intent); 591 mTestableLooper.processAllMessages(); 592 assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_CARRIER_ERROR, mResult); 593 594 // retry to verify throttled 595 mSlicePurchaseController.purchasePremiumCapability( 596 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, mHandler.obtainMessage()); 597 mTestableLooper.processAllMessages(); 598 assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_THROTTLED, mResult); 599 600 // retry to verify unthrottled 601 mTestableLooper.moveTimeForward(PURCHASE_CONDITION_TIMEOUT); 602 mTestableLooper.processAllMessages(); 603 604 testPurchasePremiumCapabilityResultSuccess(); 605 } 606 607 @Test testPurchasePremiumCapabilityResultRequestFailed()608 public void testPurchasePremiumCapabilityResultRequestFailed() { 609 sendValidPurchaseRequest(); 610 611 // broadcast REQUEST_FAILED response from slice purchase application 612 Intent intent = new Intent(); 613 intent.setAction( 614 "com.android.phone.slice.action.SLICE_PURCHASE_APP_RESPONSE_REQUEST_FAILED"); 615 intent.putExtra(SlicePurchaseController.EXTRA_PHONE_ID, PHONE_ID); 616 intent.putExtra(SlicePurchaseController.EXTRA_PREMIUM_CAPABILITY, 617 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY); 618 mContext.getBroadcastReceiver().onReceive(mContext, intent); 619 mTestableLooper.processAllMessages(); 620 assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_REQUEST_FAILED, mResult); 621 622 // retry to verify no throttling 623 testPurchasePremiumCapabilityResultSuccess(); 624 } 625 626 @Test testPurchasePremiumCapabilityResultNotDefaultDataSubscriptionResponse()627 public void testPurchasePremiumCapabilityResultNotDefaultDataSubscriptionResponse() { 628 sendValidPurchaseRequest(); 629 630 // broadcast NOT_DEFAULT_DATA_SUBSCRIPTION response from slice purchase application 631 Intent intent = new Intent(); 632 intent.setAction("com.android.phone.slice.action." 633 + "SLICE_PURCHASE_APP_RESPONSE_NOT_DEFAULT_DATA_SUBSCRIPTION"); 634 intent.putExtra(SlicePurchaseController.EXTRA_PHONE_ID, PHONE_ID); 635 intent.putExtra(SlicePurchaseController.EXTRA_PREMIUM_CAPABILITY, 636 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY); 637 mContext.getBroadcastReceiver().onReceive(mContext, intent); 638 mTestableLooper.processAllMessages(); 639 assertEquals( 640 TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_NOT_DEFAULT_DATA_SUBSCRIPTION, 641 mResult); 642 643 // retry to verify no throttling 644 testPurchasePremiumCapabilityResultSuccess(); 645 } 646 647 @Test testPurchasePremiumCapabilityResultNotificationsDisabled()648 public void testPurchasePremiumCapabilityResultNotificationsDisabled() { 649 doReturn(true).when(mFeatureFlags).slicingAdditionalErrorCodes(); 650 sendValidPurchaseRequest(); 651 652 // broadcast NOTIFICATIONS_DISABLED response from slice purchase application 653 Intent intent = new Intent(); 654 intent.setAction("com.android.phone.slice.action." 655 + "SLICE_PURCHASE_APP_RESPONSE_NOTIFICATIONS_DISABLED"); 656 intent.putExtra(SlicePurchaseController.EXTRA_PHONE_ID, PHONE_ID); 657 intent.putExtra(SlicePurchaseController.EXTRA_PREMIUM_CAPABILITY, 658 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY); 659 mContext.getBroadcastReceiver().onReceive(mContext, intent); 660 mTestableLooper.processAllMessages(); 661 assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_USER_DISABLED, mResult); 662 663 // retry to verify throttled 664 mSlicePurchaseController.purchasePremiumCapability( 665 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, mHandler.obtainMessage()); 666 mTestableLooper.processAllMessages(); 667 assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_THROTTLED, mResult); 668 669 // retry to verify unthrottled 670 mTestableLooper.moveTimeForward(THROTTLE_TIMEOUT); 671 mTestableLooper.processAllMessages(); 672 673 testPurchasePremiumCapabilityResultSuccess(); 674 } 675 676 @Test testPurchasePremiumCapabilityResultNotificationThrottled()677 public void testPurchasePremiumCapabilityResultNotificationThrottled() { 678 mSlicePurchaseController.setLocalDate(LocalDate.of(YEAR, MONTH, DATE)); 679 mSlicePurchaseController.updateNotificationCounts(); 680 681 for (int count = 1; count <= DAILY_NOTIFICATION_MAX; count++) { 682 completeSuccessfulPurchase(); 683 verify(mEditor).putInt(eq(DAILY_NOTIFICATION_COUNT_KEY), eq(count)); 684 verify(mEditor).putInt(eq(MONTHLY_NOTIFICATION_COUNT_KEY), eq(count)); 685 } 686 687 // retry to verify throttled 688 mSlicePurchaseController.purchasePremiumCapability( 689 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, mHandler.obtainMessage()); 690 mTestableLooper.processAllMessages(); 691 assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_THROTTLED, mResult); 692 693 // change the date to trigger daily reset 694 mSlicePurchaseController.setLocalDate(LocalDate.of(YEAR, MONTH, DATE + 1)); 695 Mockito.clearInvocations(mEditor); 696 697 for (int count = 1; count <= (MONTHLY_NOTIFICATION_MAX - DAILY_NOTIFICATION_MAX); count++) { 698 completeSuccessfulPurchase(); 699 verify(mEditor).putInt(eq(DAILY_NOTIFICATION_COUNT_KEY), eq(count)); 700 verify(mEditor).putInt(eq(MONTHLY_NOTIFICATION_COUNT_KEY), 701 eq(count + DAILY_NOTIFICATION_MAX)); 702 } 703 704 // retry to verify throttled 705 mSlicePurchaseController.purchasePremiumCapability( 706 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, mHandler.obtainMessage()); 707 mTestableLooper.processAllMessages(); 708 assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_THROTTLED, mResult); 709 } 710 711 @Test testIsSlicingConfigActive_emptyUrspRules()712 public void testIsSlicingConfigActive_emptyUrspRules() { 713 int capability = TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY; 714 NetworkSliceInfo sliceInfo = createNetworkSliceInfo( 715 getRandomSliceServiceType(capability), true); 716 NetworkSlicingConfig slicingConfig = new NetworkSlicingConfig( 717 Collections.emptyList(), Collections.singletonList(sliceInfo)); 718 mSlicePurchaseController.setSlicingConfig(slicingConfig); 719 720 assertFalse(mSlicePurchaseController.isSlicingConfigActive(capability)); 721 } 722 723 @Test testIsSlicingConfigActive_noMatchingTrafficDescriptor()724 public void testIsSlicingConfigActive_noMatchingTrafficDescriptor() { 725 int capability = TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY; 726 NetworkSliceInfo sliceInfo = createNetworkSliceInfo( 727 getRandomSliceServiceType(capability), true); 728 TrafficDescriptor trafficDescriptor = createTrafficDescriptor("ENTERPRISE"); 729 RouteSelectionDescriptor routeSelectionDescriptor = createRouteSelectionDescriptor( 730 Collections.singletonList(sliceInfo)); 731 NetworkSlicingConfig slicingConfig = createNetworkSlicingConfig( 732 Collections.singletonList(sliceInfo), 733 Collections.singletonList(trafficDescriptor), 734 Collections.singletonList(routeSelectionDescriptor)); 735 mSlicePurchaseController.setSlicingConfig(slicingConfig); 736 737 assertFalse(mSlicePurchaseController.isSlicingConfigActive(capability)); 738 } 739 740 @Test testIsSlicingConfigActive_multipleElements()741 public void testIsSlicingConfigActive_multipleElements() { 742 int capability = TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY; 743 NetworkSliceInfo sliceInfo1 = createNetworkSliceInfo( 744 getRandomSliceServiceType(SlicePurchaseController.PREMIUM_CAPABILITY_INVALID), 745 false); 746 NetworkSliceInfo sliceInfo2 = createNetworkSliceInfo( 747 getRandomSliceServiceType(capability), true); 748 List<NetworkSliceInfo> sliceInfos = new ArrayList<>(); 749 sliceInfos.add(sliceInfo1); 750 sliceInfos.add(sliceInfo2); 751 752 TrafficDescriptor trafficDescriptor1 = createTrafficDescriptor("ENTERPRISE"); 753 TrafficDescriptor trafficDescriptor2 = createTrafficDescriptor( 754 SlicePurchaseController.getAppId(capability)); 755 List<TrafficDescriptor> trafficDescriptors = new ArrayList<>(); 756 trafficDescriptors.add(trafficDescriptor1); 757 trafficDescriptors.add(trafficDescriptor2); 758 759 RouteSelectionDescriptor routeSelectionDescriptor1 = createRouteSelectionDescriptor( 760 Collections.emptyList()); 761 RouteSelectionDescriptor routeSelectionDescriptor2 = createRouteSelectionDescriptor( 762 sliceInfos); 763 List<RouteSelectionDescriptor> routeSelectionDescriptors = new ArrayList<>(); 764 routeSelectionDescriptors.add(routeSelectionDescriptor1); 765 routeSelectionDescriptors.add(routeSelectionDescriptor2); 766 767 NetworkSlicingConfig slicingConfig = createNetworkSlicingConfig( 768 sliceInfos, trafficDescriptors, routeSelectionDescriptors); 769 mSlicePurchaseController.setSlicingConfig(slicingConfig); 770 771 assertTrue(mSlicePurchaseController.isSlicingConfigActive(capability)); 772 } 773 completeSuccessfulPurchase()774 private void completeSuccessfulPurchase() { 775 sendValidPurchaseRequest(); 776 777 // broadcast NOTIFICATION_SHOWN response from slice purchase application 778 Intent intent = new Intent(); 779 intent.setAction( 780 "com.android.phone.slice.action.SLICE_PURCHASE_APP_RESPONSE_NOTIFICATION_SHOWN"); 781 intent.putExtra(SlicePurchaseController.EXTRA_PHONE_ID, PHONE_ID); 782 intent.putExtra(SlicePurchaseController.EXTRA_PREMIUM_CAPABILITY, 783 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY); 784 mContext.getBroadcastReceiver().onReceive(mContext, intent); 785 mTestableLooper.processAllMessages(); 786 787 // broadcast SUCCESS response from slice purchase application 788 intent.setAction("com.android.phone.slice.action.SLICE_PURCHASE_APP_RESPONSE_SUCCESS"); 789 mContext.getBroadcastReceiver().onReceive(mContext, intent); 790 mTestableLooper.processAllMessages(); 791 assertEquals(TelephonyManager.PURCHASE_PREMIUM_CAPABILITY_RESULT_SUCCESS, mResult); 792 793 // complete network setup 794 sendNetworkSlicingConfig(TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, true); 795 // purchase expired 796 sendNetworkSlicingConfig(TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, false); 797 } 798 sendValidPurchaseRequest()799 private void sendValidPurchaseRequest() { 800 clearInvocations(mContext); 801 802 // feature supported 803 doReturn((int) TelephonyManager.NETWORK_TYPE_BITMASK_NR).when(mPhone) 804 .getCachedAllowedNetworkTypesBitmask(); 805 // carrier supported 806 mBundle.putIntArray(CarrierConfigManager.KEY_SUPPORTED_PREMIUM_CAPABILITIES_INT_ARRAY, 807 new int[]{TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY}); 808 mBundle.putString(CarrierConfigManager.KEY_PREMIUM_CAPABILITY_PURCHASE_URL_STRING, 809 SlicePurchaseController.SLICE_PURCHASE_TEST_FILE); 810 mBundle.putLong(CarrierConfigManager 811 .KEY_PREMIUM_CAPABILITY_NOTIFICATION_DISPLAY_TIMEOUT_MILLIS_LONG, 812 NOTIFICATION_TIMEOUT); 813 mBundle.putLong(CarrierConfigManager.KEY_PREMIUM_CAPABILITY_NETWORK_SETUP_TIME_MILLIS_LONG, 814 NETWORK_SETUP_TIMEOUT); 815 mBundle.putLong(CarrierConfigManager 816 .KEY_PREMIUM_CAPABILITY_NOTIFICATION_BACKOFF_HYSTERESIS_TIME_MILLIS_LONG, 817 THROTTLE_TIMEOUT); 818 mBundle.putLong(CarrierConfigManager 819 .KEY_PREMIUM_CAPABILITY_PURCHASE_CONDITION_BACKOFF_HYSTERESIS_TIME_MILLIS_LONG, 820 PURCHASE_CONDITION_TIMEOUT); 821 // default data subscription 822 doReturn(SubscriptionManager.getDefaultDataSubscriptionId()).when(mPhone).getSubId(); 823 // network available 824 doReturn(TelephonyManager.NETWORK_TYPE_NR).when(mServiceState).getDataNetworkType(); 825 doReturn(true).when(mDataSettingsManager).isDataEnabledForReason(anyInt()); 826 // entitlement check passed 827 mEntitlementResponse.mEntitlementStatus = 828 PremiumNetworkEntitlementResponse.PREMIUM_NETWORK_ENTITLEMENT_STATUS_ENABLED; 829 mEntitlementResponse.mProvisionStatus = 830 PremiumNetworkEntitlementResponse.PREMIUM_NETWORK_PROVISION_STATUS_NOT_PROVISIONED; 831 832 // send purchase request 833 mSlicePurchaseController.purchasePremiumCapability( 834 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY, mHandler.obtainMessage()); 835 mTestableLooper.processAllMessages(); 836 837 // verify that the purchase request was sent successfully 838 verify(mContext).sendBroadcast(any(Intent.class)); 839 assertEquals(SlicePurchaseController.ACTION_START_SLICE_PURCHASE_APP, 840 mContext.getBroadcast().getAction()); 841 assertTrue(mSlicePurchaseController.hasMessages(4 /* EVENT_PURCHASE_TIMEOUT */, 842 TelephonyManager.PREMIUM_CAPABILITY_PRIORITIZE_LATENCY)); 843 verify(mContext).registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class), 844 eq(Context.RECEIVER_NOT_EXPORTED)); 845 } 846 sendNetworkSlicingConfig(int capability, boolean configActive)847 private void sendNetworkSlicingConfig(int capability, boolean configActive) { 848 NetworkSliceInfo sliceInfo = createNetworkSliceInfo( 849 getRandomSliceServiceType(capability), configActive); 850 TrafficDescriptor trafficDescriptor = createTrafficDescriptor( 851 SlicePurchaseController.getAppId(capability)); 852 RouteSelectionDescriptor routeSelectionDescriptor = createRouteSelectionDescriptor( 853 Collections.singletonList(sliceInfo)); 854 NetworkSlicingConfig slicingConfig = createNetworkSlicingConfig( 855 Collections.singletonList(sliceInfo), 856 Collections.singletonList(trafficDescriptor), 857 Collections.singletonList(routeSelectionDescriptor)); 858 mSlicePurchaseController.obtainMessage(2 /* EVENT_SLICING_CONFIG_CHANGED */, 859 new AsyncResult(null, slicingConfig, null)).sendToTarget(); 860 mTestableLooper.processAllMessages(); 861 } 862 getRandomSliceServiceType( @elephonyManager.PremiumCapability int capability)863 @NetworkSliceInfo.SliceServiceType private int getRandomSliceServiceType( 864 @TelephonyManager.PremiumCapability int capability) { 865 for (int sliceServiceType : SlicePurchaseController.getSliceServiceTypes(capability)) { 866 // Get a random valid sst from the set 867 return sliceServiceType; 868 } 869 return NetworkSliceInfo.SLICE_SERVICE_TYPE_NONE; 870 } 871 createNetworkSliceInfo( @etworkSliceInfo.SliceServiceType int sliceServiceType, boolean active)872 @NonNull private NetworkSliceInfo createNetworkSliceInfo( 873 @NetworkSliceInfo.SliceServiceType int sliceServiceType, boolean active) { 874 return new NetworkSliceInfo.Builder() 875 .setStatus(active ? NetworkSliceInfo.SLICE_STATUS_ALLOWED 876 : NetworkSliceInfo.SLICE_STATUS_UNKNOWN) 877 .setSliceServiceType(sliceServiceType) 878 .build(); 879 } 880 createTrafficDescriptor(@onNull String appId)881 @NonNull private TrafficDescriptor createTrafficDescriptor(@NonNull String appId) { 882 TrafficDescriptor.OsAppId osAppId = new TrafficDescriptor.OsAppId( 883 TrafficDescriptor.OsAppId.ANDROID_OS_ID, appId); 884 return new TrafficDescriptor.Builder() 885 .setOsAppId(osAppId.getBytes()) 886 .build(); 887 } 888 createRouteSelectionDescriptor( @onNull List<NetworkSliceInfo> sliceInfos)889 @NonNull private RouteSelectionDescriptor createRouteSelectionDescriptor( 890 @NonNull List<NetworkSliceInfo> sliceInfos) { 891 return new RouteSelectionDescriptor( 892 RouteSelectionDescriptor.MIN_ROUTE_PRECEDENCE, 893 RouteSelectionDescriptor.SESSION_TYPE_IPV4, 894 RouteSelectionDescriptor.ROUTE_SSC_MODE_1, 895 sliceInfos, Collections.emptyList()); 896 } 897 createNetworkSlicingConfig( @onNull List<NetworkSliceInfo> sliceInfos, @NonNull List<TrafficDescriptor> trafficDescriptors, @NonNull List<RouteSelectionDescriptor> routeSelectionDescriptors)898 @NonNull private NetworkSlicingConfig createNetworkSlicingConfig( 899 @NonNull List<NetworkSliceInfo> sliceInfos, 900 @NonNull List<TrafficDescriptor> trafficDescriptors, 901 @NonNull List<RouteSelectionDescriptor> routeSelectionDescriptors) { 902 UrspRule urspRule = new UrspRule(0, trafficDescriptors, routeSelectionDescriptors); 903 return new NetworkSlicingConfig(Collections.singletonList(urspRule), sliceInfos); 904 } 905 } 906