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

Commit de9d574d authored by Angela Wang's avatar Angela Wang
Browse files

Refactor HearingDevicePresetController

1. Move HAP profile related handling from HearingDevicesDialogDelegate
   into HearingDevicePresetController.
2. Make the HearingDevicePresetController holds the preset info cache of
   last query result and return it directly without query them again.
3. Call the PresetCallback.onPresetInfoUpdated() callback in
   HearingDevicePresetController only when the data is changed
4. Simplify some variables' naming.

Flag: EXEMPT refactor
Bug: 357878944
Test: atest HearingDevicesDialogDelegateTest
Test: atest HearingDevicePresetControllerTest
Change-Id: I3f8971eed939d2d9cc61ef4c8f7cfde01c53e472
parent e5432619
Loading
Loading
Loading
Loading
+33 −53
Original line number Diff line number Diff line
@@ -139,13 +139,11 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
    private ActivityInfo mActivityInfo;
    @Mock
    private Drawable mDrawable;
    @Mock
    private HearingDevicesPresetsController mPresetsController;

    private SystemUIDialog mDialog;
    private SystemUIDialog.Factory mDialogFactory;
    private HearingDevicesDialogDelegate mDialogDelegate;
    private TestableLooper mTestableLooper;
    private final List<CachedBluetoothDevice> mDevices = new ArrayList<>();

    @Before
    public void setUp() {
@@ -155,7 +153,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
        when(mProfileManager.getHapClientProfile()).thenReturn(mHapClientProfile);
        when(mLocalBluetoothAdapter.isEnabled()).thenReturn(true);
        when(mLocalBluetoothManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager);
        when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(mDevices);
        when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(List.of(mCachedDevice));
        when(mLocalBluetoothManager.getEventManager()).thenReturn(mBluetoothEventManager);
        when(mSysUiState.setFlag(anyLong(), anyBoolean())).thenReturn(mSysUiState);
        when(mDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED);
@@ -163,6 +161,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
        when(mCachedDevice.getDevice()).thenReturn(mDevice);
        when(mCachedDevice.getAddress()).thenReturn(DEVICE_ADDRESS);
        when(mCachedDevice.getName()).thenReturn(DEVICE_NAME);
        when(mCachedDevice.getProfiles()).thenReturn(List.of(mHapClientProfile));
        when(mCachedDevice.isActiveDevice(BluetoothProfile.HEARING_AID)).thenReturn(true);
        when(mCachedDevice.isConnectedHearingAidDevice()).thenReturn(true);
        when(mCachedDevice.isConnectedHapClientDevice()).thenReturn(true);
@@ -170,12 +169,11 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
        when(mHearingDeviceItem.getCachedBluetoothDevice()).thenReturn(mCachedDevice);

        mContext.setMockPackageManager(mPackageManager);
        mDevices.add(mCachedDevice);
    }

    @Test
    public void clickPairNewDeviceButton_intentActionMatch() {
        setUpPairNewDeviceDialog();
        setUpDeviceDialogWithPairNewDeviceButton();
        mDialog.show();

        getPairNewDeviceButton(mDialog).performClick();
@@ -191,7 +189,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {

    @Test
    public void onDeviceItemGearClicked_intentActionMatch() {
        setUpDeviceListDialog();
        setUpDeviceDialogWithoutPairNewDeviceButton();

        mDialogDelegate.onDeviceItemGearClicked(mHearingDeviceItem, new View(mContext));

@@ -206,7 +204,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {

    @Test
    public void onDeviceItemOnClicked_connectedDevice_disconnect() {
        setUpDeviceListDialog();
        setUpDeviceDialogWithoutPairNewDeviceButton();
        when(mHearingDeviceItem.getType()).thenReturn(DeviceItemType.CONNECTED_BLUETOOTH_DEVICE);

        mDialogDelegate.onDeviceItemClicked(mHearingDeviceItem, new View(mContext));
@@ -222,7 +220,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
        mContext.getOrCreateTestableResources().addOverride(
                R.array.config_quickSettingsHearingDevicesRelatedToolName, new String[]{});

        setUpPairNewDeviceDialog();
        setUpDeviceDialogWithoutPairNewDeviceButton();
        mDialog.show();

        assertToolsUi(0);
@@ -237,7 +235,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
        mContext.getOrCreateTestableResources().addOverride(
                R.array.config_quickSettingsHearingDevicesRelatedToolName, new String[]{});

        setUpPairNewDeviceDialog();
        setUpDeviceDialogWithoutPairNewDeviceButton();
        mDialog.show();

        assertToolsUi(1);
@@ -247,9 +245,8 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
    @EnableFlags(Flags.FLAG_HEARING_DEVICES_DIALOG_RELATED_TOOLS)
    public void showDialog_hasLiveCaption_oneRelatedToolInConfig_showTwoRelatedTools()
            throws PackageManager.NameNotFoundException {
        when(mPackageManager.queryIntentActivities(
                eq(LIVE_CAPTION_INTENT), anyInt())).thenReturn(
                List.of(new ResolveInfo()));
        when(mPackageManager.queryIntentActivities(eq(LIVE_CAPTION_INTENT), anyInt()))
                .thenReturn(List.of(new ResolveInfo()));
        mContext.getOrCreateTestableResources().addOverride(
                R.array.config_quickSettingsHearingDevicesRelatedToolName,
                new String[]{TEST_PKG + "/" + TEST_CLS});
@@ -260,18 +257,18 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
        when(mActivityInfo.getComponentName()).thenReturn(TEST_COMPONENT);
        when(mDrawable.mutate()).thenReturn(mDrawable);

        setUpPairNewDeviceDialog();
        setUpDeviceDialogWithoutPairNewDeviceButton();
        mDialog.show();

        assertToolsUi(2);
    }

    @Test
    public void showDialog_noPreset_presetGone() {
        when(mPresetsController.getAllPresetInfo()).thenReturn(new ArrayList<>());
        when(mPresetsController.getActivePresetIndex()).thenReturn(PRESET_INDEX_UNAVAILABLE);
    public void showDialog_noPreset_presetLayoutGone() {
        when(mHapClientProfile.getAllPresetInfo(mDevice)).thenReturn(new ArrayList<>());
        when(mHapClientProfile.getActivePresetIndex(mDevice)).thenReturn(PRESET_INDEX_UNAVAILABLE);

        setUpDeviceListDialog();
        setUpDeviceDialogWithoutPairNewDeviceButton();
        mDialog.show();

        ViewGroup presetLayout = getPresetLayout(mDialog);
@@ -281,11 +278,12 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
    @Test
    public void showDialog_presetExist_presetSelected() {
        BluetoothHapPresetInfo info = getTestPresetInfo();
        when(mPresetsController.getAllPresetInfo()).thenReturn(List.of(info));
        when(mPresetsController.getActivePresetIndex()).thenReturn(TEST_PRESET_INDEX);
        when(mHapClientProfile.getAllPresetInfo(mDevice)).thenReturn(List.of(info));
        when(mHapClientProfile.getActivePresetIndex(mDevice)).thenReturn(TEST_PRESET_INDEX);

        setUpDeviceListDialog();
        setUpDeviceDialogWithoutPairNewDeviceButton();
        mDialog.show();
        mTestableLooper.processAllMessages();

        ViewGroup presetLayout = getPresetLayout(mDialog);
        assertThat(presetLayout.getVisibility()).isEqualTo(View.VISIBLE);
@@ -295,48 +293,32 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {

    @Test
    public void onActiveDeviceChanged_presetExist_presetSelected() {
        setUpDeviceListDialog();
        setUpDeviceDialogWithoutPairNewDeviceButton();
        mDialog.show();
        BluetoothHapPresetInfo info = getTestPresetInfo();
        when(mPresetsController.getAllPresetInfo()).thenReturn(List.of(info));
        when(mPresetsController.getActivePresetIndex()).thenReturn(TEST_PRESET_INDEX);
        when(mHapClientProfile.getAllPresetInfo(mDevice)).thenReturn(List.of(info));
        when(mHapClientProfile.getActivePresetIndex(mDevice)).thenReturn(TEST_PRESET_INDEX);

        Spinner spinner = getPresetSpinner(mDialog);
        assertThat(spinner.getSelectedItemPosition()).isEqualTo(-1);

        mDialogDelegate.onActiveDeviceChanged(mCachedDevice, BluetoothProfile.LE_AUDIO);
        mTestableLooper.processAllMessages();

        ViewGroup presetLayout = getPresetLayout(mDialog);
        assertThat(presetLayout.getVisibility()).isEqualTo(View.VISIBLE);
        Spinner spinner = getPresetSpinner(mDialog);
        assertThat(spinner.getSelectedItemPosition()).isEqualTo(0);
    }

    private void setUpDeviceDialogWithPairNewDeviceButton() {
        setUpDeviceDialog(/* showPairNewDevice= */ true);
    }


    private void setUpPairNewDeviceDialog() {
        mDialogFactory = new SystemUIDialog.Factory(
                mContext,
                mSystemUIDialogManager,
                mSysUiState,
                getFakeBroadcastDispatcher(),
                mDialogTransitionAnimator
        );
        mDialogDelegate = new HearingDevicesDialogDelegate(
                mContext,
                true,
                TEST_LAUNCH_SOURCE_ID,
                mDialogFactory,
                mActivityStarter,
                mDialogTransitionAnimator,
                mLocalBluetoothManager,
                new Handler(mTestableLooper.getLooper()),
                mAudioManager,
                mUiEventLogger
        );

        mDialog = mDialogDelegate.createDialog();
    private void setUpDeviceDialogWithoutPairNewDeviceButton() {
        setUpDeviceDialog(/* showPairNewDevice= */ false);
    }

    private void setUpDeviceListDialog() {
    private void setUpDeviceDialog(boolean showPairNewDevice) {
        mDialogFactory = new SystemUIDialog.Factory(
                mContext,
                mSystemUIDialogManager,
@@ -345,8 +327,7 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
                mDialogTransitionAnimator
        );
        mDialogDelegate = new HearingDevicesDialogDelegate(
                mContext,
                false,
                showPairNewDevice,
                TEST_LAUNCH_SOURCE_ID,
                mDialogFactory,
                mActivityStarter,
@@ -356,15 +337,14 @@ public class HearingDevicesDialogDelegateTest extends SysuiTestCase {
                mAudioManager,
                mUiEventLogger
        );

        mDialog = mDialogDelegate.createDialog();
        mDialogDelegate.setHearingDevicesPresetsController(mPresetsController);
    }

    private BluetoothHapPresetInfo getTestPresetInfo() {
        BluetoothHapPresetInfo info = mock(BluetoothHapPresetInfo.class);
        when(info.getName()).thenReturn(TEST_PRESET_NAME);
        when(info.getIndex()).thenReturn(TEST_PRESET_INDEX);
        when(info.isAvailable()).thenReturn(true);
        return info;
    }

+101 −86
Original line number Diff line number Diff line
@@ -21,10 +21,10 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.kotlin.VerificationKt.never;

import static java.util.Collections.emptyList;

@@ -39,7 +39,6 @@ import androidx.test.filters.SmallTest;

import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.HapClientProfile;
import com.android.settingslib.bluetooth.LocalBluetoothProfile;
import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
import com.android.systemui.SysuiTestCase;

@@ -53,6 +52,7 @@ import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

import java.util.List;
import java.util.Set;
import java.util.concurrent.Executor;

/** Tests for {@link HearingDevicesPresetsController}. */
@@ -62,6 +62,7 @@ import java.util.concurrent.Executor;
public class HearingDevicesPresetsControllerTest extends SysuiTestCase {

    private static final int TEST_PRESET_INDEX = 1;
    private static final int TEST_UPDATED_PRESET_INDEX = 2;
    private static final String TEST_PRESET_NAME = "test_preset";
    private static final int TEST_HAP_GROUP_ID = 1;
    private static final int TEST_REASON = 1024;
@@ -74,14 +75,13 @@ public class HearingDevicesPresetsControllerTest extends SysuiTestCase {
    @Mock
    private HapClientProfile mHapClientProfile;
    @Mock
    private CachedBluetoothDevice mCachedBluetoothDevice;
    private CachedBluetoothDevice mCachedDevice;
    @Mock
    private CachedBluetoothDevice mSubCachedBluetoothDevice;
    private CachedBluetoothDevice mCachedMemberDevice;
    @Mock
    private BluetoothDevice mBluetoothDevice;
    private BluetoothDevice mDevice;
    @Mock
    private BluetoothDevice mSubBluetoothDevice;

    private BluetoothDevice mMemberDevice;
    @Mock
    private HearingDevicesPresetsController.PresetCallback mCallback;

@@ -91,15 +91,19 @@ public class HearingDevicesPresetsControllerTest extends SysuiTestCase {
    public void setUp() {
        when(mProfileManager.getHapClientProfile()).thenReturn(mHapClientProfile);
        when(mHapClientProfile.isProfileReady()).thenReturn(true);
        when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
        when(mCachedBluetoothDevice.getSubDevice()).thenReturn(mSubCachedBluetoothDevice);
        when(mSubCachedBluetoothDevice.getDevice()).thenReturn(mSubBluetoothDevice);
        when(mCachedDevice.getDevice()).thenReturn(mDevice);
        when(mCachedDevice.getProfiles()).thenReturn(List.of(mHapClientProfile));
        when(mCachedDevice.getMemberDevice()).thenReturn(Set.of(mCachedMemberDevice));
        when(mCachedMemberDevice.getDevice()).thenReturn(mMemberDevice);

        mController = new HearingDevicesPresetsController(mProfileManager, mCallback);
        mController.setDevice(mCachedDevice);
    }

    @Test
    public void onServiceConnected_callExpectedCallback() {
        preparePresetInfo(/* isValid= */ true);

        mController.onServiceConnected();

        verify(mHapClientProfile).registerCallback(any(Executor.class),
@@ -108,114 +112,128 @@ public class HearingDevicesPresetsControllerTest extends SysuiTestCase {
    }

    @Test
    public void getAllPresetInfo_setInvalidHearingDevice_getEmpty() {
        when(mCachedBluetoothDevice.getProfiles()).thenReturn(emptyList());
        mController.setHearingDeviceIfSupportHap(mCachedBluetoothDevice);
        BluetoothHapPresetInfo hapPresetInfo = getHapPresetInfo(true);
        when(mHapClientProfile.getAllPresetInfo(mBluetoothDevice)).thenReturn(
                List.of(hapPresetInfo));
    public void setDevice_nonHapDevice_getEmptyListAndInvalidActiveIndex() {
        when(mCachedDevice.getProfiles()).thenReturn(emptyList());
        preparePresetInfo(/* isValid= */ true);

        mController.setDevice(mCachedDevice);

        assertThat(mController.getAllPresetInfo()).isEmpty();
        assertThat(mController.getActivePresetIndex()).isEqualTo(
                BluetoothHapClient.PRESET_INDEX_UNAVAILABLE);
    }

    @Test
    public void getAllPresetInfo_containsNotAvailablePresetInfo_getEmpty() {
        setValidHearingDeviceSupportHap();
        BluetoothHapPresetInfo hapPresetInfo = getHapPresetInfo(false);
        when(mHapClientProfile.getAllPresetInfo(mBluetoothDevice)).thenReturn(
                List.of(hapPresetInfo));
    public void refreshPresetInfo_containsOnlyNotAvailablePresetInfo_getEmptyList() {
        preparePresetInfo(/* isValid= */ false);

        mController.refreshPresetInfo();

        assertThat(mController.getAllPresetInfo()).isEmpty();
    }

    @Test
    public void getAllPresetInfo_containsOnePresetInfo_getOnePresetInfo() {
        setValidHearingDeviceSupportHap();
        BluetoothHapPresetInfo hapPresetInfo = getHapPresetInfo(true);
        when(mHapClientProfile.getAllPresetInfo(mBluetoothDevice)).thenReturn(
                List.of(hapPresetInfo));
    public void refreshPresetInfo_containsOnePresetInfo_getOnePresetInfo() {
        List<BluetoothHapPresetInfo> infos = preparePresetInfo(/* isValid= */ true);

        assertThat(mController.getAllPresetInfo()).contains(hapPresetInfo);
        mController.refreshPresetInfo();

        List<BluetoothHapPresetInfo> presetInfos = mController.getAllPresetInfo();
        assertThat(presetInfos.size()).isEqualTo(1);
        assertThat(presetInfos).contains(infos.getFirst());
    }

    @Test
    public void getActivePresetIndex_getExpectedIndex() {
        setValidHearingDeviceSupportHap();
        when(mHapClientProfile.getActivePresetIndex(mBluetoothDevice)).thenReturn(
                TEST_PRESET_INDEX);
    public void refreshPresetInfo_getExpectedIndex() {
        preparePresetInfo(/* isValid= */ true);

        mController.refreshPresetInfo();

        assertThat(mController.getActivePresetIndex()).isEqualTo(TEST_PRESET_INDEX);
    }

    @Test
    public void onPresetSelected_presetIndex_callOnPresetInfoUpdatedWithExpectedPresetIndex() {
        setValidHearingDeviceSupportHap();
        BluetoothHapPresetInfo hapPresetInfo = getHapPresetInfo(true);
        when(mHapClientProfile.getAllPresetInfo(mBluetoothDevice)).thenReturn(
                List.of(hapPresetInfo));
        when(mHapClientProfile.getActivePresetIndex(mBluetoothDevice)).thenReturn(
                TEST_PRESET_INDEX);
    public void refreshPresetInfo_callbackIsCalledWhenNeeded() {
        List<BluetoothHapPresetInfo> infos = preparePresetInfo(/* isValid= */ true);

        mController.refreshPresetInfo();

        verify(mCallback).onPresetInfoUpdated(infos, TEST_PRESET_INDEX);

        Mockito.reset(mCallback);
        mController.refreshPresetInfo();

        verify(mCallback, never()).onPresetInfoUpdated(anyList(), anyInt());

        Mockito.reset(mCallback);
        when(mHapClientProfile.getActivePresetIndex(mDevice)).thenReturn(TEST_UPDATED_PRESET_INDEX);
        mController.refreshPresetInfo();

        verify(mCallback).onPresetInfoUpdated(infos, TEST_UPDATED_PRESET_INDEX);
    }

    @Test
    public void onPresetSelected_callOnPresetInfoUpdatedWithExpectedPresetIndex() {
        List<BluetoothHapPresetInfo> infos = preparePresetInfo(/* isValid= */ true);

        mController.onPresetSelected(mBluetoothDevice, TEST_PRESET_INDEX, TEST_REASON);
        mController.onPresetSelected(mDevice, TEST_PRESET_INDEX, TEST_REASON);

        verify(mCallback).onPresetInfoUpdated(eq(List.of(hapPresetInfo)), eq(TEST_PRESET_INDEX));
        verify(mCallback).onPresetInfoUpdated(infos, TEST_PRESET_INDEX);
    }

    @Test
    public void onPresetInfoChanged_presetIndex_callOnPresetInfoUpdatedWithExpectedPresetIndex() {
        setValidHearingDeviceSupportHap();
        BluetoothHapPresetInfo hapPresetInfo = getHapPresetInfo(true);
        when(mHapClientProfile.getAllPresetInfo(mBluetoothDevice)).thenReturn(
                List.of(hapPresetInfo));
        when(mHapClientProfile.getActivePresetIndex(mBluetoothDevice)).thenReturn(
                TEST_PRESET_INDEX);
    public void onPresetInfoChanged_callOnPresetInfoUpdatedWithExpectedPresetIndex() {
        List<BluetoothHapPresetInfo> infos = preparePresetInfo(/* isValid= */ true);

        mController.onPresetInfoChanged(mBluetoothDevice, List.of(hapPresetInfo), TEST_REASON);
        mController.onPresetInfoChanged(mDevice, infos, TEST_REASON);

        verify(mCallback).onPresetInfoUpdated(List.of(hapPresetInfo), TEST_PRESET_INDEX);
        verify(mCallback).onPresetInfoUpdated(infos, TEST_PRESET_INDEX);
    }

    @Test
    public void onPresetSelectionFailed_callOnPresetCommandFailed() {
        setValidHearingDeviceSupportHap();

        mController.onPresetSelectionFailed(mBluetoothDevice, TEST_REASON);
        mController.onPresetSelectionFailed(mDevice, TEST_REASON);

        verify(mCallback).onPresetCommandFailed(TEST_REASON);
    }

    @Test
    public void onSetPresetNameFailed_callOnPresetCommandFailed() {
        setValidHearingDeviceSupportHap();

        mController.onSetPresetNameFailed(mBluetoothDevice, TEST_REASON);
        mController.onSetPresetNameFailed(mDevice, TEST_REASON);

        verify(mCallback).onPresetCommandFailed(TEST_REASON);
    }

    @Test
    public void onPresetSelectionForGroupFailed_callSelectPresetIndividual() {
        setValidHearingDeviceSupportHap();
    public void onPresetSelectionForGroupFailed_callSelectPresetIndependently() {
        mController.selectPreset(TEST_PRESET_INDEX);
        Mockito.reset(mHapClientProfile);
        when(mHapClientProfile.getHapGroup(mBluetoothDevice)).thenReturn(TEST_HAP_GROUP_ID);
        when(mHapClientProfile.getHapGroup(mDevice)).thenReturn(TEST_HAP_GROUP_ID);

        mController.onPresetSelectionForGroupFailed(TEST_HAP_GROUP_ID, TEST_REASON);


        verify(mHapClientProfile).selectPreset(mBluetoothDevice, TEST_PRESET_INDEX);
        verify(mHapClientProfile).selectPreset(mSubBluetoothDevice, TEST_PRESET_INDEX);
        verify(mHapClientProfile).selectPreset(mDevice, TEST_PRESET_INDEX);
        verify(mHapClientProfile).selectPreset(mMemberDevice, TEST_PRESET_INDEX);
    }

    @Test
    public void onSetPresetNameForGroupFailed_callOnPresetCommandFailed() {
        setValidHearingDeviceSupportHap();

        mController.onSetPresetNameForGroupFailed(TEST_HAP_GROUP_ID, TEST_REASON);

        verify(mCallback).onPresetCommandFailed(TEST_REASON);
    }

    @Test
    public void registerHapCallback_profileNotReady_addServiceListener() {
        when(mHapClientProfile.isProfileReady()).thenReturn(false);

        mController.registerHapCallback();

        verify(mProfileManager).addServiceListener(mController);
        verify(mHapClientProfile, never()).registerCallback(any(Executor.class),
                any(BluetoothHapClient.Callback.class));
    }

    @Test
    public void registerHapCallback_callHapRegisterCallback() {
        mController.registerHapCallback();
@@ -233,9 +251,8 @@ public class HearingDevicesPresetsControllerTest extends SysuiTestCase {

    @Test
    public void selectPreset_supportSynchronized_validGroupId_callSelectPresetForGroup() {
        setValidHearingDeviceSupportHap();
        when(mHapClientProfile.supportsSynchronizedPresets(mBluetoothDevice)).thenReturn(true);
        when(mHapClientProfile.getHapGroup(mBluetoothDevice)).thenReturn(TEST_HAP_GROUP_ID);
        when(mHapClientProfile.supportsSynchronizedPresets(mDevice)).thenReturn(true);
        when(mHapClientProfile.getHapGroup(mDevice)).thenReturn(TEST_HAP_GROUP_ID);

        mController.selectPreset(TEST_PRESET_INDEX);

@@ -243,28 +260,34 @@ public class HearingDevicesPresetsControllerTest extends SysuiTestCase {
    }

    @Test
    public void selectPreset_supportSynchronized_invalidGroupId_callSelectPresetIndividual() {
        setValidHearingDeviceSupportHap();
        when(mHapClientProfile.supportsSynchronizedPresets(mBluetoothDevice)).thenReturn(true);
        when(mHapClientProfile.getHapGroup(mBluetoothDevice)).thenReturn(
    public void selectPreset_supportSynchronized_invalidGroupId_callSelectPresetIndependently() {
        when(mHapClientProfile.supportsSynchronizedPresets(mDevice)).thenReturn(true);
        when(mHapClientProfile.getHapGroup(mDevice)).thenReturn(
                BluetoothCsipSetCoordinator.GROUP_ID_INVALID);

        mController.selectPreset(TEST_PRESET_INDEX);

        verify(mHapClientProfile).selectPreset(mBluetoothDevice, TEST_PRESET_INDEX);
        verify(mHapClientProfile).selectPreset(mSubBluetoothDevice, TEST_PRESET_INDEX);
        verify(mHapClientProfile).selectPreset(mDevice, TEST_PRESET_INDEX);
        verify(mHapClientProfile).selectPreset(mMemberDevice, TEST_PRESET_INDEX);
    }

    @Test
    public void selectPreset_notSupportSynchronized_validGroupId_callSelectPresetIndividual() {
        setValidHearingDeviceSupportHap();
        when(mHapClientProfile.supportsSynchronizedPresets(mBluetoothDevice)).thenReturn(false);
        when(mHapClientProfile.getHapGroup(mBluetoothDevice)).thenReturn(TEST_HAP_GROUP_ID);
    public void selectPreset_notSupportSynchronized_validGroupId_callSelectPresetIndependently() {
        when(mHapClientProfile.supportsSynchronizedPresets(mDevice)).thenReturn(false);
        when(mHapClientProfile.getHapGroup(mDevice)).thenReturn(TEST_HAP_GROUP_ID);

        mController.selectPreset(TEST_PRESET_INDEX);

        verify(mHapClientProfile).selectPreset(mBluetoothDevice, TEST_PRESET_INDEX);
        verify(mHapClientProfile).selectPreset(mSubBluetoothDevice, TEST_PRESET_INDEX);
        verify(mHapClientProfile).selectPreset(mDevice, TEST_PRESET_INDEX);
        verify(mHapClientProfile).selectPreset(mMemberDevice, TEST_PRESET_INDEX);
    }

    private List<BluetoothHapPresetInfo> preparePresetInfo(boolean isValid) {
        BluetoothHapPresetInfo info = getHapPresetInfo(isValid);
        List<BluetoothHapPresetInfo> infos = List.of(info);
        when(mHapClientProfile.getAllPresetInfo(mDevice)).thenReturn(infos);
        when(mHapClientProfile.getActivePresetIndex(mDevice)).thenReturn(TEST_PRESET_INDEX);
        return infos;
    }

    private BluetoothHapPresetInfo getHapPresetInfo(boolean available) {
@@ -274,12 +297,4 @@ public class HearingDevicesPresetsControllerTest extends SysuiTestCase {
        when(info.isAvailable()).thenReturn(available);
        return info;
    }

    private void setValidHearingDeviceSupportHap() {
        LocalBluetoothProfile hapClientProfile = mock(HapClientProfile.class);
        List<LocalBluetoothProfile> profiles = List.of(hapClientProfile);
        when(mCachedBluetoothDevice.getProfiles()).thenReturn(profiles);

        mController.setHearingDeviceIfSupportHap(mCachedBluetoothDevice);
    }
}
+53 −85

File changed.

Preview size limit exceeded, changes collapsed.

+119 −74

File changed.

Preview size limit exceeded, changes collapsed.