Home | History | Annotate | Download | only in telephony
      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 
     17 package com.android.services.telephony;
     18 
     19 import android.os.Looper;
     20 import android.telecom.Conference;
     21 import android.telecom.Connection;
     22 import android.test.suitebuilder.annotation.SmallTest;
     23 
     24 import org.junit.Before;
     25 import org.junit.Test;
     26 
     27 import org.mockito.Mock;
     28 import org.mockito.Mockito;
     29 import org.mockito.MockitoAnnotations;
     30 import org.mockito.ArgumentCaptor;
     31 
     32 import static org.mockito.Mockito.never;
     33 import static org.mockito.Mockito.verify;
     34 import static org.mockito.Mockito.when;
     35 import static org.mockito.Mockito.any;
     36 
     37 import static org.junit.Assert.assertTrue;
     38 import static org.junit.Assert.assertFalse;
     39 
     40 import java.util.ArrayList;
     41 import java.util.Collection;
     42 
     43 /**
     44  * Tests the functionality in TelephonyConferenceController.java
     45  * Assumption: these tests are based on setting status manually
     46  */
     47 
     48 public class TelephonyConferenceControllerTest {
     49 
     50     @Mock
     51     private TelephonyConnectionServiceProxy mMockTelephonyConnectionServiceProxy;
     52 
     53     @Mock
     54     private Conference.Listener mMockListener;
     55 
     56     private MockTelephonyConnection mMockTelephonyConnectionA;
     57     private MockTelephonyConnection mMockTelephonyConnectionB;
     58 
     59     private TelephonyConferenceController mControllerTest;
     60 
     61     @Before
     62     public void setUp() throws Exception {
     63         MockitoAnnotations.initMocks(this);
     64         if (Looper.myLooper() == null) {
     65             Looper.prepare();
     66         }
     67         mMockTelephonyConnectionA = new MockTelephonyConnection();
     68         mMockTelephonyConnectionB = new MockTelephonyConnection();
     69 
     70         mControllerTest = new TelephonyConferenceController(mMockTelephonyConnectionServiceProxy);
     71     }
     72 
     73     /**
     74      * Behavior: add telephony connections B and A to conference controller,
     75      *           set status for connections and calls, remove one call
     76      * Assumption: after performing the behaviours, the status of Connection A is STATE_ACTIVE;
     77      *             the status of Connection B is STATE_HOLDING;
     78      *             the call in the original connection is Call.State.ACTIVE;
     79      *             isMultiparty of the call is false;
     80      *             isConferenceSupported of the connection is True
     81      * Expected: Connection A and Connection B are conferenceable with each other
     82      */
     83     @Test
     84     @SmallTest
     85     public void testConferenceable() {
     86 
     87         when(mMockTelephonyConnectionA.mMockRadioConnection.getCall()
     88                 .isMultiparty()).thenReturn(false);
     89         when(mMockTelephonyConnectionB.mMockRadioConnection.getCall()
     90                 .isMultiparty()).thenReturn(false);
     91 
     92         // add telephony connection B
     93         mControllerTest.add(mMockTelephonyConnectionB);
     94 
     95         // add telephony connection A
     96         mControllerTest.add(mMockTelephonyConnectionA);
     97 
     98         mMockTelephonyConnectionA.setActive();
     99         mMockTelephonyConnectionB.setOnHold();
    100 
    101         assertTrue(mMockTelephonyConnectionA.getConferenceables()
    102                 .contains(mMockTelephonyConnectionB));
    103         assertTrue(mMockTelephonyConnectionB.getConferenceables()
    104                 .contains(mMockTelephonyConnectionA));
    105 
    106         // verify addConference method is never called
    107         verify(mMockTelephonyConnectionServiceProxy, never())
    108                 .addConference(any(TelephonyConference.class));
    109 
    110         // call A removed
    111         mControllerTest.remove(mMockTelephonyConnectionA);
    112         assertFalse(mMockTelephonyConnectionB.getConferenceables()
    113                 .contains(mMockTelephonyConnectionA));
    114     }
    115 
    116     /**
    117      * Behavior: add telephony connection B and A to conference controller,
    118      *           set status for connections and merged calls, remove one call
    119      * Assumption: after performing the behaviours, the status of Connection A is STATE_ACTIVE;
    120      *             the status of Connection B is STATE_HOLDING;
    121      *             the call in the original connection is Call.State.ACTIVE;
    122      *             isMultiparty of the call is True;
    123      *             isConferenceSupported of the connection is True
    124      * Expected: Connection A and Connection B are conferenceable with each other
    125      *           addConference is called
    126      */
    127     @Test
    128     @SmallTest
    129     public void testMergeMultiPartyCalls() {
    130 
    131         // set isMultiparty() true to create the same senario of merge behaviour
    132         when(mMockTelephonyConnectionA.mMockRadioConnection.getCall()
    133                 .isMultiparty()).thenReturn(true);
    134         when(mMockTelephonyConnectionB.mMockRadioConnection.getCall()
    135                 .isMultiparty()).thenReturn(true);
    136 
    137         // Add connections into connection Service
    138         Collection<Connection> allConnections = new ArrayList<Connection>();
    139         allConnections.add(mMockTelephonyConnectionA);
    140         allConnections.add(mMockTelephonyConnectionB);
    141         when(mMockTelephonyConnectionServiceProxy.getAllConnections())
    142                 .thenReturn(allConnections);
    143 
    144         // add telephony connection B
    145         mControllerTest.add(mMockTelephonyConnectionB);
    146 
    147         // add telephony connection A
    148         mControllerTest.add(mMockTelephonyConnectionA);
    149 
    150         mMockTelephonyConnectionA.setActive();
    151         mMockTelephonyConnectionB.setOnHold();
    152 
    153         assertTrue(mMockTelephonyConnectionA.getConferenceables()
    154                 .contains(mMockTelephonyConnectionB));
    155         assertTrue(mMockTelephonyConnectionB.getConferenceables()
    156                 .contains(mMockTelephonyConnectionA));
    157 
    158         // capture the argument in the addConference method, and verify it is called
    159         ArgumentCaptor<TelephonyConference> argumentCaptor = ArgumentCaptor.
    160                 forClass(TelephonyConference.class);
    161         verify(mMockTelephonyConnectionServiceProxy).addConference(argumentCaptor.capture());
    162 
    163         // add a listener to the added conference
    164         argumentCaptor.getValue().addListener(mMockListener);
    165 
    166         verify(mMockListener, never()).onDestroyed(any(Conference.class));
    167 
    168         // call A removed
    169         mControllerTest.remove(mMockTelephonyConnectionA);
    170         assertFalse(mMockTelephonyConnectionB.getConferenceables()
    171                 .contains(mMockTelephonyConnectionA));
    172 
    173         //onDestroy should be called during the destroy
    174         verify(mMockListener).onDestroyed(any(Conference.class));
    175     }
    176 }
    177