Merge "enable telecom CTS on devices without Telephony"
diff --git a/tests/tests/telecom/src/android/telecom/cts/BaseRemoteTelecomTest.java b/tests/tests/telecom/src/android/telecom/cts/BaseRemoteTelecomTest.java
index 0548a70..0dc4918 100644
--- a/tests/tests/telecom/src/android/telecom/cts/BaseRemoteTelecomTest.java
+++ b/tests/tests/telecom/src/android/telecom/cts/BaseRemoteTelecomTest.java
@@ -16,19 +16,22 @@
 
 package android.telecom.cts;
 
-import static android.telecom.cts.TestUtils.*;
+import static android.telecom.cts.TestUtils.PACKAGE;
+import static android.telecom.cts.TestUtils.REMOTE_ACCOUNT_ID;
+import static android.telecom.cts.TestUtils.REMOTE_ACCOUNT_LABEL;
+import static android.telecom.cts.TestUtils.REMOTE_COMPONENT;
+import static android.telecom.cts.TestUtils.TAG;
+import static android.telecom.cts.TestUtils.WAIT_FOR_STATE_CHANGE_TIMEOUT_MS;
 
-import static org.hamcrest.CoreMatchers.not;
 import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.not;
 import static org.junit.Assert.assertThat;
 
 import android.content.ComponentName;
 import android.telecom.Connection;
-import android.telecom.PhoneAccount;
 import android.telecom.PhoneAccountHandle;
 import android.telecom.RemoteConference;
 import android.telecom.RemoteConnection;
-import android.text.TextUtils;
 import android.util.Log;
 
 import java.util.ArrayList;
@@ -93,7 +96,11 @@
 
     protected void tearDownRemoteConnectionService(PhoneAccountHandle remoteAccountHandle)
             throws Exception {
-        assertNumConnections(this.remoteConnectionService, 0);
+        if (this.remoteConnectionService != null) {
+            //check only if remote connection service was setup
+            //some skipped testcases may not set it up
+            assertNumConnections(this.remoteConnectionService, 0);
+        }
         mTelecomManager.unregisterPhoneAccount(remoteAccountHandle);
         CtsRemoteConnectionService.tearDown();
         //Telecom doesn't unbind the remote connection service at the end of all calls today.
diff --git a/tests/tests/telecom/src/android/telecom/cts/DefaultDialerOperationsTest.java b/tests/tests/telecom/src/android/telecom/cts/DefaultDialerOperationsTest.java
index 3db905f..f4aa52e 100644
--- a/tests/tests/telecom/src/android/telecom/cts/DefaultDialerOperationsTest.java
+++ b/tests/tests/telecom/src/android/telecom/cts/DefaultDialerOperationsTest.java
@@ -101,7 +101,7 @@
 
     /** Default dialer should be the default package handling ACTION_DIAL. */
     public void testActionDialHandling() throws Exception {
-        if (!TestUtils.shouldTestTelecom(mContext)) {
+        if (!TestUtils.shouldTestTelecom(mContext) || !TestUtils.hasTelephonyFeature(mContext)) {
             return;
         }
         Intent intent = new Intent(Intent.ACTION_DIAL);
diff --git a/tests/tests/telecom/src/android/telecom/cts/EmergencyCallTests.java b/tests/tests/telecom/src/android/telecom/cts/EmergencyCallTests.java
index c9dac92..553f135 100644
--- a/tests/tests/telecom/src/android/telecom/cts/EmergencyCallTests.java
+++ b/tests/tests/telecom/src/android/telecom/cts/EmergencyCallTests.java
@@ -109,7 +109,8 @@
     }
 
     /**
-     * Place an outgoing emergency call and ensure any incoming call is rejected automatically and
+     * Place an outgoing emergency call and ensure any incoming call is rejected
+     * automatically and
      * logged in call log as a new missed call.
      *
      * Note: PSAPs have requirements that active emergency calls can not be put on hold, so if for
diff --git a/tests/tests/telecom/src/android/telecom/cts/ExtendedInCallServiceTest.java b/tests/tests/telecom/src/android/telecom/cts/ExtendedInCallServiceTest.java
index 50a0e9d..5bd05db 100644
--- a/tests/tests/telecom/src/android/telecom/cts/ExtendedInCallServiceTest.java
+++ b/tests/tests/telecom/src/android/telecom/cts/ExtendedInCallServiceTest.java
@@ -613,7 +613,7 @@
     }
 
     public void testCanAddCall_CanAddForExistingActiveCall() {
-        if (!mShouldTestTelecom) {
+        if (!mShouldTestTelecom  || !TestUtils.hasTelephonyFeature(mContext)) {
             return;
         }
 
@@ -633,6 +633,28 @@
                 "Should be able to add call with only one active call");
     }
 
+    public void testCanAddCall_CanAddForExistingActiveCallWithoutHoldCapability() {
+        if (!mShouldTestTelecom  || !TestUtils.hasTelephonyFeature(mContext)) {
+            return;
+        }
+
+        placeAndVerifyCall();
+        final MockConnection connection = verifyConnectionForOutgoingCall();
+        final int capabilities = connection.getConnectionCapabilities();
+        connection.setConnectionCapabilities(capabilities & ~Connection.CAPABILITY_SUPPORT_HOLD);
+        connection.setConnectionCapabilities(capabilities & ~Connection.CAPABILITY_HOLD);
+
+        final MockInCallService inCallService = mInCallCallbacks.getService();
+
+        final Call call = inCallService.getLastCall();
+        assertCallState(call, Call.STATE_DIALING);
+        connection.setActive();
+        assertCallState(call, Call.STATE_ACTIVE);
+
+        assertCanAddCall(inCallService, true,
+                "Should be able to add call with only one active call without hold capability");
+    }
+
     public void testCanAddCall_CannotAddIfTooManyCalls() {
         if (!mShouldTestTelecom) {
             return;
diff --git a/tests/tests/telecom/src/android/telecom/cts/IncomingCallTest.java b/tests/tests/telecom/src/android/telecom/cts/IncomingCallTest.java
index 8fa54f1..f4a06eb 100644
--- a/tests/tests/telecom/src/android/telecom/cts/IncomingCallTest.java
+++ b/tests/tests/telecom/src/android/telecom/cts/IncomingCallTest.java
@@ -133,7 +133,7 @@
      * @throws Exception
      */
     public void testRingOnIncomingCall() throws Exception {
-        if (!mShouldTestTelecom) {
+        if (!mShouldTestTelecom  || !TestUtils.hasTelephonyFeature(mContext)) {
             return;
         }
         ShellIdentityUtils.invokeStaticMethodWithShellPermissions(
diff --git a/tests/tests/telecom/src/android/telecom/cts/OutgoingCallTest.java b/tests/tests/telecom/src/android/telecom/cts/OutgoingCallTest.java
index d2cdd0c..3168215 100644
--- a/tests/tests/telecom/src/android/telecom/cts/OutgoingCallTest.java
+++ b/tests/tests/telecom/src/android/telecom/cts/OutgoingCallTest.java
@@ -124,7 +124,7 @@
     }
 
     public void testPhoneStateListenerInvokedOnOutgoingEmergencyCall() throws Throwable {
-        if (!mShouldTestTelecom) {
+        if (!mShouldTestTelecom  || !TestUtils.hasTelephonyFeature(mContext)) {
             return;
         }
         TestUtils.setSystemDialerOverride(getInstrumentation());
diff --git a/tests/tests/telecom/src/android/telecom/cts/RemoteConnectionTest.java b/tests/tests/telecom/src/android/telecom/cts/RemoteConnectionTest.java
index 205cb5b..49a2afb 100644
--- a/tests/tests/telecom/src/android/telecom/cts/RemoteConnectionTest.java
+++ b/tests/tests/telecom/src/android/telecom/cts/RemoteConnectionTest.java
@@ -27,7 +27,6 @@
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.HandlerThread;
-import android.os.Parcel;
 import android.telecom.Call;
 import android.telecom.Conference;
 import android.telecom.Connection;
@@ -98,7 +97,8 @@
     }
 
     public void testRemoteConnectionOutgoingEmergencyCall() {
-        if (!mShouldTestTelecom) {
+        if (!mShouldTestTelecom || !TestUtils.hasTelephonyFeature(
+                getInstrumentation().getContext())) {
             return;
         }
         addRemoteConnectionOutgoingEmergencyCall();
diff --git a/tests/tests/telecom/src/android/telecom/cts/SelfManagedConnectionTest.java b/tests/tests/telecom/src/android/telecom/cts/SelfManagedConnectionTest.java
index 0a1b5c2..0b80e47 100644
--- a/tests/tests/telecom/src/android/telecom/cts/SelfManagedConnectionTest.java
+++ b/tests/tests/telecom/src/android/telecom/cts/SelfManagedConnectionTest.java
@@ -150,25 +150,44 @@
 
     @Override
     protected void setUp() throws Exception {
+        mContext = getInstrumentation().getContext();
         if (!mShouldTestTelecom) {
+            mShouldTestTelecom = false;
             return;
         }
         super.setUp();
         NewOutgoingCallBroadcastReceiver.reset();
-        mContext = getInstrumentation().getContext();
         mUiAutomation = getInstrumentation().getUiAutomation();
         if (mShouldTestTelecom) {
             mRoleManager = mContext.getSystemService(RoleManager.class);
             setupConnectionService(null, FLAG_ENABLE | FLAG_REGISTER);
             mTelecomManager.registerPhoneAccount(TestUtils.TEST_SELF_MANAGED_PHONE_ACCOUNT_4);
-            mDefaultDialer = getDefaultDialer();
+            if(mRoleManager.isRoleAvailable(RoleManager.ROLE_DIALER)) {
+                mDefaultDialer = getDefaultDialer();
+            }
         }
     }
 
     @Override
     protected void tearDown() throws Exception {
-        if (!mShouldTestTelecom) {
-            return;
+        if (mShouldTestTelecom) {
+            disableAndVerifyCarMode(mCarModeIncallServiceControlOne,
+                    Configuration.UI_MODE_TYPE_NORMAL);
+            disableAndVerifyCarMode(mCarModeIncallServiceControlTwo,
+                    Configuration.UI_MODE_TYPE_NORMAL);
+
+            disconnectAllCallsAndVerify(mCarModeIncallServiceControlOne);
+            disconnectAllCallsAndVerify(mCarModeIncallServiceControlTwo);
+
+            CtsSelfManagedConnectionService connectionService =
+                    CtsSelfManagedConnectionService.getConnectionService();
+            if (connectionService != null) {
+                connectionService.tearDown();
+                mTelecomManager.unregisterPhoneAccount(TestUtils.TEST_SELF_MANAGED_HANDLE_4);
+                if(mRoleManager.isRoleAvailable(RoleManager.ROLE_DIALER)) {
+                    assertTrue(setDefaultDialer(mDefaultDialer));
+                }
+            }
         }
         super.tearDown();
 
@@ -218,7 +237,7 @@
      * mode
      */
     public void testBindToSupportDefaultDialerNoCarMode() throws Exception {
-        if (!mShouldTestTelecom) {
+        if (!mShouldTestTelecom || !mRoleManager.isRoleAvailable(RoleManager.ROLE_DIALER)) {
             return;
         }
         TestServiceConnection controlConn = setUpControl(THIRD_PTY_CONTROL,
@@ -245,7 +264,7 @@
      * in car mode
      */
     public void testNoBindToUnsupportDefaultDialerNoCarMode() throws Exception {
-        if (!mShouldTestTelecom) {
+        if (!mShouldTestTelecom || !mRoleManager.isRoleAvailable(RoleManager.ROLE_DIALER)) {
             return;
         }
         TestServiceConnection controlConn = setUpControl(THIRD_PTY_CONTROL,
diff --git a/tests/tests/telecom/src/android/telecom/cts/TestUtils.java b/tests/tests/telecom/src/android/telecom/cts/TestUtils.java
index 2560b94..accf2ba 100644
--- a/tests/tests/telecom/src/android/telecom/cts/TestUtils.java
+++ b/tests/tests/telecom/src/android/telecom/cts/TestUtils.java
@@ -336,8 +336,13 @@
             return false;
         }
         final PackageManager pm = context.getPackageManager();
-        return pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY) &&
-                pm.hasSystemFeature(PackageManager.FEATURE_TELECOM);
+        return pm.hasSystemFeature(PackageManager.FEATURE_TELECOM);
+    }
+
+    public static boolean hasTelephonyFeature(Context context) {
+        final PackageManager pm = context.getPackageManager();
+        return (pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY) && pm.hasSystemFeature(
+                PackageManager.FEATURE_TELEPHONY_CALLING));
     }
 
     public static String setCallDiagnosticService(Instrumentation instrumentation,