Adding unit test for TelephonyConferenceController

Changes for testing:
 - create some Mock/Proxy class for using/testing some final public api
 - modify several parts to inject Proxy for testing

Test: Adding unit test for TelephonyConferenceControllerTest.java

Change-Id: Icdb0ebaf8bdd6ec434804302b08550b6052f428f
diff --git a/src/com/android/services/telephony/TelephonyConferenceController.java b/src/com/android/services/telephony/TelephonyConferenceController.java
index 5d59e17..1386e19 100644
--- a/src/com/android/services/telephony/TelephonyConferenceController.java
+++ b/src/com/android/services/telephony/TelephonyConferenceController.java
@@ -68,13 +68,12 @@
     /** The known connections. */
     private final List<TelephonyConnection> mTelephonyConnections = new ArrayList<>();
 
-    private final TelephonyConnectionService mConnectionService;
+    private final TelephonyConnectionServiceProxy mConnectionService;
     private boolean mTriggerRecalculate = false;
 
-    public TelephonyConferenceController(TelephonyConnectionService connectionService) {
+    public TelephonyConferenceController(TelephonyConnectionServiceProxy connectionService) {
         mConnectionService = connectionService;
     }
-
     /** The TelephonyConference connection object. */
     private TelephonyConference mTelephonyConference;
 
@@ -89,7 +88,6 @@
             Log.w(this, "add - connection already tracked; connection=%s", connection);
             return;
         }
-
         mTelephonyConnections.add(connection);
         connection.addConnectionListener(mConnectionListener);
         recalculate();
@@ -182,13 +180,11 @@
         for (TelephonyConnection connection : mTelephonyConnections) {
             com.android.internal.telephony.Connection radioConnection =
                 connection.getOriginalConnection();
-
             if (radioConnection != null) {
                 Call.State state = radioConnection.getState();
                 Call call = radioConnection.getCall();
                 if ((state == Call.State.ACTIVE || state == Call.State.HOLDING) &&
                         (call != null && call.isMultiparty())) {
-
                     numGsmConnections++;
                     conferencedConnections.add(connection);
                 }
diff --git a/src/com/android/services/telephony/TelephonyConnectionService.java b/src/com/android/services/telephony/TelephonyConnectionService.java
index 279f087..915651e 100644
--- a/src/com/android/services/telephony/TelephonyConnectionService.java
+++ b/src/com/android/services/telephony/TelephonyConnectionService.java
@@ -72,7 +72,17 @@
             Pattern.compile("\\*228[0-9]{0,2}");
 
     private final TelephonyConferenceController mTelephonyConferenceController =
-            new TelephonyConferenceController(this);
+            new TelephonyConferenceController(new TelephonyConnectionServiceProxy() {
+                @Override
+                public Collection<Connection> getAllConnections() {
+                    return TelephonyConnectionService.this.getAllConnections();
+                }
+
+                @Override
+                public void addConference(TelephonyConference mTelephonyConference) {
+                    TelephonyConnectionService.this.addConference(mTelephonyConference);
+                }
+            });
     private final CdmaConferenceController mCdmaConferenceController =
             new CdmaConferenceController(this);
     private final ImsConferenceController mImsConferenceController =
diff --git a/src/com/android/services/telephony/TelephonyConnectionServiceProxy.java b/src/com/android/services/telephony/TelephonyConnectionServiceProxy.java
new file mode 100644
index 0000000..384ce69
--- /dev/null
+++ b/src/com/android/services/telephony/TelephonyConnectionServiceProxy.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2016 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 com.android.services.telephony;
+
+import android.telecom.Connection;
+
+import java.util.Collection;
+
+/**
+ * TelephonyConnectionService Interface for testing purpose
+ */
+
+public interface TelephonyConnectionServiceProxy {
+    Collection<Connection> getAllConnections();
+    void addConference(TelephonyConference mTelephonyConference);
+}
diff --git a/tests/src/com/android/services/telephony/MockTelephonyConnection.java b/tests/src/com/android/services/telephony/MockTelephonyConnection.java
new file mode 100644
index 0000000..e647116
--- /dev/null
+++ b/tests/src/com/android/services/telephony/MockTelephonyConnection.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright (C) 2016 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 com.android.services.telephony;
+
+import static org.mockito.Mockito.when;
+
+import com.android.internal.telephony.Call;
+import com.android.internal.telephony.Phone;
+
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+/**
+ * Mock Telephony Connection used in TelephonyConferenceController.java for testing purpose
+ */
+
+public class MockTelephonyConnection extends TelephonyConnection {
+
+    @Mock
+    com.android.internal.telephony.Connection mMockRadioConnection;
+
+    @Mock
+    Call mMockCall;
+
+    @Mock
+    Phone mMockPhone;
+
+    @Override
+    public com.android.internal.telephony.Connection getOriginalConnection() {
+        return mMockRadioConnection;
+    }
+
+    public MockTelephonyConnection() {
+        super(null, null);
+        MockitoAnnotations.initMocks(this);
+
+        //mock radioConnection mPhone mCall
+        when(mMockRadioConnection.getState()).thenReturn(Call.State.ACTIVE);
+        when(mMockRadioConnection.getCall()).thenReturn(mMockCall);
+        //to pass the PhoneAccount
+        when(mMockPhone.getRingingCall()).thenReturn(mMockCall);
+        when(mMockCall.getState()).thenReturn(Call.State.ACTIVE);
+    }
+
+    @Override
+    public boolean isConferenceSupported() {
+        return true;
+    }
+
+    @Override
+    public Phone getPhone() {
+        return mMockPhone;
+    }
+
+    public TelephonyConnection cloneConnection() {
+        return null;
+    }
+
+}
diff --git a/tests/src/com/android/services/telephony/TelephonyConferenceControllerTest.java b/tests/src/com/android/services/telephony/TelephonyConferenceControllerTest.java
new file mode 100644
index 0000000..739359a
--- /dev/null
+++ b/tests/src/com/android/services/telephony/TelephonyConferenceControllerTest.java
@@ -0,0 +1,176 @@
+/*
+ * Copyright (C) 2016 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 com.android.services.telephony;
+
+import android.os.Looper;
+import android.telecom.Conference;
+import android.telecom.Connection;
+import android.test.suitebuilder.annotation.SmallTest;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.mockito.ArgumentCaptor;
+
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.any;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertFalse;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+/**
+ * Tests the functionality in TelephonyConferenceController.java
+ * Assumption: these tests are based on setting status manually
+ */
+
+public class TelephonyConferenceControllerTest {
+
+    @Mock
+    private TelephonyConnectionServiceProxy mMockTelephonyConnectionServiceProxy;
+
+    @Mock
+    private Conference.Listener mMockListener;
+
+    private MockTelephonyConnection mMockTelephonyConnectionA;
+    private MockTelephonyConnection mMockTelephonyConnectionB;
+
+    private TelephonyConferenceController mControllerTest;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+        if (Looper.myLooper() == null) {
+            Looper.prepare();
+        }
+        mMockTelephonyConnectionA = new MockTelephonyConnection();
+        mMockTelephonyConnectionB = new MockTelephonyConnection();
+
+        mControllerTest = new TelephonyConferenceController(mMockTelephonyConnectionServiceProxy);
+    }
+
+    /**
+     * Behavior: add telephony connections B and A to conference controller,
+     *           set status for connections and calls, remove one call
+     * Assumption: after performing the behaviours, the status of Connection A is STATE_ACTIVE;
+     *             the status of Connection B is STATE_HOLDING;
+     *             the call in the original connection is Call.State.ACTIVE;
+     *             isMultiparty of the call is false;
+     *             isConferenceSupported of the connection is True
+     * Expected: Connection A and Connection B are conferenceable with each other
+     */
+    @Test
+    @SmallTest
+    public void testConferenceable() {
+
+        when(mMockTelephonyConnectionA.mMockRadioConnection.getCall()
+                .isMultiparty()).thenReturn(false);
+        when(mMockTelephonyConnectionB.mMockRadioConnection.getCall()
+                .isMultiparty()).thenReturn(false);
+
+        // add telephony connection B
+        mControllerTest.add(mMockTelephonyConnectionB);
+
+        // add telephony connection A
+        mControllerTest.add(mMockTelephonyConnectionA);
+
+        mMockTelephonyConnectionA.setActive();
+        mMockTelephonyConnectionB.setOnHold();
+
+        assertTrue(mMockTelephonyConnectionA.getConferenceables()
+                .contains(mMockTelephonyConnectionB));
+        assertTrue(mMockTelephonyConnectionB.getConferenceables()
+                .contains(mMockTelephonyConnectionA));
+
+        // verify addConference method is never called
+        verify(mMockTelephonyConnectionServiceProxy, never())
+                .addConference(any(TelephonyConference.class));
+
+        // call A removed
+        mControllerTest.remove(mMockTelephonyConnectionA);
+        assertFalse(mMockTelephonyConnectionB.getConferenceables()
+                .contains(mMockTelephonyConnectionA));
+    }
+
+    /**
+     * Behavior: add telephony connection B and A to conference controller,
+     *           set status for connections and merged calls, remove one call
+     * Assumption: after performing the behaviours, the status of Connection A is STATE_ACTIVE;
+     *             the status of Connection B is STATE_HOLDING;
+     *             the call in the original connection is Call.State.ACTIVE;
+     *             isMultiparty of the call is True;
+     *             isConferenceSupported of the connection is True
+     * Expected: Connection A and Connection B are conferenceable with each other
+     *           addConference is called
+     */
+    @Test
+    @SmallTest
+    public void testMergeMultiPartyCalls() {
+
+        // set isMultiparty() true to create the same senario of merge behaviour
+        when(mMockTelephonyConnectionA.mMockRadioConnection.getCall()
+                .isMultiparty()).thenReturn(true);
+        when(mMockTelephonyConnectionB.mMockRadioConnection.getCall()
+                .isMultiparty()).thenReturn(true);
+
+        // Add connections into connection Service
+        Collection<Connection> allConnections = new ArrayList<Connection>();
+        allConnections.add(mMockTelephonyConnectionA);
+        allConnections.add(mMockTelephonyConnectionB);
+        when(mMockTelephonyConnectionServiceProxy.getAllConnections())
+                .thenReturn(allConnections);
+
+        // add telephony connection B
+        mControllerTest.add(mMockTelephonyConnectionB);
+
+        // add telephony connection A
+        mControllerTest.add(mMockTelephonyConnectionA);
+
+        mMockTelephonyConnectionA.setActive();
+        mMockTelephonyConnectionB.setOnHold();
+
+        assertTrue(mMockTelephonyConnectionA.getConferenceables()
+                .contains(mMockTelephonyConnectionB));
+        assertTrue(mMockTelephonyConnectionB.getConferenceables()
+                .contains(mMockTelephonyConnectionA));
+
+        // capture the argument in the addConference method, and verify it is called
+        ArgumentCaptor<TelephonyConference> argumentCaptor = ArgumentCaptor.
+                forClass(TelephonyConference.class);
+        verify(mMockTelephonyConnectionServiceProxy).addConference(argumentCaptor.capture());
+
+        // add a listener to the added conference
+        argumentCaptor.getValue().addListener(mMockListener);
+
+        verify(mMockListener, never()).onDestroyed(any(Conference.class));
+
+        // call A removed
+        mControllerTest.remove(mMockTelephonyConnectionA);
+        assertFalse(mMockTelephonyConnectionB.getConferenceables()
+                .contains(mMockTelephonyConnectionA));
+
+        //onDestroy should be called during the destroy
+        verify(mMockListener).onDestroyed(any(Conference.class));
+    }
+}