More CTS conference tests

Verifying the conference merge/swap API's and the hold/unhold API's.

BUG: 20303674
Change-Id: I8c6107764b7bd1f3ac333b8c5a4ca212ead1a9e1
diff --git a/tests/tests/telecom/src/android/telecom/cts/BaseTelecomTestWithMockServices.java b/tests/tests/telecom/src/android/telecom/cts/BaseTelecomTestWithMockServices.java
index ba94884..2469dd1 100644
--- a/tests/tests/telecom/src/android/telecom/cts/BaseTelecomTestWithMockServices.java
+++ b/tests/tests/telecom/src/android/telecom/cts/BaseTelecomTestWithMockServices.java
@@ -153,6 +153,7 @@
                 Log.i(TAG, "onCallAdded, Call: " + call + "Num Calls: " + numCalls);
                 this.lock.release();
             }
+            @Override
             public void onCallRemoved(Call call, int numCalls) {
                 Log.i(TAG, "onCallRemoved, Call: " + call + "Num Calls: " + numCalls);
             }
@@ -172,6 +173,18 @@
                         conferenceableCalls);
                 this.lock.release();
             }
+            @Override
+            public void onDetailsChanged(Call call, Call.Details details) {
+                Log.i(TAG, "onDetailsChanged, Call: " + call + "Details: " + details);
+            }
+            @Override
+            public void onCallDestroyed(Call call) {
+                Log.i(TAG, "onCallDestroyed, Call: " + call);
+            }
+            @Override
+            public void onCallStateChanged(Call call, int newState) {
+                Log.i(TAG, "onCallStateChanged, Call: " + call + "New State: " + newState);
+            }
         };
 
         MockInCallService.setCallbacks(mInCallCallbacks);
@@ -388,12 +401,15 @@
             Log.i(TAG, "Test interrupted!");
         }
     }
+
     /**
      * Disconnect the created test call and verify that Telecom has cleared all calls.
      */
     void cleanupCalls() {
         if (mInCallCallbacks != null && mInCallCallbacks.getService() != null) {
+            mInCallCallbacks.getService().disconnectAllConferenceCalls();
             mInCallCallbacks.getService().disconnectAllCalls();
+            assertNumConferenceCalls(mInCallCallbacks.getService(), 0);
             assertNumCalls(mInCallCallbacks.getService(), 0);
         }
     }
@@ -589,6 +605,24 @@
         );
     }
 
+    void assertCallDisplayName(final Call call, final String name) {
+        waitUntilConditionIsTrueOrTimeout(
+                new Condition() {
+                    @Override
+                    public Object expected() {
+                        return name;
+                    }
+
+                    @Override
+                    public Object actual() {
+                        return call.getDetails().getCallerDisplayName();
+                    }
+                },
+                WAIT_FOR_STATE_CHANGE_TIMEOUT_MS,
+                "Call should have display name: " + name
+        );
+    }
+
     void waitUntilConditionIsTrueOrTimeout(Condition condition, long timeout,
             String description) {
         final long start = System.currentTimeMillis();
diff --git a/tests/tests/telecom/src/android/telecom/cts/ConferenceTest.java b/tests/tests/telecom/src/android/telecom/cts/ConferenceTest.java
index 50c08ff..1651015 100644
--- a/tests/tests/telecom/src/android/telecom/cts/ConferenceTest.java
+++ b/tests/tests/telecom/src/android/telecom/cts/ConferenceTest.java
@@ -23,9 +23,6 @@
 import android.telecom.ConnectionRequest;
 import android.telecom.PhoneAccount;
 import android.telecom.PhoneAccountHandle;
-import android.util.Log;
-
-import java.util.List;
 
 /**
  * Extended suite of tests that use {@link CtsConnectionService} and {@link MockInCallService} to
@@ -33,9 +30,8 @@
  */
 public class ConferenceTest extends BaseTelecomTestWithMockServices {
     public static final int CONF_CAPABILITIES = Connection.CAPABILITY_SEPARATE_FROM_CONFERENCE |
-            Connection.CAPABILITY_DISCONNECT_FROM_CONFERENCE | Connection.CAPABILITY_HOLD;
-    public static final int MERGE_CONF_CAPABILITIES = Connection.CAPABILITY_MERGE_CONFERENCE |
-            Connection.CAPABILITY_SWAP_CONFERENCE;
+            Connection.CAPABILITY_DISCONNECT_FROM_CONFERENCE | Connection.CAPABILITY_HOLD |
+            Connection.CAPABILITY_MERGE_CONFERENCE | Connection.CAPABILITY_SWAP_CONFERENCE;
 
     private Call mCall1;
     private Call mCall2;
@@ -105,4 +101,41 @@
             fail("Call 1 should not be still conferenced");
         }
     }
+
+    public void testConferenceHoldAndUnhold() {
+        if (!shouldTestTelecom(mContext)) {
+            return;
+        }
+
+        addAndVerifyConferenceCall(mCall1, mCall2);
+        final Call conf = mInCallService.getLastConferenceCall();
+
+        conf.hold();
+        assertCallState(conf, Call.STATE_HOLDING);
+        assertCallState(mCall1, Call.STATE_HOLDING);
+        assertCallState(mCall2, Call.STATE_HOLDING);
+
+        conf.unhold();
+        assertCallState(conf, Call.STATE_ACTIVE);
+        assertCallState(mCall1, Call.STATE_ACTIVE);
+        assertCallState(mCall2, Call.STATE_ACTIVE);
+    }
+
+    public void testConferenceMergeAndSwap() {
+        if (!shouldTestTelecom(mContext)) {
+            return;
+        }
+
+        addAndVerifyConferenceCall(mCall1, mCall2);
+        final Call conf = mInCallService.getLastConferenceCall();
+
+        conf.mergeConference();
+        assertCallDisplayName(mCall1, TestUtils.MERGE_CALLER_NAME);
+        assertCallDisplayName(mCall2, TestUtils.MERGE_CALLER_NAME);
+
+        conf.swapConference();
+        assertCallDisplayName(mCall1, TestUtils.SWAP_CALLER_NAME);
+        assertCallDisplayName(mCall2, TestUtils.SWAP_CALLER_NAME);
+
+    }
 }
diff --git a/tests/tests/telecom/src/android/telecom/cts/CtsConnectionService.java b/tests/tests/telecom/src/android/telecom/cts/CtsConnectionService.java
index 3a5e157..eaf6072 100644
--- a/tests/tests/telecom/src/android/telecom/cts/CtsConnectionService.java
+++ b/tests/tests/telecom/src/android/telecom/cts/CtsConnectionService.java
@@ -50,9 +50,6 @@
 
     public CtsConnectionService() throws Exception {
         super();
-        if (sTelecomConnectionService != null) {
-            throw new Exception("Telecom ConnectionService exists");
-        }
         sTelecomConnectionService = this;
     }
 
diff --git a/tests/tests/telecom/src/android/telecom/cts/MockConference.java b/tests/tests/telecom/src/android/telecom/cts/MockConference.java
index 4edcc4f..ffc188f 100644
--- a/tests/tests/telecom/src/android/telecom/cts/MockConference.java
+++ b/tests/tests/telecom/src/android/telecom/cts/MockConference.java
@@ -20,6 +20,7 @@
 import android.telecom.Connection;
 import android.telecom.DisconnectCause;
 import android.telecom.PhoneAccountHandle;
+import android.telecom.TelecomManager;
 
 /**
  * {@link Conference} subclass that immediately performs any state changes that are a result of
@@ -41,10 +42,12 @@
 
     @Override
     public void onDisconnect() {
+        super.onDisconnect();
         for (Connection c : getConnections()) {
             c.setDisconnected(new DisconnectCause(DisconnectCause.REMOTE));
             c.destroy();
         }
+        destroy();
     }
 
     @Override
@@ -55,7 +58,28 @@
     }
 
     @Override
+    public void onMerge() {
+        super.onMerge();
+        // Let's just change the connection display name for testing that onMerge was invoked.
+        for (Connection c : getConnections()) {
+            c.setCallerDisplayName(
+                    TestUtils.MERGE_CALLER_NAME, TelecomManager.PRESENTATION_ALLOWED);
+        }
+    }
+
+    @Override
+    public void onSwap() {
+        super.onSwap();
+        // Let's just change the connection display name for testing that onSwap was invoked.
+        for (Connection c : getConnections()) {
+            c.setCallerDisplayName(
+                    TestUtils.SWAP_CALLER_NAME, TelecomManager.PRESENTATION_ALLOWED);
+        }
+    }
+
+    @Override
     public void onHold() {
+        super.onHold();
         for (Connection c : getConnections()) {
             c.setOnHold();
         }
@@ -64,6 +88,7 @@
 
     @Override
     public void onUnhold() {
+        super.onUnhold();
         for (Connection c : getConnections()) {
             c.setActive();
         }
diff --git a/tests/tests/telecom/src/android/telecom/cts/MockInCallService.java b/tests/tests/telecom/src/android/telecom/cts/MockInCallService.java
index c1f35c3..52fda6a 100644
--- a/tests/tests/telecom/src/android/telecom/cts/MockInCallService.java
+++ b/tests/tests/telecom/src/android/telecom/cts/MockInCallService.java
@@ -44,6 +44,8 @@
         public void onParentChanged(Call call, Call parent) {};
         public void onChildrenChanged(Call call, List<Call> children) {};
         public void onConferenceableCallsChanged(Call call, List<Call> conferenceableCalls) {};
+        public void onCallDestroyed(Call call) {};
+        public void onDetailsChanged(Call call, Call.Details details) {};
 
         final public MockInCallService getService() {
             return mService;
@@ -96,6 +98,22 @@
                 getCallbacks().onConferenceableCallsChanged(call, conferenceableCalls);
             }
         }
+
+        @Override
+        public void onCallDestroyed(Call call) {
+            super.onCallDestroyed(call);
+            if (getCallbacks() != null) {
+                getCallbacks().onCallDestroyed(call);
+            }
+        }
+
+        @Override
+        public void onDetailsChanged(Call call, Call.Details details) {
+            super.onDetailsChanged(call, details);
+            if (getCallbacks() != null) {
+                getCallbacks().onDetailsChanged(call, details);
+            }
+        }
     };
 
     private void saveVideoCall(Call call, VideoCall videoCall) {
diff --git a/tests/tests/telecom/src/android/telecom/cts/TestUtils.java b/tests/tests/telecom/src/android/telecom/cts/TestUtils.java
index 3356be0..9cdc1a8 100644
--- a/tests/tests/telecom/src/android/telecom/cts/TestUtils.java
+++ b/tests/tests/telecom/src/android/telecom/cts/TestUtils.java
@@ -50,6 +50,9 @@
 
     private static final String COMMAND_ENABLE = "telecom set-phone-account-enabled ";
 
+    public static final String MERGE_CALLER_NAME = "calls-merged";
+    public static final String SWAP_CALLER_NAME = "calls-swapped";
+
     public static boolean shouldTestTelecom(Context context) {
         if (!HAS_TELECOM) {
             return false;