Skip to content
Snippets Groups Projects
Commit 5b0a4702 authored by Sungsoo Lim's avatar Sungsoo Lim
Browse files

Add more tests for CsipSetCoordinatorStateMachine

Bug: 237467631
Test: atest BluetoothInstrumentationTests
Change-Id: Idc41fa10bc50ece8d477185eb51da350a16e23da
parent 254a7779
No related branches found
No related tags found
No related merge requests found
......@@ -47,7 +47,7 @@ public class CsipSetCoordinatorStateMachine extends StateMachine {
static final int CONNECT = 1;
static final int DISCONNECT = 2;
@VisibleForTesting static final int STACK_EVENT = 101;
private static final int CONNECT_TIMEOUT = 201;
@VisibleForTesting static final int CONNECT_TIMEOUT = 201;
// NOTE: the value is not "final" - it is modified in the unit tests
@VisibleForTesting static int sConnectTimeoutMs = 30000; // 30s
......
......@@ -17,6 +17,11 @@
package com.android.bluetooth.csip;
import static android.bluetooth.BluetoothProfile.STATE_CONNECTED;
import static android.bluetooth.BluetoothProfile.STATE_CONNECTING;
import static android.bluetooth.BluetoothProfile.STATE_DISCONNECTED;
import static android.bluetooth.BluetoothProfile.STATE_DISCONNECTING;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
......@@ -25,9 +30,10 @@ import static org.mockito.Mockito.*;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.content.Intent;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.test.suitebuilder.annotation.MediumTest;
import androidx.test.InstrumentationRegistry;
......@@ -42,6 +48,7 @@ import org.junit.*;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
@MediumTest
......@@ -49,11 +56,10 @@ import org.mockito.MockitoAnnotations;
public class CsipSetCoordinatorStateMachineTest {
private final String mFlagDexmarker = System.getProperty("dexmaker.share_classloader", "false");
private Context mTargetContext;
private BluetoothAdapter mAdapter;
private BluetoothDevice mTestDevice;
private HandlerThread mHandlerThread;
private CsipSetCoordinatorStateMachine mStateMachine;
private CsipSetCoordinatorStateMachineWrapper mStateMachine;
private static final int TIMEOUT_MS = 1000;
@Mock private AdapterService mAdapterService;
......@@ -66,7 +72,6 @@ public class CsipSetCoordinatorStateMachineTest {
System.setProperty("dexmaker.share_classloader", "true");
}
mTargetContext = InstrumentationRegistry.getTargetContext();
// Set up mocks and test assets
MockitoAnnotations.initMocks(this);
TestUtils.setAdapterService(mAdapterService);
......@@ -79,8 +84,8 @@ public class CsipSetCoordinatorStateMachineTest {
// Set up thread and looper
mHandlerThread = new HandlerThread("CsipSetCoordinatorServiceTestHandlerThread");
mHandlerThread.start();
mStateMachine = new CsipSetCoordinatorStateMachine(
mTestDevice, mService, mNativeInterface, mHandlerThread.getLooper());
mStateMachine = spy(new CsipSetCoordinatorStateMachineWrapper(
mTestDevice, mService, mNativeInterface, mHandlerThread.getLooper()));
// Override the timeout value to speed up the test
CsipSetCoordinatorStateMachine.sConnectTimeoutMs = 1000;
......@@ -103,7 +108,7 @@ public class CsipSetCoordinatorStateMachineTest {
@Test
public void testDefaultDisconnectedState() {
Assert.assertEquals(
BluetoothProfile.STATE_DISCONNECTED, mStateMachine.getConnectionState());
STATE_DISCONNECTED, mStateMachine.getConnectionState());
}
/**
......@@ -155,7 +160,7 @@ public class CsipSetCoordinatorStateMachineTest {
ArgumentCaptor<Intent> intentArgument1 = ArgumentCaptor.forClass(Intent.class);
verify(mService, timeout(TIMEOUT_MS).times(1))
.sendBroadcast(intentArgument1.capture(), anyString());
Assert.assertEquals(BluetoothProfile.STATE_CONNECTING,
Assert.assertEquals(STATE_CONNECTING,
intentArgument1.getValue().getIntExtra(BluetoothProfile.EXTRA_STATE, -1));
// Check that we are in Connecting state
......@@ -196,7 +201,7 @@ public class CsipSetCoordinatorStateMachineTest {
ArgumentCaptor<Intent> intentArgument1 = ArgumentCaptor.forClass(Intent.class);
verify(mService, timeout(TIMEOUT_MS).times(1))
.sendBroadcast(intentArgument1.capture(), anyString());
Assert.assertEquals(BluetoothProfile.STATE_CONNECTING,
Assert.assertEquals(STATE_CONNECTING,
intentArgument1.getValue().getIntExtra(BluetoothProfile.EXTRA_STATE, -1));
// Check that we are in Connecting state
......@@ -207,7 +212,7 @@ public class CsipSetCoordinatorStateMachineTest {
ArgumentCaptor<Intent> intentArgument2 = ArgumentCaptor.forClass(Intent.class);
verify(mService, timeout(CsipSetCoordinatorStateMachine.sConnectTimeoutMs * 2).times(2))
.sendBroadcast(intentArgument2.capture(), anyString());
Assert.assertEquals(BluetoothProfile.STATE_DISCONNECTED,
Assert.assertEquals(STATE_DISCONNECTED,
intentArgument2.getValue().getIntExtra(BluetoothProfile.EXTRA_STATE, -1));
// Check that we are in Disconnected state
......@@ -236,7 +241,7 @@ public class CsipSetCoordinatorStateMachineTest {
ArgumentCaptor<Intent> intentArgument1 = ArgumentCaptor.forClass(Intent.class);
verify(mService, timeout(TIMEOUT_MS).times(1))
.sendBroadcast(intentArgument1.capture(), anyString());
Assert.assertEquals(BluetoothProfile.STATE_CONNECTING,
Assert.assertEquals(STATE_CONNECTING,
intentArgument1.getValue().getIntExtra(BluetoothProfile.EXTRA_STATE, -1));
// Check that we are in Connecting state
......@@ -247,7 +252,7 @@ public class CsipSetCoordinatorStateMachineTest {
ArgumentCaptor<Intent> intentArgument2 = ArgumentCaptor.forClass(Intent.class);
verify(mService, timeout(CsipSetCoordinatorStateMachine.sConnectTimeoutMs * 2).times(2))
.sendBroadcast(intentArgument2.capture(), anyString());
Assert.assertEquals(BluetoothProfile.STATE_DISCONNECTED,
Assert.assertEquals(STATE_DISCONNECTED,
intentArgument2.getValue().getIntExtra(BluetoothProfile.EXTRA_STATE, -1));
// Check that we are in Disconnected state
......@@ -255,4 +260,427 @@ public class CsipSetCoordinatorStateMachineTest {
IsInstanceOf.instanceOf(CsipSetCoordinatorStateMachine.Disconnected.class));
verify(mNativeInterface).disconnect(eq(mTestDevice));
}
@Test
public void testGetDevice() {
Assert.assertEquals(mTestDevice, mStateMachine.getDevice());
}
@Test
public void testIsConnected() {
Assert.assertFalse(mStateMachine.isConnected());
initToConnectedState();
Assert.assertTrue(mStateMachine.isConnected());
}
@Test
public void testDumpDoesNotCrash() {
mStateMachine.dump(new StringBuilder());
}
@Test
public void testProcessDisconnectMessage_onDisconnectedState() {
mStateMachine.sendMessage(CsipSetCoordinatorStateMachine.DISCONNECT);
TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
Assert.assertEquals(STATE_DISCONNECTED, mStateMachine.getConnectionState());
}
@Test
public void testProcessConnectMessage_onDisconnectedState() {
allowConnection(false);
mStateMachine.sendMessage(CsipSetCoordinatorStateMachine.CONNECT);
TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
Assert.assertEquals(STATE_DISCONNECTED, mStateMachine.getConnectionState());
allowConnection(false);
mStateMachine.sendMessage(CsipSetCoordinatorStateMachine.CONNECT);
TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
Assert.assertEquals(STATE_DISCONNECTED, mStateMachine.getConnectionState());
allowConnection(true);
doReturn(true).when(mNativeInterface).connect(any(BluetoothDevice.class));
sendMessageAndVerifyTransition(
mStateMachine.obtainMessage(CsipSetCoordinatorStateMachine.CONNECT),
CsipSetCoordinatorStateMachine.Connecting.class);
}
@Test
public void testStackEvent_withoutStateChange_onDisconnectedState() {
allowConnection(false);
CsipSetCoordinatorStackEvent event = new CsipSetCoordinatorStackEvent(-1);
mStateMachine.sendMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event);
TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
Assert.assertEquals(STATE_DISCONNECTED, mStateMachine.getConnectionState());
event = new CsipSetCoordinatorStackEvent(
CsipSetCoordinatorStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
event.valueInt1 = CsipSetCoordinatorStackEvent.CONNECTION_STATE_DISCONNECTED;
mStateMachine.sendMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event);
TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
Assert.assertEquals(STATE_DISCONNECTED, mStateMachine.getConnectionState());
event = new CsipSetCoordinatorStackEvent(
CsipSetCoordinatorStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
event.valueInt1 = CsipSetCoordinatorStackEvent.CONNECTION_STATE_CONNECTING;
mStateMachine.sendMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event);
TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
Assert.assertEquals(STATE_DISCONNECTED, mStateMachine.getConnectionState());
verify(mNativeInterface).disconnect(mTestDevice);
Mockito.clearInvocations(mNativeInterface);
event = new CsipSetCoordinatorStackEvent(
CsipSetCoordinatorStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
event.valueInt1 = CsipSetCoordinatorStackEvent.CONNECTION_STATE_CONNECTED;
mStateMachine.sendMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event);
TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
Assert.assertEquals(STATE_DISCONNECTED, mStateMachine.getConnectionState());
verify(mNativeInterface).disconnect(mTestDevice);
event = new CsipSetCoordinatorStackEvent(
CsipSetCoordinatorStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
event.valueInt1 = CsipSetCoordinatorStackEvent.CONNECTION_STATE_DISCONNECTING;
mStateMachine.sendMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event);
TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
Assert.assertEquals(STATE_DISCONNECTED, mStateMachine.getConnectionState());
event = new CsipSetCoordinatorStackEvent(
CsipSetCoordinatorStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
event.valueInt1 = -1;
mStateMachine.sendMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event);
TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
Assert.assertEquals(STATE_DISCONNECTED, mStateMachine.getConnectionState());
}
@Test
public void testStackEvent_toConnectingState_onDisconnectedState() {
allowConnection(true);
CsipSetCoordinatorStackEvent event = new CsipSetCoordinatorStackEvent(
CsipSetCoordinatorStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
event.valueInt1 = CsipSetCoordinatorStackEvent.CONNECTION_STATE_CONNECTING;
sendMessageAndVerifyTransition(
mStateMachine.obtainMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event),
CsipSetCoordinatorStateMachine.Connecting.class);
}
@Test
public void testStackEvent_toConnectedState_onDisconnectedState() {
allowConnection(true);
CsipSetCoordinatorStackEvent event = new CsipSetCoordinatorStackEvent(
CsipSetCoordinatorStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
event.valueInt1 = CsipSetCoordinatorStackEvent.CONNECTION_STATE_CONNECTED;
sendMessageAndVerifyTransition(
mStateMachine.obtainMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event),
CsipSetCoordinatorStateMachine.Connected.class);
}
@Test
public void testProcessConnectMessage_onConnectingState() {
initToConnectingState();
mStateMachine.sendMessage(CsipSetCoordinatorStateMachine.CONNECT);
TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
Assert.assertTrue(mStateMachine.doesSuperHaveDeferredMessages(
CsipSetCoordinatorStateMachine.CONNECT));
}
@Test
public void testProcessConnectTimeoutMessage_onConnectingState() {
initToConnectingState();
Message msg = mStateMachine.obtainMessage(CsipSetCoordinatorStateMachine.CONNECT_TIMEOUT);
sendMessageAndVerifyTransition(msg, CsipSetCoordinatorStateMachine.Disconnected.class);
}
@Test
public void testProcessDisconnectMessage_onConnectingState() {
initToConnectingState();
Message msg = mStateMachine.obtainMessage(CsipSetCoordinatorStateMachine.DISCONNECT);
sendMessageAndVerifyTransition(msg, CsipSetCoordinatorStateMachine.Disconnected.class);
}
@Test
public void testStackEvent_withoutStateChange_onConnectingState() {
initToConnectingState();
CsipSetCoordinatorStackEvent event = new CsipSetCoordinatorStackEvent(-1);
mStateMachine.sendMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event);
TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
Assert.assertEquals(STATE_CONNECTING, mStateMachine.getConnectionState());
event = new CsipSetCoordinatorStackEvent(
CsipSetCoordinatorStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
event.valueInt1 = CsipSetCoordinatorStackEvent.CONNECTION_STATE_CONNECTING;
mStateMachine.sendMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event);
TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
Assert.assertEquals(STATE_CONNECTING, mStateMachine.getConnectionState());
event = new CsipSetCoordinatorStackEvent(
CsipSetCoordinatorStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
event.valueInt1 = 10000;
mStateMachine.sendMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event);
TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
Assert.assertEquals(STATE_CONNECTING, mStateMachine.getConnectionState());
}
@Test
public void testStackEvent_toDisconnectedState_onConnectingState() {
initToConnectingState();
CsipSetCoordinatorStackEvent event = new CsipSetCoordinatorStackEvent(
CsipSetCoordinatorStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
event.valueInt1 = CsipSetCoordinatorStackEvent.CONNECTION_STATE_DISCONNECTED;
sendMessageAndVerifyTransition(
mStateMachine.obtainMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event),
CsipSetCoordinatorStateMachine.Disconnected.class);
}
@Test
public void testStackEvent_toConnectedState_onConnectingState() {
initToConnectingState();
CsipSetCoordinatorStackEvent event = new CsipSetCoordinatorStackEvent(
CsipSetCoordinatorStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
event.valueInt1 = CsipSetCoordinatorStackEvent.CONNECTION_STATE_CONNECTED;
sendMessageAndVerifyTransition(
mStateMachine.obtainMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event),
CsipSetCoordinatorStateMachine.Connected.class);
}
@Test
public void testStackEvent_toDisconnectingState_onConnectingState() {
initToConnectingState();
CsipSetCoordinatorStackEvent event = new CsipSetCoordinatorStackEvent(
CsipSetCoordinatorStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
event.valueInt1 = CsipSetCoordinatorStackEvent.CONNECTION_STATE_DISCONNECTING;
sendMessageAndVerifyTransition(
mStateMachine.obtainMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event),
CsipSetCoordinatorStateMachine.Disconnecting.class);
}
@Test
public void testProcessConnectMessage_onConnectedState() {
initToConnectedState();
mStateMachine.sendMessage(CsipSetCoordinatorStateMachine.CONNECT);
TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
Assert.assertEquals(STATE_CONNECTED, mStateMachine.getConnectionState());
}
@Test
public void testProcessDisconnectMessage_onConnectedState() {
initToConnectedState();
doReturn(true).when(mNativeInterface).disconnect(any(BluetoothDevice.class));
sendMessageAndVerifyTransition(
mStateMachine.obtainMessage(CsipSetCoordinatorStateMachine.DISCONNECT),
CsipSetCoordinatorStateMachine.Disconnecting.class);
}
@Test
public void testProcessDisconnectMessage_onConnectedState_withNativeError() {
initToConnectedState();
doReturn(false).when(mNativeInterface).disconnect(any(BluetoothDevice.class));
sendMessageAndVerifyTransition(
mStateMachine.obtainMessage(CsipSetCoordinatorStateMachine.DISCONNECT),
CsipSetCoordinatorStateMachine.Disconnected.class);
}
@Test
public void testStackEvent_withoutStateChange_onConnectedState() {
initToConnectedState();
CsipSetCoordinatorStackEvent event = new CsipSetCoordinatorStackEvent(-1);
mStateMachine.sendMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event);
TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
Assert.assertEquals(STATE_CONNECTED, mStateMachine.getConnectionState());
event = new CsipSetCoordinatorStackEvent(
CsipSetCoordinatorStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
event.valueInt1 = CsipSetCoordinatorStackEvent.CONNECTION_STATE_CONNECTING;
mStateMachine.sendMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event);
TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
Assert.assertEquals(STATE_CONNECTED, mStateMachine.getConnectionState());
}
@Test
public void testStackEvent_toDisconnectedState_onConnectedState() {
initToConnectedState();
CsipSetCoordinatorStackEvent event = new CsipSetCoordinatorStackEvent(
CsipSetCoordinatorStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
event.valueInt1 = CsipSetCoordinatorStackEvent.CONNECTION_STATE_DISCONNECTED;
sendMessageAndVerifyTransition(
mStateMachine.obtainMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event),
CsipSetCoordinatorStateMachine.Disconnected.class);
}
@Test
public void testStackEvent_toDisconnectingState_onConnectedState() {
initToConnectedState();
CsipSetCoordinatorStackEvent event = new CsipSetCoordinatorStackEvent(
CsipSetCoordinatorStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
event.valueInt1 = CsipSetCoordinatorStackEvent.CONNECTION_STATE_DISCONNECTING;
sendMessageAndVerifyTransition(
mStateMachine.obtainMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event),
CsipSetCoordinatorStateMachine.Disconnecting.class);
}
@Test
public void testProcessConnectMessage_onDisconnectingState() {
initToDisconnectingState();
Message msg = mStateMachine.obtainMessage(CsipSetCoordinatorStateMachine.CONNECT);
mStateMachine.sendMessage(msg);
TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
verify(mStateMachine).deferMessage(msg);
}
@Test
public void testProcessConnectTimeoutMessage_onDisconnectingState() {
initToConnectingState();
Message msg = mStateMachine.obtainMessage(CsipSetCoordinatorStateMachine.CONNECT_TIMEOUT);
sendMessageAndVerifyTransition(msg, CsipSetCoordinatorStateMachine.Disconnected.class);
}
@Test
public void testProcessDisconnectMessage_onDisconnectingState() {
initToDisconnectingState();
Message msg = mStateMachine.obtainMessage(CsipSetCoordinatorStateMachine.DISCONNECT);
mStateMachine.sendMessage(msg);
TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
verify(mStateMachine).deferMessage(msg);
}
@Test
public void testStackEvent_withoutStateChange_onDisconnectingState() {
initToDisconnectingState();
CsipSetCoordinatorStackEvent event = new CsipSetCoordinatorStackEvent(-1);
mStateMachine.sendMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event);
TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
Assert.assertEquals(STATE_DISCONNECTING, mStateMachine.getConnectionState());
allowConnection(false);
event = new CsipSetCoordinatorStackEvent(
CsipSetCoordinatorStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
event.valueInt1 = CsipSetCoordinatorStackEvent.CONNECTION_STATE_CONNECTED;
mStateMachine.sendMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event);
TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
verify(mNativeInterface).disconnect(any());
Mockito.clearInvocations(mNativeInterface);
event = new CsipSetCoordinatorStackEvent(
CsipSetCoordinatorStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
event.valueInt1 = CsipSetCoordinatorStackEvent.CONNECTION_STATE_CONNECTING;
mStateMachine.sendMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event);
TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
verify(mNativeInterface).disconnect(any());
event = new CsipSetCoordinatorStackEvent(
CsipSetCoordinatorStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
event.valueInt1 = CsipSetCoordinatorStackEvent.CONNECTION_STATE_DISCONNECTING;
mStateMachine.sendMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event);
TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
Assert.assertEquals(STATE_DISCONNECTING, mStateMachine.getConnectionState());
event = new CsipSetCoordinatorStackEvent(
CsipSetCoordinatorStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
event.valueInt1 = 10000;
mStateMachine.sendMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event);
TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
Assert.assertEquals(STATE_DISCONNECTING, mStateMachine.getConnectionState());
}
@Test
public void testStackEvent_toConnectedState_onDisconnectingState() {
initToDisconnectingState();
allowConnection(true);
CsipSetCoordinatorStackEvent event = new CsipSetCoordinatorStackEvent(
CsipSetCoordinatorStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
event.valueInt1 = CsipSetCoordinatorStackEvent.CONNECTION_STATE_CONNECTED;
sendMessageAndVerifyTransition(
mStateMachine.obtainMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event),
CsipSetCoordinatorStateMachine.Connected.class);
}
@Test
public void testStackEvent_toConnectedState_butNotAllowed_onDisconnectingState() {
initToDisconnectingState();
allowConnection(false);
CsipSetCoordinatorStackEvent event = new CsipSetCoordinatorStackEvent(
CsipSetCoordinatorStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
event.valueInt1 = CsipSetCoordinatorStackEvent.CONNECTION_STATE_CONNECTED;
mStateMachine.sendMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event);
TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
verify(mNativeInterface).disconnect(any());
}
@Test
public void testStackEvent_toConnectingState_onDisconnectingState() {
initToDisconnectingState();
allowConnection(true);
CsipSetCoordinatorStackEvent event = new CsipSetCoordinatorStackEvent(
CsipSetCoordinatorStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
event.valueInt1 = CsipSetCoordinatorStackEvent.CONNECTION_STATE_CONNECTING;
sendMessageAndVerifyTransition(
mStateMachine.obtainMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event),
CsipSetCoordinatorStateMachine.Connecting.class);
}
@Test
public void testStackEvent_toConnectingState_butNotAllowed_onDisconnectingState() {
initToDisconnectingState();
allowConnection(false);
CsipSetCoordinatorStackEvent event = new CsipSetCoordinatorStackEvent(
CsipSetCoordinatorStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
event.valueInt1 = CsipSetCoordinatorStackEvent.CONNECTION_STATE_CONNECTED;
mStateMachine.sendMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event);
TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
verify(mNativeInterface).disconnect(any());
}
private void initToConnectingState() {
allowConnection(true);
CsipSetCoordinatorStackEvent event = new CsipSetCoordinatorStackEvent(
CsipSetCoordinatorStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
event.valueInt1 = CsipSetCoordinatorStackEvent.CONNECTION_STATE_CONNECTING;
sendMessageAndVerifyTransition(
mStateMachine.obtainMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event),
CsipSetCoordinatorStateMachine.Connecting.class);
allowConnection(false);
}
private void initToConnectedState() {
allowConnection(true);
CsipSetCoordinatorStackEvent event = new CsipSetCoordinatorStackEvent(
CsipSetCoordinatorStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
event.valueInt1 = CsipSetCoordinatorStackEvent.CONNECTION_STATE_CONNECTED;
sendMessageAndVerifyTransition(
mStateMachine.obtainMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event),
CsipSetCoordinatorStateMachine.Connected.class);
allowConnection(false);
}
private void initToDisconnectingState() {
initToConnectingState();
CsipSetCoordinatorStackEvent event = new CsipSetCoordinatorStackEvent(
CsipSetCoordinatorStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED);
event.valueInt1 = CsipSetCoordinatorStackEvent.CONNECTION_STATE_DISCONNECTING;
sendMessageAndVerifyTransition(
mStateMachine.obtainMessage(CsipSetCoordinatorStateMachine.STACK_EVENT, event),
CsipSetCoordinatorStateMachine.Disconnecting.class);
}
private <T> void sendMessageAndVerifyTransition(Message msg, Class<T> type) {
Mockito.clearInvocations(mService);
mStateMachine.sendMessage(msg);
// Verify that one connection state broadcast is executed
verify(mService, timeout(TIMEOUT_MS)).sendBroadcast(any(Intent.class), anyString());
Assert.assertThat(mStateMachine.getCurrentState(), IsInstanceOf.instanceOf(type));
}
public static class CsipSetCoordinatorStateMachineWrapper
extends CsipSetCoordinatorStateMachine {
CsipSetCoordinatorStateMachineWrapper(BluetoothDevice device,
CsipSetCoordinatorService svc,
CsipSetCoordinatorNativeInterface nativeInterface, Looper looper) {
super(device, svc, nativeInterface, looper);
}
public boolean doesSuperHaveDeferredMessages(int what) {
return super.hasDeferredMessages(what);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment