Loading android/app/src/com/android/bluetooth/vc/VolumeControlStateMachine.java +2 −1 Original line number Diff line number Diff line Loading @@ -46,7 +46,8 @@ public class VolumeControlStateMachine extends StateMachine { 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 Loading android/app/tests/unit/src/com/android/bluetooth/vc/VolumeControlStateMachineTest.java +142 −5 Original line number Diff line number Diff line Loading @@ -17,7 +17,13 @@ package com.android.bluetooth.vc; import static org.mockito.Mockito.*; import static org.mockito.Mockito.after; import static org.mockito.Mockito.any; import static org.mockito.Mockito.anyString; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.eq; import static org.mockito.Mockito.timeout; import static org.mockito.Mockito.verify; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; Loading @@ -25,24 +31,24 @@ import android.bluetooth.BluetoothProfile; import android.content.Context; import android.content.Intent; import android.os.HandlerThread; import android.os.Message; import androidx.test.InstrumentationRegistry; import androidx.test.filters.MediumTest; import androidx.test.runner.AndroidJUnit4; import com.android.bluetooth.btservice.AdapterService; import com.android.bluetooth.R; import com.android.bluetooth.TestUtils; import com.android.bluetooth.btservice.AdapterService; import org.hamcrest.core.IsInstanceOf; import org.junit.After; import org.junit.Assert; import org.junit.Assume; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; @MediumTest Loading Loading @@ -254,4 +260,135 @@ public class VolumeControlStateMachineTest { IsInstanceOf.instanceOf(VolumeControlStateMachine.Disconnected.class)); verify(mVolumeControlNativeInterface).disconnectVolumeControl(eq(mTestDevice)); } @Test public void testStatesChangesWithMessages() { allowConnection(true); doReturn(true).when(mVolumeControlNativeInterface).connectVolumeControl(any( BluetoothDevice.class)); doReturn(true).when(mVolumeControlNativeInterface).disconnectVolumeControl(any( BluetoothDevice.class)); // Check that we are in Disconnected state Assert.assertThat(mVolumeControlStateMachine.getCurrentState(), IsInstanceOf.instanceOf(VolumeControlStateMachine.Disconnected.class)); mVolumeControlStateMachine.sendMessage(mVolumeControlStateMachine.DISCONNECT); // Check that we are in Disconnected state Assert.assertThat(mVolumeControlStateMachine.getCurrentState(), IsInstanceOf.instanceOf(VolumeControlStateMachine.Disconnected.class)); // disconnected -> connecting sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage(mVolumeControlStateMachine.CONNECT), VolumeControlStateMachine.Connecting.class); // connecting -> disconnected sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage(VolumeControlStateMachine.CONNECT_TIMEOUT), VolumeControlStateMachine.Disconnected.class); // disconnected -> connecting VolumeControlStackEvent stackEvent = new VolumeControlStackEvent( VolumeControlStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED); stackEvent.device = mTestDevice; stackEvent.valueInt1 = VolumeControlStackEvent.CONNECTION_STATE_CONNECTING; sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage( mVolumeControlStateMachine.STACK_EVENT, stackEvent), VolumeControlStateMachine.Connecting.class); // connecting -> disconnected sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage(mVolumeControlStateMachine.DISCONNECT), VolumeControlStateMachine.Disconnected.class); // disconnected -> connecting sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage(mVolumeControlStateMachine.CONNECT), VolumeControlStateMachine.Connecting.class); // connecting -> disconnecting stackEvent = new VolumeControlStackEvent( VolumeControlStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED); stackEvent.device = mTestDevice; stackEvent.valueInt1 = VolumeControlStackEvent.CONNECTION_STATE_DISCONNECTING; sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage( mVolumeControlStateMachine.STACK_EVENT, stackEvent), VolumeControlStateMachine.Disconnecting.class); // disconnecting -> connecting stackEvent = new VolumeControlStackEvent( VolumeControlStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED); stackEvent.device = mTestDevice; stackEvent.valueInt1 = VolumeControlStackEvent.CONNECTION_STATE_CONNECTING; sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage( mVolumeControlStateMachine.STACK_EVENT, stackEvent), VolumeControlStateMachine.Connecting.class); // connecting -> connected stackEvent = new VolumeControlStackEvent( VolumeControlStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED); stackEvent.device = mTestDevice; stackEvent.valueInt1 = VolumeControlStackEvent.CONNECTION_STATE_CONNECTED; sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage( mVolumeControlStateMachine.STACK_EVENT, stackEvent), VolumeControlStateMachine.Connected.class); // connected -> disconnecting stackEvent = new VolumeControlStackEvent( VolumeControlStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED); stackEvent.device = mTestDevice; stackEvent.valueInt1 = VolumeControlStackEvent.CONNECTION_STATE_DISCONNECTING; sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage( mVolumeControlStateMachine.STACK_EVENT, stackEvent), VolumeControlStateMachine.Disconnecting.class); // disconnecting -> disconnected sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage(VolumeControlStateMachine.CONNECT_TIMEOUT), VolumeControlStateMachine.Disconnected.class); // disconnected -> connected stackEvent = new VolumeControlStackEvent( VolumeControlStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED); stackEvent.device = mTestDevice; stackEvent.valueInt1 = VolumeControlStackEvent.CONNECTION_STATE_CONNECTED; sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage( mVolumeControlStateMachine.STACK_EVENT, stackEvent), VolumeControlStateMachine.Connected.class); // connected -> disconnected sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage( mVolumeControlStateMachine.DISCONNECT), VolumeControlStateMachine.Disconnecting.class); // disconnecting -> connected stackEvent = new VolumeControlStackEvent( VolumeControlStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED); stackEvent.device = mTestDevice; stackEvent.valueInt1 = VolumeControlStackEvent.CONNECTION_STATE_CONNECTED; sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage( mVolumeControlStateMachine.STACK_EVENT, stackEvent), VolumeControlStateMachine.Connected.class); // connected -> disconnected stackEvent = new VolumeControlStackEvent( VolumeControlStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED); stackEvent.device = mTestDevice; stackEvent.valueInt1 = VolumeControlStackEvent.CONNECTION_STATE_DISCONNECTED; sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage( VolumeControlStateMachine.STACK_EVENT, stackEvent), VolumeControlStateMachine.Disconnected.class); } private <T> void sendMessageAndVerifyTransition(Message msg, Class<T> type) { Mockito.clearInvocations(mVolumeControlService); mVolumeControlStateMachine.sendMessage(msg); // Verify that one connection state broadcast is executed verify(mVolumeControlService, timeout(TIMEOUT_MS).times(1)).sendBroadcast( any(Intent.class), anyString()); Assert.assertThat(mVolumeControlStateMachine.getCurrentState(), IsInstanceOf.instanceOf(type)); } } Loading
android/app/src/com/android/bluetooth/vc/VolumeControlStateMachine.java +2 −1 Original line number Diff line number Diff line Loading @@ -46,7 +46,8 @@ public class VolumeControlStateMachine extends StateMachine { 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 Loading
android/app/tests/unit/src/com/android/bluetooth/vc/VolumeControlStateMachineTest.java +142 −5 Original line number Diff line number Diff line Loading @@ -17,7 +17,13 @@ package com.android.bluetooth.vc; import static org.mockito.Mockito.*; import static org.mockito.Mockito.after; import static org.mockito.Mockito.any; import static org.mockito.Mockito.anyString; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.eq; import static org.mockito.Mockito.timeout; import static org.mockito.Mockito.verify; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; Loading @@ -25,24 +31,24 @@ import android.bluetooth.BluetoothProfile; import android.content.Context; import android.content.Intent; import android.os.HandlerThread; import android.os.Message; import androidx.test.InstrumentationRegistry; import androidx.test.filters.MediumTest; import androidx.test.runner.AndroidJUnit4; import com.android.bluetooth.btservice.AdapterService; import com.android.bluetooth.R; import com.android.bluetooth.TestUtils; import com.android.bluetooth.btservice.AdapterService; import org.hamcrest.core.IsInstanceOf; import org.junit.After; import org.junit.Assert; import org.junit.Assume; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; @MediumTest Loading Loading @@ -254,4 +260,135 @@ public class VolumeControlStateMachineTest { IsInstanceOf.instanceOf(VolumeControlStateMachine.Disconnected.class)); verify(mVolumeControlNativeInterface).disconnectVolumeControl(eq(mTestDevice)); } @Test public void testStatesChangesWithMessages() { allowConnection(true); doReturn(true).when(mVolumeControlNativeInterface).connectVolumeControl(any( BluetoothDevice.class)); doReturn(true).when(mVolumeControlNativeInterface).disconnectVolumeControl(any( BluetoothDevice.class)); // Check that we are in Disconnected state Assert.assertThat(mVolumeControlStateMachine.getCurrentState(), IsInstanceOf.instanceOf(VolumeControlStateMachine.Disconnected.class)); mVolumeControlStateMachine.sendMessage(mVolumeControlStateMachine.DISCONNECT); // Check that we are in Disconnected state Assert.assertThat(mVolumeControlStateMachine.getCurrentState(), IsInstanceOf.instanceOf(VolumeControlStateMachine.Disconnected.class)); // disconnected -> connecting sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage(mVolumeControlStateMachine.CONNECT), VolumeControlStateMachine.Connecting.class); // connecting -> disconnected sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage(VolumeControlStateMachine.CONNECT_TIMEOUT), VolumeControlStateMachine.Disconnected.class); // disconnected -> connecting VolumeControlStackEvent stackEvent = new VolumeControlStackEvent( VolumeControlStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED); stackEvent.device = mTestDevice; stackEvent.valueInt1 = VolumeControlStackEvent.CONNECTION_STATE_CONNECTING; sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage( mVolumeControlStateMachine.STACK_EVENT, stackEvent), VolumeControlStateMachine.Connecting.class); // connecting -> disconnected sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage(mVolumeControlStateMachine.DISCONNECT), VolumeControlStateMachine.Disconnected.class); // disconnected -> connecting sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage(mVolumeControlStateMachine.CONNECT), VolumeControlStateMachine.Connecting.class); // connecting -> disconnecting stackEvent = new VolumeControlStackEvent( VolumeControlStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED); stackEvent.device = mTestDevice; stackEvent.valueInt1 = VolumeControlStackEvent.CONNECTION_STATE_DISCONNECTING; sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage( mVolumeControlStateMachine.STACK_EVENT, stackEvent), VolumeControlStateMachine.Disconnecting.class); // disconnecting -> connecting stackEvent = new VolumeControlStackEvent( VolumeControlStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED); stackEvent.device = mTestDevice; stackEvent.valueInt1 = VolumeControlStackEvent.CONNECTION_STATE_CONNECTING; sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage( mVolumeControlStateMachine.STACK_EVENT, stackEvent), VolumeControlStateMachine.Connecting.class); // connecting -> connected stackEvent = new VolumeControlStackEvent( VolumeControlStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED); stackEvent.device = mTestDevice; stackEvent.valueInt1 = VolumeControlStackEvent.CONNECTION_STATE_CONNECTED; sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage( mVolumeControlStateMachine.STACK_EVENT, stackEvent), VolumeControlStateMachine.Connected.class); // connected -> disconnecting stackEvent = new VolumeControlStackEvent( VolumeControlStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED); stackEvent.device = mTestDevice; stackEvent.valueInt1 = VolumeControlStackEvent.CONNECTION_STATE_DISCONNECTING; sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage( mVolumeControlStateMachine.STACK_EVENT, stackEvent), VolumeControlStateMachine.Disconnecting.class); // disconnecting -> disconnected sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage(VolumeControlStateMachine.CONNECT_TIMEOUT), VolumeControlStateMachine.Disconnected.class); // disconnected -> connected stackEvent = new VolumeControlStackEvent( VolumeControlStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED); stackEvent.device = mTestDevice; stackEvent.valueInt1 = VolumeControlStackEvent.CONNECTION_STATE_CONNECTED; sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage( mVolumeControlStateMachine.STACK_EVENT, stackEvent), VolumeControlStateMachine.Connected.class); // connected -> disconnected sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage( mVolumeControlStateMachine.DISCONNECT), VolumeControlStateMachine.Disconnecting.class); // disconnecting -> connected stackEvent = new VolumeControlStackEvent( VolumeControlStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED); stackEvent.device = mTestDevice; stackEvent.valueInt1 = VolumeControlStackEvent.CONNECTION_STATE_CONNECTED; sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage( mVolumeControlStateMachine.STACK_EVENT, stackEvent), VolumeControlStateMachine.Connected.class); // connected -> disconnected stackEvent = new VolumeControlStackEvent( VolumeControlStackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED); stackEvent.device = mTestDevice; stackEvent.valueInt1 = VolumeControlStackEvent.CONNECTION_STATE_DISCONNECTED; sendMessageAndVerifyTransition( mVolumeControlStateMachine.obtainMessage( VolumeControlStateMachine.STACK_EVENT, stackEvent), VolumeControlStateMachine.Disconnected.class); } private <T> void sendMessageAndVerifyTransition(Message msg, Class<T> type) { Mockito.clearInvocations(mVolumeControlService); mVolumeControlStateMachine.sendMessage(msg); // Verify that one connection state broadcast is executed verify(mVolumeControlService, timeout(TIMEOUT_MS).times(1)).sendBroadcast( any(Intent.class), anyString()); Assert.assertThat(mVolumeControlStateMachine.getCurrentState(), IsInstanceOf.instanceOf(type)); } }