Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2015 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 android.telecom.cts;
     18 
     19 import static org.junit.Assert.assertTrue;
     20 
     21 import android.content.Intent;
     22 import android.telecom.Conference;
     23 import android.telecom.Connection;
     24 import android.telecom.ConnectionRequest;
     25 import android.telecom.ConnectionService;
     26 import android.telecom.PhoneAccountHandle;
     27 import android.telecom.RemoteConference;
     28 import android.telecom.RemoteConnection;
     29 import android.util.Log;
     30 
     31 import java.util.Collection;
     32 import java.util.Collections;
     33 import java.util.concurrent.CountDownLatch;
     34 
     35 /**
     36  * This is the official ConnectionService for Telecom's CTS App. Since telecom requires that a
     37  * CS be registered in the AndroidManifest.xml file, we have to have a single implementation
     38  * of a CS and this is it. To test specific CS behavior, tests will implement their own CS and
     39  * tell CtsConnectionService to forward any method invocations to that test's implementation.
     40  * This is set up using {@link #setUp} and should be cleaned up before the end of the test using
     41  * {@link #tearDown}.
     42  *
     43  * sConnectionService: Contains the connection service object provided by the current test in
     44  *                     progress. We use this object to forward any communication received from the
     45  *                     Telecom framework to the test connection service.
     46  * sTelecomConnectionService: Contains the connection service object registered to the Telecom
     47  *                            framework. We use this object to forward any communication from the
     48  *                            test connection service to the Telecom framework. After Telecom
     49  *                            binds to CtsConnectionService, this is set to be the instance of
     50  *                            CtsConnectionService created by the framework after Telecom binds.
     51  */
     52 public class CtsConnectionService extends ConnectionService {
     53     private static String LOG_TAG = "CtsConnectionService";
     54     // This is the connection service implemented by the test
     55     private static ConnectionService sConnectionService;
     56     // This is the connection service registered with Telecom
     57     private static ConnectionService sTelecomConnectionService;
     58     private static boolean sIsBound = false;
     59     private static CountDownLatch sServiceUnBoundLatch = new CountDownLatch(1);
     60 
     61     public CtsConnectionService() throws Exception {
     62         super();
     63         sTelecomConnectionService = this;
     64         sIsBound = true;
     65     }
     66 
     67     private static Object sLock = new Object();
     68 
     69     public static void setUp(ConnectionService connectionService) throws Exception {
     70         synchronized(sLock) {
     71             if (sConnectionService != null) {
     72                 throw new Exception("Mock ConnectionService exists.  Failed to call tearDown().");
     73             }
     74             sConnectionService = connectionService;
     75         }
     76     }
     77 
     78     public static void tearDown() {
     79         synchronized(sLock) {
     80             sTelecomConnectionService = null;
     81             sConnectionService = null;
     82         }
     83     }
     84 
     85     @Override
     86     public Connection onCreateOutgoingConnection(PhoneAccountHandle connectionManagerPhoneAccount,
     87             ConnectionRequest request) {
     88         synchronized(sLock) {
     89             if (sConnectionService != null) {
     90                 return sConnectionService.onCreateOutgoingConnection(
     91                         connectionManagerPhoneAccount, request);
     92             } else {
     93                 return null;
     94             }
     95         }
     96     }
     97 
     98     @Override
     99     public Connection onCreateIncomingConnection(PhoneAccountHandle connectionManagerPhoneAccount,
    100             ConnectionRequest request) {
    101         synchronized(sLock) {
    102             if (sConnectionService != null) {
    103                 return sConnectionService.onCreateIncomingConnection(
    104                         connectionManagerPhoneAccount, request);
    105             } else {
    106                 return null;
    107             }
    108         }
    109     }
    110 
    111     @Override
    112     public void onConference(Connection connection1, Connection connection2) {
    113         synchronized(sLock) {
    114             if (sConnectionService != null) {
    115                 sConnectionService.onConference(connection1, connection2);
    116             }
    117         }
    118     }
    119 
    120     @Override
    121     public void onRemoteExistingConnectionAdded(RemoteConnection connection) {
    122         synchronized(sLock) {
    123             if (sConnectionService != null) {
    124                 sConnectionService.onRemoteExistingConnectionAdded(connection);
    125             }
    126         }
    127     }
    128 
    129     public static void addConferenceToTelecom(Conference conference) {
    130         synchronized(sLock) {
    131             sTelecomConnectionService.addConference(conference);
    132         }
    133     }
    134 
    135     public static void addExistingConnectionToTelecom(
    136             PhoneAccountHandle phoneAccountHandle, Connection connection) {
    137         synchronized(sLock) {
    138             sTelecomConnectionService.addExistingConnection(phoneAccountHandle, connection);
    139         }
    140     }
    141 
    142     public static Collection<Connection> getAllConnectionsFromTelecom() {
    143         synchronized(sLock) {
    144             if (sTelecomConnectionService == null) {
    145                 return Collections.EMPTY_LIST;
    146             }
    147             return sTelecomConnectionService.getAllConnections();
    148         }
    149     }
    150 
    151     public static RemoteConnection createRemoteOutgoingConnectionToTelecom(
    152             PhoneAccountHandle connectionManagerPhoneAccount,
    153             ConnectionRequest request) {
    154         synchronized(sLock) {
    155             return sTelecomConnectionService.createRemoteOutgoingConnection(
    156                     connectionManagerPhoneAccount, request);
    157         }
    158     }
    159 
    160     public static RemoteConnection createRemoteIncomingConnectionToTelecom(
    161             PhoneAccountHandle connectionManagerPhoneAccount,
    162             ConnectionRequest request) {
    163         synchronized(sLock) {
    164             return sTelecomConnectionService.createRemoteIncomingConnection(
    165                     connectionManagerPhoneAccount, request);
    166         }
    167     }
    168 
    169     @Override
    170     public void onRemoteConferenceAdded(RemoteConference conference) {
    171         synchronized(sLock) {
    172             if (sConnectionService != null) {
    173                 sConnectionService.onRemoteConferenceAdded(conference);
    174             }
    175         }
    176     }
    177 
    178     @Override
    179     public void onConnectionServiceFocusGained() {
    180         synchronized (sLock) {
    181             if (sConnectionService != null) {
    182                 sConnectionService.onConnectionServiceFocusGained();
    183             }
    184         }
    185     }
    186 
    187     @Override
    188     public void onConnectionServiceFocusLost() {
    189         synchronized (sLock) {
    190             if (sConnectionService != null) {
    191                 sConnectionService.onConnectionServiceFocusLost();
    192             }
    193         }
    194     }
    195 
    196     @Override
    197     public boolean onUnbind(Intent intent) {
    198         Log.i(LOG_TAG, "Service has been unbound");
    199         sServiceUnBoundLatch.countDown();
    200         sIsBound = false;
    201         sConnectionService = null;
    202         return super.onUnbind(intent);
    203     }
    204 
    205     public static boolean isServiceRegisteredToTelecom() {
    206         return sTelecomConnectionService != null;
    207     }
    208 
    209     public static boolean isBound() {
    210         return sIsBound;
    211     }
    212 
    213     public static boolean waitForUnBinding() {
    214         return TestUtils.waitForLatchCountDown(sServiceUnBoundLatch);
    215     }
    216 }
    217