• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.telecom.cts.TestUtils.shouldTestTelecom;
20 
21 import android.net.Uri;
22 import android.os.Build;
23 import android.os.Bundle;
24 import android.telecom.Connection;
25 import android.telecom.DisconnectCause;
26 import android.telecom.StatusHints;
27 import android.telecom.TelecomManager;
28 import android.test.AndroidTestCase;
29 
30 import java.util.Arrays;
31 import java.util.List;
32 import java.util.concurrent.Semaphore;
33 import java.util.concurrent.TimeUnit;
34 
35 public class ConnectionTest extends AndroidTestCase {
36 
testStateCallbacks()37     public void testStateCallbacks() {
38         if (!shouldTestTelecom(getContext())) {
39             return;
40         }
41 
42         final Semaphore lock = new Semaphore(0);
43         Connection connection = createConnection(lock);
44 
45         waitForStateChange(lock);
46         assertEquals(Connection.STATE_NEW, connection.getState());
47 
48         connection.setInitializing();
49         waitForStateChange(lock);
50         assertEquals(Connection.STATE_INITIALIZING, connection.getState());
51 
52         connection.setInitialized();
53         waitForStateChange(lock);
54         assertEquals(Connection.STATE_NEW, connection.getState());
55 
56         connection.setRinging();
57         waitForStateChange(lock);
58         assertEquals(Connection.STATE_RINGING, connection.getState());
59 
60         connection.setDialing();
61         waitForStateChange(lock);
62         assertEquals(Connection.STATE_DIALING, connection.getState());
63 
64         connection.setActive();
65         waitForStateChange(lock);
66         assertEquals(Connection.STATE_ACTIVE, connection.getState());
67 
68         connection.setOnHold();
69         waitForStateChange(lock);
70         assertEquals(Connection.STATE_HOLDING, connection.getState());
71 
72         connection.setDisconnected(
73                 new DisconnectCause(DisconnectCause.LOCAL, "Test call"));
74         waitForStateChange(lock);
75         assertEquals(Connection.STATE_DISCONNECTED, connection.getState());
76 
77         connection.setRinging();
78         waitForStateChange(lock);
79         assertEquals("Connection should not move out of STATE_DISCONNECTED.",
80                 Connection.STATE_DISCONNECTED, connection.getState());
81     }
82 
83     /**
84      * {@link UnsupportedOperationException} is only thrown in L MR1+.
85      */
testFailedState()86     public void testFailedState() {
87         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
88             return;
89         }
90         Connection connection = Connection.createFailedConnection(
91                 new DisconnectCause(DisconnectCause.LOCAL, "Test call"));
92         assertEquals(Connection.STATE_DISCONNECTED, connection.getState());
93 
94         try {
95             connection.setRinging();
96         } catch (UnsupportedOperationException e) {
97             return;
98         }
99         fail("Connection should not move out of STATE_DISCONNECTED");
100     }
101 
102     /**
103      * {@link UnsupportedOperationException} is only thrown in L MR1+.
104      */
testCanceledState()105     public void testCanceledState() {
106         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
107             return;
108         }
109         Connection connection = Connection.createCanceledConnection();
110         assertEquals(Connection.STATE_DISCONNECTED, connection.getState());
111 
112         try {
113             connection.setDialing();
114         } catch (UnsupportedOperationException e) {
115             return;
116         }
117         fail("Connection should not move out of STATE_DISCONNECTED");
118     }
119 
testSetAndGetCallerDisplayName()120     public void testSetAndGetCallerDisplayName() {
121         if (!shouldTestTelecom(getContext())) {
122             return;
123         }
124 
125         final Semaphore lock = new Semaphore(0);
126         Connection connection = createConnection(lock);
127         waitForStateChange(lock);
128 
129         connection.setCallerDisplayName("Test User", TelecomManager.PRESENTATION_ALLOWED);
130         assertEquals("Test User", connection.getCallerDisplayName());
131         assertEquals(TelecomManager.PRESENTATION_ALLOWED,
132                 connection.getCallerDisplayNamePresentation());
133     }
134 
testSetAndGetAddress()135     public void testSetAndGetAddress() {
136         if (!shouldTestTelecom(getContext())) {
137             return;
138         }
139 
140         final Semaphore lock = new Semaphore(0);
141         Connection connection = createConnection(lock);
142         waitForStateChange(lock);
143 
144         final Uri address = Uri.fromParts("tel", "1234567", null);
145         connection.setAddress(address, TelecomManager.PRESENTATION_UNKNOWN);
146         assertEquals(address, connection.getAddress());
147         assertEquals(TelecomManager.PRESENTATION_UNKNOWN, connection.getAddressPresentation());
148     }
149 
testSetAndGetConnectionCapabilities()150     public void testSetAndGetConnectionCapabilities() {
151         if (!shouldTestTelecom(getContext())) {
152             return;
153         }
154 
155         final Semaphore lock = new Semaphore(0);
156         Connection connection = createConnection(lock);
157         waitForStateChange(lock);
158 
159         final int capabilities = Connection.CAPABILITY_HOLD | Connection.CAPABILITY_CAN_PAUSE_VIDEO
160                 | Connection.CAPABILITY_MANAGE_CONFERENCE | Connection.CAPABILITY_RESPOND_VIA_TEXT;
161 
162         connection.setConnectionCapabilities(capabilities);
163 
164         assertEquals(capabilities, connection.getConnectionCapabilities());
165     }
166 
testSetAndGetDisconnectCause()167     public void testSetAndGetDisconnectCause() {
168         if (!shouldTestTelecom(getContext())) {
169             return;
170         }
171 
172         final Semaphore lock = new Semaphore(0);
173         Connection connection = createConnection(lock);
174         waitForStateChange(lock);
175         assertEquals(Connection.STATE_NEW, connection.getState());
176 
177         final DisconnectCause disconnectCause = new DisconnectCause(DisconnectCause.REJECTED,
178                 "No friends", "No friends to talk to", "No friends to talk to");
179 
180         connection.setDisconnected(disconnectCause);
181 
182         assertEquals(Connection.STATE_DISCONNECTED, connection.getState());
183         assertEquals(disconnectCause, connection.getDisconnectCause());
184     }
185 
testSetAndGetAudioModeIsVoip()186     public void testSetAndGetAudioModeIsVoip() {
187         if (!shouldTestTelecom(getContext())) {
188             return;
189         }
190 
191         final Semaphore lock = new Semaphore(0);
192         Connection connection = createConnection(lock);
193         waitForStateChange(lock);
194 
195         assertFalse(connection.getAudioModeIsVoip());
196         connection.setAudioModeIsVoip(true);
197         assertTrue(connection.getAudioModeIsVoip());
198     }
199 
testSetAndGetExtras()200     public void testSetAndGetExtras() {
201         if (!shouldTestTelecom(getContext())) {
202             return;
203         }
204 
205         final Semaphore lock = new Semaphore(0);
206         Connection connection = createConnection(lock);
207         waitForStateChange(lock);
208 
209         assertEquals(null, connection.getExtras());
210 
211         final Bundle extras = new Bundle();
212         extras.putBoolean("test-extra-key", true);
213         connection.setExtras(extras);
214 
215         final Bundle retrieved = connection.getExtras();
216         assertNotNull(retrieved);
217         assertTrue(extras.getBoolean("test-extra-key"));
218     }
219 
testSetAndGetStatusHints()220     public void testSetAndGetStatusHints() {
221         if (!shouldTestTelecom(getContext())) {
222             return;
223         }
224 
225         final Semaphore lock = new Semaphore(0);
226         Connection connection = createConnection(lock);
227         waitForStateChange(lock);
228 
229         assertEquals(null, connection.getStatusHints());
230 
231         final StatusHints statusHints = new StatusHints("Test", null, null);
232         connection.setStatusHints(statusHints);
233         assertEquals(statusHints, connection.getStatusHints());
234     }
235 
testSetAndGetRingbackRequested()236     public void testSetAndGetRingbackRequested() {
237         if (!shouldTestTelecom(getContext())) {
238             return;
239         }
240 
241         final Semaphore lock = new Semaphore(0);
242         Connection connection = createConnection(lock);
243         waitForStateChange(lock);
244 
245         assertFalse(connection.isRingbackRequested());
246 
247         connection.setRingbackRequested(true);
248         assertTrue(connection.isRingbackRequested());
249     }
250 
testSetAndGetVideoProvider()251     public void testSetAndGetVideoProvider() {
252         if (!shouldTestTelecom(getContext())) {
253             return;
254         }
255 
256         final Semaphore lock = new Semaphore(0);
257         Connection connection = createConnection(lock);
258         waitForStateChange(lock);
259 
260         assertNull(connection.getVideoProvider());
261 
262         final Connection.VideoProvider videoProvider = new MockVideoProvider(null);
263         connection.setVideoProvider(videoProvider);
264         assertEquals(videoProvider, connection.getVideoProvider());
265     }
266 
testStateToString()267     public void testStateToString() {
268         if (!shouldTestTelecom(getContext())) {
269             return;
270         }
271 
272         assertEquals("INITIALIZING", Connection.stateToString(Connection.STATE_INITIALIZING));
273         assertEquals("NEW", Connection.stateToString(Connection.STATE_NEW));
274         assertEquals("RINGING", Connection.stateToString(Connection.STATE_RINGING));
275         assertEquals("DIALING", Connection.stateToString(Connection.STATE_DIALING));
276         assertEquals("ACTIVE", Connection.stateToString(Connection.STATE_ACTIVE));
277         assertEquals("HOLDING", Connection.stateToString(Connection.STATE_HOLDING));
278         assertEquals("DISCONNECTED", Connection.stateToString(Connection.STATE_DISCONNECTED));
279     }
280 
testCapabilitiesToString()281     public void testCapabilitiesToString() {
282         if (!shouldTestTelecom(getContext())) {
283             return;
284         }
285 
286         assertEquals("[Capabilities: CAPABILITY_HOLD]",
287                 Connection.capabilitiesToString(Connection.CAPABILITY_HOLD));
288         assertEquals("[Capabilities: CAPABILITY_MUTE]",
289                 Connection.capabilitiesToString(Connection.CAPABILITY_MUTE));
290         assertEquals("[Capabilities: CAPABILITY_HOLD "
291                 + "CAPABILITY_RESPOND_VIA_TEXT "
292                 + "CAPABILITY_MANAGE_CONFERENCE]",
293                 Connection.capabilitiesToString(Connection.CAPABILITY_HOLD
294                         | Connection.CAPABILITY_RESPOND_VIA_TEXT
295                         | Connection.CAPABILITY_MANAGE_CONFERENCE));
296     }
297 
createConnection(final Semaphore lock)298     private static Connection createConnection(final Semaphore lock) {
299         BasicConnection connection = new BasicConnection();
300         connection.setLock(lock);
301         return connection;
302     }
303 
waitForStateChange(Semaphore lock)304     private static void waitForStateChange(Semaphore lock) {
305         try {
306             lock.tryAcquire(1000, TimeUnit.MILLISECONDS);
307         } catch (InterruptedException e) {
308             fail("State transition timed out");
309         }
310     }
311 
312     private static final class BasicConnection extends Connection {
313         private Semaphore mLock;
314 
setLock(Semaphore lock)315         public void setLock(Semaphore lock) {
316             mLock = lock;
317         }
318 
319         @Override
onStateChanged(int state)320         public void onStateChanged(int state) {
321             mLock.release();
322         }
323     }
324 }
325