Merge "Add CTS tests for CBRS APIs"
diff --git a/tests/tests/telephony/current/src/android/telephony/cts/AvailableNetworkInfoTest.java b/tests/tests/telephony/current/src/android/telephony/cts/AvailableNetworkInfoTest.java
new file mode 100644
index 0000000..1af3391
--- /dev/null
+++ b/tests/tests/telephony/current/src/android/telephony/cts/AvailableNetworkInfoTest.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package android.telephony.cts;
+
+import android.os.Parcel;
+import android.telephony.AvailableNetworkInfo;
+import android.test.AndroidTestCase;
+import java.util.ArrayList;
+import java.util.List;
+
+public class AvailableNetworkInfoTest extends AndroidTestCase {
+    private static final String OPERATOR_MCCMNC_1 = "123456";
+    private static final String OPERATOR_MCCMNC_2 = "246135";
+    private static final int SUB_ID = 123;
+
+    public void testAvailableNetworkInfo() {
+        List<String> mccMncs = new ArrayList<String>();
+        mccMncs.add(OPERATOR_MCCMNC_1);
+        mccMncs.add(OPERATOR_MCCMNC_2);
+
+        AvailableNetworkInfo availableNetworkInfo = new AvailableNetworkInfo(SUB_ID,
+                AvailableNetworkInfo.PRIORITY_HIGH, mccMncs);
+        assertEquals(0, availableNetworkInfo.describeContents());
+        assertEquals(SUB_ID, availableNetworkInfo.getSubId());
+        assertEquals(AvailableNetworkInfo.PRIORITY_HIGH, availableNetworkInfo.getPriority());
+        assertEquals(mccMncs, availableNetworkInfo.getMccMncs());
+
+        Parcel availableNetworkInfoParcel = Parcel.obtain();
+        availableNetworkInfo.writeToParcel(availableNetworkInfoParcel, 0);
+        availableNetworkInfoParcel.setDataPosition(0);
+        AvailableNetworkInfo tempAvailableNetworkInfo =
+                AvailableNetworkInfo.CREATOR.createFromParcel(availableNetworkInfoParcel);
+        assertTrue(tempAvailableNetworkInfo.equals(availableNetworkInfo));
+    }
+}
diff --git a/tests/tests/telephony/current/src/android/telephony/cts/TelephonyManagerTest.java b/tests/tests/telephony/current/src/android/telephony/cts/TelephonyManagerTest.java
index 7081b8b..698b885 100644
--- a/tests/tests/telephony/current/src/android/telephony/cts/TelephonyManagerTest.java
+++ b/tests/tests/telephony/current/src/android/telephony/cts/TelephonyManagerTest.java
@@ -39,6 +39,7 @@
 import android.telecom.PhoneAccountHandle;
 import android.telecom.TelecomManager;
 import android.telephony.AccessNetworkConstants;
+import android.telephony.AvailableNetworkInfo;
 import android.telephony.CellLocation;
 import android.telephony.NetworkRegistrationState;
 import android.telephony.PhoneStateListener;
@@ -59,6 +60,7 @@
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
@@ -1036,6 +1038,90 @@
         assertThat(raf).isNotEqualTo(TelephonyManager.NETWORK_TYPE_BITMASK_UNKNOWN);
     }
 
+    /**
+     * Tests {@link TelephonyManager#setPreferredOpportunisticDataSubscription} and
+     * {@link TelephonyManager#getPreferredOpportunisticDataSubscription}
+     */
+    @Test
+    public void testPreferredOpportunisticDataSubscription() {
+        int randomSubId = 1;
+        if (!mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
+            return;
+        }
+        int subId =
+            ShellIdentityUtils.invokeMethodWithShellPermissions(mTelephonyManager,
+                (tm) -> tm.getPreferredOpportunisticDataSubscription());
+        assertThat(subId).isEqualTo(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
+        List<SubscriptionInfo> subscriptionInfoList =
+                    ShellIdentityUtils.invokeMethodWithShellPermissions(mSubscriptionManager,
+                            (tm) -> tm.getOpportunisticSubscriptions());
+        if (subscriptionInfoList == null || subscriptionInfoList.size() == 0) {
+            boolean res = ShellIdentityUtils.invokeMethodWithShellPermissions(mTelephonyManager,
+                    (tm) -> tm.setPreferredOpportunisticDataSubscription(randomSubId));
+            assertThat(res).isEqualTo(false);
+            subId = ShellIdentityUtils.invokeMethodWithShellPermissions(mTelephonyManager,
+                    (tm) -> tm.getPreferredOpportunisticDataSubscription());
+            assertThat(subId).isEqualTo(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
+
+        } else {
+            boolean res = ShellIdentityUtils.invokeMethodWithShellPermissions(mTelephonyManager,
+                    (tm) -> tm.setPreferredOpportunisticDataSubscription(
+                            subscriptionInfoList.get(0).getSubscriptionId()));
+            assertThat(res).isEqualTo(true);
+            ShellIdentityUtils.invokeMethodWithShellPermissions(mTelephonyManager,
+                (tm) -> tm.getPreferredOpportunisticDataSubscription());
+            assertThat(subId).isEqualTo(subscriptionInfoList.get(0).getSubscriptionId());
+        }
+
+        boolean result = ShellIdentityUtils.invokeMethodWithShellPermissions(mTelephonyManager,
+                (tm) -> tm.setPreferredOpportunisticDataSubscription(
+                        SubscriptionManager.DEFAULT_SUBSCRIPTION_ID));
+        assertThat(result).isEqualTo(true);
+        subId = ShellIdentityUtils.invokeMethodWithShellPermissions(mTelephonyManager,
+            (tm) -> tm.getPreferredOpportunisticDataSubscription());
+        assertThat(subId).isEqualTo(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID);
+    }
+
+    /**
+     * Tests {@link TelephonyManager#updateAvailableNetworks}
+     */
+    @Test
+    public void testUpdateAvailableNetworks() {
+        int randomSubId = 1;
+        if (!mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
+            return;
+        }
+
+        List<SubscriptionInfo> subscriptionInfoList =
+            ShellIdentityUtils.invokeMethodWithShellPermissions(mSubscriptionManager,
+                (tm) -> tm.getOpportunisticSubscriptions());
+        List<String> mccMncs = new ArrayList<String>();
+        List<AvailableNetworkInfo> availableNetworkInfos = new ArrayList<AvailableNetworkInfo>();
+        if (subscriptionInfoList == null || subscriptionInfoList.size() == 0
+                || !mSubscriptionManager.isActiveSubscriptionId(
+                        subscriptionInfoList.get(0).getSubscriptionId())) {
+            AvailableNetworkInfo availableNetworkInfo = new AvailableNetworkInfo(randomSubId,
+                    AvailableNetworkInfo.PRIORITY_HIGH, mccMncs);
+            availableNetworkInfos.add(availableNetworkInfo);
+            boolean res = ShellIdentityUtils.invokeMethodWithShellPermissions(mTelephonyManager,
+                    (tm) -> tm.updateAvailableNetworks(availableNetworkInfos));
+            assertThat(res).isEqualTo(false);
+        } else {
+            AvailableNetworkInfo availableNetworkInfo = new AvailableNetworkInfo(
+                    subscriptionInfoList.get(0).getSubscriptionId(),
+                    AvailableNetworkInfo.PRIORITY_HIGH, mccMncs);
+            availableNetworkInfos.add(availableNetworkInfo);
+            boolean res = ShellIdentityUtils.invokeMethodWithShellPermissions(mTelephonyManager,
+                (tm) -> tm.updateAvailableNetworks(availableNetworkInfos));
+            assertThat(res).isEqualTo(true);
+            availableNetworkInfos.clear();
+            res = ShellIdentityUtils.invokeMethodWithShellPermissions(mTelephonyManager,
+                (tm) -> tm.updateAvailableNetworks(availableNetworkInfos));
+            assertThat(res).isEqualTo(true);
+        }
+    }
+
+
     public static void waitForMs(long ms) {
         try {
             Thread.sleep(ms);