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

Commit 400f4c8d authored by Jack He's avatar Jack He
Browse files

Test: Fix flaky PhonePolicyTest and ActiveDeviceManagerTest

* PhonePolicyTest needs to stub A2dpService.connect() and
  HeadsetService.connect() methods for it to pass stably
* Fixed a miscounted frequency for HeadsetService.setPriority()
  in PhonePolicyTes.testDisconnectNoAutoConnect().
* Fixed a race condition in ActiveDeviceManagerTest where dependency
  calls such as HearingAidService.setActiveDevice() happens before
  internal state such as mHearingAidActiveDevice is set. The fix
  requires ActiveDeviceManagerTest to wait for ActiveDeviceManager's
  handler looper to finish executing scheduled tasks before checking any
  internal states

Bug: 80437170
Test: run PhonePolicyTest, ActiveDeviceManagerTest 100 times
Change-Id: Iece11914c4c7f7c54abcbf78caa7ddd2ba1d740f
parent f3768d4d
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -406,6 +406,20 @@ class ActiveDeviceManager {
        resetState();
    }

    /**
     * Get the {@link Looper} for the handler thread. This is used in testing and helper
     * objects
     *
     * @return {@link Looper} for the handler thread
     */
    @VisibleForTesting
    public Looper getHandlerLooper() {
        if (mHandlerThread == null) {
            return null;
        }
        return mHandlerThread.getLooper();
    }

    private void setA2dpActiveDevice(BluetoothDevice device) {
        if (DBG) {
            Log.d(TAG, "setA2dpActiveDevice(" + device + ")");
+13 −8
Original line number Diff line number Diff line
@@ -154,7 +154,8 @@ public class ActiveDeviceManagerTest {

        a2dpActiveDeviceChanged(mA2dpDevice);
        // Don't call mA2dpService.setActiveDevice()
        verify(mA2dpService, after(TIMEOUT_MS).times(1)).setActiveDevice(mA2dpDevice);
        TestUtils.waitForLooperToFinishScheduledTask(mActiveDeviceManager.getHandlerLooper());
        verify(mA2dpService, times(1)).setActiveDevice(mA2dpDevice);
        Assert.assertEquals(mA2dpDevice, mActiveDeviceManager.getA2dpActiveDevice());
    }

@@ -204,7 +205,8 @@ public class ActiveDeviceManagerTest {

        headsetActiveDeviceChanged(mHeadsetDevice);
        // Don't call mHeadsetService.setActiveDevice()
        verify(mHeadsetService, after(TIMEOUT_MS).times(1)).setActiveDevice(mHeadsetDevice);
        TestUtils.waitForLooperToFinishScheduledTask(mActiveDeviceManager.getHandlerLooper());
        verify(mHeadsetService, times(1)).setActiveDevice(mHeadsetDevice);
        Assert.assertEquals(mHeadsetDevice, mActiveDeviceManager.getHfpActiveDevice());
    }

@@ -238,8 +240,9 @@ public class ActiveDeviceManagerTest {
        a2dpConnected(mA2dpHeadsetDevice);
        headsetConnected(mA2dpHeadsetDevice);

        verify(mA2dpService, timeout(TIMEOUT_MS).times(0)).setActiveDevice(mA2dpHeadsetDevice);
        verify(mHeadsetService, timeout(TIMEOUT_MS).times(0)).setActiveDevice(mA2dpHeadsetDevice);
        TestUtils.waitForLooperToFinishScheduledTask(mActiveDeviceManager.getHandlerLooper());
        verify(mA2dpService, never()).setActiveDevice(mA2dpHeadsetDevice);
        verify(mHeadsetService, never()).setActiveDevice(mA2dpHeadsetDevice);
    }

    /**
@@ -254,9 +257,10 @@ public class ActiveDeviceManagerTest {
        a2dpConnected(mA2dpHeadsetDevice);
        a2dpActiveDeviceChanged(mA2dpHeadsetDevice);

        verify(mHearingAidService, timeout(TIMEOUT_MS)).setActiveDevice(isNull());
        TestUtils.waitForLooperToFinishScheduledTask(mActiveDeviceManager.getHandlerLooper());
        verify(mHearingAidService).setActiveDevice(isNull());
        // Don't call mA2dpService.setActiveDevice()
        verify(mA2dpService, timeout(TIMEOUT_MS).times(0)).setActiveDevice(mA2dpHeadsetDevice);
        verify(mA2dpService, never()).setActiveDevice(mA2dpHeadsetDevice);
        Assert.assertEquals(mA2dpHeadsetDevice, mActiveDeviceManager.getA2dpActiveDevice());
        Assert.assertEquals(null, mActiveDeviceManager.getHearingAidActiveDevice());
    }
@@ -273,9 +277,10 @@ public class ActiveDeviceManagerTest {
        headsetConnected(mA2dpHeadsetDevice);
        headsetActiveDeviceChanged(mA2dpHeadsetDevice);

        verify(mHearingAidService, timeout(TIMEOUT_MS)).setActiveDevice(isNull());
        TestUtils.waitForLooperToFinishScheduledTask(mActiveDeviceManager.getHandlerLooper());
        verify(mHearingAidService).setActiveDevice(isNull());
        // Don't call mHeadsetService.setActiveDevice()
        verify(mHeadsetService, timeout(TIMEOUT_MS).times(0)).setActiveDevice(mA2dpHeadsetDevice);
        verify(mHeadsetService, never()).setActiveDevice(mA2dpHeadsetDevice);
        Assert.assertEquals(mA2dpHeadsetDevice, mActiveDeviceManager.getHfpActiveDevice());
        Assert.assertEquals(null, mActiveDeviceManager.getHearingAidActiveDevice());
    }
+5 −2
Original line number Diff line number Diff line
@@ -66,6 +66,9 @@ public class PhonePolicyTest {
    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        // Stub A2DP and HFP
        when(mHeadsetService.connect(any(BluetoothDevice.class))).thenReturn(true);
        when(mA2dpService.connect(any(BluetoothDevice.class))).thenReturn(true);
        // Prepare the TestUtils
        TestUtils.setAdapterService(mAdapterService);
        // Configure the maximum connected audio devices
@@ -213,8 +216,8 @@ public class PhonePolicyTest {
        mPhonePolicy.getBroadcastReceiver().onReceive(null /* context */, intent);

        // This device should be set to auto connect while the first device is reset to ON
        verify(mHeadsetService, timeout(ASYNC_CALL_TIMEOUT_MILLIS)).setPriority(bondedDevices[0],
                BluetoothProfile.PRIORITY_ON);
        verify(mHeadsetService, timeout(ASYNC_CALL_TIMEOUT_MILLIS).times(2)).setPriority(
                bondedDevices[0], BluetoothProfile.PRIORITY_ON);
        verify(mHeadsetService, timeout(ASYNC_CALL_TIMEOUT_MILLIS)).setPriority(bondedDevices[1],
                BluetoothProfile.PRIORITY_AUTO_CONNECT);
        verify(mHeadsetService, never()).setPriority(eq(bondedDevices[3]), anyInt());