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

Commit a79c8109 authored by William Escande's avatar William Escande
Browse files

Add waiting timeout in test to fix flakiness

Bug: 271351629
Bug: 290831073
Test: atest HeadsetServiceAndStateMachineTest
Change-Id: I0c761349b5a291f5213febb25d1329dd85f79d91
parent 8a1048e5
Loading
Loading
Loading
Loading
+42 −32
Original line number Diff line number Diff line
@@ -59,7 +59,6 @@ import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -84,7 +83,7 @@ import java.util.concurrent.LinkedBlockingQueue;
@RunWith(AndroidJUnit4.class)
public class HeadsetServiceAndStateMachineTest {
    private static final String TAG = "HeadsetServiceAndStateMachineTest";
    private static final int ASYNC_CALL_TIMEOUT_MILLIS = 250;
    private static final int ASYNC_CALL_TIMEOUT_MILLIS = 500;
    private static final int START_VR_TIMEOUT_MILLIS = 1000;
    private static final int START_VR_TIMEOUT_WAIT_MILLIS = START_VR_TIMEOUT_MILLIS * 3 / 2;
    private static final int STATE_CHANGE_TIMEOUT_MILLIS = 1000;
@@ -98,8 +97,6 @@ public class HeadsetServiceAndStateMachineTest {
    private Context mTargetContext;
    private HeadsetService mHeadsetService;
    private BluetoothAdapter mAdapter;
    private ArgumentCaptor<HeadsetStateMachine> mStateMachineArgument =
            ArgumentCaptor.forClass(HeadsetStateMachine.class);
    private HashSet<BluetoothDevice> mBondedDevices = new HashSet<>();
    private final BlockingQueue<Intent> mConnectionStateChangedQueue = new LinkedBlockingQueue<>();
    private final BlockingQueue<Intent> mActiveDeviceChangedQueue = new LinkedBlockingQueue<>();
@@ -350,9 +347,11 @@ public class HeadsetServiceAndStateMachineTest {
        unbondIntent.putExtra(BluetoothDevice.EXTRA_DEVICE, device);
        InstrumentationRegistry.getTargetContext().sendBroadcast(unbondIntent);
        // Check that the state machine is actually destroyed
        verify(mObjectsFactory, timeout(ASYNC_CALL_TIMEOUT_MILLIS)).destroyStateMachine(
                mStateMachineArgument.capture());
        Assert.assertEquals(device, mStateMachineArgument.getValue().getDevice());
        ArgumentCaptor<HeadsetStateMachine> stateMachineArgument =
                ArgumentCaptor.forClass(HeadsetStateMachine.class);
        verify(mObjectsFactory, timeout(ASYNC_CALL_TIMEOUT_MILLIS))
                .destroyStateMachine(stateMachineArgument.capture());
        Assert.assertEquals(device, stateMachineArgument.getValue().getDevice());
    }

    /**
@@ -407,10 +406,11 @@ public class HeadsetServiceAndStateMachineTest {
        waitAndVerifyConnectionStateIntent(ASYNC_CALL_TIMEOUT_MILLIS, device,
                BluetoothProfile.STATE_DISCONNECTED, BluetoothProfile.STATE_CONNECTED);
        // Check that the state machine is destroyed after another async call
        verify(mObjectsFactory, timeout(ASYNC_CALL_TIMEOUT_MILLIS)).destroyStateMachine(
                mStateMachineArgument.capture());
        Assert.assertEquals(device, mStateMachineArgument.getValue().getDevice());

        ArgumentCaptor<HeadsetStateMachine> stateMachineArgument =
                ArgumentCaptor.forClass(HeadsetStateMachine.class);
        verify(mObjectsFactory, timeout(ASYNC_CALL_TIMEOUT_MILLIS))
                .destroyStateMachine(stateMachineArgument.capture());
        Assert.assertEquals(device, stateMachineArgument.getValue().getDevice());
    }

    /**
@@ -671,7 +671,7 @@ public class HeadsetServiceAndStateMachineTest {
        Assert.assertTrue(mHeadsetService.setActiveDevice(device));
        verify(mNativeInterface).setActiveDevice(device);
        Assert.assertEquals(device, mHeadsetService.getActiveDevice());
        verify(mNativeInterface).sendBsir(eq(device), eq(true));
        verify(mNativeInterface, timeout(ASYNC_CALL_TIMEOUT_MILLIS)).sendBsir(eq(device), eq(true));
        // Start voice recognition
        startVoiceRecognitionFromHf(device);
    }
@@ -695,7 +695,7 @@ public class HeadsetServiceAndStateMachineTest {
        Assert.assertTrue(mHeadsetService.setActiveDevice(device));
        verify(mNativeInterface).setActiveDevice(device);
        Assert.assertEquals(device, mHeadsetService.getActiveDevice());
        verify(mNativeInterface).sendBsir(eq(device), eq(true));
        verify(mNativeInterface, timeout(ASYNC_CALL_TIMEOUT_MILLIS)).sendBsir(eq(device), eq(true));
        // Start voice recognition
        startVoiceRecognitionFromHf(device);
        // Stop voice recognition
@@ -730,7 +730,7 @@ public class HeadsetServiceAndStateMachineTest {
        Assert.assertTrue(mHeadsetService.setActiveDevice(device));
        verify(mNativeInterface).setActiveDevice(device);
        Assert.assertEquals(device, mHeadsetService.getActiveDevice());
        verify(mNativeInterface).sendBsir(eq(device), eq(true));
        verify(mNativeInterface, timeout(ASYNC_CALL_TIMEOUT_MILLIS)).sendBsir(eq(device), eq(true));
        // Start voice recognition
        HeadsetStackEvent startVrEvent =
                new HeadsetStackEvent(HeadsetStackEvent.EVENT_TYPE_VR_STATE_CHANGED,
@@ -763,7 +763,7 @@ public class HeadsetServiceAndStateMachineTest {
        Assert.assertTrue(mHeadsetService.setActiveDevice(device));
        verify(mNativeInterface).setActiveDevice(device);
        Assert.assertEquals(device, mHeadsetService.getActiveDevice());
        verify(mNativeInterface).sendBsir(eq(device), eq(true));
        verify(mNativeInterface, timeout(ASYNC_CALL_TIMEOUT_MILLIS)).sendBsir(eq(device), eq(true));
        // Start voice recognition
        HeadsetStackEvent startVrEvent =
                new HeadsetStackEvent(HeadsetStackEvent.EVENT_TYPE_VR_STATE_CHANGED,
@@ -796,7 +796,7 @@ public class HeadsetServiceAndStateMachineTest {
        Assert.assertTrue(mHeadsetService.setActiveDevice(device));
        verify(mNativeInterface).setActiveDevice(device);
        Assert.assertEquals(device, mHeadsetService.getActiveDevice());
        verify(mNativeInterface).sendBsir(eq(device), eq(true));
        verify(mNativeInterface, timeout(ASYNC_CALL_TIMEOUT_MILLIS)).sendBsir(eq(device), eq(true));
        // Start voice recognition
        startVoiceRecognitionFromAg();
    }
@@ -870,7 +870,7 @@ public class HeadsetServiceAndStateMachineTest {
        Assert.assertTrue(mHeadsetService.setActiveDevice(device));
        verify(mNativeInterface).setActiveDevice(device);
        Assert.assertEquals(device, mHeadsetService.getActiveDevice());
        verify(mNativeInterface).sendBsir(eq(device), eq(true));
        verify(mNativeInterface, timeout(ASYNC_CALL_TIMEOUT_MILLIS)).sendBsir(eq(device), eq(true));
        // Start voice recognition
        startVoiceRecognitionFromAg();
        // Stop voice recognition
@@ -920,9 +920,12 @@ public class HeadsetServiceAndStateMachineTest {
        BluetoothDevice deviceB = TestUtils.getTestDevice(mAdapter, 1);
        connectTestDevice(deviceB);
        InOrder inOrder = inOrder(mNativeInterface);
        inOrder.verify(mNativeInterface).sendBsir(eq(deviceA), eq(true));
        inOrder.verify(mNativeInterface).sendBsir(eq(deviceB), eq(false));
        inOrder.verify(mNativeInterface).sendBsir(eq(deviceA), eq(false));
        inOrder.verify(mNativeInterface, timeout(ASYNC_CALL_TIMEOUT_MILLIS))
                .sendBsir(eq(deviceA), eq(true));
        inOrder.verify(mNativeInterface, timeout(ASYNC_CALL_TIMEOUT_MILLIS))
                .sendBsir(eq(deviceB), eq(false));
        inOrder.verify(mNativeInterface, timeout(ASYNC_CALL_TIMEOUT_MILLIS))
                .sendBsir(eq(deviceA), eq(false));
        // Set active device to device B
        Assert.assertTrue(mHeadsetService.setActiveDevice(deviceB));
        verify(mNativeInterface).setActiveDevice(deviceB);
@@ -970,14 +973,17 @@ public class HeadsetServiceAndStateMachineTest {
    @Test
    public void testVoiceRecognition_MultiHfInitiatedSwitchActiveDeviceReplyWrongHfSuccess() {
        // Connect two devices
        InOrder inOrder = inOrder(mNativeInterface);
        BluetoothDevice deviceA = TestUtils.getTestDevice(mAdapter, 0);
        connectTestDevice(deviceA);
        inOrder.verify(mNativeInterface, timeout(ASYNC_CALL_TIMEOUT_MILLIS))
                .sendBsir(eq(deviceA), eq(true));
        BluetoothDevice deviceB = TestUtils.getTestDevice(mAdapter, 1);
        connectTestDevice(deviceB);
        InOrder inOrder = inOrder(mNativeInterface);
        inOrder.verify(mNativeInterface).sendBsir(eq(deviceA), eq(true));
        inOrder.verify(mNativeInterface).sendBsir(eq(deviceB), eq(false));
        inOrder.verify(mNativeInterface).sendBsir(eq(deviceA), eq(false));
        inOrder.verify(mNativeInterface, timeout(ASYNC_CALL_TIMEOUT_MILLIS))
                .sendBsir(eq(deviceB), eq(false));
        inOrder.verify(mNativeInterface, timeout(ASYNC_CALL_TIMEOUT_MILLIS))
                .sendBsir(eq(deviceA), eq(false));
        // Set active device to device B
        Assert.assertTrue(mHeadsetService.setActiveDevice(deviceB));
        verify(mNativeInterface).setActiveDevice(deviceB);
@@ -1023,7 +1029,6 @@ public class HeadsetServiceAndStateMachineTest {
     * Reference: Section 4.25, Page 64/144 of HFP 1.7.1 specification
     */
    @Test
    @Ignore("b/271351629")
    public void testVoiceRecognition_MultiAgInitiatedSuccess() {
        // Connect two devices
        BluetoothDevice deviceA = TestUtils.getTestDevice(mAdapter, 0);
@@ -1031,9 +1036,12 @@ public class HeadsetServiceAndStateMachineTest {
        BluetoothDevice deviceB = TestUtils.getTestDevice(mAdapter, 1);
        connectTestDevice(deviceB);
        InOrder inOrder = inOrder(mNativeInterface);
        inOrder.verify(mNativeInterface).sendBsir(eq(deviceA), eq(true));
        inOrder.verify(mNativeInterface).sendBsir(eq(deviceB), eq(false));
        inOrder.verify(mNativeInterface).sendBsir(eq(deviceA), eq(false));
        inOrder.verify(mNativeInterface, timeout(ASYNC_CALL_TIMEOUT_MILLIS))
                .sendBsir(eq(deviceA), eq(true));
        inOrder.verify(mNativeInterface, timeout(ASYNC_CALL_TIMEOUT_MILLIS))
                .sendBsir(eq(deviceB), eq(false));
        inOrder.verify(mNativeInterface, timeout(ASYNC_CALL_TIMEOUT_MILLIS))
                .sendBsir(eq(deviceA), eq(false));
        // Set active device to device B
        Assert.assertTrue(mHeadsetService.setActiveDevice(deviceB));
        verify(mNativeInterface).setActiveDevice(deviceB);
@@ -1065,7 +1073,6 @@ public class HeadsetServiceAndStateMachineTest {
     * Reference: Section 4.25, Page 64/144 of HFP 1.7.1 specification
     */
    @Test
    @Ignore("b/271351629")
    public void testVoiceRecognition_MultiAgInitiatedDeviceNotActive() {
        // Connect two devices
        BluetoothDevice deviceA = TestUtils.getTestDevice(mAdapter, 0);
@@ -1073,9 +1080,12 @@ public class HeadsetServiceAndStateMachineTest {
        BluetoothDevice deviceB = TestUtils.getTestDevice(mAdapter, 1);
        connectTestDevice(deviceB);
        InOrder inOrder = inOrder(mNativeInterface);
        inOrder.verify(mNativeInterface).sendBsir(eq(deviceA), eq(true));
        inOrder.verify(mNativeInterface).sendBsir(eq(deviceB), eq(false));
        inOrder.verify(mNativeInterface).sendBsir(eq(deviceA), eq(false));
        inOrder.verify(mNativeInterface, timeout(ASYNC_CALL_TIMEOUT_MILLIS))
                .sendBsir(eq(deviceA), eq(true));
        inOrder.verify(mNativeInterface, timeout(ASYNC_CALL_TIMEOUT_MILLIS))
                .sendBsir(eq(deviceB), eq(false));
        inOrder.verify(mNativeInterface, timeout(ASYNC_CALL_TIMEOUT_MILLIS))
                .sendBsir(eq(deviceA), eq(false));
        // Set active device to device B
        Assert.assertTrue(mHeadsetService.setActiveDevice(deviceB));
        verify(mNativeInterface).setActiveDevice(deviceB);