Update tests for unmetered override

Test: atest SubscriptionManagerTest
Bug: 175723642
Change-Id: I0ccf4dd604b711bc20be6ab3e30221b60e160d02
diff --git a/tests/tests/telephony/current/src/android/telephony/cts/SubscriptionManagerTest.java b/tests/tests/telephony/current/src/android/telephony/cts/SubscriptionManagerTest.java
index b6a0f6f..b3b4feb 100644
--- a/tests/tests/telephony/current/src/android/telephony/cts/SubscriptionManagerTest.java
+++ b/tests/tests/telephony/current/src/android/telephony/cts/SubscriptionManagerTest.java
@@ -18,7 +18,6 @@
 
 import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET;
 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_CONGESTED;
-import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED;
 import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED;
 import static android.net.NetworkCapabilities.NET_CAPABILITY_TEMPORARILY_NOT_METERED;
 import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
@@ -42,6 +41,8 @@
 import android.net.NetworkRequest;
 import android.os.Looper;
 import android.os.ParcelUuid;
+import android.os.PersistableBundle;
+import android.telephony.CarrierConfigManager;
 import android.telephony.SubscriptionInfo;
 import android.telephony.SubscriptionManager;
 import android.telephony.SubscriptionPlan;
@@ -296,7 +297,8 @@
             final CountDownLatch latch = waitForNetworkCapabilities(net, caps -> {
                 return !caps.hasCapability(NET_CAPABILITY_NOT_CONGESTED);
             });
-            mSm.setSubscriptionOverrideCongested(mSubId, true, 0);
+            mSm.setSubscriptionOverrideCongested(
+                    mSubId, true, TelephonyManager.getAllNetworkTypes(), 0);
             assertTrue(latch.await(10, TimeUnit.SECONDS));
         }
 
@@ -312,7 +314,8 @@
         // Now revoke our access
         setSubPlanOwner(mSubId, null);
         try {
-            mSm.setSubscriptionOverrideCongested(mSubId, true, 0);
+            mSm.setSubscriptionOverrideCongested(
+                    mSubId, true, TelephonyManager.getAllNetworkTypes(), 0);
             fail();
         } catch (SecurityException | IllegalStateException expected) {
         }
@@ -346,29 +349,30 @@
         final Network net = findCellularNetwork();
         assertNotNull("Active cellular network required", net);
 
-        // Make ourselves the owner and define some plans
-        setSubPlanOwner(mSubId, mPackageName);
-        mSm.setSubscriptionPlans(mSubId,
-                Arrays.asList(buildValidSubscriptionPlan(System.currentTimeMillis())));
+        // TODO: Remove this check after b/176119724 is fixed.
+        if (!isUnmetered5GSupported()) return;
 
         // Cellular is metered by default
-        assertFalse(cm.getNetworkCapabilities(net).hasCapability(NET_CAPABILITY_NOT_METERED));
+        assertFalse(cm.getNetworkCapabilities(net).hasCapability(
+                NET_CAPABILITY_TEMPORARILY_NOT_METERED));
 
         // Override should make it go temporarily unmetered
         {
             final CountDownLatch latch = waitForNetworkCapabilities(net, caps -> {
-                return caps.hasCapability(NET_CAPABILITY_NOT_METERED);
+                return caps.hasCapability(NET_CAPABILITY_TEMPORARILY_NOT_METERED);
             });
-            mSm.setSubscriptionOverrideUnmetered(mSubId, true, 0);
+            mSm.setSubscriptionOverrideUnmetered(
+                    mSubId, true, TelephonyManager.getAllNetworkTypes(), 0);
             assertTrue(latch.await(10, TimeUnit.SECONDS));
         }
 
         // Clearing override should make it go metered
         {
             final CountDownLatch latch = waitForNetworkCapabilities(net, caps -> {
-                return !caps.hasCapability(NET_CAPABILITY_NOT_METERED);
+                return !caps.hasCapability(NET_CAPABILITY_TEMPORARILY_NOT_METERED);
             });
-            mSm.setSubscriptionOverrideUnmetered(mSubId, false, 0);
+            mSm.setSubscriptionOverrideUnmetered(
+                    mSubId, false, TelephonyManager.getAllNetworkTypes(), 0);
             assertTrue(latch.await(10, TimeUnit.SECONDS));
         }
     }
@@ -382,6 +386,9 @@
         final Network net = findCellularNetwork();
         assertNotNull("Active cellular network required", net);
 
+        // TODO: Remove this check after b/176119724 is fixed.
+        if (!isUnmetered5GSupported()) return;
+
         // Make ourselves the owner and define some plans
         setSubPlanOwner(mSubId, mPackageName);
         mSm.setSubscriptionPlans(mSubId,
@@ -946,4 +953,24 @@
         SystemUtil.runShellCommand(InstrumentationRegistry.getInstrumentation(),
                 "cmd netpolicy set sub-plan-owner " + subId + " " + packageName);
     }
+
+    private boolean isUnmetered5GSupported() {
+        final CarrierConfigManager ccm = InstrumentationRegistry.getContext()
+                .getSystemService(CarrierConfigManager.class);
+        PersistableBundle carrierConfig = ccm.getConfigForSubId(mSubId);
+
+        final TelephonyManager tm = InstrumentationRegistry.getContext()
+                .getSystemService(TelephonyManager.class);
+        int dataNetworkType = tm.getDataNetworkType(mSubId);
+        long supportedRats = ShellIdentityUtils.invokeMethodWithShellPermissions(tm,
+                TelephonyManager::getSupportedRadioAccessFamily);
+
+        boolean validCarrier = carrierConfig.getBoolean(
+                CarrierConfigManager.KEY_NETWORK_TEMP_NOT_METERED_SUPPORTED_BOOL);
+        boolean validCapabilities = (supportedRats & TelephonyManager.NETWORK_TYPE_BITMASK_NR) != 0;
+        // TODO: need to check for TelephonyDisplayInfo override for NR NSA
+        boolean validNetworkType = dataNetworkType == TelephonyManager.NETWORK_TYPE_NR;
+
+        return validCarrier && validNetworkType && validCapabilities;
+    }
 }