1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.services.telephony;
18
19import static org.mockito.Mockito.never;
20import static org.mockito.Mockito.verify;
21import static org.mockito.Mockito.when;
22import static org.mockito.Mockito.any;
23import static org.mockito.Mockito.times;
24
25import static org.junit.Assert.assertTrue;
26import static org.junit.Assert.assertFalse;
27
28import android.os.Looper;
29import android.test.suitebuilder.annotation.SmallTest;
30
31import com.android.internal.telephony.PhoneConstants;
32
33import org.junit.Before;
34import org.junit.Test;
35
36import org.mockito.Mock;
37import org.mockito.MockitoAnnotations;
38
39/**
40 * Tests the functionality in ImsConferenceController.java
41 */
42
43public class ImsConferenceControllerTest {
44
45    @Mock
46    private TelephonyConnectionServiceProxy mMockTelephonyConnectionServiceProxy;
47
48    private TelecomAccountRegistry mTelecomAccountRegistry;
49
50    private TestTelephonyConnection mTestTelephonyConnectionA;
51    private TestTelephonyConnection mTestTelephonyConnectionB;
52
53    private ImsConferenceController mControllerTest;
54
55    @Before
56    public void setUp() throws Exception {
57        MockitoAnnotations.initMocks(this);
58        if (Looper.myLooper() == null) {
59            Looper.prepare();
60        }
61        mTelecomAccountRegistry = TelecomAccountRegistry.getInstance(null);
62        mTestTelephonyConnectionA = new TestTelephonyConnection();
63        mTestTelephonyConnectionB = new TestTelephonyConnection();
64
65        mControllerTest = new ImsConferenceController(mTelecomAccountRegistry,
66                mMockTelephonyConnectionServiceProxy);
67    }
68
69    /**
70     * Behavior: add telephony connections B and A to conference controller,
71     *           set status for connections, remove one call
72     * Assumption: after performing the behaviors, the status of Connection A is STATE_ACTIVE;
73     *             the status of Connection B is STATE_HOLDING
74     * Expected: Connection A and Connection B are conferenceable with each other;
75     *           Connection B is not conferenceable with Connection A after A is removed;
76     *           addConference for ImsConference is not called
77     */
78    @Test
79    @SmallTest
80    public void testConferenceable() {
81
82        mControllerTest.add(mTestTelephonyConnectionB);
83        mControllerTest.add(mTestTelephonyConnectionA);
84
85        mTestTelephonyConnectionA.setActive();
86        mTestTelephonyConnectionB.setOnHold();
87
88        assertTrue(mTestTelephonyConnectionA.getConferenceables()
89                .contains(mTestTelephonyConnectionB));
90        assertTrue(mTestTelephonyConnectionB.getConferenceables()
91                .contains(mTestTelephonyConnectionA));
92
93        // verify addConference method is never called
94        verify(mMockTelephonyConnectionServiceProxy, never())
95                .addConference(any(ImsConference.class));
96
97        // call A removed
98        mControllerTest.remove(mTestTelephonyConnectionA);
99        assertFalse(mTestTelephonyConnectionB.getConferenceables()
100                .contains(mTestTelephonyConnectionA));
101    }
102
103    /**
104     * Behavior: add telephony connection B and A to conference controller,
105     *           set status for merged connections
106     * Assumption: after performing the behaviors, the status of Connection A is STATE_ACTIVE;
107     *             the status of Connection B is STATE_HOLDING;
108     *             getPhoneType() in the original connection of the telephony connection
109     *             is PhoneConstants.PHONE_TYPE_IMS
110     * Expected: addConference for ImsConference is called twice
111     */
112    @Test
113    @SmallTest
114    public void testMergeMultiPartyCalls() {
115
116        when(mTestTelephonyConnectionA.mMockRadioConnection.getPhoneType())
117                .thenReturn(PhoneConstants.PHONE_TYPE_IMS);
118        when(mTestTelephonyConnectionB.mMockRadioConnection.getPhoneType())
119                .thenReturn(PhoneConstants.PHONE_TYPE_IMS);
120        when(mTestTelephonyConnectionA.mMockRadioConnection.isMultiparty()).thenReturn(true);
121        when(mTestTelephonyConnectionB.mMockRadioConnection.isMultiparty()).thenReturn(true);
122
123        mControllerTest.add(mTestTelephonyConnectionB);
124        mControllerTest.add(mTestTelephonyConnectionA);
125
126        mTestTelephonyConnectionA.setActive();
127        mTestTelephonyConnectionB.setOnHold();
128
129        verify(mMockTelephonyConnectionServiceProxy, times(2))
130                .addConference(any(ImsConference.class));
131
132    }
133}
134