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

Commit e09f19a8 authored by AI test gen's avatar AI test gen Committed by Ang Li
Browse files

Add test for device selection with specific addresses

Adds a new test case to verify that `selectDevice` correctly updates the selected device state when dealing with multiple devices of the same type but with different addresses.

This test validates the logic in `isSelectedDevice`, ensuring that the device address is properly considered when determining the selected device. It also improves the existing `selectDevice` test by ensuring the device is in the available list before being selected, reflecting a more realistic scenario.

Please help fill out the survey for feedback: https://docs.google.com/forms/d/e/1FAIpQLSeKFKpHImCAqZIa_OR801cw72HQUreM2oGM25C3mKKT2tBFnw/viewform?usp=pp_url&entry.1586624956=ag/35420942

Please feel free to use your domain knowledge to make changes to the generated tests to make it more valuable for your team.
Original Change: ag/34813753
Test: ATP tests passed http://go/forrest-run/L09000030017475354
Bug: 431235865
Flag: TEST_ONLY

Change-Id: Ia611a91dcfe8b261199869102de6f47b918bf7a8
parent 7c077e50
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);
    }
}