Snap for 5726885 from 30f0c5d6ce42957fe417b5d737a7f7f1f7d01895 to qt-qpr1-release

Change-Id: I26b5fe53953d2f61cac1e527c5504308cc8d348c
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 35de8c1..92fd67d 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -26,15 +26,17 @@
         android:process="com.android.phone"
         android:persistent="true">
 
-        <receiver android:name="ONSAutoBoot">
+        <receiver android:name="ONSAutoBoot"
+            android:directBootAware="true">
             <intent-filter>
-                <action android:name="android.intent.action.BOOT_COMPLETED" />
+                <action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
             </intent-filter>
         </receiver>
 
         <service android:name=".OpportunisticNetworkService"
                  android:enabled="true"
-                 android:exported="true"/>
+                 android:exported="true"
+                 android:directBootAware="true"/>
 
     </application>
 </manifest>
diff --git a/src/com/android/ons/ONSAutoBoot.java b/src/com/android/ons/ONSAutoBoot.java
index 7d8ef75..77c5c8f 100644
--- a/src/com/android/ons/ONSAutoBoot.java
+++ b/src/com/android/ons/ONSAutoBoot.java
@@ -27,7 +27,8 @@
 
     @Override
     public void onReceive(Context context, Intent intent) {
-        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
+        Log.d(TAG, "Received " + intent.getAction());
+        if (Intent.ACTION_LOCKED_BOOT_COMPLETED.equals(intent.getAction())) {
             ComponentName comp = new ComponentName(context.getPackageName(),
                     OpportunisticNetworkService.class.getName());
             ComponentName service = context.startService(new Intent().setComponent(comp));
diff --git a/src/com/android/ons/ONSProfileSelector.java b/src/com/android/ons/ONSProfileSelector.java
index 1922514..6a156d8 100644
--- a/src/com/android/ons/ONSProfileSelector.java
+++ b/src/com/android/ons/ONSProfileSelector.java
@@ -117,7 +117,9 @@
                     if (subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
                         sendUpdateNetworksCallbackHelper(mNetworkScanCallback,
                                 TelephonyManager.UPDATE_AVAILABLE_NETWORKS_INVALID_ARGUMENTS);
-                        mNetworkScanCallback = null;
+                        synchronized (mLock) {
+                            mNetworkScanCallback = null;
+                        }
                         return;
                     }
 
@@ -129,14 +131,16 @@
                 @Override
                 public void onError(int error) {
                     log("Network scan failed with error " + error);
-                    if (mIsEnabled && mAvailableNetworkInfos != null
+                    synchronized (mLock) {
+                        if (mIsEnabled && mAvailableNetworkInfos != null
                             && mAvailableNetworkInfos.size() > 0) {
-                        handleNetworkScanResult(mAvailableNetworkInfos.get(0).getSubId());
-                    } else {
-                        if (mNetworkScanCallback != null) {
-                            sendUpdateNetworksCallbackHelper(mNetworkScanCallback,
+                            handleNetworkScanResult(mAvailableNetworkInfos.get(0).getSubId());
+                        } else {
+                            if (mNetworkScanCallback != null) {
+                                sendUpdateNetworksCallbackHelper(mNetworkScanCallback,
                                     TelephonyManager.UPDATE_AVAILABLE_NETWORKS_INVALID_ARGUMENTS);
-                            mNetworkScanCallback = null;
+                                mNetworkScanCallback = null;
+                            }
                         }
                     }
                 }
@@ -152,7 +156,10 @@
                                 TelephonyManager.UPDATE_AVAILABLE_NETWORKS_ABORTED);
                         }
                         mProfileSelectionCallback.onProfileSelectionDone();
-                        mNetworkScanCallback = null;
+                        synchronized (mLock) {
+                            mNetworkScanCallback = null;
+                            mAvailableNetworkInfos = null;
+                        }
                     } else {
                         logDebug("switch to sub:" + subId);
                         switchToSubscription(subId);
@@ -247,13 +254,17 @@
 
     private int getSubIdUsingAvailableNetworks(String mcc, String mnc, int priorityLevel) {
         String mccMnc = mcc + mnc;
-        for (AvailableNetworkInfo availableNetworkInfo : mAvailableNetworkInfos) {
-            if (availableNetworkInfo.getPriority() != priorityLevel) {
-                continue;
-            }
-            for (String availableMccMnc : availableNetworkInfo.getMccMncs()) {
-                if (TextUtils.equals(availableMccMnc, mccMnc)) {
-                    return availableNetworkInfo.getSubId();
+        synchronized (mLock) {
+            if (mAvailableNetworkInfos != null) {
+                for (AvailableNetworkInfo availableNetworkInfo : mAvailableNetworkInfos) {
+                    if (availableNetworkInfo.getPriority() != priorityLevel) {
+                        continue;
+                    }
+                    for (String availableMccMnc : availableNetworkInfo.getMccMncs()) {
+                        if (TextUtils.equals(availableMccMnc, mccMnc)) {
+                            return availableNetworkInfo.getSubId();
+                        }
+                    }
                 }
             }
         }
@@ -315,8 +326,7 @@
         callbackIntent.putExtra("subId", subId);
         mSubId = subId;
         PendingIntent replyIntent = PendingIntent.getService(mContext,
-                1, callbackIntent,
-                Intent.FILL_IN_ACTION);
+                1, callbackIntent, PendingIntent.FLAG_ONE_SHOT);
         mSubscriptionManager.switchToSubscription(subId, replyIntent);
     }
 
@@ -346,6 +356,8 @@
                 TelephonyManager.UPDATE_AVAILABLE_NETWORKS_ABORTED);
         }
         mProfileSelectionCallback.onProfileSelectionDone();
+        mNetworkScanCallback = null;
+        mAvailableNetworkInfos = null;
     }
 
     private void updateToken() {
@@ -453,6 +465,13 @@
         }
 
         if (isSame(availableNetworks, mAvailableNetworkInfos)) {
+            logDebug("received duplicate requests");
+            /* If we receive same request more than once, send abort response for earlier one
+               and send actual response for the latest callback.
+            */
+            sendUpdateNetworksCallbackHelper(mNetworkScanCallback,
+                TelephonyManager.UPDATE_AVAILABLE_NETWORKS_ABORTED);
+            mNetworkScanCallback = callbackStub;
             return;
         }
 
@@ -484,6 +503,7 @@
                             TelephonyManager.UPDATE_AVAILABLE_NETWORKS_ABORTED);
                     }
                     mProfileSelectionCallback.onProfileSelectionDone();
+                    mAvailableNetworkInfos = null;
                 }
             } else {
                 mNetworkScanCallback = callbackStub;
@@ -604,13 +624,14 @@
     }
 
     private void stopProfileScanningPrecedure() {
-        if (mNetworkScanCallback != null) {
-            sendUpdateNetworksCallbackHelper(mNetworkScanCallback,
-                    TelephonyManager.UPDATE_AVAILABLE_NETWORKS_ABORTED);
-            mNetworkScanCallback = null;
-        }
-        mNetworkScanCtlr.stopNetworkScan();
         synchronized (mLock) {
+            if (mNetworkScanCallback != null) {
+                sendUpdateNetworksCallbackHelper(mNetworkScanCallback,
+                        TelephonyManager.UPDATE_AVAILABLE_NETWORKS_ABORTED);
+                mNetworkScanCallback = null;
+            }
+            mNetworkScanCtlr.stopNetworkScan();
+
             mAvailableNetworkInfos = null;
             mIsEnabled = false;
         }
@@ -697,7 +718,6 @@
                 return;
             }
             mCurrentDataSubId = subId;
-            sendSetOpptCallbackHelper(callbackStub, TelephonyManager.SET_OPPORTUNISTIC_SUB_SUCCESS);
         } else {
             log("Inactive sub passed for preferred data " + subId);
             sendSetOpptCallbackHelper(callbackStub,
diff --git a/src/com/android/ons/OpportunisticNetworkService.java b/src/com/android/ons/OpportunisticNetworkService.java
index da35b65..6db5fa8 100644
--- a/src/com/android/ons/OpportunisticNetworkService.java
+++ b/src/com/android/ons/OpportunisticNetworkService.java
@@ -363,7 +363,8 @@
         mContext = context;
         mTelephonyManager = TelephonyManager.from(mContext);
         mProfileSelector = new ONSProfileSelector(mContext, mProfileSelectionCallback);
-        mSharedPref = mContext.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
+        mSharedPref = mContext.createDeviceProtectedStorageContext().getSharedPreferences(
+                PREF_NAME, Context.MODE_PRIVATE);
         mSubscriptionManager = (SubscriptionManager) mContext.getSystemService(
                 Context.TELEPHONY_SUBSCRIPTION_SERVICE);
         mONSConfigInputHashMap = new HashMap<String, ONSConfigInput>();
diff --git a/tests/src/com/android/ons/ONSProfileSelectorTest.java b/tests/src/com/android/ons/ONSProfileSelectorTest.java
index e43f78d..a3b5a72 100644
--- a/tests/src/com/android/ons/ONSProfileSelectorTest.java
+++ b/tests/src/com/android/ons/ONSProfileSelectorTest.java
@@ -42,6 +42,7 @@
 import java.util.List;
 
 public class ONSProfileSelectorTest extends ONSBaseTest {
+
     private MyONSProfileSelector mONSProfileSelector;
     private boolean testFailed;
     private boolean mCallbackInvoked;
@@ -55,20 +56,21 @@
     private static final String TAG = "ONSProfileSelectorTest";
 
     MyONSProfileSelector.ONSProfileSelectionCallback mONSProfileSelectionCallback =
-            new MyONSProfileSelector.ONSProfileSelectionCallback() {
-        public void onProfileSelectionDone() {
-            mCallbackInvoked = true;
-            setReady(true);
-        }
-    };
+        new MyONSProfileSelector.ONSProfileSelectionCallback() {
+            public void onProfileSelectionDone() {
+                mCallbackInvoked = true;
+                setReady(true);
+            }
+        };
 
     public class MyONSProfileSelector extends ONSProfileSelector {
+
         public SubscriptionManager.OnOpportunisticSubscriptionsChangedListener mProfileChngLstnrCpy;
         public BroadcastReceiver mProfileSelectorBroadcastReceiverCpy;
         public ONSNetworkScanCtlr.NetworkAvailableCallBack mNetworkAvailableCallBackCpy;
 
         public MyONSProfileSelector(Context c,
-                MyONSProfileSelector.ONSProfileSelectionCallback aNSProfileSelectionCallback) {
+            MyONSProfileSelector.ONSProfileSelectionCallback aNSProfileSelectionCallback) {
             super(c, aNSProfileSelectionCallback);
         }
 
@@ -81,7 +83,7 @@
         }
 
         protected void init(Context c,
-                MyONSProfileSelector.ONSProfileSelectionCallback aNSProfileSelectionCallback) {
+            MyONSProfileSelector.ONSProfileSelectionCallback aNSProfileSelectionCallback) {
             super.init(c, aNSProfileSelectionCallback);
             this.mSubscriptionManager = ONSProfileSelectorTest.this.mSubscriptionManager;
             this.mSubscriptionBoundTelephonyManager =
@@ -116,11 +118,11 @@
         CellIdentityLte cellIdentityLte = new CellIdentityLte(310, 210, 1, 1, 1);
         CellInfoLte cellInfoLte = new CellInfoLte();
         cellInfoLte.setCellIdentity(cellIdentityLte);
-        results2.add((CellInfo)cellInfoLte);
+        results2.add((CellInfo) cellInfoLte);
         ArrayList<String> mccMncs = new ArrayList<>();
         mccMncs.add("310210");
         AvailableNetworkInfo availableNetworkInfo = new AvailableNetworkInfo(1, 1, mccMncs,
-                new ArrayList<Integer>());
+            new ArrayList<Integer>());
         ArrayList<AvailableNetworkInfo> availableNetworkInfos = new ArrayList<AvailableNetworkInfo>();
         availableNetworkInfos.add(availableNetworkInfo);
 
@@ -141,9 +143,9 @@
                 Looper.prepare();
                 doReturn(true).when(mONSNetworkScanCtlr).startFastNetworkScan(anyObject());
                 doReturn(new ArrayList<>()).when(mSubscriptionManager)
-                        .getOpportunisticSubscriptions();
+                    .getOpportunisticSubscriptions();
                 mONSProfileSelector = new MyONSProfileSelector(mContext,
-                        mONSProfileSelectionCallback);
+                    mONSProfileSelectionCallback);
                 mONSProfileSelector.updateOppSubs();
                 mONSProfileSelector.startProfileSelection(availableNetworkInfos, mCallback);
                 mLooper = Looper.myLooper();
@@ -168,20 +170,20 @@
     public void testStartProfileSelectionSuccess() {
         List<SubscriptionInfo> subscriptionInfoList = new ArrayList<SubscriptionInfo>();
         SubscriptionInfo subscriptionInfo = new SubscriptionInfo(5, "", 1, "TMO", "TMO", 1, 1,
-                "123", 1, null, "310", "210", "", false, null, "1");
+            "123", 1, null, "310", "210", "", false, null, "1");
         SubscriptionInfo subscriptionInfo2 = new SubscriptionInfo(5, "", 1, "TMO", "TMO", 1, 1,
-                "123", 1, null, "310", "211", "", false, null, "1");
+            "123", 1, null, "310", "211", "", false, null, "1");
         subscriptionInfoList.add(subscriptionInfo);
 
         List<CellInfo> results2 = new ArrayList<CellInfo>();
         CellIdentityLte cellIdentityLte = new CellIdentityLte(310, 210, 1, 1, 1);
         CellInfoLte cellInfoLte = new CellInfoLte();
         cellInfoLte.setCellIdentity(cellIdentityLte);
-        results2.add((CellInfo)cellInfoLte);
+        results2.add((CellInfo) cellInfoLte);
         ArrayList<String> mccMncs = new ArrayList<>();
         mccMncs.add("310210");
         AvailableNetworkInfo availableNetworkInfo = new AvailableNetworkInfo(1, 1, mccMncs,
-                new ArrayList<Integer>());
+            new ArrayList<Integer>());
         ArrayList<AvailableNetworkInfo> availableNetworkInfos = new ArrayList<AvailableNetworkInfo>();
         availableNetworkInfos.add(availableNetworkInfo);
 
@@ -198,16 +200,17 @@
             @Override
             public void run() {
                 Looper.prepare();
-                doReturn(subscriptionInfoList).when(mSubscriptionManager).getOpportunisticSubscriptions();
+                doReturn(subscriptionInfoList).when(mSubscriptionManager)
+                    .getOpportunisticSubscriptions();
                 doReturn(true).when(mSubscriptionManager).isActiveSubId(anyInt());
                 doReturn(true).when(mSubscriptionBoundTelephonyManager).enableModemForSlot(
                     anyInt(), anyBoolean());
                 mONSProfileSelector = new MyONSProfileSelector(mContext,
-                        new MyONSProfileSelector.ONSProfileSelectionCallback() {
-                    public void onProfileSelectionDone() {
-                        setReady(true);
-                    }
-                });
+                    new MyONSProfileSelector.ONSProfileSelectionCallback() {
+                        public void onProfileSelectionDone() {
+                            setReady(true);
+                        }
+                    });
                 mONSProfileSelector.updateOppSubs();
                 mONSProfileSelector.startProfileSelection(availableNetworkInfos, mCallback);
                 mLooper = Looper.myLooper();
@@ -265,7 +268,8 @@
             @Override
             public void run() {
                 Looper.prepare();
-                doReturn(opportunisticSubscriptionInfoList).when(mSubscriptionManager).getOpportunisticSubscriptions();
+                doReturn(opportunisticSubscriptionInfoList).when(mSubscriptionManager)
+                    .getOpportunisticSubscriptions();
                 doReturn(false).when(mSubscriptionManager).isActiveSubId(anyInt());
                 doReturn(activeSubscriptionInfoList).when(mSubscriptionManager)
                     .getActiveSubscriptionInfoList(anyBoolean());
@@ -316,9 +320,10 @@
             public void run() {
                 Looper.prepare();
                 mONSProfileSelector = new MyONSProfileSelector(mContext,
-                        new MyONSProfileSelector.ONSProfileSelectionCallback() {
-                            public void onProfileSelectionDone() {}
-                        });
+                    new MyONSProfileSelector.ONSProfileSelectionCallback() {
+                        public void onProfileSelectionDone() {
+                        }
+                    });
                 mLooper = Looper.myLooper();
                 setReady(true);
                 Looper.loop();
@@ -337,7 +342,7 @@
     public void testselectProfileForDataWithInActiveSub() {
         List<SubscriptionInfo> subscriptionInfoList = new ArrayList<SubscriptionInfo>();
         SubscriptionInfo subscriptionInfo = new SubscriptionInfo(5, "", 1, "TMO", "TMO", 1, 1,
-                "123", 1, null, "310", "210", "", false, null, "1");
+            "123", 1, null, "310", "210", "", false, null, "1");
         subscriptionInfoList.add(subscriptionInfo);
         mReady = false;
         doReturn(new ArrayList<>()).when(mSubscriptionManager).getOpportunisticSubscriptions();
@@ -346,9 +351,10 @@
             public void run() {
                 Looper.prepare();
                 mONSProfileSelector = new MyONSProfileSelector(mContext,
-                        new MyONSProfileSelector.ONSProfileSelectionCallback() {
-                            public void onProfileSelectionDone() {}
-                        });
+                    new MyONSProfileSelector.ONSProfileSelectionCallback() {
+                        public void onProfileSelectionDone() {
+                        }
+                    });
                 mLooper = Looper.myLooper();
                 setReady(true);
                 Looper.loop();
@@ -366,20 +372,21 @@
     public void testselectProfileForDataWithInvalidSubId() {
         List<SubscriptionInfo> subscriptionInfoList = new ArrayList<SubscriptionInfo>();
         SubscriptionInfo subscriptionInfo = new SubscriptionInfo(5, "", 1, "TMO", "TMO", 1, 1,
-                "123", 1, null, "310", "210", "", false, null, "1");
+            "123", 1, null, "310", "210", "", false, null, "1");
         subscriptionInfoList.add(subscriptionInfo);
         mReady = false;
         doReturn(subscriptionInfoList).when(mSubscriptionManager).getOpportunisticSubscriptions();
         doNothing().when(mSubscriptionManager).setPreferredDataSubscriptionId(
-                anyInt(), anyBoolean(), any(), any());
+            anyInt(), anyBoolean(), any(), any());
         new Thread(new Runnable() {
             @Override
             public void run() {
                 Looper.prepare();
                 mONSProfileSelector = new MyONSProfileSelector(mContext,
-                        new MyONSProfileSelector.ONSProfileSelectionCallback() {
-                            public void onProfileSelectionDone() {}
-                        });
+                    new MyONSProfileSelector.ONSProfileSelectionCallback() {
+                        public void onProfileSelectionDone() {
+                        }
+                    });
                 mLooper = Looper.myLooper();
                 setReady(true);
                 Looper.loop();
@@ -392,30 +399,31 @@
         // Testing selectProfileForData with INVALID_SUBSCRIPTION_ID and the function should
         // return true.
         mONSProfileSelector.selectProfileForData(
-                SubscriptionManager.INVALID_SUBSCRIPTION_ID, false, null);
+            SubscriptionManager.INVALID_SUBSCRIPTION_ID, false, null);
     }
 
     @Test
     public void testselectProfileForDataWithValidSub() {
         List<SubscriptionInfo> subscriptionInfoList = new ArrayList<SubscriptionInfo>();
         SubscriptionInfo subscriptionInfo = new SubscriptionInfo(5, "", 1, "TMO", "TMO", 1, 1,
-                "123", 1, null, "310", "210", "", false, null, "1");
+            "123", 1, null, "310", "210", "", false, null, "1");
         subscriptionInfoList.add(subscriptionInfo);
         mReady = false;
         doReturn(subscriptionInfoList).when(mSubscriptionManager)
-                .getActiveSubscriptionInfoList();
+            .getActiveSubscriptionInfoList();
         doNothing().when(mSubscriptionManager).setPreferredDataSubscriptionId(
-                anyInt(), anyBoolean(), any(), any());
+            anyInt(), anyBoolean(), any(), any());
         new Thread(new Runnable() {
             @Override
             public void run() {
                 Looper.prepare();
                 doReturn(subscriptionInfoList).when(mSubscriptionManager)
-                        .getOpportunisticSubscriptions();
+                    .getOpportunisticSubscriptions();
                 mONSProfileSelector = new MyONSProfileSelector(mContext,
-                        new MyONSProfileSelector.ONSProfileSelectionCallback() {
-                            public void onProfileSelectionDone() {}
-                        });
+                    new MyONSProfileSelector.ONSProfileSelectionCallback() {
+                        public void onProfileSelectionDone() {
+                        }
+                    });
                 mONSProfileSelector.updateOppSubs();
                 mLooper = Looper.myLooper();
                 setReady(true);
@@ -430,4 +438,109 @@
         // return true.
         mONSProfileSelector.selectProfileForData(5, false, null);
     }
+
+    @Test
+    public void testStartProfileSelectionSuccessWithSameArgumentsAgain() {
+        List<SubscriptionInfo> subscriptionInfoList = new ArrayList<SubscriptionInfo>();
+        SubscriptionInfo subscriptionInfo = new SubscriptionInfo(5, "", 1, "TMO", "TMO", 1, 1,
+            "123", 1, null, "310", "210", "", false, null, "1");
+        SubscriptionInfo subscriptionInfo2 = new SubscriptionInfo(5, "", 1, "TMO", "TMO", 1, 1,
+            "123", 1, null, "310", "211", "", false, null, "1");
+        subscriptionInfoList.add(subscriptionInfo);
+
+        List<CellInfo> results2 = new ArrayList<CellInfo>();
+        CellIdentityLte cellIdentityLte = new CellIdentityLte(310, 210, 1, 1, 1);
+        CellInfoLte cellInfoLte = new CellInfoLte();
+        cellInfoLte.setCellIdentity(cellIdentityLte);
+        results2.add((CellInfo) cellInfoLte);
+        ArrayList<String> mccMncs = new ArrayList<>();
+        mccMncs.add("310210");
+        AvailableNetworkInfo availableNetworkInfo = new AvailableNetworkInfo(1, 1, mccMncs,
+            new ArrayList<Integer>());
+        ArrayList<AvailableNetworkInfo> availableNetworkInfos = new ArrayList<AvailableNetworkInfo>();
+        availableNetworkInfos.add(availableNetworkInfo);
+
+        IUpdateAvailableNetworksCallback mCallback = new IUpdateAvailableNetworksCallback.Stub() {
+            @Override
+            public void onComplete(int result) {
+                mResult = result;
+            }
+        };
+
+        mResult = -1;
+        mReady = false;
+        mONSProfileSelector = new MyONSProfileSelector(mContext,
+            new MyONSProfileSelector.ONSProfileSelectionCallback() {
+                public void onProfileSelectionDone() {
+                    setReady(true);
+                }
+            });
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+                Looper.prepare();
+                doReturn(subscriptionInfoList).when(mSubscriptionManager)
+                    .getOpportunisticSubscriptions();
+                doReturn(true).when(mSubscriptionManager).isActiveSubId(anyInt());
+                doReturn(true).when(mSubscriptionBoundTelephonyManager).enableModemForSlot(
+                    anyInt(), anyBoolean());
+
+                mONSProfileSelector.updateOppSubs();
+                mONSProfileSelector.startProfileSelection(availableNetworkInfos, mCallback);
+                mLooper = Looper.myLooper();
+                setReady(true);
+                Looper.loop();
+            }
+        }).start();
+
+        // Wait till initialization is complete.
+        waitUntilReady();
+        mReady = false;
+        mDataSubId = -1;
+
+        // Testing startProfileSelection with oppotunistic sub.
+        // On success onProfileSelectionDone must get invoked.
+        assertFalse(mReady);
+        waitForMs(500);
+        mONSProfileSelector.mNetworkAvailableCallBackCpy.onNetworkAvailability(results2);
+        Intent callbackIntent = new Intent(MyONSProfileSelector.ACTION_SUB_SWITCH);
+        callbackIntent.putExtra("sequenceId", 1);
+        callbackIntent.putExtra("subId", 5);
+        waitUntilReady();
+        assertEquals(TelephonyManager.UPDATE_AVAILABLE_NETWORKS_SUCCESS, mResult);
+        assertTrue(mReady);
+
+        mResult = -1;
+        mReady = false;
+        new Thread(new Runnable() {
+            @Override
+            public void run() {
+                Looper.prepare();
+                doReturn(subscriptionInfoList).when(mSubscriptionManager)
+                    .getOpportunisticSubscriptions();
+                doReturn(true).when(mSubscriptionManager).isActiveSubId(anyInt());
+                doReturn(true).when(mSubscriptionBoundTelephonyManager).enableModemForSlot(
+                    anyInt(), anyBoolean());
+                mONSProfileSelector.updateOppSubs();
+                mONSProfileSelector.startProfileSelection(availableNetworkInfos, mCallback);
+                mLooper = Looper.myLooper();
+                setReady(true);
+                Looper.loop();
+            }
+        }).start();
+
+        // Wait till initialization is complete.
+        waitUntilReady();
+        mReady = false;
+        mDataSubId = -1;
+
+        // Testing startProfileSelection with oppotunistic sub.
+        // On success onProfileSelectionDone must get invoked.
+        assertFalse(mReady);
+        waitForMs(500);
+        mONSProfileSelector.mNetworkAvailableCallBackCpy.onNetworkAvailability(results2);
+        waitUntilReady();
+        assertEquals(TelephonyManager.UPDATE_AVAILABLE_NETWORKS_SUCCESS, mResult);
+        assertTrue(mReady);
+    }
 }
diff --git a/tests/src/com/android/ons/OpportunisticNetworkServiceTest.java b/tests/src/com/android/ons/OpportunisticNetworkServiceTest.java
index 88fbb7c..ae28862 100644
--- a/tests/src/com/android/ons/OpportunisticNetworkServiceTest.java
+++ b/tests/src/com/android/ons/OpportunisticNetworkServiceTest.java
@@ -1,4 +1,4 @@
-/*
+  /*
  * Copyright (C) 2019 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -148,7 +148,7 @@
         mOpportunisticNetworkService.mIsEnabled = true;
         mOpportunisticNetworkService.mONSConfigInputHashMap = mockONSConfigInputHashMap;
         mOpportunisticNetworkService.handleSimStateChange();
-        waitForMs(500);
+        waitForMs(50);
         verify(mockONSConfigInputHashMap,times(1)).get(SYSTEM_APP_CONFIG_NAME);
     }