Check Cellular support before CarrierServiceTest.

CarrierServiceTest currently does not check for Cellular feature support
before running tests that rely on those features to function properly.
Checks are added to verify that Cellular is supported on the device - if
not, then the test exits early.

Bug: 142265632
Test: atest CarrierServiceTest
Change-Id: I76e4a109f718b4e8e67001ca2d6fca6718011fcb
Merged-In: I76e4a109f718b4e8e67001ca2d6fca6718011fcb
(cherry picked from commit 297f66468a69a8d5abaa62d262b29a7a83d9ccf4)
diff --git a/tests/tests/telephony/src/android/telephony/cts/CarrierServiceTest.java b/tests/tests/telephony/src/android/telephony/cts/CarrierServiceTest.java
index 492f8ff..9cc5e7a 100644
--- a/tests/tests/telephony/src/android/telephony/cts/CarrierServiceTest.java
+++ b/tests/tests/telephony/src/android/telephony/cts/CarrierServiceTest.java
@@ -16,20 +16,51 @@
 
 package android.telephony.cts;
 
+import static androidx.test.InstrumentationRegistry.getInstrumentation;
+
 import android.content.Intent;
+import android.content.pm.PackageManager;
 import android.os.PersistableBundle;
 import android.service.carrier.CarrierIdentifier;
 import android.service.carrier.CarrierService;
+import android.telephony.TelephonyManager;
 import android.test.ServiceTestCase;
+import android.util.Log;
 
 public class CarrierServiceTest extends ServiceTestCase<CarrierServiceTest.TestCarrierService> {
+    private static final String TAG = CarrierServiceTest.class.getSimpleName();
+
+    private boolean mHasCellular;
+
     public CarrierServiceTest() { super(TestCarrierService.class); }
 
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+
+        mHasCellular = hasCellular();
+        if (!mHasCellular) {
+            Log.e(TAG, "No cellular support, all tests will be skipped.");
+        }
+    }
+
+    private static boolean hasCellular() {
+        PackageManager packageManager = getInstrumentation().getContext().getPackageManager();
+        TelephonyManager telephonyManager =
+                getInstrumentation().getContext().getSystemService(TelephonyManager.class);
+        return packageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)
+                && telephonyManager.getPhoneCount() > 0;
+    }
+
     public void testNotifyCarrierNetworkChange_true() {
+        if (!mHasCellular) return;
+
         notifyCarrierNetworkChange(true);
     }
 
     public void testNotifyCarrierNetworkChange_false() {
+        if (!mHasCellular) return;
+
         notifyCarrierNetworkChange(false);
     }