Merge "Correct behavior answering incoming call with dialing self-managed call."
am: e65966af99

Change-Id: Ifaa55533e383f509a3eadf9420a8a31fc7099eea
diff --git a/src/com/android/server/telecom/Call.java b/src/com/android/server/telecom/Call.java
index 20adb84..e4f2f44 100644
--- a/src/com/android/server/telecom/Call.java
+++ b/src/com/android/server/telecom/Call.java
@@ -2498,7 +2498,7 @@
      * networks at least), so we still enable this feature even though
      * SMSes to that number will silently fail.
      */
-    boolean isRespondViaSmsCapable() {
+    public boolean isRespondViaSmsCapable() {
         if (mState != CallState.RINGING) {
             return false;
         }
diff --git a/src/com/android/server/telecom/CallsManager.java b/src/com/android/server/telecom/CallsManager.java
index f38e781..62f9f99 100644
--- a/src/com/android/server/telecom/CallsManager.java
+++ b/src/com/android/server/telecom/CallsManager.java
@@ -1943,7 +1943,7 @@
         } else {
             // Hold or disconnect the active call and request call focus for the incoming call.
             Call activeCall = (Call) mConnectionSvrFocusMgr.getCurrentFocusCall();
-            Log.d(this, "Incoming call = %s Ongoing call %s", call, activeCall);
+            Log.d(this, "answerCall: Incoming call = %s Ongoing call %s", call, activeCall);
             holdActiveCallForNewCall(call);
             mConnectionSvrFocusMgr.requestFocus(
                     call,
@@ -4447,7 +4447,7 @@
     }
 
     private boolean canHold(Call call) {
-        return call.can(Connection.CAPABILITY_HOLD);
+        return call.can(Connection.CAPABILITY_HOLD) && call.getState() != CallState.DIALING;
     }
 
     private boolean supportsHold(Call call) {
diff --git a/testapps/src/com/android/server/telecom/testapps/SelfManagedConnectionService.java b/testapps/src/com/android/server/telecom/testapps/SelfManagedConnectionService.java
index f2b6496..e6e35d8 100644
--- a/testapps/src/com/android/server/telecom/testapps/SelfManagedConnectionService.java
+++ b/testapps/src/com/android/server/telecom/testapps/SelfManagedConnectionService.java
@@ -104,6 +104,8 @@
         if (isIncoming) {
             connection.setIsIncomingCallUiShowing(request.shouldShowIncomingCallUi());
             connection.setRinging();
+        } else {
+            connection.setDialing();
         }
         Bundle requestExtras = request.getExtras();
         if (requestExtras != null) {
diff --git a/tests/src/com/android/server/telecom/tests/CallsManagerTest.java b/tests/src/com/android/server/telecom/tests/CallsManagerTest.java
index e2e2f30..df19ce8 100644
--- a/tests/src/com/android/server/telecom/tests/CallsManagerTest.java
+++ b/tests/src/com/android/server/telecom/tests/CallsManagerTest.java
@@ -115,7 +115,7 @@
     private static final PhoneAccountHandle VOIP_1_HANDLE = new PhoneAccountHandle(
             ComponentName.unflattenFromString("com.voip/.Stuff"), "Voip1");
     private static final PhoneAccountHandle SELF_MANAGED_HANDLE = new PhoneAccountHandle(
-            ComponentName.unflattenFromString("com.foo/.Self"), "Self");
+            ComponentName.unflattenFromString("com.baz/.Self"), "Self");
     private static final PhoneAccount SIM_1_ACCOUNT = new PhoneAccount.Builder(SIM_1_HANDLE, "Sim1")
             .setCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION
                     | PhoneAccount.CAPABILITY_CALL_PROVIDER)
@@ -822,6 +822,39 @@
 
     @SmallTest
     @Test
+    public void testDisconnectDialingCallOnIncoming() {
+        // GIVEN a CallsManager with a self-managed call which is dialing, and this call can be held
+        Call ongoingCall = addSpyCall(SELF_MANAGED_HANDLE);
+        ongoingCall.setState(CallState.DIALING, "test");
+        doReturn(true).when(ongoingCall).can(Connection.CAPABILITY_HOLD);
+        doReturn(true).when(ongoingCall).can(Connection.CAPABILITY_SUPPORT_HOLD);
+        doReturn(true).when(ongoingCall).isSelfManaged();
+        doReturn(ongoingCall).when(mConnectionSvrFocusMgr).getCurrentFocusCall();
+
+        // and a new incoming managed call
+        Call newCall = addSpyCall();
+        doReturn(false).when(newCall).isRespondViaSmsCapable();
+        newCall.setState(CallState.RINGING, "test");
+
+        // WHEN answering the new call
+        mCallsManager.answerCall(newCall, VideoProfile.STATE_AUDIO_ONLY);
+
+        // THEN the ongoing call is disconnected
+        verify(ongoingCall).disconnect();
+
+        // AND focus is requested for the new call
+        ArgumentCaptor<CallsManager.RequestCallback> requestCaptor =
+                ArgumentCaptor.forClass(CallsManager.RequestCallback.class);
+        verify(mConnectionSvrFocusMgr).requestFocus(eq(newCall), requestCaptor.capture());
+        // since we're mocking the focus manager, we'll just pretend it did its thing.
+        requestCaptor.getValue().onRequestFocusDone(newCall);
+
+        // and the new call is marked answered
+        assertEquals(CallState.ANSWERED, newCall.getState());
+    }
+
+    @SmallTest
+    @Test
     public void testNoFilteringOfSelfManagedCalls() {
         // GIVEN an incoming call which is self managed.
         Call incomingCall = addSpyCall(SELF_MANAGED_HANDLE);