1 /*
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  */
16 package com.android.ons;
17 
18 import static org.mockito.Mockito.*;
19 
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.os.IBinder;
24 import android.os.Looper;
25 import android.os.ServiceManager;
26 import android.telephony.AvailableNetworkInfo;
27 import android.telephony.CellIdentityLte;
28 import android.telephony.CellIdentityNr;
29 import android.telephony.CellInfo;
30 import android.telephony.CellInfoLte;
31 import android.telephony.CellInfoNr;
32 import android.telephony.SubscriptionInfo;
33 import android.telephony.SubscriptionManager;
34 import android.telephony.TelephonyManager;
35 import android.telephony.UiccCardInfo;
36 import android.telephony.UiccPortInfo;
37 import android.telephony.euicc.EuiccManager;
38 import android.util.Log;
39 
40 import com.android.internal.telephony.ISub;
41 import com.android.internal.telephony.IUpdateAvailableNetworksCallback;
42 
43 import org.junit.After;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.mockito.Mock;
47 import org.mockito.MockitoAnnotations;
48 
49 import java.lang.reflect.Field;
50 import java.util.ArrayList;
51 import java.util.Collections;
52 import java.util.List;
53 import java.util.Map;
54 
55 public class ONSProfileSelectorTest extends ONSBaseTest {
56 
57     private MyONSProfileSelector mONSProfileSelector;
58     private boolean testFailed;
59     private boolean mCallbackInvoked;
60     private int mDataSubId;
61     private int mResult;
62     @Mock
63     EuiccManager mMockEuiccManager;
64     @Mock
65     ONSNetworkScanCtlr mONSNetworkScanCtlr;
66     @Mock
67     TelephonyManager mSubscriptionBoundTelephonyManager;
68     @Mock
69     ISub mISubMock;
70     @Mock
71     IBinder mISubBinderMock;
72     @Mock
73     SubscriptionInfo mSubInfo;
74     private Looper mLooper;
75     private static final String TAG = "ONSProfileSelectorTest";
76 
77     MyONSProfileSelector.ONSProfileSelectionCallback mONSProfileSelectionCallback =
78         new MyONSProfileSelector.ONSProfileSelectionCallback() {
79             public void onProfileSelectionDone() {
80                 mCallbackInvoked = true;
81                 setReady(true);
82             }
83         };
84 
85     public class MyONSProfileSelector extends ONSProfileSelector {
86 
87         public SubscriptionManager.OnOpportunisticSubscriptionsChangedListener mProfileChngLstnrCpy;
88         public BroadcastReceiver mProfileSelectorBroadcastReceiverCpy;
89         public ONSNetworkScanCtlr.NetworkAvailableCallBack mNetworkAvailableCallBackCpy;
90 
MyONSProfileSelector(Context c, MyONSProfileSelector.ONSProfileSelectionCallback aNSProfileSelectionCallback)91         public MyONSProfileSelector(Context c,
92             MyONSProfileSelector.ONSProfileSelectionCallback aNSProfileSelectionCallback) {
93             super(c, aNSProfileSelectionCallback);
94         }
95 
triggerProfileUpdate()96         public void triggerProfileUpdate() {
97             mHandler.sendEmptyMessage(1);
98         }
99 
updateOppSubs()100         public void updateOppSubs() {
101             updateOpportunisticSubscriptions();
102         }
103 
getCurrentPreferredData()104         public int getCurrentPreferredData() {
105             return mCurrentDataSubId;
106         }
107 
setCurrentPreferredData(int subId)108         public void setCurrentPreferredData(int subId) {
109             mCurrentDataSubId = subId;
110         }
111 
init(Context c, MyONSProfileSelector.ONSProfileSelectionCallback aNSProfileSelectionCallback)112         protected void init(Context c,
113             MyONSProfileSelector.ONSProfileSelectionCallback aNSProfileSelectionCallback) {
114             super.init(c, aNSProfileSelectionCallback);
115             this.mSubscriptionManager = ONSProfileSelectorTest.this.mSubscriptionManager;
116             this.mSubscriptionBoundTelephonyManager =
117                 ONSProfileSelectorTest.this.mSubscriptionBoundTelephonyManager;
118             mProfileChngLstnrCpy = mProfileChangeListener;
119             mProfileSelectorBroadcastReceiverCpy = null;
120             mNetworkAvailableCallBackCpy = mNetworkAvailableCallBack;
121             mNetworkScanCtlr = mONSNetworkScanCtlr;
122         }
123     }
124 
addISubService()125     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 
removeISubService()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 
138     @Before
setUp()139     public void setUp() throws Exception {
140         super.setUp("ONSTest");
141         mLooper = null;
142         MockitoAnnotations.initMocks(this);
143         addISubService();
144     }
145 
146     @After
tearDown()147     public void tearDown() throws Exception {
148         removeISubService();
149         super.tearDown();
150         if (mLooper != null) {
151             mLooper.quit();
152             mLooper.getThread().join();
153         }
154     }
155 
156 
157     @Test
testStartProfileSelectionWithNoOpportunisticSub()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);
163         results2.add((CellInfo) cellInfoLte);
164         ArrayList<String> mccMncs = new ArrayList<>();
165         mccMncs.add("310210");
166         AvailableNetworkInfo availableNetworkInfo = new AvailableNetworkInfo(1, 1, mccMncs,
167             new ArrayList<Integer>());
168         ArrayList<AvailableNetworkInfo> availableNetworkInfos = new ArrayList<AvailableNetworkInfo>();
169         availableNetworkInfos.add(availableNetworkInfo);
170 
171         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 
183         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;
192         mReady = false;
193         mCallbackInvoked = false;
194         new Thread(new Runnable() {
195             @Override
196             public void run() {
197                 Looper.prepare();
198                 doReturn(true).when(mONSNetworkScanCtlr).startFastNetworkScan(anyObject());
199                 doReturn(new ArrayList<>()).when(mSubscriptionManager)
200                     .getOpportunisticSubscriptions();
201                 mONSProfileSelector = new MyONSProfileSelector(mContext,
202                     mONSProfileSelectionCallback);
203                 mONSProfileSelector.mTelephonyManager = mMockTelephonyManager;
204                 mONSProfileSelector.mEuiccManager = mMockEuiccManager;
205                 mONSProfileSelector.updateOppSubs();
206                 mONSProfileSelector.startProfileSelection(availableNetworkInfos, mCallback);
207                 mLooper = Looper.myLooper();
208                 setReady(true);
209                 Looper.loop();
210             }
211         }).start();
212 
213         // 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.
219         waitUntilReady(100);
220         assertEquals(
221                 TelephonyManager.UPDATE_AVAILABLE_NETWORKS_NO_OPPORTUNISTIC_SUB_AVAILABLE, mResult);
222         assertFalse(mCallbackInvoked);
223     }
224 
225     @Test
testStartProfileSelectionSuccess()226     public void testStartProfileSelectionSuccess() {
227         int subId = 5;
228         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);
239 
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);
244         results2.add((CellInfo) cellInfoLte);
245         ArrayList<String> mccMncs = new ArrayList<>();
246         mccMncs.add("310210");
247         AvailableNetworkInfo availableNetworkInfo = new AvailableNetworkInfo(subId, 1, mccMncs,
248             new ArrayList<Integer>());
249         ArrayList<AvailableNetworkInfo> availableNetworkInfos = new ArrayList<AvailableNetworkInfo>();
250         availableNetworkInfos.add(availableNetworkInfo);
251 
252         IUpdateAvailableNetworksCallback mCallback = new IUpdateAvailableNetworksCallback.Stub() {
253             @Override
254             public void onComplete(int result) {
255                 mResult = result;
256             }
257         };
258 
259         mResult = -1;
260         mReady = false;
261         new Thread(new Runnable() {
262             @Override
263             public void run() {
264                 Looper.prepare();
265                 doReturn(subscriptionInfo1).when(mSubscriptionManager)
266                         .getActiveSubscriptionInfo(subId);
267                 doReturn(oppSubscriptionInfoList).when(mSubscriptionManager)
268                     .getOpportunisticSubscriptions();
269                 doReturn(activeSubscriptionInfoList).when(mSubscriptionManager)
270                         .getActiveSubscriptionInfoList();
271                 doReturn(activeSubscriptionInfoList).when(mSubscriptionManager)
272                         .getCompleteActiveSubscriptionInfoList();
273                 doReturn(true).when(mSubscriptionManager).isActiveSubId(subId);
274                 doReturn(true).when(mSubscriptionBoundTelephonyManager).enableModemForSlot(
275                     anyInt(), anyBoolean());
276                 mONSProfileSelector = new MyONSProfileSelector(mContext,
277                     new MyONSProfileSelector.ONSProfileSelectionCallback() {
278                         public void onProfileSelectionDone() {
279                             setReady(true);
280                         }
281                     });
282                 mONSProfileSelector.updateOppSubs();
283                 mONSProfileSelector.startProfileSelection(availableNetworkInfos, mCallback);
284                 mLooper = Looper.myLooper();
285                 setReady(true);
286                 Looper.loop();
287             }
288         }).start();
289 
290         // 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.
297         assertFalse(mReady);
298         waitForMs(500);
299         mONSProfileSelector.mNetworkAvailableCallBackCpy.onNetworkAvailability(results2);
300         Intent callbackIntent = new Intent(MyONSProfileSelector.ACTION_SUB_SWITCH);
301         callbackIntent.putExtra("sequenceId", 1);
302         callbackIntent.putExtra("subId", subId);
303         waitUntilReady();
304         assertEquals(TelephonyManager.UPDATE_AVAILABLE_NETWORKS_SUCCESS, mResult);
305         assertTrue(mReady);
306     }
307 
308     @Test
testStartProfileSelectionWithDifferentPrioritySubInfo()309     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,
315                 "123", 1, null, "310", "210", "", true, null, "1", 1, true, null, false, 1, 1, 1,
316                 null, null, false, 0);
317         subscriptionInfoList.add(subscriptionInfo);
318         SubscriptionInfo subscriptionInfo_2 = new SubscriptionInfo(8, "", 1, "Vzw", "Vzw", 1, 1,
319                 "456", 1, null, "311", "480", "", true, null, "1", 1, true, null, false, 1, 1, 1,
320                 null, null, false, 1);
321         subscriptionInfoList.add(subscriptionInfo_2);
322         doReturn(subscriptionInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(5);
323         doReturn(subscriptionInfo_2).when(mSubscriptionManager).getActiveSubscriptionInfo(8);
324         doReturn(subscriptionInfoList).when(mSubscriptionManager)
325                 .getCompleteActiveSubscriptionInfoList();
326         doReturn(subscriptionInfoList).when(mSubscriptionManager).getOpportunisticSubscriptions();
327 
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();
363                 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
testStartProfileSelectionWithActivePrimarySimOnESim()389     public void testStartProfileSelectionWithActivePrimarySimOnESim() {
390         List<SubscriptionInfo> opportunisticSubscriptionInfoList = new ArrayList<SubscriptionInfo>();
391         List<SubscriptionInfo> activeSubscriptionInfoList = new ArrayList<SubscriptionInfo>();
392         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);
395         SubscriptionInfo subscriptionInfo2 = new SubscriptionInfo(6, "", 1, "TMO", "TMO", 1, 1,
396                 "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);
400         activeSubscriptionInfoList.add(subscriptionInfo2);
401         doReturn(subscriptionInfo1).when(mSubscriptionManager).getActiveSubscriptionInfo(5);
402         doReturn(subscriptionInfo2).when(mSubscriptionManager).getActiveSubscriptionInfo(6);
403 
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 
411         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 
424         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();
437                 doReturn(opportunisticSubscriptionInfoList).when(mSubscriptionManager)
438                     .getOpportunisticSubscriptions();
439                 doReturn(false).when(mSubscriptionManager).isActiveSubId(anyInt());
440                 doReturn(activeSubscriptionInfoList).when(mSubscriptionManager)
441                     .getActiveSubscriptionInfoList(anyBoolean());
442                 doReturn(activeSubscriptionInfoList).when(mSubscriptionManager)
443                         .getActiveSubscriptionInfoList();
444                 doReturn(true).when(mSubscriptionBoundTelephonyManager).enableModemForSlot(
445                         anyInt(), anyBoolean());
446                 mONSProfileSelector = new MyONSProfileSelector(mContext,
447                     new MyONSProfileSelector.ONSProfileSelectionCallback() {
448                         public void onProfileSelectionDone() {
449                             setReady(true);
450                         }
451                     });
452                 mONSProfileSelector.mTelephonyManager = mMockTelephonyManager;
453                 mONSProfileSelector.mEuiccManager = mMockEuiccManager;
454                 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();
475         assertEquals(TelephonyManager.UPDATE_AVAILABLE_NETWORKS_NO_OPPORTUNISTIC_SUB_AVAILABLE,
476                 mResult);
477     }
478 
waitForMs(long ms)479     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 
487     @Test
testselectProfileForDataWithNoOpportunsticSub()488     public void testselectProfileForDataWithNoOpportunsticSub() {
489         mReady = false;
490         doReturn(new ArrayList<>()).when(mSubscriptionManager).getOpportunisticSubscriptions();
491         new Thread(new Runnable() {
492             @Override
493             public void run() {
494                 Looper.prepare();
495                 mONSProfileSelector = new MyONSProfileSelector(mContext,
496                     new MyONSProfileSelector.ONSProfileSelectionCallback() {
497                         public void onProfileSelectionDone() {
498                         }
499                     });
500                 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.
511         mONSProfileSelector.selectProfileForData(1, false, null);
512     }
513 
514     @Test
testselectProfileForDataWithInActiveSub()515     public void testselectProfileForDataWithInActiveSub() {
516         List<SubscriptionInfo> subscriptionInfoList = new ArrayList<SubscriptionInfo>();
517         SubscriptionInfo subscriptionInfo = new SubscriptionInfo(5, "", 1, "TMO", "TMO", 1, 1,
518             "123", 1, null, "310", "210", "", false, null, "1");
519         subscriptionInfoList.add(subscriptionInfo);
520         doReturn(subscriptionInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(5);
521         mReady = false;
522         doReturn(new ArrayList<>()).when(mSubscriptionManager).getOpportunisticSubscriptions();
523         new Thread(new Runnable() {
524             @Override
525             public void run() {
526                 Looper.prepare();
527                 mONSProfileSelector = new MyONSProfileSelector(mContext,
528                     new MyONSProfileSelector.ONSProfileSelectionCallback() {
529                         public void onProfileSelectionDone() {
530                         }
531                     });
532                 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.
542         mONSProfileSelector.selectProfileForData(5, false, null);
543     }
544 
545     @Test
testselectProfileForDataWithInvalidSubId()546     public void testselectProfileForDataWithInvalidSubId() {
547         List<SubscriptionInfo> subscriptionInfoList = new ArrayList<SubscriptionInfo>();
548         SubscriptionInfo subscriptionInfo = new SubscriptionInfo(5, "", 1, "TMO", "TMO", 1, 1,
549             "123", 1, null, "310", "210", "", false, null, "1");
550         subscriptionInfoList.add(subscriptionInfo);
551         doReturn(subscriptionInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(5);
552         mReady = false;
553         doReturn(subscriptionInfoList).when(mSubscriptionManager).getOpportunisticSubscriptions();
554         doNothing().when(mSubscriptionManager).setPreferredDataSubscriptionId(
555             anyInt(), anyBoolean(), any(), any());
556         new Thread(new Runnable() {
557             @Override
558             public void run() {
559                 Looper.prepare();
560                 mONSProfileSelector = new MyONSProfileSelector(mContext,
561                     new MyONSProfileSelector.ONSProfileSelectionCallback() {
562                         public void onProfileSelectionDone() {
563                         }
564                     });
565                 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.
576         mONSProfileSelector.selectProfileForData(
577             SubscriptionManager.INVALID_SUBSCRIPTION_ID, false, null);
578     }
579 
580     @Test
testselectProfileForDataWithValidSub()581     public void testselectProfileForDataWithValidSub() {
582         List<SubscriptionInfo> subscriptionInfoList = new ArrayList<SubscriptionInfo>();
583         SubscriptionInfo subscriptionInfo = new SubscriptionInfo(5, "", 1, "TMO", "TMO", 1, 1,
584             "123", 1, null, "310", "210", "", false, null, "1");
585         subscriptionInfoList.add(subscriptionInfo);
586         doReturn(subscriptionInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(5);
587         mReady = false;
588         doReturn(subscriptionInfoList).when(mSubscriptionManager)
589             .getActiveSubscriptionInfoList();
590         doNothing().when(mSubscriptionManager).setPreferredDataSubscriptionId(
591             anyInt(), anyBoolean(), any(), any());
592         new Thread(new Runnable() {
593             @Override
594             public void run() {
595                 Looper.prepare();
596                 doReturn(subscriptionInfoList).when(mSubscriptionManager)
597                     .getOpportunisticSubscriptions();
598                 mONSProfileSelector = new MyONSProfileSelector(mContext,
599                     new MyONSProfileSelector.ONSProfileSelectionCallback() {
600                         public void onProfileSelectionDone() {
601                         }
602                     });
603                 mONSProfileSelector.updateOppSubs();
604                 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.
615         mONSProfileSelector.selectProfileForData(5, false, null);
616     }
617 
618     @Test
testStartProfileSelectionSuccessWithSameArgumentsAgain()619     public void testStartProfileSelectionSuccessWithSameArgumentsAgain() {
620         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);
625         SubscriptionInfo subscriptionInfo2 = new SubscriptionInfo(6, "", 1, "TMO", "TMO", 1, 1,
626                 "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);
631         doReturn(subscriptionInfo2).when(mSubscriptionManager).getActiveSubscriptionInfo(6);
632 
633         activeSubscriptionInfoList.add(subscriptionInfo1);
634         activeSubscriptionInfoList.add(subscriptionInfo2);
635         doReturn(activeSubscriptionInfoList).when(mSubscriptionManager)
636                 .getCompleteActiveSubscriptionInfoList();
637 
638         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");
645         AvailableNetworkInfo availableNetworkInfo = new AvailableNetworkInfo(5, 1, mccMncs,
646             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();
669                 doReturn(oppSubscriptionInfoList).when(mSubscriptionManager)
670                     .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();
706                 doReturn(oppSubscriptionInfoList).when(mSubscriptionManager)
707                     .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     }
733 
734     @Test
testStopProfileSelectionWithPreferredDataOnSame()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);
740         doReturn(subscriptionInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(5);
741 
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);
780         assertEquals(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, mONSProfileSelector.getCurrentPreferredData());
781     }
782 
783     @Test
testStopProfileSelectionWithPreferredDataOnDifferent()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);
789         doReturn(subscriptionInfo).when(mSubscriptionManager).getActiveSubscriptionInfo(5);
790 
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);
829         assertEquals(5, mONSProfileSelector.getCurrentPreferredData());
830     }
831 
832     @Test
testAvailablePortWhenTwoPrimarySIMsAreActive()833     public void testAvailablePortWhenTwoPrimarySIMsAreActive() {
834         /**
835          * 2 - Primary active subscriptions and
836          * 1 - Inactive opportunistic subscription
837          */
838 
839         List<SubscriptionInfo> activeSubscriptionInfoList = new ArrayList<SubscriptionInfo>();
840         List<SubscriptionInfo> opportunisticInfoList = new ArrayList<SubscriptionInfo>();
841 
842         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);
845 
846         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);
856 
857         doReturn(opportunisticInfoList).when(mSubscriptionManager).getOpportunisticSubscriptions();
858         doReturn(activeSubscriptionInfoList).when(mSubscriptionManager)
859                 .getCompleteActiveSubscriptionInfoList();
860 
861         UiccPortInfo uiccPortInfo1 = new UiccPortInfo("", 0, 0, true);
862         UiccPortInfo uiccPortInfo2 = new UiccPortInfo("", 1, 0, true);
863         ArrayList<UiccPortInfo> uiccPortInfoList = new ArrayList<>();
864         uiccPortInfoList.add(uiccPortInfo1);
865         uiccPortInfoList.add(uiccPortInfo2);
866 
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);
873         doReturn(false).when(mMockEuiccManager).isSimPortAvailable(0);
874         doReturn(false).when(mMockEuiccManager).isSimPortAvailable(1);
875 
876         mONSProfileSelector = new MyONSProfileSelector(mContext, null);
877         mONSProfileSelector.mTelephonyManager = mMockTelephonyManager;
878         mONSProfileSelector.mEuiccManager = mMockEuiccManager;
879 
880         int portIdx = mONSProfileSelector.getAvailableESIMPortIndex();
881         assertEquals(TelephonyManager.INVALID_PORT_INDEX, portIdx);
882     }
883 
884     @Test
testAvailablePortWhenOpportunisticEsimIsActive()885     public void testAvailablePortWhenOpportunisticEsimIsActive() {
886         /**
887          * 1 - Primary active subscriptions and
888          * 1 - Active opportunistic subscription
889          */
890 
891         List<SubscriptionInfo> activeSubscriptionInfoList = new ArrayList<SubscriptionInfo>();
892         List<SubscriptionInfo> opportunisticInfoList = new ArrayList<SubscriptionInfo>();
893 
894         SubscriptionInfo oppSubInfo = new SubscriptionInfo(5, "", 1, "TMO", "TMO", 1, 1,
895                 "123", 1, null, "310", "210", "", true, null, "1", 1, true, null, false, 1839, 1,
896                 1, null, null, false, 0);
897 
898         SubscriptionInfo primarySubInfo = new SubscriptionInfo(6, "", 1, "TMO", "TMO", 1, 1,
899                 "456", 1, null, "310", "211", "", true, null, "1", 1, false, null, false, 1839, 1,
900                 1, null, null, false, 1);
901 
902         opportunisticInfoList.add(oppSubInfo);
903         activeSubscriptionInfoList.add(oppSubInfo);
904         activeSubscriptionInfoList.add(primarySubInfo);
905 
906         doReturn(opportunisticInfoList).when(mSubscriptionManager)
907                 .getOpportunisticSubscriptions();
908         doReturn(activeSubscriptionInfoList).when(mSubscriptionManager)
909                 .getCompleteActiveSubscriptionInfoList();
910 
911         mONSProfileSelector = new MyONSProfileSelector(mContext, null);
912         int portIdx = mONSProfileSelector.getAvailableESIMPortIndex();
913         assertEquals(0, portIdx);
914     }
915 
916     @Test
testAvailablePortWhenTwoOpportunisticEsimsAreActive()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
testAvailablePortWhenOpportunisticEsimIsActiveAndInactiveSubscriptions()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
testAvailablePortWhenOnlyInactiveSubscriptions()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);
1044     }
1045 
1046     @Test
testGetMncMccFromCellInfoNr()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     }
1058 }
1059