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

Commit d2f75fe8 authored by Ang Li's avatar Ang Li Committed by Android (Google) Code Review
Browse files

Merge "Add test for device selection with specific addresses" into main

parents 72e7981e e09f19a8
Loading
Loading
Loading
Loading
+69 −0
Original line number Diff line number Diff line
@@ -62,6 +62,7 @@ public class InputRouteManagerTest {
    private static final int INPUT_USB_HEADSET_ID = 4;
    private static final int INPUT_USB_ACCESSORY_ID = 5;
    private static final int HDMI_ID = 6;
    private static final int INPUT_USB_DEVICE_2_ID = 7;
    private static final int MAX_VOLUME = 1;
    private static final int CURRENT_VOLUME = 0;
    private static final boolean VOLUME_FIXED_TRUE = true;
@@ -69,6 +70,8 @@ public class InputRouteManagerTest {
    private static final String PRODUCT_NAME_WIRED_HEADSET = "My Wired Headset";
    private static final String PRODUCT_NAME_USB_HEADSET = "My USB Headset";
    private static final String PRODUCT_NAME_USB_DEVICE = "My USB Device";
    private static final String PRODUCT_NAME_USB_DEVICE_1 = "USB Device 1";
    private static final String PRODUCT_NAME_USB_DEVICE_2 = "USB Device 2";
    private static final String PRODUCT_NAME_USB_ACCESSORY = "My USB Accessory";
    private static final String PRODUCT_NAME_HDMI_DEVICE = "HDMI device";

@@ -110,6 +113,18 @@ public class InputRouteManagerTest {
        return info;
    }

    private AudioDeviceInfo mockUsbDeviceInfoWithAddress(
            int id, String address, String productName) {
        final AudioDeviceInfo info = mock(AudioDeviceInfo.class);
        when(info.getType()).thenReturn(AudioDeviceInfo.TYPE_USB_DEVICE);
        when(info.getId()).thenReturn(id);
        when(info.getAddress()).thenReturn(address);
        when(info.getProductName()).thenReturn(productName);
        when(info.isSource()).thenReturn(true);
        when(info.isSink()).thenReturn(false);
        return info;
    }

    private AudioDeviceInfo mockUsbHeadsetInfo() {
        final AudioDeviceInfo info = mock(AudioDeviceInfo.class);
        when(info.getType()).thenReturn(AudioDeviceInfo.TYPE_USB_HEADSET);
@@ -175,6 +190,11 @@ public class InputRouteManagerTest {
                /* address= */ "");
    }

    private AudioDeviceAttributes getUsbDeviceAttributesWithAddress(String address) {
        return new AudioDeviceAttributes(
                AudioDeviceAttributes.ROLE_INPUT, AudioDeviceInfo.TYPE_USB_DEVICE, address);
    }

    private AudioDeviceAttributes getHdmiDeviceAttributes() {
        return new AudioDeviceAttributes(
                AudioDeviceAttributes.ROLE_INPUT, AudioDeviceInfo.TYPE_HDMI, /* address= */ "");
@@ -312,6 +332,7 @@ public class InputRouteManagerTest {
                        VOLUME_FIXED_TRUE,
                        /* isSelected= */ false,
                        PRODUCT_NAME_BUILTIN_MIC);
        mInputRouteManager.mInputMediaDevices.add(builtinMicDevice);
        mInputRouteManager.selectDevice(builtinMicDevice);

        for (@MediaRecorder.Source int preset : PRESETS) {
@@ -320,6 +341,46 @@ public class InputRouteManagerTest {
        }
    }

    @Test
    public void selectDevice_withAddress_updatesSelectionCorrectly() {
        // 1. Setup: Create devices with same type but different addresses.
        final AudioDeviceInfo builtinMicInfo = mockBuiltinMicInfo();
        final AudioDeviceInfo usbDevice1Info =
                mockUsbDeviceInfoWithAddress(
                        INPUT_USB_DEVICE_ID, "address1", PRODUCT_NAME_USB_DEVICE_1);
        final AudioDeviceInfo usbDevice2Info =
                mockUsbDeviceInfoWithAddress(
                        INPUT_USB_DEVICE_2_ID, "address2", PRODUCT_NAME_USB_DEVICE_2);

        AudioDeviceInfo[] devices = {builtinMicInfo, usbDevice1Info, usbDevice2Info};
        when(mAudioManager.getDevices(AudioManager.GET_DEVICES_INPUTS)).thenReturn(devices);

        // Initial state: Built-in mic is selected by default.
        when(mAudioManager.getDevicesForAttributes(INPUT_ATTRIBUTES))
                .thenReturn(Collections.singletonList(getBuiltinMicDeviceAttributes()));
        mInputRouteManager = new InputRouteManager(mContext, mAudioManager, mInfoMediaManager);
        onPreferredDevicesForCapturePresetChanged();

        assertThat(getSelectedInputDevice().getAudioDeviceInfoType())
                .isEqualTo(AudioDeviceInfo.TYPE_BUILTIN_MIC);

        // 2. Action: Select the first USB device.
        MediaDevice usbDevice1 = getInputDeviceById(INPUT_USB_DEVICE_ID);
        mInputRouteManager.selectDevice(usbDevice1);
        onPreferredDevicesForCapturePresetChanged(); // This triggers dispatchInputDeviceListUpdate.

        // 3. Assertions.
        // Verify the selected device in the list is correct.
        InputMediaDevice selectedDevice = getSelectedInputDevice();
        assertThat(selectedDevice.getId()).isEqualTo(String.valueOf(INPUT_USB_DEVICE_ID));

        // Verify other devices are not selected.
        assertThat(getInputDeviceById(INPUT_USB_DEVICE_2_ID).isSelected())
                .isFalse();
        assertThat(getInputDeviceById(BUILTIN_MIC_ID).isSelected())
                .isFalse();
    }

    @Test
    public void onInitiation_shouldApplyDefaultSelectedDeviceToAllPresets() {
        verify(mAudioManager, atLeastOnce()).getDevicesForAttributes(INPUT_ATTRIBUTES);
@@ -526,4 +587,12 @@ public class InputRouteManagerTest {
        return (InputMediaDevice) mInputRouteManager.mInputMediaDevices.stream().filter(
                MediaDevice::isSelected).findFirst().orElse(null);
    }

    @Nullable
    private InputMediaDevice getInputDeviceById(int deviceId) {
        return (InputMediaDevice) mInputRouteManager.mInputMediaDevices.stream()
                        .filter(d -> d.getId().equals(String.valueOf(deviceId)))
                        .findFirst()
                        .orElse(null);
    }
}