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