1 /* 2 * Copyright (C) 2021 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.ons; 18 19 import static org.mockito.Mockito.doReturn; 20 import static org.mockito.Mockito.verify; 21 import static org.mockito.Mockito.verifyNoMoreInteractions; 22 23 import android.app.PendingIntent; 24 import android.content.Intent; 25 import android.os.Looper; 26 import android.os.Message; 27 import android.os.ParcelUuid; 28 import android.os.PersistableBundle; 29 import android.telephony.CarrierConfigManager; 30 import android.telephony.SubscriptionInfo; 31 import android.telephony.SubscriptionManager; 32 import android.telephony.euicc.EuiccManager; 33 34 import org.junit.After; 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.mockito.Mock; 38 import org.mockito.MockitoAnnotations; 39 40 import java.util.ArrayList; 41 import java.util.UUID; 42 43 public class ONSProfileConfiguratorTest extends ONSBaseTest { 44 private static final String TAG = ONSProfileConfiguratorTest.class.getName(); 45 private static final int TEST_SUB_ID = 1; 46 @Mock 47 SubscriptionManager mMockSubManager; 48 @Mock 49 SubscriptionInfo mMockSubscriptionInfo1; 50 @Mock 51 SubscriptionInfo mMockSubscriptionInfo2; 52 @Mock 53 EuiccManager mMockEuiccMngr; 54 @Mock 55 EuiccManager mMockEuiccMngrCard1; 56 @Mock 57 EuiccManager mMockEuiccMngrCard2; 58 @Mock 59 CarrierConfigManager mMockCarrierConfigManager; 60 @Mock 61 private ONSProfileConfigurator.ONSProfConfigListener mMockConfigListener; 62 63 @Before setUp()64 public void setUp() throws Exception { 65 super.setUp("ONSTest"); 66 MockitoAnnotations.initMocks(this); 67 doReturn(mMockEuiccMngrCard1).when(mMockEuiccMngr).createForCardId(1); 68 doReturn(mMockEuiccMngrCard2).when(mMockEuiccMngr).createForCardId(2); 69 Looper.prepare(); 70 } 71 72 @Test testDeleteSubscription()73 public void testDeleteSubscription() { 74 ONSProfileConfigurator mOnsProfileConfigurator = new ONSProfileConfigurator(mContext, 75 mMockSubManager, mMockCarrierConfigManager, mMockEuiccMngr, mMockConfigListener); 76 77 Intent intent = new Intent(); 78 intent.putExtra( 79 ONSProfileConfigurator.PARAM_REQUEST_TYPE, 80 ONSProfileConfigurator.REQUEST_CODE_DELETE_SUB); 81 intent.putExtra(ONSProfileConfigurator.PARAM_SUB_ID, TEST_SUB_ID); 82 Message msg = new Message(); 83 msg.obj = intent; 84 msg.arg1 = EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_OK; 85 mOnsProfileConfigurator.callbackMsgHandler(msg); 86 87 verify(mMockConfigListener).onOppSubscriptionDeleted(TEST_SUB_ID); 88 } 89 90 @Test testGroupSubscriptionAndSetOpportunistic()91 public void testGroupSubscriptionAndSetOpportunistic() { 92 doReturn(TEST_SUB_ID).when(mMockSubscriptionInfo1).getSubscriptionId(); 93 94 ONSProfileConfigurator mOnsProfileConfigurator = new ONSProfileConfigurator(mContext, 95 mMockSubManager, mMockCarrierConfigManager, mMockEuiccMngr, mMockConfigListener); 96 97 ParcelUuid parcelUuid = new ParcelUuid(new UUID(1, 2)); 98 mOnsProfileConfigurator.groupWithPSIMAndSetOpportunistic( 99 mMockSubscriptionInfo1, parcelUuid); 100 101 ArrayList<Integer> subList = new ArrayList<>(); 102 subList.add(TEST_SUB_ID); 103 verify(mMockSubManager).addSubscriptionsIntoGroup(subList, parcelUuid); 104 } 105 106 @Test testAlreadyGroupedSubscriptions()107 public void testAlreadyGroupedSubscriptions() { 108 doReturn(TEST_SUB_ID).when(mMockSubscriptionInfo1).getSubscriptionId(); 109 doReturn(true).when(mMockSubscriptionInfo1).isOpportunistic(); 110 111 ONSProfileConfigurator mOnsProfileConfigurator = new ONSProfileConfigurator(mContext, 112 mMockSubManager, mMockCarrierConfigManager, mMockEuiccMngr, mMockConfigListener); 113 114 ParcelUuid uuid = new ParcelUuid(new UUID(1, 2)); 115 doReturn(uuid).when(mMockSubscriptionInfo1).getGroupUuid(); 116 117 mOnsProfileConfigurator.groupWithPSIMAndSetOpportunistic(mMockSubscriptionInfo1, uuid); 118 119 verifyNoMoreInteractions(mMockSubManager); 120 } 121 122 @Test testActivateSubscription()123 public void testActivateSubscription() { 124 ONSProfileConfigurator mOnsProfileConfigurator = new ONSProfileConfigurator(mContext, 125 mMockSubManager, mMockCarrierConfigManager, mMockEuiccMngr, mMockConfigListener); 126 127 Intent intent = new Intent(mContext, ONSProfileResultReceiver.class); 128 intent.setAction(ONSProfileConfigurator.ACTION_ONS_ESIM_CONFIG); 129 intent.putExtra( 130 ONSProfileConfigurator.PARAM_REQUEST_TYPE, 131 ONSProfileConfigurator.REQUEST_CODE_ACTIVATE_SUB); 132 intent.putExtra(ONSProfileConfigurator.PARAM_SUB_ID, TEST_SUB_ID); 133 PendingIntent callbackIntent = 134 PendingIntent.getBroadcast( 135 mContext, 136 ONSProfileConfigurator.REQUEST_CODE_ACTIVATE_SUB, 137 intent, 138 PendingIntent.FLAG_IMMUTABLE); 139 140 ArrayList<SubscriptionInfo> activeSubInfos = new ArrayList<>(); 141 142 doReturn(1).when(mMockSubscriptionInfo1).getSubscriptionId(); 143 doReturn(1).when(mMockSubscriptionInfo1).getCardId(); 144 activeSubInfos.add(mMockSubscriptionInfo1); 145 146 doReturn(2).when(mMockSubscriptionInfo2).getSubscriptionId(); 147 doReturn(2).when(mMockSubscriptionInfo2).getCardId(); 148 activeSubInfos.add(mMockSubscriptionInfo2); 149 doReturn(activeSubInfos).when(mMockSubManager).getAvailableSubscriptionInfoList(); 150 151 mOnsProfileConfigurator.activateSubscription(TEST_SUB_ID); 152 verify(mMockEuiccMngrCard1).switchToSubscription(TEST_SUB_ID, callbackIntent); 153 } 154 155 @Test testdeleteInactiveOpportunisticSubscriptionsWithNoneSavedOppSubs()156 public void testdeleteInactiveOpportunisticSubscriptionsWithNoneSavedOppSubs() { 157 ONSProfileConfigurator mOnsProfileConfigurator = new ONSProfileConfigurator(mContext, 158 mMockSubManager, mMockCarrierConfigManager, mMockEuiccMngr, mMockConfigListener); 159 160 PersistableBundle persistableBundle = new PersistableBundle(); 161 persistableBundle.putIntArray( 162 CarrierConfigManager.KEY_OPPORTUNISTIC_CARRIER_IDS_INT_ARRAY, null); 163 doReturn(persistableBundle).when(mMockCarrierConfigManager).getConfigForSubId(TEST_SUB_ID); 164 165 boolean res = mOnsProfileConfigurator.deleteInactiveOpportunisticSubscriptions(TEST_SUB_ID); 166 // verify(mOnsProfileConfigurator).deleteOldOpportunisticESimsOfPSIMOperator(TEST_SUB_ID); 167 assertEquals(res, false); 168 } 169 170 @Test testdeleteInactiveOpportunisticSubscriptionsWithSavedOppSubs()171 public void testdeleteInactiveOpportunisticSubscriptionsWithSavedOppSubs() { 172 ONSProfileConfigurator mOnsProfileConfigurator = new ONSProfileConfigurator(mContext, 173 mMockSubManager, mMockCarrierConfigManager, mMockEuiccMngr, mMockConfigListener); 174 175 doReturn(1).when(mMockSubscriptionInfo1).getSubscriptionId(); 176 doReturn(1).when(mMockSubscriptionInfo1).getCardId(); 177 doReturn(true).when(mMockSubManager).isActiveSubscriptionId(1); 178 179 doReturn(2).when(mMockSubscriptionInfo2).getSubscriptionId(); 180 doReturn(2).when(mMockSubscriptionInfo2).getCardId(); 181 doReturn(false).when(mMockSubManager).isActiveSubscriptionId(2); 182 183 ArrayList<SubscriptionInfo> oppSubList = new ArrayList<>(); 184 oppSubList.add(mMockSubscriptionInfo1); 185 oppSubList.add(mMockSubscriptionInfo2); 186 doReturn(oppSubList).when(mMockSubManager).getOpportunisticSubscriptions(); 187 doReturn(oppSubList).when(mMockSubManager).getAvailableSubscriptionInfoList(); 188 189 Intent intent = new Intent(mContext, ONSProfileResultReceiver.class); 190 intent.setAction(ONSProfileConfigurator.ACTION_ONS_ESIM_CONFIG); 191 intent.putExtra( 192 ONSProfileConfigurator.PARAM_REQUEST_TYPE, 193 ONSProfileConfigurator.REQUEST_CODE_DELETE_SUB); 194 intent.putExtra(ONSProfileConfigurator.PARAM_SUB_ID, 2); 195 PendingIntent callbackIntent2 = 196 PendingIntent.getBroadcast( 197 mContext, 198 ONSProfileConfigurator.REQUEST_CODE_DELETE_SUB, 199 intent, 200 PendingIntent.FLAG_MUTABLE); 201 202 boolean res = mOnsProfileConfigurator.deleteInactiveOpportunisticSubscriptions(2); 203 verify(mMockEuiccMngrCard2).deleteSubscription(2, callbackIntent2); 204 verifyNoMoreInteractions(mMockEuiccMngrCard2); 205 //verify(mMockEuiccManager).deleteSubscription(2, callbackIntent2); 206 // verify(mOnsProfileConfigurator).deleteOldOpportunisticESimsOfPSIMOperator(TEST_SUB_ID); 207 assertEquals(res, true); 208 } 209 210 @Test testFindOpportunisticSubscription()211 public void testFindOpportunisticSubscription() { 212 ONSProfileConfigurator mOnsProfileConfigurator = new ONSProfileConfigurator(mContext, 213 mMockSubManager, mMockCarrierConfigManager, mMockEuiccMngr, mMockConfigListener); 214 215 int[] oppCarrierList = {2}; 216 PersistableBundle persistableBundle = new PersistableBundle(); 217 persistableBundle.putIntArray( 218 CarrierConfigManager.KEY_OPPORTUNISTIC_CARRIER_IDS_INT_ARRAY, oppCarrierList); 219 doReturn(persistableBundle).when(mMockCarrierConfigManager).getConfigForSubId(TEST_SUB_ID); 220 221 ArrayList<SubscriptionInfo> oppSubList = new ArrayList<>(); 222 oppSubList.add(mMockSubscriptionInfo1); 223 oppSubList.add(mMockSubscriptionInfo2); 224 doReturn(oppSubList).when(mMockSubManager).getAvailableSubscriptionInfoList(); 225 226 ParcelUuid groupUUID = new ParcelUuid(new UUID(0, 100)); 227 doReturn(groupUUID).when(mMockSubscriptionInfo1).getGroupUuid(); 228 doReturn(true).when(mMockSubscriptionInfo1).isEmbedded(); 229 doReturn(1).when(mMockSubscriptionInfo1).getCarrierId(); 230 doReturn(mMockSubscriptionInfo1).when(mMockSubManager) 231 .getActiveSubscriptionInfo(TEST_SUB_ID); 232 doReturn(TEST_SUB_ID).when(mMockSubscriptionInfo1).getSubscriptionId(); 233 234 doReturn(null).when(mMockSubscriptionInfo2).getGroupUuid(); 235 doReturn(true).when(mMockSubscriptionInfo2).isEmbedded(); 236 doReturn(2).when(mMockSubscriptionInfo2).getCarrierId(); 237 doReturn(mMockSubscriptionInfo2).when(mMockSubManager).getActiveSubscriptionInfo(2); 238 doReturn(2).when(mMockSubscriptionInfo2).getSubscriptionId(); 239 240 SubscriptionInfo oppSubscription = mOnsProfileConfigurator 241 .findOpportunisticSubscription(TEST_SUB_ID); 242 assertEquals(oppSubscription, mMockSubscriptionInfo2); 243 244 doReturn(groupUUID).when(mMockSubscriptionInfo2).getGroupUuid(); 245 assertEquals(oppSubscription, mMockSubscriptionInfo2); 246 } 247 248 @After tearDown()249 public void tearDown() throws Exception { 250 super.tearDown(); 251 } 252 } 253