Add test for BassClientStateMachine#Disconnected

Bug: 237467631
Test: atest BluetoothInstrumentationTests:BassClientStateMachineTest
Change-Id: Id31e13a4b9f5c8182f7247e4f524ebf9b3241b6a
Merged-In: Id31e13a4b9f5c8182f7247e4f524ebf9b3241b6a
(cherry picked from commit 82673798ecea84bc88cd4819e84f1c81aca0e78f)
diff --git a/android/app/tests/unit/src/com/android/bluetooth/bass_client/BassClientStateMachineTest.java b/android/app/tests/unit/src/com/android/bluetooth/bass_client/BassClientStateMachineTest.java
index 5647f47..f8c7aa5 100644
--- a/android/app/tests/unit/src/com/android/bluetooth/bass_client/BassClientStateMachineTest.java
+++ b/android/app/tests/unit/src/com/android/bluetooth/bass_client/BassClientStateMachineTest.java
@@ -19,7 +19,10 @@
 import static android.bluetooth.BluetoothGatt.GATT_SUCCESS;
 
 import static com.android.bluetooth.bass_client.BassClientStateMachine.CONNECT;
+import static com.android.bluetooth.bass_client.BassClientStateMachine.CONNECTION_STATE_CHANGED;
 import static com.android.bluetooth.bass_client.BassClientStateMachine.CONNECT_TIMEOUT;
+import static com.android.bluetooth.bass_client.BassClientStateMachine.DISCONNECT;
+import static com.android.bluetooth.bass_client.BassClientStateMachine.PSYNC_ACTIVE_TIMEOUT;
 
 import static com.google.common.truth.Truth.assertThat;
 
@@ -30,6 +33,7 @@
 import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.Mockito.after;
 import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.never;
 import static org.mockito.Mockito.timeout;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
@@ -53,6 +57,7 @@
 
 import com.android.bluetooth.TestUtils;
 import com.android.bluetooth.btservice.AdapterService;
+import com.android.internal.util.State;
 
 import org.hamcrest.core.IsInstanceOf;
 import org.junit.After;
@@ -266,12 +271,12 @@
                 BassClientStateMachine.Connecting.class);
         sendMessageAndVerifyTransition(
                 mBassClientStateMachine.obtainMessage(
-                        BassClientStateMachine.CONNECTION_STATE_CHANGED,
+                        CONNECTION_STATE_CHANGED,
                         Integer.valueOf(BluetoothProfile.STATE_CONNECTED)),
                 BassClientStateMachine.Connected.class);
         sendMessageAndVerifyTransition(
                 mBassClientStateMachine.obtainMessage(
-                        BassClientStateMachine.CONNECTION_STATE_CHANGED,
+                        CONNECTION_STATE_CHANGED,
                         Integer.valueOf(BluetoothProfile.STATE_DISCONNECTED)),
                 BassClientStateMachine.Disconnected.class);
 
@@ -281,7 +286,7 @@
                 BassClientStateMachine.Connecting.class);
         sendMessageAndVerifyTransition(
                 mBassClientStateMachine.obtainMessage(
-                        BassClientStateMachine.CONNECTION_STATE_CHANGED,
+                        CONNECTION_STATE_CHANGED,
                         Integer.valueOf(BluetoothProfile.STATE_DISCONNECTED)),
                 BassClientStateMachine.Disconnected.class);
 
@@ -291,7 +296,7 @@
                 BassClientStateMachine.Connecting.class);
         sendMessageAndVerifyTransition(
                 mBassClientStateMachine.obtainMessage(
-                        BassClientStateMachine.CONNECTION_STATE_CHANGED,
+                        CONNECTION_STATE_CHANGED,
                         Integer.valueOf(BluetoothProfile.STATE_CONNECTED)),
                 BassClientStateMachine.Connected.class);
 
@@ -452,6 +457,76 @@
         verify(mBassClientService).updateBase(anyInt(), any());
     }
 
+    @Test
+    public void sendConnectMessage_inDisconnectedState() {
+        initToDisconnectedState();
+
+        BassClientStateMachine.BluetoothGattTestableWrapper btGatt = Mockito.mock(
+                BassClientStateMachine.BluetoothGattTestableWrapper.class);
+        mBassClientStateMachine.mBluetoothGatt = btGatt;
+
+        sendMessageAndVerifyTransition(
+                mBassClientStateMachine.obtainMessage(CONNECT),
+                BassClientStateMachine.Connecting.class);
+        verify(btGatt).disconnect();
+        verify(btGatt).close();
+    }
+
+    @Test
+    public void sendDisconnectMessage_inDisconnectedState() {
+        initToDisconnectedState();
+
+        BassClientStateMachine.BluetoothGattTestableWrapper btGatt = Mockito.mock(
+                BassClientStateMachine.BluetoothGattTestableWrapper.class);
+        mBassClientStateMachine.mBluetoothGatt = btGatt;
+
+        mBassClientStateMachine.sendMessage(DISCONNECT);
+        verify(btGatt, timeout(TIMEOUT_MS)).disconnect();
+        verify(btGatt).close();
+    }
+
+    @Test
+    public void sendStateChangedMessage_inDisconnectedState() {
+        initToDisconnectedState();
+
+        BassClientStateMachine.BluetoothGattTestableWrapper btGatt = Mockito.mock(
+                BassClientStateMachine.BluetoothGattTestableWrapper.class);
+        mBassClientStateMachine.mBluetoothGatt = btGatt;
+
+        Message msgToConnectingState =
+                mBassClientStateMachine.obtainMessage(CONNECTION_STATE_CHANGED);
+        msgToConnectingState.obj = BluetoothProfile.STATE_CONNECTING;
+
+        mBassClientStateMachine.sendMessage(msgToConnectingState);
+        TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
+        verify(mBassClientService, never()).sendBroadcast(any(Intent.class), anyString(), any());
+
+        Message msgToConnectedState =
+                mBassClientStateMachine.obtainMessage(CONNECTION_STATE_CHANGED);
+        msgToConnectedState.obj = BluetoothProfile.STATE_CONNECTED;
+        sendMessageAndVerifyTransition(msgToConnectedState, BassClientStateMachine.Connected.class);
+    }
+
+    @Test
+    public void sendOtherMessages_inDisconnectedState_doesNotChangeState() {
+        initToDisconnectedState();
+
+        mBassClientStateMachine.sendMessage(PSYNC_ACTIVE_TIMEOUT);
+        TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
+        verify(mBassClientService, never()).sendBroadcast(any(Intent.class), anyString(), any());
+
+        mBassClientStateMachine.sendMessage(-1);
+        TestUtils.waitForLooperToFinishScheduledTask(mHandlerThread.getLooper());
+        verify(mBassClientService, never()).sendBroadcast(any(Intent.class), anyString(), any());
+    }
+
+    private void initToDisconnectedState() {
+        allowConnection(true);
+        allowConnectGatt(true);
+        assertThat(mBassClientStateMachine.getCurrentState())
+                .isInstanceOf(BassClientStateMachine.Disconnected.class);
+    }
+
     private <T> void sendMessageAndVerifyTransition(Message msg, Class<T> type) {
         Mockito.clearInvocations(mBassClientService);
         mBassClientStateMachine.sendMessage(msg);