Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e1a8b146 authored by Sungsoo Lim's avatar Sungsoo Lim Committed by Automerger Merge Worker
Browse files

Merge "Test for state changes of VolumeControlStateMachine" am: 0f3bf617 am: 9ca6359a

parents e515815a 9ca6359a
Loading
Loading
Loading
Loading
+2 −1
Original line number Original line Diff line number Diff line
@@ -46,7 +46,8 @@ public class VolumeControlStateMachine extends StateMachine {
    static final int DISCONNECT = 2;
    static final int DISCONNECT = 2;
    @VisibleForTesting
    @VisibleForTesting
    static final int STACK_EVENT = 101;
    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
    // NOTE: the value is not "final" - it is modified in the unit tests
    @VisibleForTesting
    @VisibleForTesting
+142 −5
Original line number Original line Diff line number Diff line
@@ -17,7 +17,13 @@


package com.android.bluetooth.vc;
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.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothDevice;
@@ -25,24 +31,24 @@ import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.content.Context;
import android.content.Intent;
import android.content.Intent;
import android.os.HandlerThread;
import android.os.HandlerThread;
import android.os.Message;


import androidx.test.InstrumentationRegistry;
import androidx.test.InstrumentationRegistry;
import androidx.test.filters.MediumTest;
import androidx.test.filters.MediumTest;
import androidx.test.runner.AndroidJUnit4;
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.TestUtils;
import com.android.bluetooth.btservice.AdapterService;


import org.hamcrest.core.IsInstanceOf;
import org.hamcrest.core.IsInstanceOf;
import org.junit.After;
import org.junit.After;
import org.junit.Assert;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Before;
import org.junit.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.MockitoAnnotations;


@MediumTest
@MediumTest
@@ -254,4 +260,135 @@ public class VolumeControlStateMachineTest {
                IsInstanceOf.instanceOf(VolumeControlStateMachine.Disconnected.class));
                IsInstanceOf.instanceOf(VolumeControlStateMachine.Disconnected.class));
        verify(mVolumeControlNativeInterface).disconnectVolumeControl(eq(mTestDevice));
        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));
    }
}
}