• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 package com.android.internal.telephony.euicc;
17 
18 import static android.telephony.euicc.EuiccManager.EUICC_OTA_STATUS_UNAVAILABLE;
19 
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertNull;
24 import static org.junit.Assert.assertTrue;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.anyBoolean;
27 import static org.mockito.ArgumentMatchers.anyInt;
28 import static org.mockito.ArgumentMatchers.anyString;
29 import static org.mockito.ArgumentMatchers.eq;
30 import static org.mockito.Mockito.doAnswer;
31 import static org.mockito.Mockito.doNothing;
32 import static org.mockito.Mockito.doReturn;
33 import static org.mockito.Mockito.doThrow;
34 import static org.mockito.Mockito.never;
35 import static org.mockito.Mockito.verify;
36 import static org.mockito.Mockito.when;
37 
38 import android.Manifest;
39 import android.annotation.Nullable;
40 import android.app.PendingIntent;
41 import android.content.Context;
42 import android.content.Intent;
43 import android.content.pm.PackageInfo;
44 import android.content.pm.PackageManager;
45 import android.content.pm.Signature;
46 import android.os.Parcelable;
47 import android.os.RemoteException;
48 import android.provider.Settings;
49 import android.service.euicc.DownloadSubscriptionResult;
50 import android.service.euicc.EuiccService;
51 import android.service.euicc.GetDefaultDownloadableSubscriptionListResult;
52 import android.service.euicc.GetDownloadableSubscriptionMetadataResult;
53 import android.telephony.SubscriptionInfo;
54 import android.telephony.SubscriptionManager;
55 import android.telephony.TelephonyManager;
56 import android.telephony.UiccAccessRule;
57 import android.telephony.UiccCardInfo;
58 import android.telephony.euicc.DownloadableSubscription;
59 import android.telephony.euicc.EuiccInfo;
60 import android.telephony.euicc.EuiccManager;
61 
62 import androidx.test.runner.AndroidJUnit4;
63 
64 import com.android.internal.telephony.TelephonyTest;
65 import com.android.internal.telephony.euicc.EuiccConnector.GetOtaStatusCommandCallback;
66 import com.android.internal.telephony.euicc.EuiccConnector.OtaStatusChangedCallback;
67 
68 import org.junit.After;
69 import org.junit.Before;
70 import org.junit.Test;
71 import org.junit.runner.RunWith;
72 import org.mockito.Mock;
73 import org.mockito.Mockito;
74 import org.mockito.invocation.InvocationOnMock;
75 import org.mockito.stubbing.Answer;
76 import org.mockito.stubbing.Stubber;
77 
78 import java.security.MessageDigest;
79 import java.security.NoSuchAlgorithmException;
80 import java.util.ArrayList;
81 import java.util.Arrays;
82 import java.util.Collections;
83 
84 @RunWith(AndroidJUnit4.class)
85 public class EuiccControllerTest extends TelephonyTest {
86     private static final DownloadableSubscription SUBSCRIPTION =
87             DownloadableSubscription.forActivationCode("abcde");
88 
89     private static final String PACKAGE_NAME = "test.package";
90     private static final String CARRIER_NAME = "test name";
91     private static final byte[] SIGNATURE_BYTES = new byte[] {1, 2, 3, 4, 5};
92 
93     private static final UiccAccessRule ACCESS_RULE;
94     static {
95         try {
96             ACCESS_RULE = new UiccAccessRule(
97                     MessageDigest.getInstance("SHA-256").digest(SIGNATURE_BYTES),
98                     PACKAGE_NAME,
99                     0);
100         } catch (NoSuchAlgorithmException e) {
101             throw new RuntimeException("SHA-256 must exist");
102         }
103     }
104 
105     private static final DownloadableSubscription SUBSCRIPTION_WITH_METADATA =
106             DownloadableSubscription.forActivationCode("abcde");
107     static {
108         SUBSCRIPTION_WITH_METADATA.setCarrierName("test name");
109         SUBSCRIPTION_WITH_METADATA.setAccessRules(
Arrays.asList(new UiccAccessRule[] { ACCESS_RULE })110                 Arrays.asList(new UiccAccessRule[] { ACCESS_RULE }));
111     }
112 
113     private static final String OS_VERSION = "1.0";
114     private static final EuiccInfo EUICC_INFO = new EuiccInfo(OS_VERSION);
115 
116     private static final int SUBSCRIPTION_ID = 12345;
117     private static final String ICC_ID = "54321";
118     private static final int CARD_ID = 25;
119 
120     @Mock private EuiccConnector mMockConnector;
121     private TestEuiccController mController;
122     private int mSavedEuiccProvisionedValue;
123 
124     private static class TestEuiccController extends EuiccController {
125         // Captured arguments to addResolutionIntent
126         private String mResolutionAction;
127         private EuiccOperation mOp;
128 
129         // Captured arguments to sendResult.
130         private PendingIntent mCallbackIntent;
131         private int mResultCode;
132         private Intent mExtrasIntent;
133 
134         // Whether refreshSubscriptionsAndSendResult was called.
135         private boolean mCalledRefreshSubscriptionsAndSendResult;
136 
137         // Number of OTA status changed.
138         private int mNumOtaStatusChanged;
139 
TestEuiccController(Context context, EuiccConnector connector)140         TestEuiccController(Context context, EuiccConnector connector) {
141             super(context, connector);
142             mNumOtaStatusChanged = 0;
143         }
144 
145         @Override
addResolutionIntent( Intent extrasIntent, String resolutionAction, String callingPackage, int resolvableErrors, boolean confirmationCodeRetried, EuiccOperation op, int cardId)146         public void addResolutionIntent(
147                 Intent extrasIntent, String resolutionAction, String callingPackage,
148                 int resolvableErrors, boolean confirmationCodeRetried, EuiccOperation op,
149                 int cardId) {
150             mResolutionAction = resolutionAction;
151             mOp = op;
152         }
153 
154         @Override
sendResult(PendingIntent callbackIntent, int resultCode, Intent extrasIntent)155         public void sendResult(PendingIntent callbackIntent, int resultCode, Intent extrasIntent) {
156             assertNull("sendResult called twice unexpectedly", mCallbackIntent);
157             mCallbackIntent = callbackIntent;
158             mResultCode = resultCode;
159             mExtrasIntent = extrasIntent;
160         }
161 
162         @Override
refreshSubscriptionsAndSendResult( PendingIntent callbackIntent, int resultCode, Intent extrasIntent)163         public void refreshSubscriptionsAndSendResult(
164                 PendingIntent callbackIntent, int resultCode, Intent extrasIntent) {
165             mCalledRefreshSubscriptionsAndSendResult = true;
166             sendResult(callbackIntent, resultCode, extrasIntent);
167         }
168 
169         @Override
sendOtaStatusChangedBroadcast()170         public void sendOtaStatusChangedBroadcast() {
171             ++mNumOtaStatusChanged;
172         }
173     }
174 
175     @Before
setUp()176     public void setUp() throws Exception {
177         super.setUp("EuiccControllerTest");
178         mController = new TestEuiccController(mContext, mMockConnector);
179 
180         PackageInfo pi = new PackageInfo();
181         pi.packageName = PACKAGE_NAME;
182         pi.signatures = new Signature[] { new Signature(SIGNATURE_BYTES) };
183         when(mPackageManager.getPackageInfo(eq(PACKAGE_NAME), anyInt())).thenReturn(pi);
184 
185         mSavedEuiccProvisionedValue = Settings.Global.getInt(mContext.getContentResolver(),
186                 Settings.Global.EUICC_PROVISIONED, 0);
187         Settings.Global.putInt(mContext.getContentResolver(),
188                 Settings.Global.EUICC_PROVISIONED, 0);
189     }
190 
191     @After
tearDown()192     public void tearDown() throws Exception {
193         super.tearDown();
194         Settings.Global.putInt(mContext.getContentResolver(),
195                 Settings.Global.EUICC_PROVISIONED, mSavedEuiccProvisionedValue);
196     }
197 
198     @Test(expected = SecurityException.class)
testGetEid_noPrivileges()199     public void testGetEid_noPrivileges() throws Exception {
200         setGetEidPermissions(false /* hasPhoneStatePrivileged */, false /* hasCarrierPrivileges */);
201         callGetEid(true /* success */, "ABCDE" /* eid */, CARD_ID);
202     }
203 
204     @Test
testGetEid_withPhoneStatePrivileged()205     public void testGetEid_withPhoneStatePrivileged() throws Exception {
206         setGetEidPermissions(true /* hasPhoneStatePrivileged */, false /* hasCarrierPrivileges */);
207         assertEquals("ABCDE", callGetEid(true /* success */, "ABCDE" /* eid */, CARD_ID));
208     }
209 
210     @Test
testGetEid_withCarrierPrivileges()211     public void testGetEid_withCarrierPrivileges() throws Exception {
212         setGetEidPermissions(false /* hasPhoneStatePrivileged */, true /* hasCarrierPrivileges */);
213         assertEquals("ABCDE", callGetEid(true /* success */, "ABCDE" /* eid */, CARD_ID));
214     }
215 
216     @Test
testGetEid_failure()217     public void testGetEid_failure() throws Exception {
218         setGetEidPermissions(true /* hasPhoneStatePrivileged */, false /* hasCarrierPrivileges */);
219         assertNull(callGetEid(false /* success */, null /* eid */, CARD_ID));
220     }
221 
222     @Test
testGetEid_nullReturnValue()223     public void testGetEid_nullReturnValue() throws Exception {
224         setGetEidPermissions(true /* hasPhoneStatePrivileged */, false /* hasCarrierPrivileges */);
225         assertNull(callGetEid(true /* success */, null /* eid */, CARD_ID));
226     }
227 
228     @Test
testGetEid_unsupportedCardId()229     public void testGetEid_unsupportedCardId() throws Exception {
230         setGetEidPermissions(false /* hasPhoneStatePrivileged */, true /* hasCarrierPrivileges */);
231         assertEquals("ABCDE", callGetEid(true /* success */, "ABCDE" /* eid */,
232                 TelephonyManager.UNSUPPORTED_CARD_ID));
233     }
234 
235     @Test(expected = SecurityException.class)
testGetOtaStatus_noPrivileges()236     public void testGetOtaStatus_noPrivileges() {
237         setHasWriteEmbeddedPermission(false /* hasPermission */);
238         callGetOtaStatus(true /* success */, 1 /* status */);
239     }
240 
241     @Test
testGetOtaStatus_withWriteEmbeddedPermission()242     public void testGetOtaStatus_withWriteEmbeddedPermission() {
243         setHasWriteEmbeddedPermission(true /* hasPermission */);
244         assertEquals(1, callGetOtaStatus(true /* success */, 1 /* status */));
245     }
246 
247     @Test
testGetOtaStatus_failure()248     public void testGetOtaStatus_failure() {
249         setHasWriteEmbeddedPermission(true /* hasPermission */);
250         assertEquals(
251                 EUICC_OTA_STATUS_UNAVAILABLE,
252                 callGetOtaStatus(false /* success */, 1 /* status */));
253     }
254 
255     @Test
testStartOtaUpdatingIfNecessary_serviceNotAvailable()256     public void testStartOtaUpdatingIfNecessary_serviceNotAvailable() {
257         setHasWriteEmbeddedPermission(true /* hasPermission */);
258         callStartOtaUpdatingIfNecessary(
259                 false /* serviceAvailable */, EuiccManager.EUICC_OTA_IN_PROGRESS);
260         assertEquals(mController.mNumOtaStatusChanged, 0);
261     }
262 
263     @Test
testStartOtaUpdatingIfNecessary_otaStatusChanged()264     public void testStartOtaUpdatingIfNecessary_otaStatusChanged() {
265         setHasWriteEmbeddedPermission(true /* hasPermission */);
266         callStartOtaUpdatingIfNecessary(
267                 true /* serviceAvailable */, EuiccManager.EUICC_OTA_IN_PROGRESS);
268         callStartOtaUpdatingIfNecessary(
269                 true /* serviceAvailable */, EuiccManager.EUICC_OTA_FAILED);
270         callStartOtaUpdatingIfNecessary(
271                 true /* serviceAvailable */, EuiccManager.EUICC_OTA_SUCCEEDED);
272         callStartOtaUpdatingIfNecessary(
273                 true /* serviceAvailable */, EuiccManager.EUICC_OTA_NOT_NEEDED);
274         callStartOtaUpdatingIfNecessary(
275                 true /* serviceAvailable */, EuiccManager.EUICC_OTA_STATUS_UNAVAILABLE);
276 
277         assertEquals(mController.mNumOtaStatusChanged, 5);
278     }
279 
280 
281     @Test
testGetEuiccInfo_success()282     public void testGetEuiccInfo_success() {
283         assertEquals(OS_VERSION, callGetEuiccInfo(true /* success */, EUICC_INFO).getOsVersion());
284     }
285 
286     @Test
testGetEuiccInfo_failure()287     public void testGetEuiccInfo_failure() {
288         assertNull(callGetEuiccInfo(false /* success */, null /* euiccInfo */));
289     }
290 
291     @Test
testGetEuiccInfo_nullReturnValue()292     public void testGetEuiccInfo_nullReturnValue() {
293         assertNull(callGetEuiccInfo(true /* success */, null /* euiccInfo */));
294     }
295 
296     @Test
testGetDownloadableSubscriptionMetadata_serviceUnavailable()297     public void testGetDownloadableSubscriptionMetadata_serviceUnavailable() throws Exception {
298         setHasWriteEmbeddedPermission(true);
299         callGetDownloadableSubscriptionMetadata(
300                 SUBSCRIPTION, false /* complete */, null /* result */);
301         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR,
302                 0 /* detailedCode */);
303         verify(mMockConnector).getDownloadableSubscriptionMetadata(anyInt(), any(), anyBoolean(),
304                 any());
305     }
306 
307     @Test
testGetDownloadableSubscriptionMetadata_error()308     public void testGetDownloadableSubscriptionMetadata_error() throws Exception {
309         setHasWriteEmbeddedPermission(true);
310         GetDownloadableSubscriptionMetadataResult result =
311                 new GetDownloadableSubscriptionMetadataResult(42, null /* subscription */);
312         callGetDownloadableSubscriptionMetadata(SUBSCRIPTION, true /* complete */, result);
313         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR,
314                 42 /* detailedCode */);
315     }
316 
317     @Test
testGetDownloadableSubscriptionMetadata_mustDeactivateSim()318     public void testGetDownloadableSubscriptionMetadata_mustDeactivateSim()
319             throws Exception {
320         setHasWriteEmbeddedPermission(true);
321         GetDownloadableSubscriptionMetadataResult result =
322                 new GetDownloadableSubscriptionMetadataResult(
323                         EuiccService.RESULT_MUST_DEACTIVATE_SIM, null /* subscription */);
324         callGetDownloadableSubscriptionMetadata(SUBSCRIPTION, true /* complete */, result);
325         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR,
326                 0 /* detailedCode */);
327         verifyResolutionIntent(EuiccService.ACTION_RESOLVE_DEACTIVATE_SIM,
328                 EuiccOperation.ACTION_GET_METADATA_DEACTIVATE_SIM);
329     }
330 
331     @Test
testGetDownloadableSubscriptionMetadata_success()332     public void testGetDownloadableSubscriptionMetadata_success() throws Exception {
333         setHasWriteEmbeddedPermission(true);
334         GetDownloadableSubscriptionMetadataResult result =
335                 new GetDownloadableSubscriptionMetadataResult(
336                         EuiccService.RESULT_OK, SUBSCRIPTION_WITH_METADATA);
337         callGetDownloadableSubscriptionMetadata(SUBSCRIPTION, true /* complete */, result);
338         Intent intent = verifyIntentSent(
339                 EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */);
340         DownloadableSubscription receivedSubscription = intent.getParcelableExtra(
341                 EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DOWNLOADABLE_SUBSCRIPTION);
342         assertNotNull(receivedSubscription);
343         assertEquals(CARRIER_NAME, receivedSubscription.getCarrierName());
344     }
345 
346     @Test
testGetDefaultDownloadableSubscriptionList_serviceUnavailable()347     public void testGetDefaultDownloadableSubscriptionList_serviceUnavailable() throws Exception {
348         setHasWriteEmbeddedPermission(true);
349         callGetDefaultDownloadableSubscriptionList(false /* complete */, null /* result */);
350         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR,
351                 0 /* detailedCode */);
352     }
353 
354     @Test
testGetDefaultDownloadableSubscriptionList_error()355     public void testGetDefaultDownloadableSubscriptionList_error() throws Exception {
356         setHasWriteEmbeddedPermission(true);
357         GetDefaultDownloadableSubscriptionListResult result =
358                 new GetDefaultDownloadableSubscriptionListResult(42, null /* subscriptions */);
359         callGetDefaultDownloadableSubscriptionList(true /* complete */, result);
360         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR,
361                 42 /* detailedCode */);
362         verify(mMockConnector).getDefaultDownloadableSubscriptionList(anyInt(), anyBoolean(),
363                 any());
364     }
365 
366     @Test
testGetDefaultDownloadableSubscriptionList_mustDeactivateSim()367     public void testGetDefaultDownloadableSubscriptionList_mustDeactivateSim()
368             throws Exception {
369         setHasWriteEmbeddedPermission(true);
370         GetDefaultDownloadableSubscriptionListResult result =
371                 new GetDefaultDownloadableSubscriptionListResult(
372                         EuiccService.RESULT_MUST_DEACTIVATE_SIM, null /* subscriptions */);
373         callGetDefaultDownloadableSubscriptionList(true /* complete */, result);
374         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR,
375                 0 /* detailedCode */);
376         verifyResolutionIntent(EuiccService.ACTION_RESOLVE_DEACTIVATE_SIM,
377                 EuiccOperation.ACTION_GET_DEFAULT_LIST_DEACTIVATE_SIM);
378     }
379 
380     @Test
testGetDefaultDownloadableSubscriptionList_success()381     public void testGetDefaultDownloadableSubscriptionList_success() throws Exception {
382         setHasWriteEmbeddedPermission(true);
383         GetDefaultDownloadableSubscriptionListResult result =
384                 new GetDefaultDownloadableSubscriptionListResult(
385                         EuiccService.RESULT_OK,
386                         new DownloadableSubscription[] { SUBSCRIPTION_WITH_METADATA });
387         callGetDefaultDownloadableSubscriptionList(true /* complete */, result);
388         Intent intent = verifyIntentSent(
389                 EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */);
390         Parcelable[] receivedSubscriptions = intent.getParcelableArrayExtra(
391                 EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DOWNLOADABLE_SUBSCRIPTIONS);
392         assertNotNull(receivedSubscriptions);
393         assertEquals(1, receivedSubscriptions.length);
394         assertEquals(CARRIER_NAME,
395                 ((DownloadableSubscription) receivedSubscriptions[0]).getCarrierName());
396     }
397 
398     @Test
testDownloadSubscription_serviceUnavailable()399     public void testDownloadSubscription_serviceUnavailable() throws Exception {
400         setHasWriteEmbeddedPermission(true);
401         callDownloadSubscription(
402                 SUBSCRIPTION, true /* switchAfterDownload */, false /* complete */,
403                 0 /* result */,  0 /* resolvableError */, "whatever" /* callingPackage */);
404         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR,
405                 0 /* detailedCode */);
406         verify(mMockConnector).downloadSubscription(anyInt(),
407                     any(), anyBoolean(), anyBoolean(), any(), any());
408     }
409 
410     @Test
testDownloadSubscription_error()411     public void testDownloadSubscription_error() throws Exception {
412         setHasWriteEmbeddedPermission(true);
413         callDownloadSubscription(SUBSCRIPTION, false /* switchAfterDownload */, true /* complete */,
414                 42,  0 /* resolvableError */, "whatever" /* callingPackage */);
415         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR,
416                 42 /* detailedCode */);
417     }
418 
419     @Test
testDownloadSubscription_mustDeactivateSim()420     public void testDownloadSubscription_mustDeactivateSim() throws Exception {
421         setHasWriteEmbeddedPermission(true);
422         callDownloadSubscription(SUBSCRIPTION, false /* switchAfterDownload */, true /* complete */,
423                 EuiccService.RESULT_MUST_DEACTIVATE_SIM, 0 /* resolvableError */,
424                 "whatever" /* callingPackage */);
425         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR,
426                 0 /* detailedCode */);
427         verifyResolutionIntent(EuiccService.ACTION_RESOLVE_DEACTIVATE_SIM,
428                 EuiccOperation.ACTION_DOWNLOAD_DEACTIVATE_SIM);
429     }
430 
431     @Test
testDownloadSubscription_needConfirmationCode()432     public void testDownloadSubscription_needConfirmationCode() throws Exception {
433         setHasWriteEmbeddedPermission(true);
434         callDownloadSubscription(SUBSCRIPTION, false /* switchAfterDownload */, true /* complete */,
435                 EuiccService.RESULT_RESOLVABLE_ERRORS, 0b01 /* resolvableError */,
436                 "whatever" /* callingPackage */);
437         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR,
438                 0 /* detailedCode */);
439         verifyResolutionIntent(EuiccService.ACTION_RESOLVE_RESOLVABLE_ERRORS,
440                 EuiccOperation.ACTION_DOWNLOAD_RESOLVABLE_ERRORS);
441     }
442 
443     @Test
testDownloadSubscription_success()444     public void testDownloadSubscription_success() throws Exception {
445         setHasWriteEmbeddedPermission(true);
446         callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */,
447                 EuiccService.RESULT_OK, 0 /* resolvableError */, "whatever" /* callingPackage */);
448         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */);
449         // switchAfterDownload = true so no refresh should occur.
450         assertFalse(mController.mCalledRefreshSubscriptionsAndSendResult);
451     }
452 
453     @Test
testDownloadSubscription_noSwitch_success()454     public void testDownloadSubscription_noSwitch_success() throws Exception {
455         setHasWriteEmbeddedPermission(true);
456         callDownloadSubscription(SUBSCRIPTION, false /* switchAfterDownload */, true /* complete */,
457                 EuiccService.RESULT_OK, 0 /* resolvableError */, "whatever" /* callingPackage */);
458         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */);
459         assertTrue(mController.mCalledRefreshSubscriptionsAndSendResult);
460     }
461 
462     @Test
testDownloadSubscription_noPrivileges_getMetadata_serviceUnavailable()463     public void testDownloadSubscription_noPrivileges_getMetadata_serviceUnavailable()
464             throws Exception {
465         setHasWriteEmbeddedPermission(false);
466         prepareGetDownloadableSubscriptionMetadataCall(false /* complete */, null /* result */);
467         callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */,
468                 12345, 0 /* resolvableError */, PACKAGE_NAME /* callingPackage */);
469         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR,
470                 0 /* detailedCode */);
471         verify(mMockConnector, never()).downloadSubscription(anyInt(),
472                 any(), anyBoolean(), anyBoolean(), any(), any());
473     }
474 
475     @Test
testDownloadSubscription_noPrivileges_getMetadata_serviceUnavailable_canManageSim()476     public void testDownloadSubscription_noPrivileges_getMetadata_serviceUnavailable_canManageSim()
477             throws Exception {
478         setHasWriteEmbeddedPermission(false);
479         prepareGetDownloadableSubscriptionMetadataCall(false /* complete */, null /* result */);
480         setCanManageSubscriptionOnTargetSim(true /* isTargetEuicc */, true /* hasPrivileges */);
481         callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */,
482                 12345, 0 /* resolvableError */, PACKAGE_NAME /* callingPackage */);
483         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR,
484                 0 /* detailedCode */);
485         verify(mMockConnector, never()).downloadSubscription(anyInt(),
486                 any(), anyBoolean(), anyBoolean(), any(), any());
487     }
488 
489     @Test
testDownloadSubscription_noPrivileges_getMetadata_error()490     public void testDownloadSubscription_noPrivileges_getMetadata_error()
491             throws Exception {
492         setHasWriteEmbeddedPermission(false);
493         GetDownloadableSubscriptionMetadataResult result =
494                 new GetDownloadableSubscriptionMetadataResult(42, null /* subscription */);
495         prepareGetDownloadableSubscriptionMetadataCall(true /* complete */, result);
496         callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */,
497                 12345, 0 /* resolvableError */, PACKAGE_NAME /* callingPackage */);
498         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR,
499                 0 /* detailedCode */);
500         verify(mMockConnector, never()).downloadSubscription(anyInt(),
501                 any(), anyBoolean(), anyBoolean(), any(), any());
502     }
503 
504     @Test
testDownloadSubscription_noPrivileges_getMetadata_error_canManageSim()505     public void testDownloadSubscription_noPrivileges_getMetadata_error_canManageSim()
506             throws Exception {
507         setHasWriteEmbeddedPermission(false);
508         GetDownloadableSubscriptionMetadataResult result =
509                 new GetDownloadableSubscriptionMetadataResult(42, null /* subscription */);
510         prepareGetDownloadableSubscriptionMetadataCall(true /* complete */, result);
511         setCanManageSubscriptionOnTargetSim(true /* isTargetEuicc */, true /* hasPrivileges */);
512         callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */,
513                 12345, 0 /* resolvableError */, PACKAGE_NAME /* callingPackage */);
514         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR,
515                 42 /* detailedCode */);
516         verify(mMockConnector, never()).downloadSubscription(anyInt(),
517                 any(), anyBoolean(), anyBoolean(), any(), any());
518     }
519 
520     @Test
testDownloadSubscription_noPrivileges_getMetadata_mustDeactivateSim()521     public void testDownloadSubscription_noPrivileges_getMetadata_mustDeactivateSim()
522             throws Exception {
523         setHasWriteEmbeddedPermission(false);
524         GetDownloadableSubscriptionMetadataResult result =
525                 new GetDownloadableSubscriptionMetadataResult(
526                         EuiccService.RESULT_MUST_DEACTIVATE_SIM, null /* subscription */);
527         prepareGetDownloadableSubscriptionMetadataCall(true /* complete */, result);
528         callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */,
529                 12345, 0 /* resolvableError */, PACKAGE_NAME /* callingPackage */);
530         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR,
531                 0 /* detailedCode */);
532         // In this case we go with the potentially stronger NO_PRIVILEGES consent dialog to avoid
533         // double prompting.
534         verifyResolutionIntent(EuiccService.ACTION_RESOLVE_NO_PRIVILEGES,
535                 EuiccOperation.ACTION_DOWNLOAD_NO_PRIVILEGES_OR_DEACTIVATE_SIM_CHECK_METADATA);
536     }
537 
538     @Test
testDownloadSubscription_noPrivileges_getMetadata_mustDeactivateSim_canManageSim()539     public void testDownloadSubscription_noPrivileges_getMetadata_mustDeactivateSim_canManageSim()
540             throws Exception {
541         setHasWriteEmbeddedPermission(false);
542         GetDownloadableSubscriptionMetadataResult result =
543                 new GetDownloadableSubscriptionMetadataResult(
544                     EuiccService.RESULT_MUST_DEACTIVATE_SIM, null /* subscription */);
545         prepareGetDownloadableSubscriptionMetadataCall(true /* complete */, result);
546         setCanManageSubscriptionOnTargetSim(true /* isTargetEuicc */, true /* hasPrivileges */);
547         callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */,
548                 12345, 0 /* resolvableError */, PACKAGE_NAME /* callingPackage */);
549         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR,
550                 0 /* detailedCode */);
551         // In this case we go with the potentially stronger NO_PRIVILEGES consent dialog to avoid
552         // double prompting.
553         verifyResolutionIntent(EuiccService.ACTION_RESOLVE_DEACTIVATE_SIM,
554                 EuiccOperation.ACTION_DOWNLOAD_NO_PRIVILEGES_OR_DEACTIVATE_SIM_CHECK_METADATA);
555     }
556 
557     @Test
testDownloadSubscription_noPrivileges_hasCarrierPrivileges()558     public void testDownloadSubscription_noPrivileges_hasCarrierPrivileges() throws Exception {
559         setHasWriteEmbeddedPermission(false);
560         GetDownloadableSubscriptionMetadataResult result =
561                 new GetDownloadableSubscriptionMetadataResult(
562                         EuiccService.RESULT_OK, SUBSCRIPTION_WITH_METADATA);
563         prepareGetDownloadableSubscriptionMetadataCall(true /* complete */, result);
564         when(mTelephonyManager.getPhoneCount()).thenReturn(1);
565         setHasCarrierPrivilegesOnActiveSubscription(true);
566         callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */,
567                 EuiccService.RESULT_OK, 0 /* resolvableError */, PACKAGE_NAME /* callingPackage */);
568         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */);
569         // switchAfterDownload = true so no refresh should occur.
570         assertFalse(mController.mCalledRefreshSubscriptionsAndSendResult);
571     }
572 
573     @Test
testDownloadSubscription_noPrivileges_hasCarrierPrivileges_multiSim()574     public void testDownloadSubscription_noPrivileges_hasCarrierPrivileges_multiSim()
575             throws Exception {
576         setHasWriteEmbeddedPermission(false);
577         GetDownloadableSubscriptionMetadataResult result =
578                 new GetDownloadableSubscriptionMetadataResult(
579                     EuiccService.RESULT_OK, SUBSCRIPTION_WITH_METADATA);
580         prepareGetDownloadableSubscriptionMetadataCall(true /* complete */, result);
581         when(mTelephonyManager.getPhoneCount()).thenReturn(2);
582         setCanManageSubscriptionOnTargetSim(true /* isTargetEuicc */, true /* hasPrivileges */);
583         callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */,
584                 EuiccService.RESULT_OK, 0 /* resolvableError */, PACKAGE_NAME /* callingPackage */);
585         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */);
586         // switchAfterDownload = true so no refresh should occur.
587         assertFalse(mController.mCalledRefreshSubscriptionsAndSendResult);
588     }
589 
590     @Test
testDownloadSubscription_noPrivileges_hasCarrierPrivileges_needsConsent()591     public void testDownloadSubscription_noPrivileges_hasCarrierPrivileges_needsConsent()
592             throws Exception {
593         setHasWriteEmbeddedPermission(false);
594         GetDownloadableSubscriptionMetadataResult result =
595                 new GetDownloadableSubscriptionMetadataResult(
596                         EuiccService.RESULT_OK, SUBSCRIPTION_WITH_METADATA);
597         prepareGetDownloadableSubscriptionMetadataCall(true /* complete */, result);
598         when(mTelephonyManager.getPhoneCount()).thenReturn(1);
599         setHasCarrierPrivilegesOnActiveSubscription(false);
600         callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */,
601                 12345, 0 /* resolvableError */, PACKAGE_NAME /* callingPackage */);
602         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR,
603                 0 /* detailedCode */);
604         verify(mMockConnector, never()).downloadSubscription(anyInt(),
605                 any(), anyBoolean(), anyBoolean(), any(), any());
606         verifyResolutionIntent(EuiccService.ACTION_RESOLVE_NO_PRIVILEGES,
607                 EuiccOperation.ACTION_DOWNLOAD_NO_PRIVILEGES_OR_DEACTIVATE_SIM_CHECK_METADATA);
608     }
609 
610     @Test
testDownloadSubscription_noPrivileges_hasCarrierPrivileges_needsConsent_multiSim()611     public void testDownloadSubscription_noPrivileges_hasCarrierPrivileges_needsConsent_multiSim()
612             throws Exception {
613         setHasWriteEmbeddedPermission(false);
614         GetDownloadableSubscriptionMetadataResult result =
615                 new GetDownloadableSubscriptionMetadataResult(
616                     EuiccService.RESULT_OK, SUBSCRIPTION_WITH_METADATA);
617         prepareGetDownloadableSubscriptionMetadataCall(true /* complete */, result);
618         when(mTelephonyManager.getPhoneCount()).thenReturn(2);
619         setCanManageSubscriptionOnTargetSim(true /* isTargetEuicc */, false /* hasPrivileges */);
620         callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */,
621                 12345, 0 /* resolvableError */, PACKAGE_NAME /* callingPackage */);
622         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR,
623                 0 /* detailedCode */);
624         verify(mMockConnector, never()).downloadSubscription(anyInt(),
625                 any(), anyBoolean(), anyBoolean(), any(), any());
626         verifyResolutionIntent(EuiccService.ACTION_RESOLVE_NO_PRIVILEGES,
627                 EuiccOperation.ACTION_DOWNLOAD_NO_PRIVILEGES_OR_DEACTIVATE_SIM_CHECK_METADATA);
628     }
629 
630     @Test
testDownloadSubscription_noPriv_hasCarrierPrivi_needsConsent_multiSim_targetPsim()631     public void testDownloadSubscription_noPriv_hasCarrierPrivi_needsConsent_multiSim_targetPsim()
632             throws Exception {
633         setHasWriteEmbeddedPermission(false);
634         GetDownloadableSubscriptionMetadataResult result =
635                 new GetDownloadableSubscriptionMetadataResult(
636                     EuiccService.RESULT_OK, SUBSCRIPTION_WITH_METADATA);
637         prepareGetDownloadableSubscriptionMetadataCall(true /* complete */, result);
638         when(mTelephonyManager.getPhoneCount()).thenReturn(2);
639         setCanManageSubscriptionOnTargetSim(false /* isTargetEuicc */, true /* hasPrivileges */);
640         callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */,
641                 12345, 0 /* resolvableError */, PACKAGE_NAME /* callingPackage */);
642         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR,
643                 0 /* detailedCode */);
644         verify(mMockConnector, never()).downloadSubscription(anyInt(),
645                 any(), anyBoolean(), anyBoolean(), any(), any());
646         verifyResolutionIntent(EuiccService.ACTION_RESOLVE_NO_PRIVILEGES,
647                 EuiccOperation.ACTION_DOWNLOAD_NO_PRIVILEGES_OR_DEACTIVATE_SIM_CHECK_METADATA);
648     }
649 
650     @Test
testDownloadSubscription_noPrivileges_noCarrierPrivileges()651     public void testDownloadSubscription_noPrivileges_noCarrierPrivileges() throws Exception {
652         setHasWriteEmbeddedPermission(false);
653         GetDownloadableSubscriptionMetadataResult result =
654                 new GetDownloadableSubscriptionMetadataResult(
655                         EuiccService.RESULT_OK, SUBSCRIPTION_WITH_METADATA);
656         prepareGetDownloadableSubscriptionMetadataCall(true /* complete */, result);
657         PackageInfo pi = new PackageInfo();
658         pi.packageName = PACKAGE_NAME;
659         pi.signatures = new Signature[] { new Signature(new byte[] { 5, 4, 3, 2, 1 }) };
660         when(mPackageManager.getPackageInfo(eq(PACKAGE_NAME), anyInt())).thenReturn(pi);
661         callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */,
662                 12345, 0 /* resolvableError */, PACKAGE_NAME /* callingPackage */);
663         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR,
664                 0 /* detailedCode */);
665         verify(mTelephonyManager, never()).checkCarrierPrivilegesForPackage(PACKAGE_NAME);
666         verify(mMockConnector, never()).downloadSubscription(anyInt(),
667                 any(), anyBoolean(), anyBoolean(), any(), any());
668     }
669 
670     @Test
testDownloadSubscription_noPrivileges_noCarrierPrivileges_canManagerTargetSim()671     public void testDownloadSubscription_noPrivileges_noCarrierPrivileges_canManagerTargetSim()
672             throws Exception {
673         setHasWriteEmbeddedPermission(false);
674         GetDownloadableSubscriptionMetadataResult result =
675                 new GetDownloadableSubscriptionMetadataResult(
676                     EuiccService.RESULT_OK, SUBSCRIPTION_WITH_METADATA);
677         prepareGetDownloadableSubscriptionMetadataCall(true /* complete */, result);
678         PackageInfo pi = new PackageInfo();
679         pi.packageName = PACKAGE_NAME;
680         pi.signatures = new Signature[] { new Signature(new byte[] { 5, 4, 3, 2, 1 }) };
681         when(mPackageManager.getPackageInfo(eq(PACKAGE_NAME), anyInt())).thenReturn(pi);
682         setCanManageSubscriptionOnTargetSim(true /* isTargetEuicc */, true /* hasPrivileges */);
683         callDownloadSubscription(SUBSCRIPTION, true /* switchAfterDownload */, true /* complete */,
684                 12345, 0 /* resolvableError */, PACKAGE_NAME /* callingPackage */);
685         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR,
686                 0 /* detailedCode */);
687         verify(mTelephonyManager, never()).checkCarrierPrivilegesForPackage(PACKAGE_NAME);
688         verify(mMockConnector, never()).downloadSubscription(anyInt(),
689                 any(), anyBoolean(), anyBoolean(), any(), any());
690     }
691 
692     @Test
testDeleteSubscription_noSuchSubscription()693     public void testDeleteSubscription_noSuchSubscription() throws Exception {
694         setHasWriteEmbeddedPermission(true);
695         callDeleteSubscription(
696                 SUBSCRIPTION_ID, ICC_ID, false /* complete */,
697                 0 /* result */, "whatever" /* callingPackage */);
698         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR,
699                 0 /* detailedCode */);
700         verify(mMockConnector, never()).deleteSubscription(anyInt(), anyString(), any());
701     }
702 
703     @Test
testDeleteSubscription_serviceUnavailable()704     public void testDeleteSubscription_serviceUnavailable() throws Exception {
705         setHasWriteEmbeddedPermission(true);
706         prepareOperationSubscription(false /* hasPrivileges */);
707         callDeleteSubscription(
708                 SUBSCRIPTION_ID, ICC_ID, false /* complete */,
709                 0 /* result */, "whatever" /* callingPackage */);
710         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR,
711                 0 /* detailedCode */);
712     }
713 
714     @Test
testDeleteSubscription_error()715     public void testDeleteSubscription_error() throws Exception {
716         setHasWriteEmbeddedPermission(true);
717         prepareOperationSubscription(false /* hasPrivileges */);
718         callDeleteSubscription(
719                 SUBSCRIPTION_ID, ICC_ID, true /* complete */,
720                 42 /* result */, "whatever" /* callingPackage */);
721         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR,
722                 42 /* detailedCode */);
723     }
724 
725     @Test
testDeleteSubscription_success()726     public void testDeleteSubscription_success() throws Exception {
727         setHasWriteEmbeddedPermission(true);
728         prepareOperationSubscription(false /* hasPrivileges */);
729         callDeleteSubscription(
730                 SUBSCRIPTION_ID, ICC_ID, true /* complete */,
731                 EuiccService.RESULT_OK, "whatever" /* callingPackage */);
732         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */);
733         assertTrue(mController.mCalledRefreshSubscriptionsAndSendResult);
734     }
735 
736     @Test
testDeleteSubscription_noPrivileges()737     public void testDeleteSubscription_noPrivileges() throws Exception {
738         setHasWriteEmbeddedPermission(false);
739         prepareOperationSubscription(false /* hasPrivileges */);
740         callDeleteSubscription(
741                 SUBSCRIPTION_ID, ICC_ID, false /* complete */,
742                 0 /* result */, "whatever" /* callingPackage */);
743         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR,
744                 0 /* detailedCode */);
745         verify(mMockConnector, never()).deleteSubscription(anyInt(), anyString(), any());
746     }
747 
748     @Test
testDeleteSubscription_carrierPrivileges_success()749     public void testDeleteSubscription_carrierPrivileges_success() throws Exception {
750         setHasWriteEmbeddedPermission(false);
751         prepareOperationSubscription(true /* hasPrivileges */);
752         callDeleteSubscription(
753                 SUBSCRIPTION_ID, ICC_ID, true /* complete */, EuiccService.RESULT_OK, PACKAGE_NAME);
754         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */);
755         assertTrue(mController.mCalledRefreshSubscriptionsAndSendResult);
756     }
757 
758     @Test
testSwitchToSubscription_noSuchSubscription()759     public void testSwitchToSubscription_noSuchSubscription() throws Exception {
760         setHasWriteEmbeddedPermission(true);
761         callSwitchToSubscription(
762                 12345, ICC_ID, false /* complete */, 0 /* result */,
763                 "whatever" /* callingPackage */);
764         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR,
765                 0 /* detailedCode */);
766         verify(mMockConnector, never()).switchToSubscription(anyInt(), anyString(), anyBoolean(),
767                 any());
768     }
769 
770     @Test
testSwitchToSubscription_emptySubscription_noPrivileges()771     public void testSwitchToSubscription_emptySubscription_noPrivileges() throws Exception {
772         setHasWriteEmbeddedPermission(false);
773         callSwitchToSubscription(
774                 SubscriptionManager.INVALID_SUBSCRIPTION_ID, null /* iccid */, false /* complete */,
775                 0 /* result */, "whatever" /* callingPackage */);
776         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR,
777                 0 /* detailedCode */);
778         verify(mMockConnector, never()).switchToSubscription(anyInt(), anyString(), anyBoolean(),
779                 any());
780     }
781 
782     @Test
testSwitchToSubscription_serviceUnavailable()783     public void testSwitchToSubscription_serviceUnavailable() throws Exception {
784         setHasWriteEmbeddedPermission(true);
785         prepareOperationSubscription(false /* hasPrivileges */);
786         callSwitchToSubscription(
787                 SUBSCRIPTION_ID, ICC_ID, false /* complete */, 0 /* result */,
788                 "whatever" /* callingPackage */);
789         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR,
790                 0 /* detailedCode */);
791         verify(mMockConnector).switchToSubscription(anyInt(), anyString(), anyBoolean(), any());
792     }
793 
794     @Test
testSwitchToSubscription_error()795     public void testSwitchToSubscription_error() throws Exception {
796         setHasWriteEmbeddedPermission(true);
797         prepareOperationSubscription(false /* hasPrivileges */);
798         callSwitchToSubscription(
799                 SUBSCRIPTION_ID, ICC_ID, true /* complete */, 42 /* result */,
800                 "whatever" /* callingPackage */);
801         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR,
802                 42 /* detailedCode */);
803     }
804 
805     @Test
testSwitchToSubscription_success()806     public void testSwitchToSubscription_success() throws Exception {
807         setHasWriteEmbeddedPermission(true);
808         prepareOperationSubscription(false /* hasPrivileges */);
809         callSwitchToSubscription(
810                 SUBSCRIPTION_ID, ICC_ID, true /* complete */, EuiccService.RESULT_OK,
811                 "whatever" /* callingPackage */);
812         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */);
813     }
814 
815     @Test
testSwitchToSubscription_emptySubscription_success()816     public void testSwitchToSubscription_emptySubscription_success() throws Exception {
817         setHasWriteEmbeddedPermission(true);
818         callSwitchToSubscription(
819                 SubscriptionManager.INVALID_SUBSCRIPTION_ID, null /* iccid */, true /* complete */,
820                 EuiccService.RESULT_OK, "whatever" /* callingPackage */);
821         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */);
822     }
823 
824     @Test
testSwitchToSubscription_noPrivileges()825     public void testSwitchToSubscription_noPrivileges() throws Exception {
826         setHasWriteEmbeddedPermission(false);
827         prepareOperationSubscription(false /* hasPrivileges */);
828         callSwitchToSubscription(
829                 SUBSCRIPTION_ID, ICC_ID, false /* complete */, 0 /* result */,
830                 "whatever" /* callingPackage */);
831         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR,
832                 0 /* detailedCode */);
833         verify(mMockConnector, never()).switchToSubscription(anyInt(), anyString(), anyBoolean(),
834                 any());
835     }
836 
837     @Test
testSwitchToSubscription_hasCarrierPrivileges()838     public void testSwitchToSubscription_hasCarrierPrivileges() throws Exception {
839         setHasWriteEmbeddedPermission(false);
840         prepareOperationSubscription(true /* hasPrivileges */);
841         when(mTelephonyManager.getPhoneCount()).thenReturn(1);
842         setHasCarrierPrivilegesOnActiveSubscription(true);
843         callSwitchToSubscription(
844                 SUBSCRIPTION_ID, ICC_ID, true /* complete */, EuiccService.RESULT_OK, PACKAGE_NAME);
845         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */);
846     }
847 
848     @Test
testSwitchToSubscription_hasCarrierPrivileges_multiSim()849     public void testSwitchToSubscription_hasCarrierPrivileges_multiSim() throws Exception {
850         setHasWriteEmbeddedPermission(false);
851         prepareOperationSubscription(true /* hasPrivileges */);
852         when(mTelephonyManager.getPhoneCount()).thenReturn(2);
853         setCanManageSubscriptionOnTargetSim(true /* isTargetEuicc */, true /* hasPrivileges */);
854         callSwitchToSubscription(
855                 SUBSCRIPTION_ID, ICC_ID, true /* complete */, EuiccService.RESULT_OK, PACKAGE_NAME);
856         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */);
857     }
858 
859     @Test
testSwitchToSubscription_hasCarrierPrivileges_needsConsent()860     public void testSwitchToSubscription_hasCarrierPrivileges_needsConsent() throws Exception {
861         setHasWriteEmbeddedPermission(false);
862         prepareOperationSubscription(true /* hasPrivileges */);
863         setHasCarrierPrivilegesOnActiveSubscription(false);
864         when(mTelephonyManager.getPhoneCount()).thenReturn(1);
865         callSwitchToSubscription(
866                 SUBSCRIPTION_ID, ICC_ID, false /* complete */, 0 /* result */, PACKAGE_NAME);
867         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR,
868                 0 /* detailedCode */);
869         verify(mMockConnector, never()).switchToSubscription(anyInt(), anyString(), anyBoolean(),
870                 any());
871         verifyResolutionIntent(EuiccService.ACTION_RESOLVE_NO_PRIVILEGES,
872                 EuiccOperation.ACTION_SWITCH_NO_PRIVILEGES);
873     }
874 
875     @Test
testSwitchToSubscription_hasCarrierPrivileges_needsConsent_multiSim()876     public void testSwitchToSubscription_hasCarrierPrivileges_needsConsent_multiSim()
877             throws Exception {
878         setHasWriteEmbeddedPermission(false);
879         prepareOperationSubscription(true /* hasPrivileges */);
880         when(mTelephonyManager.getPhoneCount()).thenReturn(2);
881         setCanManageSubscriptionOnTargetSim(true /* isTargetEuicc */, false /* hasPrivileges */);
882         callSwitchToSubscription(
883                 SUBSCRIPTION_ID, ICC_ID, false /* complete */, 0 /* result */, PACKAGE_NAME);
884         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR,
885                 0 /* detailedCode */);
886         verify(mMockConnector, never()).switchToSubscription(anyInt(), anyString(), anyBoolean(),
887                 any());
888         verifyResolutionIntent(EuiccService.ACTION_RESOLVE_NO_PRIVILEGES,
889                 EuiccOperation.ACTION_SWITCH_NO_PRIVILEGES);
890     }
891 
892     @Test
testSwitchToSubscription_hasCarrierPrivileges_needsConsent_multiSim_targetPsim()893     public void testSwitchToSubscription_hasCarrierPrivileges_needsConsent_multiSim_targetPsim()
894             throws Exception {
895         setHasWriteEmbeddedPermission(false);
896         prepareOperationSubscription(true /* hasPrivileges */);
897         when(mTelephonyManager.getPhoneCount()).thenReturn(2);
898         setCanManageSubscriptionOnTargetSim(false /* isTargetEuicc */, true /* hasPrivileges */);
899         callSwitchToSubscription(
900                 SUBSCRIPTION_ID, ICC_ID, false /* complete */, 0 /* result */, PACKAGE_NAME);
901         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_RESOLVABLE_ERROR,
902                 0 /* detailedCode */);
903         verify(mMockConnector, never()).switchToSubscription(anyInt(), anyString(), anyBoolean(),
904                 any());
905         verifyResolutionIntent(EuiccService.ACTION_RESOLVE_NO_PRIVILEGES,
906                 EuiccOperation.ACTION_SWITCH_NO_PRIVILEGES);
907     }
908 
909     @Test
testUpdateSubscriptionNickname_noPrivileges()910     public void testUpdateSubscriptionNickname_noPrivileges() throws Exception {
911         setHasWriteEmbeddedPermission(false);
912         prepareOperationSubscription(false);
913         callUpdateSubscriptionNickname(
914                 SUBSCRIPTION_ID, ICC_ID, "nickname", false /* complete */, 0 /* result */,
915                 PACKAGE_NAME);
916         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR,
917                 0 /* detailedCode */);
918         verify(mMockConnector, never()).updateSubscriptionNickname(anyInt(), anyString(),
919                 anyString(), any());
920     }
921 
922     @Test
testUpdateSubscriptionNickname_noSuchSubscription()923     public void testUpdateSubscriptionNickname_noSuchSubscription() throws Exception {
924         setHasWriteEmbeddedPermission(true);
925         callUpdateSubscriptionNickname(
926                 SUBSCRIPTION_ID, ICC_ID, "nickname", false /* complete */, 0 /* result */,
927                 PACKAGE_NAME);
928         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR,
929                 0 /* detailedCode */);
930         verify(mMockConnector, never()).updateSubscriptionNickname(anyInt(), anyString(),
931                 anyString(), any());
932     }
933 
934     @Test
testUpdateSubscriptionNickname_serviceUnavailable()935     public void testUpdateSubscriptionNickname_serviceUnavailable() throws Exception {
936         setHasWriteEmbeddedPermission(true);
937         prepareOperationSubscription(false /* hasPrivileges */);
938         callUpdateSubscriptionNickname(
939                 SUBSCRIPTION_ID, ICC_ID, "nickname", false /* complete */, 0 /* result */,
940                 PACKAGE_NAME);
941         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR,
942                 0 /* detailedCode */);
943         verify(mMockConnector).updateSubscriptionNickname(anyInt(), anyString(), anyString(),
944                 any());
945     }
946 
947     @Test
testUpdateSubscriptionNickname_error()948     public void testUpdateSubscriptionNickname_error() throws Exception {
949         setHasWriteEmbeddedPermission(true);
950         prepareOperationSubscription(false /* hasPrivileges */);
951         callUpdateSubscriptionNickname(
952                 SUBSCRIPTION_ID, ICC_ID, "nickname", true /* complete */, 42 /* result */,
953                 PACKAGE_NAME);
954         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR,
955                 42 /* detailedCode */);
956     }
957 
958     @Test
testUpdateSubscriptionNickname_success()959     public void testUpdateSubscriptionNickname_success() throws Exception {
960         setHasWriteEmbeddedPermission(true);
961         prepareOperationSubscription(false /* hasPrivileges */);
962         callUpdateSubscriptionNickname(
963                 SUBSCRIPTION_ID, ICC_ID, "nickname", true /* complete */, EuiccService.RESULT_OK,
964                 PACKAGE_NAME);
965         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */);
966     }
967 
968     @Test(expected = SecurityException.class)
testEraseSubscriptions_noPrivileges()969     public void testEraseSubscriptions_noPrivileges() throws Exception {
970         setHasWriteEmbeddedPermission(false);
971         callEraseSubscriptions(false /* complete */, 0 /* result */);
972     }
973 
974     @Test
testEraseSubscriptions_serviceUnavailable()975     public void testEraseSubscriptions_serviceUnavailable() throws Exception {
976         setHasWriteEmbeddedPermission(true);
977         callEraseSubscriptions(false /* complete */, 0 /* result */);
978         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR,
979                 0 /* detailedCode */);
980         verify(mMockConnector).eraseSubscriptions(anyInt(), any());
981     }
982 
983     @Test
testEraseSubscriptions_error()984     public void testEraseSubscriptions_error() throws Exception {
985         setHasWriteEmbeddedPermission(true);
986         callEraseSubscriptions(true /* complete */, 42 /* result */);
987         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR,
988                 42 /* detailedCode */);
989     }
990 
991     @Test
testEraseSubscriptions_success()992     public void testEraseSubscriptions_success() throws Exception {
993         setHasWriteEmbeddedPermission(true);
994         callEraseSubscriptions(true /* complete */, EuiccService.RESULT_OK);
995         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */);
996         assertTrue(mController.mCalledRefreshSubscriptionsAndSendResult);
997     }
998 
999     @Test(expected = SecurityException.class)
testRetainSubscriptionsForFactoryReset_noPrivileges()1000     public void testRetainSubscriptionsForFactoryReset_noPrivileges() throws Exception {
1001         setHasMasterClearPermission(false);
1002         callRetainSubscriptionsForFactoryReset(false /* complete */, 0 /* result */);
1003     }
1004 
1005     @Test
testRetainSubscriptionsForFactoryReset_serviceUnavailable()1006     public void testRetainSubscriptionsForFactoryReset_serviceUnavailable() throws Exception {
1007         setHasMasterClearPermission(true);
1008         callRetainSubscriptionsForFactoryReset(false /* complete */, 0 /* result */);
1009         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 0 /* detailedCode */);
1010         verify(mMockConnector).retainSubscriptions(anyInt(), any());
1011     }
1012 
1013     @Test
testRetainSubscriptionsForFactoryReset_error()1014     public void testRetainSubscriptionsForFactoryReset_error() throws Exception {
1015         setHasMasterClearPermission(true);
1016         callRetainSubscriptionsForFactoryReset(true /* complete */, 42 /* result */);
1017         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, 42 /* detailedCode */);
1018     }
1019 
1020     @Test
testRetainSubscriptionsForFactoryReset_success()1021     public void testRetainSubscriptionsForFactoryReset_success() throws Exception {
1022         setHasMasterClearPermission(true);
1023         callRetainSubscriptionsForFactoryReset(true /* complete */, EuiccService.RESULT_OK);
1024         verifyIntentSent(EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK, 0 /* detailedCode */);
1025     }
1026 
setGetEidPermissions( boolean hasPhoneStatePrivileged, boolean hasCarrierPrivileges)1027     private void setGetEidPermissions(
1028             boolean hasPhoneStatePrivileged, boolean hasCarrierPrivileges) throws Exception {
1029         doReturn(hasPhoneStatePrivileged
1030                 ? PackageManager.PERMISSION_GRANTED : PackageManager.PERMISSION_DENIED)
1031                 .when(mContext)
1032                 .checkCallingOrSelfPermission(Manifest.permission.READ_PRIVILEGED_PHONE_STATE);
1033         when(mTelephonyManager.getPhoneCount()).thenReturn(1);
1034         setHasCarrierPrivilegesOnActiveSubscription(hasCarrierPrivileges);
1035     }
1036 
setHasWriteEmbeddedPermission(boolean hasPermission)1037     private void setHasWriteEmbeddedPermission(boolean hasPermission) {
1038         doReturn(hasPermission
1039                 ? PackageManager.PERMISSION_GRANTED : PackageManager.PERMISSION_DENIED)
1040                 .when(mContext)
1041                 .checkCallingOrSelfPermission(Manifest.permission.WRITE_EMBEDDED_SUBSCRIPTIONS);
1042     }
1043 
setHasMasterClearPermission(boolean hasPermission)1044     private void setHasMasterClearPermission(boolean hasPermission) {
1045         Stubber stubber = hasPermission ? doNothing() : doThrow(new SecurityException());
1046         stubber.when(mContext).enforceCallingPermission(
1047                 eq(Manifest.permission.MASTER_CLEAR), anyString());
1048     }
1049 
setHasCarrierPrivilegesOnActiveSubscription(boolean hasPrivileges)1050     private void setHasCarrierPrivilegesOnActiveSubscription(boolean hasPrivileges)
1051             throws Exception {
1052         SubscriptionInfo subInfo = new SubscriptionInfo(
1053                 0, "", 0, "", "", 0, 0, "", 0, null, "", "", "", true /* isEmbedded */,
1054                 hasPrivileges ? new UiccAccessRule[] { ACCESS_RULE } : null, "", CARD_ID,
1055                 false, null, false, 0, 0, 0, null);
1056         when(mSubscriptionManager.canManageSubscription(subInfo, PACKAGE_NAME)).thenReturn(
1057                 hasPrivileges);
1058         when(mSubscriptionManager.getActiveSubscriptionInfoList(anyBoolean())).thenReturn(
1059                 Collections.singletonList(subInfo));
1060     }
1061 
setCanManageSubscriptionOnTargetSim(boolean isTargetEuicc, boolean hasPrivileges)1062     private void setCanManageSubscriptionOnTargetSim(boolean isTargetEuicc, boolean hasPrivileges)
1063             throws Exception {
1064         UiccCardInfo cardInfo1 = new UiccCardInfo(isTargetEuicc, CARD_ID, "", "", 0,
1065                 false /* isRemovable */);
1066         UiccCardInfo cardInfo2 = new UiccCardInfo(true /* isEuicc */, 1 /* cardId */,
1067                 "", "", 0, false /* isRemovable */);
1068         ArrayList<UiccCardInfo> cardInfos = new ArrayList<>();
1069         cardInfos.add(cardInfo1);
1070         cardInfos.add(cardInfo2);
1071         when(mTelephonyManager.getUiccCardsInfo()).thenReturn(cardInfos);
1072 
1073         SubscriptionInfo subInfo1 = new SubscriptionInfo(
1074                 0, "", 0, "", "", 0, 0, "", 0, null, "", "", "", true /* isEmbedded */,
1075                 hasPrivileges ? new UiccAccessRule[] { ACCESS_RULE } : null, "", CARD_ID,
1076                 false, null, false, 0, 0, 0, null);
1077         SubscriptionInfo subInfo2 = new SubscriptionInfo(
1078                 0, "", 0, "", "", 0, 0, "", 0, null, "", "", "", true /* isEmbedded */,
1079                 hasPrivileges ? new UiccAccessRule[] { ACCESS_RULE } : null, "",
1080                 1 /* cardId */, false, null, false, 0, 0, 0, null);
1081         when(mSubscriptionManager.canManageSubscription(subInfo1, PACKAGE_NAME)).thenReturn(
1082                 hasPrivileges);
1083         when(mSubscriptionManager.canManageSubscription(subInfo2, PACKAGE_NAME)).thenReturn(
1084                 hasPrivileges);
1085         ArrayList<SubscriptionInfo> subInfos = new ArrayList<>(Arrays.asList(subInfo1, subInfo2));
1086         when(mSubscriptionManager.getActiveSubscriptionInfoList(anyBoolean())).thenReturn(subInfos);
1087     }
1088 
prepareOperationSubscription(boolean hasPrivileges)1089     private void prepareOperationSubscription(boolean hasPrivileges) throws Exception {
1090         SubscriptionInfo subInfo = new SubscriptionInfo(
1091                 SUBSCRIPTION_ID, ICC_ID, 0, "", "", 0, 0, "", 0, null, "0", "0", "",
1092                 true /* isEmbedded */, hasPrivileges ? new UiccAccessRule[] { ACCESS_RULE } : null,
1093                 null);
1094         when(mSubscriptionManager.canManageSubscription(subInfo, PACKAGE_NAME)).thenReturn(
1095                 hasPrivileges);
1096         when(mSubscriptionManager.getAvailableSubscriptionInfoList()).thenReturn(
1097                 Collections.singletonList(subInfo));
1098     }
1099 
callGetEid(final boolean success, final @Nullable String eid, int cardId)1100     private String callGetEid(final boolean success, final @Nullable String eid, int cardId) {
1101         doAnswer(new Answer<Void>() {
1102             @Override
1103             public Void answer(InvocationOnMock invocation) throws Exception {
1104                 EuiccConnector.GetEidCommandCallback cb = invocation
1105                         .getArgument(1 /* resultCallback */);
1106                 if (success) {
1107                     cb.onGetEidComplete(eid);
1108                 } else {
1109                     cb.onEuiccServiceUnavailable();
1110                 }
1111                 return null;
1112             }
1113         }).when(mMockConnector).getEid(anyInt(),
1114                 Mockito.<EuiccConnector.GetEidCommandCallback>any());
1115         return mController.getEid(cardId, PACKAGE_NAME);
1116     }
1117 
callGetOtaStatus(final boolean success, final int status)1118     private int callGetOtaStatus(final boolean success, final int status) {
1119         doAnswer(new Answer<Void>() {
1120             @Override
1121             public Void answer(InvocationOnMock invocation) throws Exception {
1122                 GetOtaStatusCommandCallback cb = invocation.getArgument(1 /* resultCallback */);
1123                 if (success) {
1124                     cb.onGetOtaStatusComplete(status);
1125                 } else {
1126                     cb.onEuiccServiceUnavailable();
1127                 }
1128                 return null;
1129             }
1130         }).when(mMockConnector).getOtaStatus(anyInt(), Mockito.<GetOtaStatusCommandCallback>any());
1131         return mController.getOtaStatus(CARD_ID);
1132     }
1133 
callStartOtaUpdatingIfNecessary( final boolean serviceAvailable, int status)1134     private void callStartOtaUpdatingIfNecessary(
1135             final boolean serviceAvailable, int status) {
1136         doAnswer(new Answer<Void>() {
1137             @Override
1138             public Void answer(InvocationOnMock invocation) throws Exception {
1139                 OtaStatusChangedCallback cb = invocation.getArgument(1 /* resultCallback */);
1140                 if (!serviceAvailable) {
1141                     cb.onEuiccServiceUnavailable();
1142                 } else {
1143                     cb.onOtaStatusChanged(status);
1144                 }
1145                 return null;
1146             }
1147         }).when(mMockConnector).startOtaIfNecessary(anyInt(),
1148                 Mockito.<OtaStatusChangedCallback>any());
1149 
1150         mController.startOtaUpdatingIfNecessary();
1151     }
1152 
callGetEuiccInfo(final boolean success, final @Nullable EuiccInfo euiccInfo)1153     private EuiccInfo callGetEuiccInfo(final boolean success, final @Nullable EuiccInfo euiccInfo) {
1154         doAnswer(new Answer<Void>() {
1155             @Override
1156             public Void answer(InvocationOnMock invocation) throws Exception {
1157                 EuiccConnector.GetEuiccInfoCommandCallback cb = invocation
1158                         .getArgument(1 /* resultCallback */);
1159                 if (success) {
1160                     cb.onGetEuiccInfoComplete(euiccInfo);
1161                 } else {
1162                     cb.onEuiccServiceUnavailable();
1163                 }
1164                 return null;
1165             }
1166         }).when(mMockConnector).getEuiccInfo(anyInt(), any());
1167         return mController.getEuiccInfo(CARD_ID);
1168     }
1169 
prepareGetDownloadableSubscriptionMetadataCall( final boolean complete, final GetDownloadableSubscriptionMetadataResult result)1170     private void prepareGetDownloadableSubscriptionMetadataCall(
1171             final boolean complete, final GetDownloadableSubscriptionMetadataResult result) {
1172         doAnswer(new Answer<Void>() {
1173             @Override
1174             public Void answer(InvocationOnMock invocation) throws Exception {
1175                 EuiccConnector.GetMetadataCommandCallback cb = invocation
1176                         .getArgument(3 /* resultCallback */);
1177                 if (complete) {
1178                     cb.onGetMetadataComplete(CARD_ID, result);
1179                 } else {
1180                     cb.onEuiccServiceUnavailable();
1181                 }
1182                 return null;
1183             }
1184         }).when(mMockConnector).getDownloadableSubscriptionMetadata(anyInt(), any(), anyBoolean(),
1185                 any());
1186     }
1187 
callGetDownloadableSubscriptionMetadata(DownloadableSubscription subscription, boolean complete, GetDownloadableSubscriptionMetadataResult result)1188     private void callGetDownloadableSubscriptionMetadata(DownloadableSubscription subscription,
1189             boolean complete, GetDownloadableSubscriptionMetadataResult result) {
1190         prepareGetDownloadableSubscriptionMetadataCall(complete, result);
1191         PendingIntent resultCallback = PendingIntent.getBroadcast(mContext, 0, new Intent(), 0);
1192         mController.getDownloadableSubscriptionMetadata(0, subscription, PACKAGE_NAME,
1193                 resultCallback);
1194     }
1195 
callGetDefaultDownloadableSubscriptionList( boolean complete, GetDefaultDownloadableSubscriptionListResult result)1196     private void callGetDefaultDownloadableSubscriptionList(
1197             boolean complete, GetDefaultDownloadableSubscriptionListResult result) {
1198         doAnswer(new Answer<Void>() {
1199             @Override
1200             public Void answer(InvocationOnMock invocation) throws Exception {
1201                 EuiccConnector.GetDefaultListCommandCallback cb = invocation
1202                         .getArgument(2 /* resultCallBack */);
1203                 if (complete) {
1204                     cb.onGetDefaultListComplete(CARD_ID, result);
1205                 } else {
1206                     cb.onEuiccServiceUnavailable();
1207                 }
1208                 return null;
1209             }
1210         }).when(mMockConnector).getDefaultDownloadableSubscriptionList(anyInt(), anyBoolean(),
1211                 any());
1212         PendingIntent resultCallback = PendingIntent.getBroadcast(mContext, 0, new Intent(), 0);
1213         mController.getDefaultDownloadableSubscriptionList(CARD_ID, PACKAGE_NAME, resultCallback);
1214     }
1215 
callDownloadSubscription(DownloadableSubscription subscription, boolean switchAfterDownload, final boolean complete, final int result, final int resolvableError, String callingPackage)1216     private void callDownloadSubscription(DownloadableSubscription subscription,
1217             boolean switchAfterDownload, final boolean complete, final int result,
1218             final int resolvableError, String callingPackage) {
1219         PendingIntent resultCallback = PendingIntent.getBroadcast(mContext, 0, new Intent(), 0);
1220         doAnswer(new Answer<Void>() {
1221             @Override
1222             public Void answer(InvocationOnMock invocation) throws Exception {
1223                 EuiccConnector.DownloadCommandCallback cb = invocation
1224                         .getArgument(5 /* resultCallback */);
1225                 if (complete) {
1226                     DownloadSubscriptionResult downloadRes = new DownloadSubscriptionResult(
1227                             result, resolvableError, -1 /* cardId */);
1228                     cb.onDownloadComplete(downloadRes);
1229                 } else {
1230                     cb.onEuiccServiceUnavailable();
1231                 }
1232                 return null;
1233             }
1234         }).when(mMockConnector).downloadSubscription(anyInt(),
1235                 any(), eq(switchAfterDownload), anyBoolean(), any(), any());
1236         mController.downloadSubscription(CARD_ID, subscription, switchAfterDownload, callingPackage,
1237                 null /* resolvedBundle */, resultCallback);
1238         // EUICC_PROVISIONED setting should match whether the download was successful.
1239         assertEquals(complete && result == EuiccService.RESULT_OK ? 1 : 0,
1240                 Settings.Global.getInt(mContext.getContentResolver(),
1241                         Settings.Global.EUICC_PROVISIONED, 0));
1242     }
1243 
callDeleteSubscription(int subscriptionId, String iccid, final boolean complete, final int result, String callingPackage)1244     private void callDeleteSubscription(int subscriptionId, String iccid, final boolean complete,
1245             final int result, String callingPackage) {
1246         PendingIntent resultCallback = PendingIntent.getBroadcast(mContext, 0, new Intent(), 0);
1247         doAnswer(new Answer<Void>() {
1248             @Override
1249             public Void answer(InvocationOnMock invocation) throws Exception {
1250                 EuiccConnector.DeleteCommandCallback cb = invocation
1251                         .getArgument(2 /* resultCallback */);
1252                 if (complete) {
1253                     cb.onDeleteComplete(result);
1254                 } else {
1255                     cb.onEuiccServiceUnavailable();
1256                 }
1257                 return null;
1258             }
1259         }).when(mMockConnector).deleteSubscription(anyInt(), eq(iccid), any());
1260         mController.deleteSubscription(CARD_ID, subscriptionId, callingPackage, resultCallback);
1261     }
1262 
callSwitchToSubscription(int subscriptionId, String iccid, final boolean complete, final int result, String callingPackage)1263     private void callSwitchToSubscription(int subscriptionId, String iccid, final boolean complete,
1264             final int result, String callingPackage) {
1265         PendingIntent resultCallback = PendingIntent.getBroadcast(mContext, 0, new Intent(), 0);
1266         doAnswer(new Answer<Void>() {
1267             @Override
1268             public Void answer(InvocationOnMock invocation) throws Exception {
1269                 EuiccConnector.SwitchCommandCallback cb = invocation
1270                         .getArgument(3 /* resultCallback */);
1271                 if (complete) {
1272                     cb.onSwitchComplete(result);
1273                 } else {
1274                     cb.onEuiccServiceUnavailable();
1275                 }
1276                 return null;
1277             }
1278         }).when(mMockConnector).switchToSubscription(anyInt(), eq(iccid), anyBoolean(), any());
1279         mController.switchToSubscription(CARD_ID, subscriptionId, callingPackage, resultCallback);
1280     }
1281 
callUpdateSubscriptionNickname(int subscriptionId, String iccid, String nickname, final boolean complete, final int result, String callingPackage)1282     private void callUpdateSubscriptionNickname(int subscriptionId, String iccid, String nickname,
1283             final boolean complete, final int result, String callingPackage) {
1284         PendingIntent resultCallback = PendingIntent.getBroadcast(mContext, 0, new Intent(), 0);
1285         doAnswer(new Answer<Void>() {
1286             @Override
1287             public Void answer(InvocationOnMock invocation) throws Exception {
1288                 EuiccConnector.UpdateNicknameCommandCallback cb = invocation
1289                         .getArgument(3 /* resultCallback */);
1290                 if (complete) {
1291                     cb.onUpdateNicknameComplete(result);
1292                 } else {
1293                     cb.onEuiccServiceUnavailable();
1294                 }
1295                 return null;
1296             }
1297         }).when(mMockConnector).updateSubscriptionNickname(anyInt(), eq(iccid), eq(nickname),
1298                 any());
1299         mController.updateSubscriptionNickname(CARD_ID, subscriptionId, nickname, callingPackage,
1300                 resultCallback);
1301     }
1302 
callEraseSubscriptions(final boolean complete, final int result)1303     private void callEraseSubscriptions(final boolean complete, final int result) {
1304         PendingIntent resultCallback = PendingIntent.getBroadcast(mContext, 0, new Intent(), 0);
1305         doAnswer(new Answer<Void>() {
1306             @Override
1307             public Void answer(InvocationOnMock invocation) throws Exception {
1308                 EuiccConnector.EraseCommandCallback cb = invocation
1309                         .getArgument(1 /* resultCallback */);
1310                 if (complete) {
1311                     cb.onEraseComplete(result);
1312                 } else {
1313                     cb.onEuiccServiceUnavailable();
1314                 }
1315                 return null;
1316             }
1317         }).when(mMockConnector).eraseSubscriptions(anyInt(), any());
1318         mController.eraseSubscriptions(CARD_ID, resultCallback);
1319     }
1320 
callRetainSubscriptionsForFactoryReset(final boolean complete, final int result)1321     private void callRetainSubscriptionsForFactoryReset(final boolean complete, final int result) {
1322         PendingIntent resultCallback = PendingIntent.getBroadcast(mContext, 0, new Intent(), 0);
1323         doAnswer(new Answer<Void>() {
1324             @Override
1325             public Void answer(InvocationOnMock invocation) throws Exception {
1326                 EuiccConnector.RetainSubscriptionsCommandCallback cb = invocation
1327                         .getArgument(1 /* resultCallback */);
1328                 if (complete) {
1329                     cb.onRetainSubscriptionsComplete(result);
1330                 } else {
1331                     cb.onEuiccServiceUnavailable();
1332                 }
1333                 return null;
1334             }
1335         }).when(mMockConnector).retainSubscriptions(anyInt(), any());
1336         mController.retainSubscriptionsForFactoryReset(CARD_ID, resultCallback);
1337     }
1338 
verifyResolutionIntent(String euiccUiAction, @EuiccOperation.Action int action)1339     private void verifyResolutionIntent(String euiccUiAction, @EuiccOperation.Action int action) {
1340         assertEquals(euiccUiAction, mController.mResolutionAction);
1341         assertNotNull(mController.mOp);
1342         assertEquals(action, mController.mOp.mAction);
1343     }
1344 
verifyIntentSent(int resultCode, int detailedCode)1345     private Intent verifyIntentSent(int resultCode, int detailedCode)
1346             throws RemoteException {
1347         assertNotNull(mController.mCallbackIntent);
1348         assertEquals(resultCode, mController.mResultCode);
1349         if (mController.mExtrasIntent == null) {
1350             assertEquals(0, detailedCode);
1351         } else {
1352             assertEquals(detailedCode,
1353                     mController.mExtrasIntent.getIntExtra(
1354                             EuiccManager.EXTRA_EMBEDDED_SUBSCRIPTION_DETAILED_CODE, 0));
1355         }
1356         return mController.mExtrasIntent;
1357     }
1358 }
1359