blob: 9a1ebc94c04b2eb47e11edc18564bf371174466e [file] [log] [blame]
/*
* Copyright (C) 2015 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 android.telecom.cts;
import static android.telecom.cts.TestUtils.shouldTestTelecom;
import android.os.Build;
import android.telecom.Connection;
import android.telecom.DisconnectCause;
import android.test.AndroidTestCase;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
public class ConnectionTest extends AndroidTestCase {
public void testStateCallbacks() {
if (!shouldTestTelecom(getContext())) {
return;
}
final Semaphore lock = new Semaphore(0);
Connection connection = createConnection(lock);
waitForStateChange(lock);
assertEquals(Connection.STATE_NEW, connection.getState());
connection.setInitializing();
waitForStateChange(lock);
assertEquals(Connection.STATE_INITIALIZING, connection.getState());
connection.setInitialized();
waitForStateChange(lock);
assertEquals(Connection.STATE_NEW, connection.getState());
connection.setRinging();
waitForStateChange(lock);
assertEquals(Connection.STATE_RINGING, connection.getState());
connection.setDialing();
waitForStateChange(lock);
assertEquals(Connection.STATE_DIALING, connection.getState());
connection.setActive();
waitForStateChange(lock);
assertEquals(Connection.STATE_ACTIVE, connection.getState());
connection.setOnHold();
waitForStateChange(lock);
assertEquals(Connection.STATE_HOLDING, connection.getState());
connection.setDisconnected(
new DisconnectCause(DisconnectCause.LOCAL, "Test call"));
waitForStateChange(lock);
assertEquals(Connection.STATE_DISCONNECTED, connection.getState());
connection.setRinging();
waitForStateChange(lock);
assertEquals("Connection should not move out of STATE_DISCONNECTED.",
Connection.STATE_DISCONNECTED, connection.getState());
}
/**
* {@link UnsupportedOperationException} is only thrown in L MR1+.
*/
public void testFailedState() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
return;
}
Connection connection = Connection.createFailedConnection(
new DisconnectCause(DisconnectCause.LOCAL, "Test call"));
assertEquals(Connection.STATE_DISCONNECTED, connection.getState());
try {
connection.setRinging();
} catch (UnsupportedOperationException e) {
return;
}
fail("Connection should not move out of STATE_DISCONNECTED");
}
/**
* {@link UnsupportedOperationException} is only thrown in L MR1+.
*/
public void testCanceledState() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
return;
}
Connection connection = Connection.createCanceledConnection();
assertEquals(Connection.STATE_DISCONNECTED, connection.getState());
try {
connection.setDialing();
} catch (UnsupportedOperationException e) {
return;
}
fail("Connection should not move out of STATE_DISCONNECTED");
}
private static Connection createConnection(final Semaphore lock) {
BasicConnection connection = new BasicConnection();
connection.setLock(lock);
return connection;
}
private static void waitForStateChange(Semaphore lock) {
try {
lock.tryAcquire(1000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
fail("State transition timed out");
}
}
private static final class BasicConnection extends Connection {
private Semaphore mLock;
public void setLock(Semaphore lock) {
mLock = lock;
}
@Override
public void onStateChanged(int state) {
mLock.release();
}
}
}