Add some ConnectionServiceTests

Add static methods in CtsConnectionService to expose APIs on
ConnectionService that need to be tested.

Relax constraints on having simultaneous incoming and outgoing
connections inside verifyConnectionForIncomingCall and
verifyConnectionForOutgoingCall

Bug: 23016039
Change-Id: I1c0c676be4770799b12df9fa4c5e7a9ad4f3c548
diff --git a/tests/tests/telecom/src/android/telecom/cts/BaseTelecomTestWithMockServices.java b/tests/tests/telecom/src/android/telecom/cts/BaseTelecomTestWithMockServices.java
index bf063f7..31ca09b 100644
--- a/tests/tests/telecom/src/android/telecom/cts/BaseTelecomTestWithMockServices.java
+++ b/tests/tests/telecom/src/android/telecom/cts/BaseTelecomTestWithMockServices.java
@@ -322,8 +322,6 @@
 
         assertThat("Telecom should create outgoing connection for outgoing call",
                 connectionService.outgoingConnections.size(), not(equalTo(0)));
-        assertEquals("Telecom should not create incoming connections for outgoing calls",
-                0, connectionService.incomingConnections.size());
         MockConnection connection = connectionService.outgoingConnections.get(connectionIndex);
         return connection;
     }
@@ -345,8 +343,6 @@
 
         assertThat("Telecom should create incoming connections for incoming calls",
                 connectionService.incomingConnections.size(), not(equalTo(0)));
-        assertEquals("Telecom should not create outgoing connections for incoming calls",
-                0, connectionService.outgoingConnections.size());
         MockConnection connection = connectionService.incomingConnections.get(connectionIndex);
         setAndVerifyConnectionForIncomingCall(connection);
         return connection;
diff --git a/tests/tests/telecom/src/android/telecom/cts/ConnectionServiceTest.java b/tests/tests/telecom/src/android/telecom/cts/ConnectionServiceTest.java
new file mode 100644
index 0000000..afaa3eb
--- /dev/null
+++ b/tests/tests/telecom/src/android/telecom/cts/ConnectionServiceTest.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.telecom.cts;
+
+import static android.telecom.cts.TestUtils.*;
+
+import android.telecom.Call;
+import android.telecom.Connection;
+import android.telecom.ConnectionService;
+import android.util.Log;
+
+import java.util.Collection;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Test some additional {@link ConnectionService} APIs not already covered by other tests.
+ */
+public class ConnectionServiceTest extends BaseTelecomTestWithMockServices {
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        mContext = getInstrumentation().getContext();
+        if (mShouldTestTelecom) {
+            setupConnectionService(null, FLAG_REGISTER | FLAG_ENABLE);
+        }
+    }
+
+    public void testAddExistingConnection() {
+        if (!mShouldTestTelecom) {
+            return;
+        }
+
+        placeAndVerifyCall();
+        verifyConnectionForOutgoingCall();
+
+        final MockConnection connection = new MockConnection();
+        connection.setOnHold();
+        CtsConnectionService.addExistingConnectionToTelecom(TEST_PHONE_ACCOUNT_HANDLE, connection);
+
+        try {
+            if (!mInCallCallbacks.lock.tryAcquire(3, TimeUnit.SECONDS)) {
+                fail("No call added to InCallService.");
+            }
+        } catch (InterruptedException e) {
+            Log.i(TAG, "Test interrupted!");
+        }
+
+        final MockInCallService inCallService = mInCallCallbacks.getService();
+        final Call call = inCallService.getLastCall();
+        assertCallState(call, Call.STATE_HOLDING);
+    }
+
+    public void testGetAllConnections() {
+        if (!mShouldTestTelecom) {
+            return;
+        }
+
+        // Add first connection (outgoing call)
+        placeAndVerifyCall();
+        final Connection connection1 = verifyConnectionForOutgoingCall();
+
+        Collection<Connection> connections = CtsConnectionService.getAllConnectionsFromTelecom();
+        assertEquals(1, connections.size());
+        assertTrue(connections.contains(connection1));
+
+        // Add second connection (add existing connection)
+        final Connection connection2 = new MockConnection();
+        CtsConnectionService.addExistingConnectionToTelecom(TEST_PHONE_ACCOUNT_HANDLE, connection2);
+
+        connections = CtsConnectionService.getAllConnectionsFromTelecom();
+        assertEquals(2, connections.size());
+        assertTrue(connections.contains(connection2));
+
+        // Add third connection (incoming call)
+        addAndVerifyNewIncomingCall(getTestNumber(), null);
+        final Connection connection3 = verifyConnectionForIncomingCall();
+        connections = CtsConnectionService.getAllConnectionsFromTelecom();
+        assertEquals(3, connections.size());
+        assertTrue(connections.contains(connection3));
+    }
+}
diff --git a/tests/tests/telecom/src/android/telecom/cts/CtsConnectionService.java b/tests/tests/telecom/src/android/telecom/cts/CtsConnectionService.java
index f7e08ed6f..3c30e6b 100644
--- a/tests/tests/telecom/src/android/telecom/cts/CtsConnectionService.java
+++ b/tests/tests/telecom/src/android/telecom/cts/CtsConnectionService.java
@@ -24,6 +24,8 @@
 import android.telecom.RemoteConference;
 import android.telecom.RemoteConnection;
 
+import java.util.Collection;
+
 /**
  * This is the official ConnectionService for Telecom's CTS App. Since telecom requires that a
  * CS be registered in the AndroidManifest.xml file, we have to have a single implementation
@@ -135,6 +137,19 @@
         }
     }
 
+    public static void addExistingConnectionToTelecom(
+            PhoneAccountHandle phoneAccountHandle, Connection connection) {
+        synchronized(sLock) {
+            sTelecomConnectionService.addExistingConnection(phoneAccountHandle, connection);
+        }
+    }
+
+    public static Collection<Connection> getAllConnectionsFromTelecom() {
+        synchronized(sLock) {
+            return sTelecomConnectionService.getAllConnections();
+        }
+    }
+
     public static RemoteConnection createRemoteOutgoingConnectionToTelecom(
             PhoneAccountHandle connectionManagerPhoneAccount,
             ConnectionRequest request) {