blob: 68925f7c52a72d7e3b9ee53ce1a6bc47a1891ae7 [file] [log] [blame]
Sooraj Sasindran33345152018-10-01 11:22:56 -07001/*
2 * Copyright (C) 2018 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 */
Sooraj Sasindran653edbd2019-01-08 13:17:29 -080016package com.android.ons;
Sooraj Sasindran33345152018-10-01 11:22:56 -070017
Sooraj Sasindran33345152018-10-01 11:22:56 -070018import static org.mockito.Mockito.*;
19
20import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
Malcolm Chen9059f472019-12-18 14:33:32 -080023import android.os.IBinder;
Sooraj Sasindran33345152018-10-01 11:22:56 -070024import android.os.Looper;
Malcolm Chen9059f472019-12-18 14:33:32 -080025import android.os.ServiceManager;
Sooraj Sasindran23942272018-10-30 23:04:43 -070026import android.telephony.AvailableNetworkInfo;
Sooraj Sasindran33345152018-10-01 11:22:56 -070027import android.telephony.CellIdentityLte;
Avinash Malipatil284c5ef2022-08-17 07:29:56 +000028import android.telephony.CellIdentityNr;
Sooraj Sasindran33345152018-10-01 11:22:56 -070029import android.telephony.CellInfo;
30import android.telephony.CellInfoLte;
Avinash Malipatil284c5ef2022-08-17 07:29:56 +000031import android.telephony.CellInfoNr;
Sooraj Sasindran33345152018-10-01 11:22:56 -070032import android.telephony.SubscriptionInfo;
33import android.telephony.SubscriptionManager;
danielwbhuang9949a6a2019-03-27 17:56:31 +080034import android.telephony.TelephonyManager;
Avinash Malipatil79572952022-05-26 08:48:31 +000035import android.telephony.UiccCardInfo;
36import android.telephony.UiccPortInfo;
37import android.telephony.euicc.EuiccManager;
danielwbhuang9949a6a2019-03-27 17:56:31 +080038import android.util.Log;
39
Malcolm Chen9059f472019-12-18 14:33:32 -080040import com.android.internal.telephony.ISub;
danielwbhuang9949a6a2019-03-27 17:56:31 +080041import com.android.internal.telephony.IUpdateAvailableNetworksCallback;
Sooraj Sasindran33345152018-10-01 11:22:56 -070042
43import org.junit.After;
44import org.junit.Before;
45import org.junit.Test;
46import org.mockito.Mock;
47import org.mockito.MockitoAnnotations;
48
Malcolm Chen9059f472019-12-18 14:33:32 -080049import java.lang.reflect.Field;
Sooraj Sasindran33345152018-10-01 11:22:56 -070050import java.util.ArrayList;
Avinash Malipatil284c5ef2022-08-17 07:29:56 +000051import java.util.Collections;
Sooraj Sasindran33345152018-10-01 11:22:56 -070052import java.util.List;
Malcolm Chen9059f472019-12-18 14:33:32 -080053import java.util.Map;
Sooraj Sasindran33345152018-10-01 11:22:56 -070054
Sooraj Sasindran653edbd2019-01-08 13:17:29 -080055public class ONSProfileSelectorTest extends ONSBaseTest {
Sooraj Sasindran81f153f2019-07-09 10:58:24 -070056
Sooraj Sasindran653edbd2019-01-08 13:17:29 -080057 private MyONSProfileSelector mONSProfileSelector;
Sooraj Sasindran33345152018-10-01 11:22:56 -070058 private boolean testFailed;
59 private boolean mCallbackInvoked;
60 private int mDataSubId;
danielwbhuang9949a6a2019-03-27 17:56:31 +080061 private int mResult;
Sooraj Sasindran33345152018-10-01 11:22:56 -070062 @Mock
Avinash Malipatil79572952022-05-26 08:48:31 +000063 EuiccManager mMockEuiccManager;
64 @Mock
Sooraj Sasindran653edbd2019-01-08 13:17:29 -080065 ONSNetworkScanCtlr mONSNetworkScanCtlr;
danielwbhuangf699f742019-07-03 21:42:59 +080066 @Mock
67 TelephonyManager mSubscriptionBoundTelephonyManager;
Malcolm Chen9059f472019-12-18 14:33:32 -080068 @Mock
69 ISub mISubMock;
70 @Mock
71 IBinder mISubBinderMock;
72 @Mock
73 SubscriptionInfo mSubInfo;
Sooraj Sasindran33345152018-10-01 11:22:56 -070074 private Looper mLooper;
Sooraj Sasindran653edbd2019-01-08 13:17:29 -080075 private static final String TAG = "ONSProfileSelectorTest";
Sooraj Sasindran33345152018-10-01 11:22:56 -070076
Sooraj Sasindran653edbd2019-01-08 13:17:29 -080077 MyONSProfileSelector.ONSProfileSelectionCallback mONSProfileSelectionCallback =
Sooraj Sasindran81f153f2019-07-09 10:58:24 -070078 new MyONSProfileSelector.ONSProfileSelectionCallback() {
79 public void onProfileSelectionDone() {
80 mCallbackInvoked = true;
81 setReady(true);
82 }
83 };
Sooraj Sasindran33345152018-10-01 11:22:56 -070084
Sooraj Sasindran653edbd2019-01-08 13:17:29 -080085 public class MyONSProfileSelector extends ONSProfileSelector {
Sooraj Sasindran81f153f2019-07-09 10:58:24 -070086
Sooraj Sasindran33345152018-10-01 11:22:56 -070087 public SubscriptionManager.OnOpportunisticSubscriptionsChangedListener mProfileChngLstnrCpy;
88 public BroadcastReceiver mProfileSelectorBroadcastReceiverCpy;
Sooraj Sasindran653edbd2019-01-08 13:17:29 -080089 public ONSNetworkScanCtlr.NetworkAvailableCallBack mNetworkAvailableCallBackCpy;
Sooraj Sasindran33345152018-10-01 11:22:56 -070090
Sooraj Sasindran653edbd2019-01-08 13:17:29 -080091 public MyONSProfileSelector(Context c,
Sooraj Sasindran81f153f2019-07-09 10:58:24 -070092 MyONSProfileSelector.ONSProfileSelectionCallback aNSProfileSelectionCallback) {
Sooraj Sasindran33345152018-10-01 11:22:56 -070093 super(c, aNSProfileSelectionCallback);
94 }
95
96 public void triggerProfileUpdate() {
97 mHandler.sendEmptyMessage(1);
98 }
99
Sooraj Sasindran5f719622018-10-30 23:04:43 -0700100 public void updateOppSubs() {
101 updateOpportunisticSubscriptions();
102 }
103
Sooraj Sasindran4b19d8f2019-11-19 11:24:28 -0800104 public int getCurrentPreferredData() {
105 return mCurrentDataSubId;
106 }
107
108 public void setCurrentPreferredData(int subId) {
109 mCurrentDataSubId = subId;
110 }
111
Sooraj Sasindran33345152018-10-01 11:22:56 -0700112 protected void init(Context c,
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700113 MyONSProfileSelector.ONSProfileSelectionCallback aNSProfileSelectionCallback) {
Sooraj Sasindran33345152018-10-01 11:22:56 -0700114 super.init(c, aNSProfileSelectionCallback);
Sooraj Sasindran653edbd2019-01-08 13:17:29 -0800115 this.mSubscriptionManager = ONSProfileSelectorTest.this.mSubscriptionManager;
danielwbhuangf699f742019-07-03 21:42:59 +0800116 this.mSubscriptionBoundTelephonyManager =
117 ONSProfileSelectorTest.this.mSubscriptionBoundTelephonyManager;
Sooraj Sasindran33345152018-10-01 11:22:56 -0700118 mProfileChngLstnrCpy = mProfileChangeListener;
Sooraj Sasindran3247a4b2019-04-01 15:30:16 -0700119 mProfileSelectorBroadcastReceiverCpy = null;
Sooraj Sasindran33345152018-10-01 11:22:56 -0700120 mNetworkAvailableCallBackCpy = mNetworkAvailableCallBack;
Sooraj Sasindran653edbd2019-01-08 13:17:29 -0800121 mNetworkScanCtlr = mONSNetworkScanCtlr;
Sooraj Sasindran33345152018-10-01 11:22:56 -0700122 }
123 }
124
Malcolm Chen9059f472019-12-18 14:33:32 -0800125 private void addISubService() throws Exception {
126 Field field = ServiceManager.class.getDeclaredField("sCache");
127 field.setAccessible(true);
128 ((Map<String, IBinder>)field.get(null)).put("isub", mISubBinderMock);
129 doReturn(mISubMock).when(mISubBinderMock).queryLocalInterface(any());
130 }
131
132 private void removeISubService() throws Exception {
133 Field field = ServiceManager.class.getDeclaredField("sCache");
134 field.setAccessible(true);
135 ((Map<String, IBinder>)field.get(null)).remove("isub");
136 }
137
Sooraj Sasindran33345152018-10-01 11:22:56 -0700138 @Before
139 public void setUp() throws Exception {
Sooraj Sasindran653edbd2019-01-08 13:17:29 -0800140 super.setUp("ONSTest");
Sooraj Sasindran33345152018-10-01 11:22:56 -0700141 mLooper = null;
142 MockitoAnnotations.initMocks(this);
Malcolm Chen9059f472019-12-18 14:33:32 -0800143 addISubService();
Sooraj Sasindran33345152018-10-01 11:22:56 -0700144 }
145
146 @After
147 public void tearDown() throws Exception {
Malcolm Chen9059f472019-12-18 14:33:32 -0800148 removeISubService();
Sooraj Sasindran33345152018-10-01 11:22:56 -0700149 super.tearDown();
150 if (mLooper != null) {
151 mLooper.quit();
152 mLooper.getThread().join();
153 }
154 }
155
danielwbhuang9949a6a2019-03-27 17:56:31 +0800156
Sooraj Sasindran33345152018-10-01 11:22:56 -0700157 @Test
158 public void testStartProfileSelectionWithNoOpportunisticSub() {
159 List<CellInfo> results2 = new ArrayList<CellInfo>();
160 CellIdentityLte cellIdentityLte = new CellIdentityLte(310, 210, 1, 1, 1);
161 CellInfoLte cellInfoLte = new CellInfoLte();
162 cellInfoLte.setCellIdentity(cellIdentityLte);
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700163 results2.add((CellInfo) cellInfoLte);
Sooraj Sasindran23942272018-10-30 23:04:43 -0700164 ArrayList<String> mccMncs = new ArrayList<>();
165 mccMncs.add("310210");
Sooraj Sasindran825d5a82019-03-08 08:08:04 -0800166 AvailableNetworkInfo availableNetworkInfo = new AvailableNetworkInfo(1, 1, mccMncs,
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700167 new ArrayList<Integer>());
Sooraj Sasindran23942272018-10-30 23:04:43 -0700168 ArrayList<AvailableNetworkInfo> availableNetworkInfos = new ArrayList<AvailableNetworkInfo>();
169 availableNetworkInfos.add(availableNetworkInfo);
Sooraj Sasindran33345152018-10-01 11:22:56 -0700170
Avinash Malipatil79572952022-05-26 08:48:31 +0000171 UiccPortInfo uiccPortInfo = new UiccPortInfo("", 1, 1, false);
172 ArrayList<UiccPortInfo> uiccPortInfoList = new ArrayList<>();
173 uiccPortInfoList.add(uiccPortInfo);
174
175 UiccCardInfo uiccCardInfo = new UiccCardInfo(true, 1, "", 0, false, true, uiccPortInfoList);
176 ArrayList<UiccCardInfo> uiccCardInfoList = new ArrayList<>();
177 uiccCardInfoList.add(uiccCardInfo);
178
179 doReturn(uiccCardInfoList).when(mMockTelephonyManager).getUiccCardsInfo();
180 doReturn(mMockEuiccManager).when(mMockEuiccManager).createForCardId(1);
181 doReturn(true).when(mMockEuiccManager).isSimPortAvailable(1);
182
danielwbhuang9949a6a2019-03-27 17:56:31 +0800183 IUpdateAvailableNetworksCallback mCallback = new IUpdateAvailableNetworksCallback.Stub() {
184 @Override
185 public void onComplete(int result) {
186 Log.d(TAG, "mResult end:" + result);
187 mResult = result;
188 }
189 };
190
191 mResult = -1;
Sooraj Sasindran33345152018-10-01 11:22:56 -0700192 mReady = false;
193 mCallbackInvoked = false;
194 new Thread(new Runnable() {
195 @Override
196 public void run() {
197 Looper.prepare();
danielwbhuang9949a6a2019-03-27 17:56:31 +0800198 doReturn(true).when(mONSNetworkScanCtlr).startFastNetworkScan(anyObject());
Sooraj Sasindrand6b19aa2019-04-10 17:15:12 -0700199 doReturn(new ArrayList<>()).when(mSubscriptionManager)
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700200 .getOpportunisticSubscriptions();
Sooraj Sasindran653edbd2019-01-08 13:17:29 -0800201 mONSProfileSelector = new MyONSProfileSelector(mContext,
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700202 mONSProfileSelectionCallback);
Avinash Malipatil79572952022-05-26 08:48:31 +0000203 mONSProfileSelector.mTelephonyManager = mMockTelephonyManager;
204 mONSProfileSelector.mEuiccManager = mMockEuiccManager;
danielwbhuang9949a6a2019-03-27 17:56:31 +0800205 mONSProfileSelector.updateOppSubs();
206 mONSProfileSelector.startProfileSelection(availableNetworkInfos, mCallback);
Sooraj Sasindran33345152018-10-01 11:22:56 -0700207 mLooper = Looper.myLooper();
208 setReady(true);
209 Looper.loop();
210 }
211 }).start();
212
Sooraj Sasindran33345152018-10-01 11:22:56 -0700213 // Wait till initialization is complete.
214 waitUntilReady();
215 mReady = false;
216
217 // Testing startProfileSelection without any oppotunistic data.
218 // should not get any callback invocation.
Sooraj Sasindran33345152018-10-01 11:22:56 -0700219 waitUntilReady(100);
danielwbhuang0245be22019-10-29 20:29:50 +0800220 assertEquals(
221 TelephonyManager.UPDATE_AVAILABLE_NETWORKS_NO_OPPORTUNISTIC_SUB_AVAILABLE, mResult);
Sooraj Sasindran33345152018-10-01 11:22:56 -0700222 assertFalse(mCallbackInvoked);
223 }
224
225 @Test
226 public void testStartProfileSelectionSuccess() {
Malcolm Chen9059f472019-12-18 14:33:32 -0800227 int subId = 5;
Avinash Malipatil79572952022-05-26 08:48:31 +0000228 List<SubscriptionInfo> activeSubscriptionInfoList = new ArrayList<SubscriptionInfo>();
229 List<SubscriptionInfo> oppSubscriptionInfoList = new ArrayList<SubscriptionInfo>();
230 SubscriptionInfo subscriptionInfo1 = new SubscriptionInfo(subId, "", 1, "TMO", "TMO", 1, 1,
231 "123", 1, null, "310", "210", "", true, null, "1", 1, true, null, false, 1, 1, 1,
232 null, null, false, 0);
233 SubscriptionInfo subscriptionInfo2 = new SubscriptionInfo(6, "", 1, "TMO", "TMO", 1, 1,
234 "123", 1, null, "310", "211", "", true, null, "1", 1, false, null, false, 1, 1, 1,
235 null, null, false, 0);
236 oppSubscriptionInfoList.add(subscriptionInfo1);
237 activeSubscriptionInfoList.add(subscriptionInfo1);
238 activeSubscriptionInfoList.add(subscriptionInfo2);
Sooraj Sasindran33345152018-10-01 11:22:56 -0700239
240 List<CellInfo> results2 = new ArrayList<CellInfo>();
241 CellIdentityLte cellIdentityLte = new CellIdentityLte(310, 210, 1, 1, 1);
242 CellInfoLte cellInfoLte = new CellInfoLte();
243 cellInfoLte.setCellIdentity(cellIdentityLte);
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700244 results2.add((CellInfo) cellInfoLte);
Sooraj Sasindran23942272018-10-30 23:04:43 -0700245 ArrayList<String> mccMncs = new ArrayList<>();
246 mccMncs.add("310210");
Malcolm Chen9059f472019-12-18 14:33:32 -0800247 AvailableNetworkInfo availableNetworkInfo = new AvailableNetworkInfo(subId, 1, mccMncs,
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700248 new ArrayList<Integer>());
Sooraj Sasindran23942272018-10-30 23:04:43 -0700249 ArrayList<AvailableNetworkInfo> availableNetworkInfos = new ArrayList<AvailableNetworkInfo>();
250 availableNetworkInfos.add(availableNetworkInfo);
Sooraj Sasindran33345152018-10-01 11:22:56 -0700251
danielwbhuang9949a6a2019-03-27 17:56:31 +0800252 IUpdateAvailableNetworksCallback mCallback = new IUpdateAvailableNetworksCallback.Stub() {
253 @Override
254 public void onComplete(int result) {
255 mResult = result;
256 }
257 };
258
259 mResult = -1;
Sooraj Sasindran33345152018-10-01 11:22:56 -0700260 mReady = false;
261 new Thread(new Runnable() {
262 @Override
263 public void run() {
264 Looper.prepare();
Avinash Malipatil79572952022-05-26 08:48:31 +0000265 doReturn(subscriptionInfo1).when(mSubscriptionManager)
266 .getActiveSubscriptionInfo(subId);
267 doReturn(oppSubscriptionInfoList).when(mSubscriptionManager)
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700268 .getOpportunisticSubscriptions();
Avinash Malipatil79572952022-05-26 08:48:31 +0000269 doReturn(activeSubscriptionInfoList).when(mSubscriptionManager)
270 .getActiveSubscriptionInfoList();
Avinash Malipatilee917672022-07-25 17:35:38 +0000271 doReturn(activeSubscriptionInfoList).when(mSubscriptionManager)
272 .getCompleteActiveSubscriptionInfoList();
Malcolm Chen9059f472019-12-18 14:33:32 -0800273 doReturn(true).when(mSubscriptionManager).isActiveSubId(subId);
danielwbhuangf699f742019-07-03 21:42:59 +0800274 doReturn(true).when(mSubscriptionBoundTelephonyManager).enableModemForSlot(
275 anyInt(), anyBoolean());
Sooraj Sasindran653edbd2019-01-08 13:17:29 -0800276 mONSProfileSelector = new MyONSProfileSelector(mContext,
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700277 new MyONSProfileSelector.ONSProfileSelectionCallback() {
278 public void onProfileSelectionDone() {
279 setReady(true);
280 }
281 });
danielwbhuang9949a6a2019-03-27 17:56:31 +0800282 mONSProfileSelector.updateOppSubs();
283 mONSProfileSelector.startProfileSelection(availableNetworkInfos, mCallback);
Sooraj Sasindran33345152018-10-01 11:22:56 -0700284 mLooper = Looper.myLooper();
285 setReady(true);
286 Looper.loop();
287 }
288 }).start();
289
Sooraj Sasindran33345152018-10-01 11:22:56 -0700290 // Wait till initialization is complete.
291 waitUntilReady();
292 mReady = false;
293 mDataSubId = -1;
294
295 // Testing startProfileSelection with oppotunistic sub.
296 // On success onProfileSelectionDone must get invoked.
Sooraj Sasindran33345152018-10-01 11:22:56 -0700297 assertFalse(mReady);
danielwbhuang9949a6a2019-03-27 17:56:31 +0800298 waitForMs(500);
Sooraj Sasindran653edbd2019-01-08 13:17:29 -0800299 mONSProfileSelector.mNetworkAvailableCallBackCpy.onNetworkAvailability(results2);
300 Intent callbackIntent = new Intent(MyONSProfileSelector.ACTION_SUB_SWITCH);
Sooraj Sasindran33345152018-10-01 11:22:56 -0700301 callbackIntent.putExtra("sequenceId", 1);
Malcolm Chen9059f472019-12-18 14:33:32 -0800302 callbackIntent.putExtra("subId", subId);
Sooraj Sasindran5f719622018-10-30 23:04:43 -0700303 waitUntilReady();
danielwbhuang9949a6a2019-03-27 17:56:31 +0800304 assertEquals(TelephonyManager.UPDATE_AVAILABLE_NETWORKS_SUCCESS, mResult);
Sooraj Sasindrane59c0e82018-10-30 23:02:36 -0700305 assertTrue(mReady);
Sooraj Sasindran33345152018-10-01 11:22:56 -0700306 }
Sooraj Sasindran5f719622018-10-30 23:04:43 -0700307
Sooraj Sasindrand6b19aa2019-04-10 17:15:12 -0700308 @Test
danielwbhuangb220c292019-12-12 18:04:28 +0800309 public void testStartProfileSelectionWithDifferentPrioritySubInfo() {
310 int PRIORITY_HIGH = 1;
311 int PRIORITY_MED = 2;
312
313 List<SubscriptionInfo> subscriptionInfoList = new ArrayList<SubscriptionInfo>();
314 SubscriptionInfo subscriptionInfo = new SubscriptionInfo(5, "", 1, "TMO", "TMO", 1, 1,
Avinash Malipatilee917672022-07-25 17:35:38 +0000315 "123", 1, null, "310", "210", "", true, null, "1", 1, true, null, false, 1, 1, 1,
316 null, null, false, 0);
danielwbhuangb220c292019-12-12 18:04:28 +0800317 subscriptionInfoList.add(subscriptionInfo);
318 SubscriptionInfo subscriptionInfo_2 = new SubscriptionInfo(8, "", 1, "Vzw", "Vzw", 1, 1,
Avinash Malipatilee917672022-07-25 17:35:38 +0000319 "456", 1, null, "311", "480", "", true, null, "1", 1, true, null, false, 1, 1, 1,
320 null, null, false, 1);
danielwbhuangb220c292019-12-12 18:04:28 +0800321 subscriptionInfoList.add(subscriptionInfo_2);
Malcolm Chen9059f472019-12-18 14:33:32 -0800322 doReturn(subscriptionInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(5);
323 doReturn(subscriptionInfo_2).when(mSubscriptionManager).getActiveSubscriptionInfo(8);
Avinash Malipatilee917672022-07-25 17:35:38 +0000324 doReturn(subscriptionInfoList).when(mSubscriptionManager)
325 .getCompleteActiveSubscriptionInfoList();
326 doReturn(subscriptionInfoList).when(mSubscriptionManager).getOpportunisticSubscriptions();
danielwbhuangb220c292019-12-12 18:04:28 +0800327
328 List<CellInfo> results2 = new ArrayList<CellInfo>();
329 CellIdentityLte cellIdentityLte = new CellIdentityLte(310, 210, 1, 1, 1);
330 CellInfoLte cellInfoLte = new CellInfoLte();
331 cellInfoLte.setCellIdentity(cellIdentityLte);
332 results2.add((CellInfo) cellInfoLte);
333 CellIdentityLte cellIdentityLte_2 = new CellIdentityLte(311, 480, 1, 1, 1);
334 CellInfoLte cellInfoLte_2 = new CellInfoLte();
335 cellInfoLte_2.setCellIdentity(cellIdentityLte_2);
336 results2.add((CellInfo) cellInfoLte_2);
337
338 ArrayList<String> mccMncs = new ArrayList<>();
339 mccMncs.add("310210");
340 AvailableNetworkInfo availableNetworkInfo = new AvailableNetworkInfo(5, PRIORITY_MED,
341 mccMncs, new ArrayList<Integer>());
342 ArrayList<AvailableNetworkInfo> availableNetworkInfos = new ArrayList<>();
343 availableNetworkInfos.add(availableNetworkInfo);
344 ArrayList<String> mccMncs_2 = new ArrayList<>();
345 mccMncs_2.add("311480");
346 AvailableNetworkInfo availableNetworkInfo_2 = new AvailableNetworkInfo(8, PRIORITY_HIGH,
347 mccMncs_2, new ArrayList<Integer>());
348 availableNetworkInfos.add(availableNetworkInfo_2);
349
350 IUpdateAvailableNetworksCallback mCallback = new IUpdateAvailableNetworksCallback.Stub() {
351 @Override
352 public void onComplete(int result) {
353 mResult = result;
354 }
355 };
356
357 mResult = -1;
358 mReady = false;
359 new Thread(new Runnable() {
360 @Override
361 public void run() {
362 Looper.prepare();
danielwbhuangb220c292019-12-12 18:04:28 +0800363 doReturn(true).when(mSubscriptionManager).isActiveSubId(anyInt());
364 doReturn(true).when(mSubscriptionBoundTelephonyManager).enableModemForSlot(
365 anyInt(), anyBoolean());
366 mONSProfileSelector = new MyONSProfileSelector(mContext,
367 new MyONSProfileSelector.ONSProfileSelectionCallback() {
368 public void onProfileSelectionDone() {
369 setReady(true);
370 }
371 });
372 mONSProfileSelector.updateOppSubs();
373 mONSProfileSelector.startProfileSelection(availableNetworkInfos, mCallback);
374 mLooper = Looper.myLooper();
375 setReady(true);
376 Looper.loop();
377 }
378 }).start();
379 waitUntilReady();
380 waitForMs(500);
381 // get high priority subId
382 int retrieveSubId = mONSProfileSelector.retrieveBestSubscription(results2);
383 mONSProfileSelector.mNetworkAvailableCallBackCpy.onNetworkAvailability(results2);
384 assertEquals(8, retrieveSubId);
385 assertEquals(TelephonyManager.UPDATE_AVAILABLE_NETWORKS_SUCCESS, mResult);
386 }
387
388 @Test
Sooraj Sasindrand6b19aa2019-04-10 17:15:12 -0700389 public void testStartProfileSelectionWithActivePrimarySimOnESim() {
390 List<SubscriptionInfo> opportunisticSubscriptionInfoList = new ArrayList<SubscriptionInfo>();
391 List<SubscriptionInfo> activeSubscriptionInfoList = new ArrayList<SubscriptionInfo>();
Avinash Malipatil79572952022-05-26 08:48:31 +0000392 SubscriptionInfo subscriptionInfo1 = new SubscriptionInfo(5, "", 1, "TMO", "TMO", 1, 1,
393 "123", 1, null, "310", "210", "", true, null, "1", 1, true, null, false, 1839, 1,
394 1, null, null, false, 1);
Sooraj Sasindrand6b19aa2019-04-10 17:15:12 -0700395 SubscriptionInfo subscriptionInfo2 = new SubscriptionInfo(6, "", 1, "TMO", "TMO", 1, 1,
Avinash Malipatil79572952022-05-26 08:48:31 +0000396 "456", 1, null, "310", "211", "", true, null, "1", 1, false, null, false, 1839, 1,
397 1, null, null, false, 2);
398
399 activeSubscriptionInfoList.add(subscriptionInfo1);
Sooraj Sasindrand6b19aa2019-04-10 17:15:12 -0700400 activeSubscriptionInfoList.add(subscriptionInfo2);
Avinash Malipatil79572952022-05-26 08:48:31 +0000401 doReturn(subscriptionInfo1).when(mSubscriptionManager).getActiveSubscriptionInfo(5);
Malcolm Chen9059f472019-12-18 14:33:32 -0800402 doReturn(subscriptionInfo2).when(mSubscriptionManager).getActiveSubscriptionInfo(6);
Sooraj Sasindrand6b19aa2019-04-10 17:15:12 -0700403
404 ArrayList<String> mccMncs = new ArrayList<>();
405 mccMncs.add("310210");
406 AvailableNetworkInfo availableNetworkInfo = new AvailableNetworkInfo(5, 2, mccMncs,
407 new ArrayList<Integer>());
408 ArrayList<AvailableNetworkInfo> availableNetworkInfos = new ArrayList<AvailableNetworkInfo>();
409 availableNetworkInfos.add(availableNetworkInfo);
410
Avinash Malipatil79572952022-05-26 08:48:31 +0000411 ArrayList<UiccPortInfo> uiccPortInfoList = new ArrayList<>();
412 uiccPortInfoList.add(new UiccPortInfo("1", 0, 0, false));
413 uiccPortInfoList.add(new UiccPortInfo("2", 1, 1, true));
414
415 UiccCardInfo uiccCardInfo = new UiccCardInfo(
416 true, 1, "1", 0, false, true, uiccPortInfoList);
417 ArrayList<UiccCardInfo> uiccCardInfoList = new ArrayList<>();
418 uiccCardInfoList.add(uiccCardInfo);
419
420 doReturn(uiccCardInfoList).when(mMockTelephonyManager).getUiccCardsInfo();
421 doReturn(mMockEuiccManager).when(mMockEuiccManager).createForCardId(1);
422 doReturn(true).when(mMockEuiccManager).isSimPortAvailable(1);
423
Sooraj Sasindrand6b19aa2019-04-10 17:15:12 -0700424 IUpdateAvailableNetworksCallback mCallback = new IUpdateAvailableNetworksCallback.Stub() {
425 @Override
426 public void onComplete(int result) {
427 mResult = result;
428 }
429 };
430
431 mResult = -1;
432 mReady = false;
433 new Thread(new Runnable() {
434 @Override
435 public void run() {
436 Looper.prepare();
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700437 doReturn(opportunisticSubscriptionInfoList).when(mSubscriptionManager)
438 .getOpportunisticSubscriptions();
Sooraj Sasindrand6b19aa2019-04-10 17:15:12 -0700439 doReturn(false).when(mSubscriptionManager).isActiveSubId(anyInt());
440 doReturn(activeSubscriptionInfoList).when(mSubscriptionManager)
441 .getActiveSubscriptionInfoList(anyBoolean());
Avinash Malipatil79572952022-05-26 08:48:31 +0000442 doReturn(activeSubscriptionInfoList).when(mSubscriptionManager)
443 .getActiveSubscriptionInfoList();
444 doReturn(true).when(mSubscriptionBoundTelephonyManager).enableModemForSlot(
445 anyInt(), anyBoolean());
Sooraj Sasindrand6b19aa2019-04-10 17:15:12 -0700446 mONSProfileSelector = new MyONSProfileSelector(mContext,
447 new MyONSProfileSelector.ONSProfileSelectionCallback() {
448 public void onProfileSelectionDone() {
449 setReady(true);
450 }
451 });
Avinash Malipatil79572952022-05-26 08:48:31 +0000452 mONSProfileSelector.mTelephonyManager = mMockTelephonyManager;
453 mONSProfileSelector.mEuiccManager = mMockEuiccManager;
Sooraj Sasindrand6b19aa2019-04-10 17:15:12 -0700454 mONSProfileSelector.updateOppSubs();
455 mONSProfileSelector.startProfileSelection(availableNetworkInfos, mCallback);
456 mLooper = Looper.myLooper();
457 setReady(true);
458 Looper.loop();
459 }
460 }).start();
461
462 // Wait till initialization is complete.
463 waitUntilReady();
464 mReady = false;
465 mDataSubId = -1;
466
467 // Testing startProfileSelection with opportunistic sub.
468 // On success onProfileSelectionDone must get invoked.
469 assertFalse(mReady);
470 waitForMs(100);
471 Intent callbackIntent = new Intent(MyONSProfileSelector.ACTION_SUB_SWITCH);
472 callbackIntent.putExtra("sequenceId", 1);
473 callbackIntent.putExtra("subId", 5);
474 waitUntilReady();
Avinash Malipatil79572952022-05-26 08:48:31 +0000475 assertEquals(TelephonyManager.UPDATE_AVAILABLE_NETWORKS_NO_OPPORTUNISTIC_SUB_AVAILABLE,
476 mResult);
Sooraj Sasindrand6b19aa2019-04-10 17:15:12 -0700477 }
478
danielwbhuang9949a6a2019-03-27 17:56:31 +0800479 public static void waitForMs(long ms) {
480 try {
481 Thread.sleep(ms);
482 } catch (InterruptedException e) {
483 Log.d(TAG, "InterruptedException while waiting: " + e);
484 }
485 }
486
Sooraj Sasindran5f719622018-10-30 23:04:43 -0700487 @Test
488 public void testselectProfileForDataWithNoOpportunsticSub() {
489 mReady = false;
Sooraj Sasindrand6b19aa2019-04-10 17:15:12 -0700490 doReturn(new ArrayList<>()).when(mSubscriptionManager).getOpportunisticSubscriptions();
Sooraj Sasindran5f719622018-10-30 23:04:43 -0700491 new Thread(new Runnable() {
492 @Override
493 public void run() {
494 Looper.prepare();
Sooraj Sasindran653edbd2019-01-08 13:17:29 -0800495 mONSProfileSelector = new MyONSProfileSelector(mContext,
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700496 new MyONSProfileSelector.ONSProfileSelectionCallback() {
497 public void onProfileSelectionDone() {
498 }
499 });
Sooraj Sasindran5f719622018-10-30 23:04:43 -0700500 mLooper = Looper.myLooper();
501 setReady(true);
502 Looper.loop();
503 }
504 }).start();
505
506 // Wait till initialization is complete.
507 waitUntilReady();
508
509 // Testing selectProfileForData with no oppotunistic sub and the function should
510 // return false.
Sooraj Sasindran1be32b22019-02-28 14:47:45 -0800511 mONSProfileSelector.selectProfileForData(1, false, null);
Sooraj Sasindran5f719622018-10-30 23:04:43 -0700512 }
513
514 @Test
515 public void testselectProfileForDataWithInActiveSub() {
516 List<SubscriptionInfo> subscriptionInfoList = new ArrayList<SubscriptionInfo>();
517 SubscriptionInfo subscriptionInfo = new SubscriptionInfo(5, "", 1, "TMO", "TMO", 1, 1,
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700518 "123", 1, null, "310", "210", "", false, null, "1");
Sooraj Sasindran5f719622018-10-30 23:04:43 -0700519 subscriptionInfoList.add(subscriptionInfo);
Malcolm Chen9059f472019-12-18 14:33:32 -0800520 doReturn(subscriptionInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(5);
Sooraj Sasindran5f719622018-10-30 23:04:43 -0700521 mReady = false;
Sooraj Sasindrand6b19aa2019-04-10 17:15:12 -0700522 doReturn(new ArrayList<>()).when(mSubscriptionManager).getOpportunisticSubscriptions();
Sooraj Sasindran5f719622018-10-30 23:04:43 -0700523 new Thread(new Runnable() {
524 @Override
525 public void run() {
526 Looper.prepare();
Sooraj Sasindran653edbd2019-01-08 13:17:29 -0800527 mONSProfileSelector = new MyONSProfileSelector(mContext,
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700528 new MyONSProfileSelector.ONSProfileSelectionCallback() {
529 public void onProfileSelectionDone() {
530 }
531 });
Sooraj Sasindran5f719622018-10-30 23:04:43 -0700532 mLooper = Looper.myLooper();
533 setReady(true);
534 Looper.loop();
535 }
536 }).start();
537
538 // Wait till initialization is complete.
539 waitUntilReady();
540
541 // Testing selectProfileForData with in active sub and the function should return false.
Sooraj Sasindran1be32b22019-02-28 14:47:45 -0800542 mONSProfileSelector.selectProfileForData(5, false, null);
Sooraj Sasindran5f719622018-10-30 23:04:43 -0700543 }
544
545 @Test
546 public void testselectProfileForDataWithInvalidSubId() {
547 List<SubscriptionInfo> subscriptionInfoList = new ArrayList<SubscriptionInfo>();
548 SubscriptionInfo subscriptionInfo = new SubscriptionInfo(5, "", 1, "TMO", "TMO", 1, 1,
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700549 "123", 1, null, "310", "210", "", false, null, "1");
Sooraj Sasindran5f719622018-10-30 23:04:43 -0700550 subscriptionInfoList.add(subscriptionInfo);
Malcolm Chen9059f472019-12-18 14:33:32 -0800551 doReturn(subscriptionInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(5);
Sooraj Sasindran5f719622018-10-30 23:04:43 -0700552 mReady = false;
553 doReturn(subscriptionInfoList).when(mSubscriptionManager).getOpportunisticSubscriptions();
Malcolm Chen9341f632019-02-05 18:41:46 -0800554 doNothing().when(mSubscriptionManager).setPreferredDataSubscriptionId(
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700555 anyInt(), anyBoolean(), any(), any());
Sooraj Sasindran5f719622018-10-30 23:04:43 -0700556 new Thread(new Runnable() {
557 @Override
558 public void run() {
559 Looper.prepare();
Sooraj Sasindran653edbd2019-01-08 13:17:29 -0800560 mONSProfileSelector = new MyONSProfileSelector(mContext,
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700561 new MyONSProfileSelector.ONSProfileSelectionCallback() {
562 public void onProfileSelectionDone() {
563 }
564 });
Sooraj Sasindran5f719622018-10-30 23:04:43 -0700565 mLooper = Looper.myLooper();
566 setReady(true);
567 Looper.loop();
568 }
569 }).start();
570
571 // Wait till initialization is complete.
572 waitUntilReady();
573
574 // Testing selectProfileForData with INVALID_SUBSCRIPTION_ID and the function should
575 // return true.
Sooraj Sasindran1be32b22019-02-28 14:47:45 -0800576 mONSProfileSelector.selectProfileForData(
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700577 SubscriptionManager.INVALID_SUBSCRIPTION_ID, false, null);
Sooraj Sasindran5f719622018-10-30 23:04:43 -0700578 }
579
580 @Test
581 public void testselectProfileForDataWithValidSub() {
582 List<SubscriptionInfo> subscriptionInfoList = new ArrayList<SubscriptionInfo>();
583 SubscriptionInfo subscriptionInfo = new SubscriptionInfo(5, "", 1, "TMO", "TMO", 1, 1,
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700584 "123", 1, null, "310", "210", "", false, null, "1");
Sooraj Sasindran5f719622018-10-30 23:04:43 -0700585 subscriptionInfoList.add(subscriptionInfo);
Malcolm Chen9059f472019-12-18 14:33:32 -0800586 doReturn(subscriptionInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(5);
Sooraj Sasindran5f719622018-10-30 23:04:43 -0700587 mReady = false;
588 doReturn(subscriptionInfoList).when(mSubscriptionManager)
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700589 .getActiveSubscriptionInfoList();
Malcolm Chen9341f632019-02-05 18:41:46 -0800590 doNothing().when(mSubscriptionManager).setPreferredDataSubscriptionId(
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700591 anyInt(), anyBoolean(), any(), any());
Sooraj Sasindran5f719622018-10-30 23:04:43 -0700592 new Thread(new Runnable() {
593 @Override
594 public void run() {
595 Looper.prepare();
596 doReturn(subscriptionInfoList).when(mSubscriptionManager)
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700597 .getOpportunisticSubscriptions();
Sooraj Sasindran653edbd2019-01-08 13:17:29 -0800598 mONSProfileSelector = new MyONSProfileSelector(mContext,
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700599 new MyONSProfileSelector.ONSProfileSelectionCallback() {
600 public void onProfileSelectionDone() {
601 }
602 });
Sooraj Sasindran653edbd2019-01-08 13:17:29 -0800603 mONSProfileSelector.updateOppSubs();
Sooraj Sasindran5f719622018-10-30 23:04:43 -0700604 mLooper = Looper.myLooper();
605 setReady(true);
606 Looper.loop();
607 }
608 }).start();
609
610 // Wait till initialization is complete.
611 waitUntilReady();
612
613 // Testing selectProfileForData with valid opportunistic sub and the function should
614 // return true.
Sooraj Sasindran1be32b22019-02-28 14:47:45 -0800615 mONSProfileSelector.selectProfileForData(5, false, null);
Sooraj Sasindran5f719622018-10-30 23:04:43 -0700616 }
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700617
618 @Test
619 public void testStartProfileSelectionSuccessWithSameArgumentsAgain() {
Avinash Malipatil79572952022-05-26 08:48:31 +0000620 List<SubscriptionInfo> activeSubscriptionInfoList = new ArrayList<SubscriptionInfo>();
621 List<SubscriptionInfo> oppSubscriptionInfoList = new ArrayList<SubscriptionInfo>();
622 SubscriptionInfo subscriptionInfo1 = new SubscriptionInfo(5, "", 1, "TMO", "TMO", 1, 1,
623 "123", 1, null, "310", "210", "", true, null, "1", 1, true, null, false, 1, 1, 1,
624 null, null, false, 0);
Malcolm Chen9059f472019-12-18 14:33:32 -0800625 SubscriptionInfo subscriptionInfo2 = new SubscriptionInfo(6, "", 1, "TMO", "TMO", 1, 1,
Avinash Malipatil79572952022-05-26 08:48:31 +0000626 "123", 1, null, "310", "211", "", true, null, "1", 1, false, null, false, 1, 1, 1,
627 null, null, false, 0);
628
629 oppSubscriptionInfoList.add(subscriptionInfo1);
630 doReturn(subscriptionInfo1).when(mSubscriptionManager).getActiveSubscriptionInfo(5);
Malcolm Chen9059f472019-12-18 14:33:32 -0800631 doReturn(subscriptionInfo2).when(mSubscriptionManager).getActiveSubscriptionInfo(6);
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700632
Avinash Malipatil79572952022-05-26 08:48:31 +0000633 activeSubscriptionInfoList.add(subscriptionInfo1);
634 activeSubscriptionInfoList.add(subscriptionInfo2);
635 doReturn(activeSubscriptionInfoList).when(mSubscriptionManager)
Avinash Malipatilee917672022-07-25 17:35:38 +0000636 .getCompleteActiveSubscriptionInfoList();
Avinash Malipatil79572952022-05-26 08:48:31 +0000637
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700638 List<CellInfo> results2 = new ArrayList<CellInfo>();
639 CellIdentityLte cellIdentityLte = new CellIdentityLte(310, 210, 1, 1, 1);
640 CellInfoLte cellInfoLte = new CellInfoLte();
641 cellInfoLte.setCellIdentity(cellIdentityLte);
642 results2.add((CellInfo) cellInfoLte);
643 ArrayList<String> mccMncs = new ArrayList<>();
644 mccMncs.add("310210");
Malcolm Chen9059f472019-12-18 14:33:32 -0800645 AvailableNetworkInfo availableNetworkInfo = new AvailableNetworkInfo(5, 1, mccMncs,
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700646 new ArrayList<Integer>());
647 ArrayList<AvailableNetworkInfo> availableNetworkInfos = new ArrayList<AvailableNetworkInfo>();
648 availableNetworkInfos.add(availableNetworkInfo);
649
650 IUpdateAvailableNetworksCallback mCallback = new IUpdateAvailableNetworksCallback.Stub() {
651 @Override
652 public void onComplete(int result) {
653 mResult = result;
654 }
655 };
656
657 mResult = -1;
658 mReady = false;
659 mONSProfileSelector = new MyONSProfileSelector(mContext,
660 new MyONSProfileSelector.ONSProfileSelectionCallback() {
661 public void onProfileSelectionDone() {
662 setReady(true);
663 }
664 });
665 new Thread(new Runnable() {
666 @Override
667 public void run() {
668 Looper.prepare();
Avinash Malipatil79572952022-05-26 08:48:31 +0000669 doReturn(oppSubscriptionInfoList).when(mSubscriptionManager)
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700670 .getOpportunisticSubscriptions();
671 doReturn(true).when(mSubscriptionManager).isActiveSubId(anyInt());
672 doReturn(true).when(mSubscriptionBoundTelephonyManager).enableModemForSlot(
673 anyInt(), anyBoolean());
674
675 mONSProfileSelector.updateOppSubs();
676 mONSProfileSelector.startProfileSelection(availableNetworkInfos, mCallback);
677 mLooper = Looper.myLooper();
678 setReady(true);
679 Looper.loop();
680 }
681 }).start();
682
683 // Wait till initialization is complete.
684 waitUntilReady();
685 mReady = false;
686 mDataSubId = -1;
687
688 // Testing startProfileSelection with oppotunistic sub.
689 // On success onProfileSelectionDone must get invoked.
690 assertFalse(mReady);
691 waitForMs(500);
692 mONSProfileSelector.mNetworkAvailableCallBackCpy.onNetworkAvailability(results2);
693 Intent callbackIntent = new Intent(MyONSProfileSelector.ACTION_SUB_SWITCH);
694 callbackIntent.putExtra("sequenceId", 1);
695 callbackIntent.putExtra("subId", 5);
696 waitUntilReady();
697 assertEquals(TelephonyManager.UPDATE_AVAILABLE_NETWORKS_SUCCESS, mResult);
698 assertTrue(mReady);
699
700 mResult = -1;
701 mReady = false;
702 new Thread(new Runnable() {
703 @Override
704 public void run() {
705 Looper.prepare();
Avinash Malipatil79572952022-05-26 08:48:31 +0000706 doReturn(oppSubscriptionInfoList).when(mSubscriptionManager)
Sooraj Sasindran81f153f2019-07-09 10:58:24 -0700707 .getOpportunisticSubscriptions();
708 doReturn(true).when(mSubscriptionManager).isActiveSubId(anyInt());
709 doReturn(true).when(mSubscriptionBoundTelephonyManager).enableModemForSlot(
710 anyInt(), anyBoolean());
711 mONSProfileSelector.updateOppSubs();
712 mONSProfileSelector.startProfileSelection(availableNetworkInfos, mCallback);
713 mLooper = Looper.myLooper();
714 setReady(true);
715 Looper.loop();
716 }
717 }).start();
718
719 // Wait till initialization is complete.
720 waitUntilReady();
721 mReady = false;
722 mDataSubId = -1;
723
724 // Testing startProfileSelection with oppotunistic sub.
725 // On success onProfileSelectionDone must get invoked.
726 assertFalse(mReady);
727 waitForMs(500);
728 mONSProfileSelector.mNetworkAvailableCallBackCpy.onNetworkAvailability(results2);
729 waitUntilReady();
730 assertEquals(TelephonyManager.UPDATE_AVAILABLE_NETWORKS_SUCCESS, mResult);
731 assertTrue(mReady);
732 }
Sooraj Sasindran4b19d8f2019-11-19 11:24:28 -0800733
734 @Test
735 public void testStopProfileSelectionWithPreferredDataOnSame() {
736 List<SubscriptionInfo> subscriptionInfoList = new ArrayList<SubscriptionInfo>();
737 SubscriptionInfo subscriptionInfo = new SubscriptionInfo(5, "", 1, "TMO", "TMO", 1, 1,
738 "123", 1, null, "310", "210", "", true, null, "1", true, null, 0, 0);
739 subscriptionInfoList.add(subscriptionInfo);
Malcolm Chen9059f472019-12-18 14:33:32 -0800740 doReturn(subscriptionInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(5);
Sooraj Sasindran4b19d8f2019-11-19 11:24:28 -0800741
742 IUpdateAvailableNetworksCallback mCallback = new IUpdateAvailableNetworksCallback.Stub() {
743 @Override
744 public void onComplete(int result) {
745 mResult = result;
746 }
747 };
748
749 mResult = -1;
750 mReady = false;
751 new Thread(new Runnable() {
752 @Override
753 public void run() {
754 Looper.prepare();
755 doReturn(subscriptionInfoList).when(mSubscriptionManager)
756 .getOpportunisticSubscriptions();
757 doReturn(true).when(mSubscriptionManager).isActiveSubId(anyInt());
758 doReturn(true).when(mSubscriptionBoundTelephonyManager).enableModemForSlot(
759 anyInt(), anyBoolean());
760 doReturn(5).when(mSubscriptionManager).getPreferredDataSubscriptionId();
761 doReturn(subscriptionInfoList).when(mSubscriptionManager)
762 .getActiveSubscriptionInfoList(anyBoolean());
763
764 mONSProfileSelector = new MyONSProfileSelector(mContext,
765 new MyONSProfileSelector.ONSProfileSelectionCallback() {
766 public void onProfileSelectionDone() {
767 setReady(true);
768 }
769 });
770 mONSProfileSelector.updateOppSubs();
771 mONSProfileSelector.setCurrentPreferredData(5);
772 mONSProfileSelector.stopProfileSelection(null);
773 mLooper = Looper.myLooper();
774 setReady(true);
775 Looper.loop();
776 }
777 }).start();
778 waitUntilReady();
779 waitForMs(500);
Malcolm Chen9059f472019-12-18 14:33:32 -0800780 assertEquals(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, mONSProfileSelector.getCurrentPreferredData());
Sooraj Sasindran4b19d8f2019-11-19 11:24:28 -0800781 }
782
783 @Test
784 public void testStopProfileSelectionWithPreferredDataOnDifferent() {
785 List<SubscriptionInfo> subscriptionInfoList = new ArrayList<SubscriptionInfo>();
786 SubscriptionInfo subscriptionInfo = new SubscriptionInfo(5, "", 1, "TMO", "TMO", 1, 1,
787 "123", 1, null, "310", "210", "", true, null, "1", true, null, 0, 0);
788 subscriptionInfoList.add(subscriptionInfo);
Malcolm Chen9059f472019-12-18 14:33:32 -0800789 doReturn(subscriptionInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(5);
Sooraj Sasindran4b19d8f2019-11-19 11:24:28 -0800790
791 IUpdateAvailableNetworksCallback mCallback = new IUpdateAvailableNetworksCallback.Stub() {
792 @Override
793 public void onComplete(int result) {
794 mResult = result;
795 }
796 };
797
798 mResult = -1;
799 mReady = false;
800 new Thread(new Runnable() {
801 @Override
802 public void run() {
803 Looper.prepare();
804 doReturn(subscriptionInfoList).when(mSubscriptionManager)
805 .getOpportunisticSubscriptions();
806 doReturn(true).when(mSubscriptionManager).isActiveSubId(anyInt());
807 doReturn(true).when(mSubscriptionBoundTelephonyManager).enableModemForSlot(
808 anyInt(), anyBoolean());
809 doReturn(4).when(mSubscriptionManager).getPreferredDataSubscriptionId();
810 doReturn(subscriptionInfoList).when(mSubscriptionManager)
811 .getActiveSubscriptionInfoList(anyBoolean());
812
813 mONSProfileSelector = new MyONSProfileSelector(mContext,
814 new MyONSProfileSelector.ONSProfileSelectionCallback() {
815 public void onProfileSelectionDone() {
816 setReady(true);
817 }
818 });
819 mONSProfileSelector.updateOppSubs();
820 mONSProfileSelector.setCurrentPreferredData(5);
821 mONSProfileSelector.stopProfileSelection(null);
822 mLooper = Looper.myLooper();
823 setReady(true);
824 Looper.loop();
825 }
826 }).start();
827 waitUntilReady();
828 waitForMs(500);
Avinash Malipatilee917672022-07-25 17:35:38 +0000829 assertEquals(5, mONSProfileSelector.getCurrentPreferredData());
Sooraj Sasindran4b19d8f2019-11-19 11:24:28 -0800830 }
Avinash Malipatil79572952022-05-26 08:48:31 +0000831
832 @Test
833 public void testAvailablePortWhenTwoPrimarySIMsAreActive() {
Avinash Malipatilee917672022-07-25 17:35:38 +0000834 /**
835 * 2 - Primary active subscriptions and
836 * 1 - Inactive opportunistic subscription
837 */
838
Avinash Malipatil79572952022-05-26 08:48:31 +0000839 List<SubscriptionInfo> activeSubscriptionInfoList = new ArrayList<SubscriptionInfo>();
840 List<SubscriptionInfo> opportunisticInfoList = new ArrayList<SubscriptionInfo>();
841
Avinash Malipatilee917672022-07-25 17:35:38 +0000842 SubscriptionInfo oppSubInfo = new SubscriptionInfo(4, "", -1, "TMO", "TMO", 1, 1,
843 "001", 1, null, "110", "210", "", true, null, "1", 1, true, null, false, 2839, 1,
844 1, null, null, false, TelephonyManager.INVALID_PORT_INDEX);
Avinash Malipatil79572952022-05-26 08:48:31 +0000845
Avinash Malipatilee917672022-07-25 17:35:38 +0000846 SubscriptionInfo primarySubInfo1 = new SubscriptionInfo(5, "", 0, "TMO", "TMO", 1, 1,
847 "123", 1, null, "310", "210", "", true, null, "1", 1, false, null, false, 1839, 1,
848 1, null, null, false, 0);
849 SubscriptionInfo primarySubInfo2 = new SubscriptionInfo(6, "", 0, "TMO", "TMO", 1, 1,
850 "456", 1, null, "310", "211", "", true, null, "1", 1, false, null, false, 1839, 1,
851 1, null, null, false, 1);
852
853 activeSubscriptionInfoList.add(primarySubInfo1);
854 activeSubscriptionInfoList.add(primarySubInfo2);
855 opportunisticInfoList.add(oppSubInfo);
Avinash Malipatil79572952022-05-26 08:48:31 +0000856
857 doReturn(opportunisticInfoList).when(mSubscriptionManager).getOpportunisticSubscriptions();
858 doReturn(activeSubscriptionInfoList).when(mSubscriptionManager)
Avinash Malipatilee917672022-07-25 17:35:38 +0000859 .getCompleteActiveSubscriptionInfoList();
Avinash Malipatil79572952022-05-26 08:48:31 +0000860
Avinash Malipatilee917672022-07-25 17:35:38 +0000861 UiccPortInfo uiccPortInfo1 = new UiccPortInfo("", 0, 0, true);
862 UiccPortInfo uiccPortInfo2 = new UiccPortInfo("", 1, 0, true);
Avinash Malipatil79572952022-05-26 08:48:31 +0000863 ArrayList<UiccPortInfo> uiccPortInfoList = new ArrayList<>();
Avinash Malipatilee917672022-07-25 17:35:38 +0000864 uiccPortInfoList.add(uiccPortInfo1);
865 uiccPortInfoList.add(uiccPortInfo2);
Avinash Malipatil79572952022-05-26 08:48:31 +0000866
867 UiccCardInfo uiccCardInfo = new UiccCardInfo(true, 1, "", 0, false, true, uiccPortInfoList);
868 ArrayList<UiccCardInfo> uiccCardInfoList = new ArrayList<>();
869 uiccCardInfoList.add(uiccCardInfo);
870
871 doReturn(uiccCardInfoList).when(mMockTelephonyManager).getUiccCardsInfo();
872 doReturn(mMockEuiccManager).when(mMockEuiccManager).createForCardId(1);
Avinash Malipatilee917672022-07-25 17:35:38 +0000873 doReturn(false).when(mMockEuiccManager).isSimPortAvailable(0);
Avinash Malipatil79572952022-05-26 08:48:31 +0000874 doReturn(false).when(mMockEuiccManager).isSimPortAvailable(1);
875
876 mONSProfileSelector = new MyONSProfileSelector(mContext, null);
Avinash Malipatilee917672022-07-25 17:35:38 +0000877 mONSProfileSelector.mTelephonyManager = mMockTelephonyManager;
878 mONSProfileSelector.mEuiccManager = mMockEuiccManager;
879
Avinash Malipatil79572952022-05-26 08:48:31 +0000880 int portIdx = mONSProfileSelector.getAvailableESIMPortIndex();
Avinash Malipatilee917672022-07-25 17:35:38 +0000881 assertEquals(TelephonyManager.INVALID_PORT_INDEX, portIdx);
Avinash Malipatil79572952022-05-26 08:48:31 +0000882 }
883
884 @Test
885 public void testAvailablePortWhenOpportunisticEsimIsActive() {
Avinash Malipatilee917672022-07-25 17:35:38 +0000886 /**
887 * 1 - Primary active subscriptions and
888 * 1 - Active opportunistic subscription
889 */
890
Avinash Malipatil79572952022-05-26 08:48:31 +0000891 List<SubscriptionInfo> activeSubscriptionInfoList = new ArrayList<SubscriptionInfo>();
892 List<SubscriptionInfo> opportunisticInfoList = new ArrayList<SubscriptionInfo>();
893
Avinash Malipatilee917672022-07-25 17:35:38 +0000894 SubscriptionInfo oppSubInfo = new SubscriptionInfo(5, "", 1, "TMO", "TMO", 1, 1,
Avinash Malipatil79572952022-05-26 08:48:31 +0000895 "123", 1, null, "310", "210", "", true, null, "1", 1, true, null, false, 1839, 1,
Avinash Malipatilee917672022-07-25 17:35:38 +0000896 1, null, null, false, 0);
897
898 SubscriptionInfo primarySubInfo = new SubscriptionInfo(6, "", 1, "TMO", "TMO", 1, 1,
Avinash Malipatil79572952022-05-26 08:48:31 +0000899 "456", 1, null, "310", "211", "", true, null, "1", 1, false, null, false, 1839, 1,
Avinash Malipatilee917672022-07-25 17:35:38 +0000900 1, null, null, false, 1);
Avinash Malipatil79572952022-05-26 08:48:31 +0000901
Avinash Malipatilee917672022-07-25 17:35:38 +0000902 opportunisticInfoList.add(oppSubInfo);
903 activeSubscriptionInfoList.add(oppSubInfo);
904 activeSubscriptionInfoList.add(primarySubInfo);
Avinash Malipatil79572952022-05-26 08:48:31 +0000905
Avinash Malipatilee917672022-07-25 17:35:38 +0000906 doReturn(opportunisticInfoList).when(mSubscriptionManager)
907 .getOpportunisticSubscriptions();
908 doReturn(activeSubscriptionInfoList).when(mSubscriptionManager)
909 .getCompleteActiveSubscriptionInfoList();
Avinash Malipatil79572952022-05-26 08:48:31 +0000910
911 mONSProfileSelector = new MyONSProfileSelector(mContext, null);
912 int portIdx = mONSProfileSelector.getAvailableESIMPortIndex();
Avinash Malipatilee917672022-07-25 17:35:38 +0000913 assertEquals(0, portIdx);
914 }
915
916 @Test
917 public void testAvailablePortWhenTwoOpportunisticEsimsAreActive() {
918 /**
919 * 2 - Active opportunistic subscriptions.
920 */
921
922 List<SubscriptionInfo> activeSubscriptionInfoList = new ArrayList<SubscriptionInfo>();
923 List<SubscriptionInfo> opportunisticInfoList = new ArrayList<SubscriptionInfo>();
924
925 SubscriptionInfo opportunisticSubInfo1 = new SubscriptionInfo(5, "", 1, "TMO", "TMO", 1, 1,
926 "123", 1, null, "310", "210", "", true, null, "1", 1, true, null, false, 1839, 1,
927 1, null, null, false, 0);
928
929 SubscriptionInfo opportunisticSubInfo2 = new SubscriptionInfo(6, "", 1, "TMO", "TMO", 1, 1,
930 "456", 1, null, "310", "211", "", true, null, "1", 1, true, null, false, 1839, 1,
931 1, null, null, false, 1);
932
933 opportunisticInfoList.add(opportunisticSubInfo1);
934 opportunisticInfoList.add(opportunisticSubInfo2);
935 activeSubscriptionInfoList.add(opportunisticSubInfo1);
936 activeSubscriptionInfoList.add(opportunisticSubInfo2);
937
938 doReturn(opportunisticInfoList).when(mSubscriptionManager)
939 .getOpportunisticSubscriptions();
940 doReturn(activeSubscriptionInfoList).when(mSubscriptionManager)
941 .getCompleteActiveSubscriptionInfoList();
942
943 mONSProfileSelector = new MyONSProfileSelector(mContext, null);
944 int portIdx = mONSProfileSelector.getAvailableESIMPortIndex();
945
946 /* one of the opportunistic eSIM port should be selected */
947 assertTrue(portIdx == 0 || portIdx == 1);
948 }
949
950 @Test
951 public void testAvailablePortWhenOpportunisticEsimIsActiveAndInactiveSubscriptions() {
952 /**
953 * 1 - Primary active subscription and
954 * 1 - Active opportunistic subscription and
955 * 2 - Inactive opportunistic subscriptions
956 */
957
958 List<SubscriptionInfo> activeSubscriptionInfoList = new ArrayList<SubscriptionInfo>();
959 List<SubscriptionInfo> opportunisticInfoList = new ArrayList<SubscriptionInfo>();
960
961 SubscriptionInfo opportunisticSubInfo1 = new SubscriptionInfo(5, "", 1, "TMO", "TMO", 1, 1,
962 "123", 1, null, "310", "210", "", true, null, "1", 1, true, null, false, 1839, 1,
963 1, null, null, false, 1);
964 SubscriptionInfo primarySubInfo = new SubscriptionInfo(6, "", 1, "TMO", "TMO", 1, 1,
965 "456", 1, null, "310", "211", "", true, null, "1", 1, false, null, false, 1839, 1,
966 1, null, null, false, 0);
967
968 SubscriptionInfo opportunisticSubInfo2 = new SubscriptionInfo(7, "", 1, "TMO", "TMO", 1, 1,
969 "789", 1, null, "310", "210", "", true, null, "1", 1, true, null, false, 1839, 1,
970 1, null, null, false, TelephonyManager.INVALID_PORT_INDEX);
971
972 SubscriptionInfo oppSubInfo3 = new SubscriptionInfo(8, "", 1, "TMO", "TMO", 1, 1,
973 "012", 1, null, "310", "210", "", true, null, "1", 1, true, null, false, 1839, 1,
974 1, null, null, false, TelephonyManager.INVALID_PORT_INDEX);
975
976 opportunisticInfoList.add(opportunisticSubInfo1);
977 opportunisticInfoList.add(opportunisticSubInfo2);
978 opportunisticInfoList.add(oppSubInfo3);
979 activeSubscriptionInfoList.add(opportunisticSubInfo1);
980 activeSubscriptionInfoList.add(primarySubInfo);
981
982 doReturn(opportunisticInfoList).when(mSubscriptionManager)
983 .getOpportunisticSubscriptions();
984 doReturn(activeSubscriptionInfoList).when(mSubscriptionManager)
985 .getCompleteActiveSubscriptionInfoList();
986
987 mONSProfileSelector = new MyONSProfileSelector(mContext, null);
988 int portIdx = mONSProfileSelector.getAvailableESIMPortIndex();
989 assertEquals(1, portIdx);
990 }
991
992 @Test
993 public void testAvailablePortWhenOnlyInactiveSubscriptions() {
994 /**
995 * 1 - Primary inactive subscription and
996 * 2 - Inactive opportunistic subscriptions
997 */
998
999 List<SubscriptionInfo> activeSubscriptionInfoList = new ArrayList<SubscriptionInfo>();
1000 List<SubscriptionInfo> opportunisticInfoList = new ArrayList<SubscriptionInfo>();
1001
1002 SubscriptionInfo oppSubInfo1 = new SubscriptionInfo(5, "", 1, "TMO", "TMO", 1, 1,
1003 "123", 1, null, "310", "210", "", true, null, "1", 1, true, null, false, 1839, 1,
1004 1, null, null, false, TelephonyManager.INVALID_PORT_INDEX);
1005
1006 // Not used in activeSubscriptionInfoList or opportunisticInfoList
1007 /*SubscriptionInfo primarySubInfo = new SubscriptionInfo(6, "", 1, "TMO", "TMO", 1, 1,
1008 "456", 1, null, "310", "211", "", true, null, "1", 1, false, null, false, 1839, 1,
1009 1, null, null, false, 2);*/
1010
1011 SubscriptionInfo oppSubInfo2 = new SubscriptionInfo(7, "", 1, "TMO", "TMO", 1, 1,
1012 "789", 1, null, "310", "210", "", true, null, "1", 1, true, null, false, 1839, 1,
1013 1, null, null, false, TelephonyManager.INVALID_PORT_INDEX);
1014
1015 opportunisticInfoList.add(oppSubInfo1);
1016 opportunisticInfoList.add(oppSubInfo2);
1017
1018 doReturn(opportunisticInfoList).when(mSubscriptionManager)
1019 .getOpportunisticSubscriptions();
1020 doReturn(activeSubscriptionInfoList).when(mSubscriptionManager)
1021 .getCompleteActiveSubscriptionInfoList();
1022
1023 UiccPortInfo uiccPortInfo1 = new UiccPortInfo("", 0, 0, false);
1024 UiccPortInfo uiccPortInfo2 = new UiccPortInfo("", 1, 0, false);
1025 ArrayList<UiccPortInfo> uiccPortInfoList = new ArrayList<>();
1026 uiccPortInfoList.add(uiccPortInfo1);
1027 uiccPortInfoList.add(uiccPortInfo2);
1028
1029 UiccCardInfo uiccCardInfo = new UiccCardInfo(true, 1, "", 0, false, true, uiccPortInfoList);
1030 ArrayList<UiccCardInfo> uiccCardInfoList = new ArrayList<>();
1031 uiccCardInfoList.add(uiccCardInfo);
1032
1033 doReturn(uiccCardInfoList).when(mMockTelephonyManager).getUiccCardsInfo();
1034 doReturn(mMockEuiccManager).when(mMockEuiccManager).createForCardId(1);
1035 doReturn(true).when(mMockEuiccManager).isSimPortAvailable(0);
1036 doReturn(true).when(mMockEuiccManager).isSimPortAvailable(1);
1037
1038 mONSProfileSelector = new MyONSProfileSelector(mContext, null);
1039 mONSProfileSelector.mTelephonyManager = mMockTelephonyManager;
1040 mONSProfileSelector.mEuiccManager = mMockEuiccManager;
1041
1042 int portIdx = mONSProfileSelector.getAvailableESIMPortIndex();
1043 assertTrue(portIdx == 0 || portIdx == 1);
Avinash Malipatil79572952022-05-26 08:48:31 +00001044 }
Avinash Malipatil284c5ef2022-08-17 07:29:56 +00001045
1046 @Test
1047 public void testGetMncMccFromCellInfoNr() {
1048 mONSProfileSelector = new MyONSProfileSelector(mContext, null);
1049
1050 CellIdentityNr cellIdentityNr = new CellIdentityNr(0, 0, 0, new int[]{0}, "111", "222", 0,
1051 "", "", Collections.emptyList());
1052
1053 CellInfoNr cellinfoNr = new CellInfoNr(0, true, 0, cellIdentityNr, null);
1054
1055 assertEquals(mONSProfileSelector.getMcc(cellinfoNr), "111");
1056 assertEquals(mONSProfileSelector.getMnc(cellinfoNr), "222");
1057 }
Sooraj Sasindran33345152018-10-01 11:22:56 -07001058}