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

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

Merge "Fix HearingAidServiceTest failure" into main am: 1f7ddfb2

parents aa506302 1f7ddfb2
Loading
Loading
Loading
Loading
+23 −10
Original line number Original line Diff line number Diff line
@@ -78,7 +78,7 @@ public class HearingAidServiceTest {
    private HashMap<BluetoothDevice, LinkedBlockingQueue<Intent>> mDeviceQueueMap;
    private HashMap<BluetoothDevice, LinkedBlockingQueue<Intent>> mDeviceQueueMap;
    private static final int TIMEOUT_MS = 1000;
    private static final int TIMEOUT_MS = 1000;


    private BroadcastReceiver mHearingAidIntentReceiver;
    private HearingAidIntentReceiver mHearingAidIntentReceiver;


    @Mock private AdapterService mAdapterService;
    @Mock private AdapterService mAdapterService;
    @Mock private ActiveDeviceManager mActiveDeviceManager;
    @Mock private ActiveDeviceManager mActiveDeviceManager;
@@ -113,13 +113,6 @@ public class HearingAidServiceTest {
        // Override the timeout value to speed up the test
        // Override the timeout value to speed up the test
        HearingAidStateMachine.sConnectTimeoutMs = TIMEOUT_MS;    // 1s
        HearingAidStateMachine.sConnectTimeoutMs = TIMEOUT_MS;    // 1s


        // Set up the Connection State Changed receiver
        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED);
        mHearingAidIntentReceiver = new HearingAidIntentReceiver();
        mTargetContext.registerReceiver(mHearingAidIntentReceiver, filter);

        // Get a device for testing
        // Get a device for testing
        mLeftDevice = TestUtils.getTestDevice(mAdapter, 0);
        mLeftDevice = TestUtils.getTestDevice(mAdapter, 0);
        mRightDevice = TestUtils.getTestDevice(mAdapter, 1);
        mRightDevice = TestUtils.getTestDevice(mAdapter, 1);
@@ -132,12 +125,20 @@ public class HearingAidServiceTest {
                .getBondState(any(BluetoothDevice.class));
                .getBondState(any(BluetoothDevice.class));
        doReturn(new ParcelUuid[]{BluetoothUuid.HEARING_AID}).when(mAdapterService)
        doReturn(new ParcelUuid[]{BluetoothUuid.HEARING_AID}).when(mAdapterService)
                .getRemoteUuids(any(BluetoothDevice.class));
                .getRemoteUuids(any(BluetoothDevice.class));

        // Set up the Connection State Changed receiver
        IntentFilter filter = new IntentFilter();
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
        filter.addAction(BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED);
        mHearingAidIntentReceiver = new HearingAidIntentReceiver(mDeviceQueueMap);
        mTargetContext.registerReceiver(mHearingAidIntentReceiver, filter);
    }
    }


    @After
    @After
    public void tearDown() throws Exception {
    public void tearDown() throws Exception {
        stopService();
        stopService();
        mTargetContext.unregisterReceiver(mHearingAidIntentReceiver);
        mTargetContext.unregisterReceiver(mHearingAidIntentReceiver);
        mHearingAidIntentReceiver.clear();
        mDeviceQueueMap.clear();
        mDeviceQueueMap.clear();
        TestUtils.clearAdapterService(mAdapterService);
        TestUtils.clearAdapterService(mAdapterService);
        reset(mAudioManager);
        reset(mAudioManager);
@@ -156,10 +157,22 @@ public class HearingAidServiceTest {
        Assert.assertNull(mService);
        Assert.assertNull(mService);
    }
    }


    private class HearingAidIntentReceiver extends BroadcastReceiver {
    private static class HearingAidIntentReceiver extends BroadcastReceiver {
        HashMap<BluetoothDevice, LinkedBlockingQueue<Intent>> mDeviceQueueMap;

        public HearingAidIntentReceiver(
                HashMap<BluetoothDevice, LinkedBlockingQueue<Intent>> deviceQueueMap) {
            mDeviceQueueMap = deviceQueueMap;
        }

        public void clear() {
            mDeviceQueueMap = null;
        }

        @Override
        @Override
        public void onReceive(Context context, Intent intent) {
        public void onReceive(Context context, Intent intent) {
            if (BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED.equals(intent.getAction())) {
            if (BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED.equals(intent.getAction())
                    && mDeviceQueueMap != null) {
                try {
                try {
                    BluetoothDevice device = intent.getParcelableExtra(
                    BluetoothDevice device = intent.getParcelableExtra(
                            BluetoothDevice.EXTRA_DEVICE);
                            BluetoothDevice.EXTRA_DEVICE);