diff options
5 files changed, 284 insertions, 110 deletions
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/HapClientProfile.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/HapClientProfile.java index cf4d6be9a042..0613676113f5 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/HapClientProfile.java +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/HapClientProfile.java @@ -99,6 +99,8 @@ public class HapClientProfile implements LocalBluetoothProfile { device.refresh(); } + // Check current list of CachedDevices to see if any are hearing aid devices. + mDeviceManager.updateHearingAidsDevices(); mIsProfileReady = true; mProfileManager.callServiceConnectedListeners(); } diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/HearingAidDeviceManager.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/HearingAidDeviceManager.java index 3a15b7108e3f..9fd174d4586c 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/HearingAidDeviceManager.java +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/HearingAidDeviceManager.java @@ -15,8 +15,8 @@ */ package com.android.settingslib.bluetooth; -import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothHearingAid; +import android.bluetooth.BluetoothLeAudio; import android.bluetooth.BluetoothProfile; import android.bluetooth.BluetoothUuid; import android.bluetooth.le.ScanFilter; @@ -68,14 +68,9 @@ public class HearingAidDeviceManager { void initHearingAidDeviceIfNeeded(CachedBluetoothDevice newDevice, List<ScanFilter> leScanFilters) { - long hiSyncId = getHiSyncId(newDevice.getDevice()); - if (isValidHiSyncId(hiSyncId)) { - // Once hiSyncId is valid, assign hearing aid info - final HearingAidInfo.Builder infoBuilder = new HearingAidInfo.Builder() - .setAshaDeviceSide(getDeviceSide(newDevice.getDevice())) - .setAshaDeviceMode(getDeviceMode(newDevice.getDevice())) - .setHiSyncId(hiSyncId); - newDevice.setHearingAidInfo(infoBuilder.build()); + HearingAidInfo info = generateHearingAidInfo(newDevice); + if (info != null) { + newDevice.setHearingAidInfo(info); } else if (leScanFilters != null && !newDevice.isHearingAidDevice()) { // If the device is added with hearing aid scan filter during pairing, set an empty // hearing aid info to indicate it's a hearing aid device. The info will be updated @@ -94,38 +89,6 @@ public class HearingAidDeviceManager { } } - private long getHiSyncId(BluetoothDevice device) { - final LocalBluetoothProfileManager profileManager = mBtManager.getProfileManager(); - final HearingAidProfile profileProxy = profileManager.getHearingAidProfile(); - if (profileProxy == null) { - return BluetoothHearingAid.HI_SYNC_ID_INVALID; - } - - return profileProxy.getHiSyncId(device); - } - - private int getDeviceSide(BluetoothDevice device) { - final LocalBluetoothProfileManager profileManager = mBtManager.getProfileManager(); - final HearingAidProfile profileProxy = profileManager.getHearingAidProfile(); - if (profileProxy == null) { - Log.w(TAG, "HearingAidProfile is not supported and not ready to fetch device side"); - return HearingAidProfile.DeviceSide.SIDE_INVALID; - } - - return profileProxy.getDeviceSide(device); - } - - private int getDeviceMode(BluetoothDevice device) { - final LocalBluetoothProfileManager profileManager = mBtManager.getProfileManager(); - final HearingAidProfile profileProxy = profileManager.getHearingAidProfile(); - if (profileProxy == null) { - Log.w(TAG, "HearingAidProfile is not supported and not ready to fetch device mode"); - return HearingAidProfile.DeviceMode.MODE_INVALID; - } - - return profileProxy.getDeviceMode(device); - } - boolean setSubDeviceIfNeeded(CachedBluetoothDevice newDevice) { final long hiSyncId = newDevice.getHiSyncId(); if (isValidHiSyncId(hiSyncId)) { @@ -157,21 +120,17 @@ public class HearingAidDeviceManager { // To collect all HearingAid devices and call #onHiSyncIdChanged to group device by HiSyncId void updateHearingAidsDevices() { - final Set<Long> newSyncIdSet = new HashSet<Long>(); + final Set<Long> newSyncIdSet = new HashSet<>(); for (CachedBluetoothDevice cachedDevice : mCachedDevices) { // Do nothing if HiSyncId has been assigned - if (!isValidHiSyncId(cachedDevice.getHiSyncId())) { - final long newHiSyncId = getHiSyncId(cachedDevice.getDevice()); - // Do nothing if there is no HiSyncId on Bluetooth device - if (isValidHiSyncId(newHiSyncId)) { - // Once hiSyncId is valid, assign hearing aid info - final HearingAidInfo.Builder infoBuilder = new HearingAidInfo.Builder() - .setAshaDeviceSide(getDeviceSide(cachedDevice.getDevice())) - .setAshaDeviceMode(getDeviceMode(cachedDevice.getDevice())) - .setHiSyncId(newHiSyncId); - cachedDevice.setHearingAidInfo(infoBuilder.build()); - - newSyncIdSet.add(newHiSyncId); + if (isValidHiSyncId(cachedDevice.getHiSyncId())) { + continue; + } + HearingAidInfo info = generateHearingAidInfo(cachedDevice); + if (info != null) { + cachedDevice.setHearingAidInfo(info); + if (isValidHiSyncId(info.getHiSyncId())) { + newSyncIdSet.add(info.getHiSyncId()); } } } @@ -378,6 +337,54 @@ public class HearingAidDeviceManager { return null; } + private boolean isLeAudioHearingAid(CachedBluetoothDevice cachedDevice) { + List<LocalBluetoothProfile> profiles = cachedDevice.getProfiles(); + boolean supportLeAudio = profiles.stream().anyMatch(p -> p instanceof LeAudioProfile); + boolean supportHapClient = profiles.stream().anyMatch(p -> p instanceof HapClientProfile); + return supportLeAudio && supportHapClient; + } + + private boolean isAshaHearingAid(CachedBluetoothDevice cachedDevice) { + return cachedDevice.getProfiles().stream().anyMatch(p -> p instanceof HearingAidProfile); + } + + private HearingAidInfo generateHearingAidInfo(CachedBluetoothDevice cachedDevice) { + final LocalBluetoothProfileManager profileManager = mBtManager.getProfileManager(); + if (isAshaHearingAid(cachedDevice)) { + final HearingAidProfile asha = profileManager.getHearingAidProfile(); + if (asha == null) { + Log.w(TAG, "HearingAidProfile is not supported on this device"); + } else { + long hiSyncId = asha.getHiSyncId(cachedDevice.getDevice()); + if (isValidHiSyncId(hiSyncId)) { + final HearingAidInfo.Builder infoBuilder = new HearingAidInfo.Builder() + .setAshaDeviceSide(asha.getDeviceSide(cachedDevice.getDevice())) + .setAshaDeviceMode(asha.getDeviceMode(cachedDevice.getDevice())) + .setHiSyncId(hiSyncId); + return infoBuilder.build(); + } + } + } + if (isLeAudioHearingAid(cachedDevice)) { + final HapClientProfile hapClientProfile = profileManager.getHapClientProfile(); + final LeAudioProfile leAudioProfile = profileManager.getLeAudioProfile(); + if (hapClientProfile == null || leAudioProfile == null) { + Log.w(TAG, "HapClientProfile or LeAudioProfile is not supported on this device"); + } else { + int audioLocation = leAudioProfile.getAudioLocation(cachedDevice.getDevice()); + int hearingAidType = hapClientProfile.getHearingAidType(cachedDevice.getDevice()); + if (audioLocation != BluetoothLeAudio.AUDIO_LOCATION_INVALID + && hearingAidType != HapClientProfile.HearingAidType.TYPE_INVALID) { + final HearingAidInfo.Builder infoBuilder = new HearingAidInfo.Builder() + .setLeAudioLocation(audioLocation) + .setHapDeviceType(hearingAidType); + return infoBuilder.build(); + } + } + } + return null; + } + private void log(String msg) { if (DEBUG) { Log.d(TAG, msg); diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/HearingAidProfile.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/HearingAidProfile.java index 14fab16de3e6..f2450de60878 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/HearingAidProfile.java +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/HearingAidProfile.java @@ -109,7 +109,7 @@ public class HearingAidProfile implements LocalBluetoothProfile { device.refresh(); } - // Check current list of CachedDevices to see if any are Hearing Aid devices. + // Check current list of CachedDevices to see if any are hearing aid devices. mDeviceManager.updateHearingAidsDevices(); mIsProfileReady = true; mProfileManager.callServiceConnectedListeners(); diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/LeAudioProfile.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/LeAudioProfile.java index 57012aabb123..03ac1f43792e 100644 --- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/LeAudioProfile.java +++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/LeAudioProfile.java @@ -83,6 +83,8 @@ public class LeAudioProfile implements LocalBluetoothProfile { device.refresh(); } + // Check current list of CachedDevices to see if any are hearing aid devices. + mDeviceManager.updateHearingAidsDevices(); mProfileManager.callServiceConnectedListeners(); mIsProfileReady = true; } diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/HearingAidDeviceManagerTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/HearingAidDeviceManagerTest.java index e7487e857464..aa5a2984e70c 100644 --- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/HearingAidDeviceManagerTest.java +++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/bluetooth/HearingAidDeviceManagerTest.java @@ -15,6 +15,15 @@ */ package com.android.settingslib.bluetooth; +import static android.bluetooth.BluetoothHearingAid.HI_SYNC_ID_INVALID; +import static android.bluetooth.BluetoothLeAudio.AUDIO_LOCATION_FRONT_LEFT; +import static android.bluetooth.BluetoothLeAudio.AUDIO_LOCATION_INVALID; + +import static com.android.settingslib.bluetooth.HapClientProfile.HearingAidType.TYPE_BINAURAL; +import static com.android.settingslib.bluetooth.HapClientProfile.HearingAidType.TYPE_INVALID; +import static com.android.settingslib.bluetooth.HearingAidProfile.DeviceMode.MODE_BINAURAL; +import static com.android.settingslib.bluetooth.HearingAidProfile.DeviceSide.SIDE_RIGHT; + import static com.google.common.truth.Truth.assertThat; import static org.mockito.ArgumentMatchers.any; @@ -32,7 +41,6 @@ import static org.mockito.Mockito.when; import android.bluetooth.BluetoothClass; import android.bluetooth.BluetoothDevice; -import android.bluetooth.BluetoothHearingAid; import android.bluetooth.BluetoothProfile; import android.bluetooth.BluetoothUuid; import android.bluetooth.le.ScanFilter; @@ -92,6 +100,10 @@ public class HearingAidDeviceManagerTest { @Mock private HearingAidProfile mHearingAidProfile; @Mock + private LeAudioProfile mLeAudioProfile; + @Mock + private HapClientProfile mHapClientProfile; + @Mock private AudioProductStrategy mAudioStrategy; @Mock private BluetoothDevice mDevice1; @@ -123,6 +135,8 @@ public class HearingAidDeviceManagerTest { when(mLocalBluetoothManager.getEventManager()).thenReturn(mBluetoothEventManager); when(mLocalBluetoothManager.getProfileManager()).thenReturn(mLocalProfileManager); when(mLocalProfileManager.getHearingAidProfile()).thenReturn(mHearingAidProfile); + when(mLocalProfileManager.getLeAudioProfile()).thenReturn(mLeAudioProfile); + when(mLocalProfileManager.getHapClientProfile()).thenReturn(mHapClientProfile); when(mAudioStrategy.getAudioAttributesForLegacyStreamType( AudioManager.STREAM_MUSIC)) .thenReturn((new AudioAttributes.Builder()).build()); @@ -140,34 +154,43 @@ public class HearingAidDeviceManagerTest { } /** - * Test initHearingAidDeviceIfNeeded, set HearingAid's information, including HiSyncId, - * deviceSide, deviceMode. + * Test initHearingAidDeviceIfNeeded + * + * Conditions: + * 1) ASHA hearing aid + * 2) Valid HiSyncId + * Result: + * Set hearing aid info to the device. */ @Test - public void initHearingAidDeviceIfNeeded_validHiSyncId_setHearingAidInfo() { + public void initHearingAidDeviceIfNeeded_asha_validHiSyncId_setHearingAidInfo() { + when(mCachedDevice1.getProfiles()).thenReturn(List.of(mHearingAidProfile)); when(mHearingAidProfile.getHiSyncId(mDevice1)).thenReturn(HISYNCID1); - when(mHearingAidProfile.getDeviceMode(mDevice1)).thenReturn( - HearingAidProfile.DeviceMode.MODE_BINAURAL); - when(mHearingAidProfile.getDeviceSide(mDevice1)).thenReturn( - HearingAidProfile.DeviceSide.SIDE_RIGHT); + when(mHearingAidProfile.getDeviceMode(mDevice1)).thenReturn(MODE_BINAURAL); + when(mHearingAidProfile.getDeviceSide(mDevice1)).thenReturn(SIDE_RIGHT); assertThat(mCachedDevice1.getHiSyncId()).isNotEqualTo(HISYNCID1); mHearingAidDeviceManager.initHearingAidDeviceIfNeeded(mCachedDevice1, null); assertThat(mCachedDevice1.getHiSyncId()).isEqualTo(HISYNCID1); + assertThat(mCachedDevice1.getDeviceSide()).isEqualTo(HearingAidInfo.DeviceSide.SIDE_RIGHT); assertThat(mCachedDevice1.getDeviceMode()).isEqualTo( HearingAidInfo.DeviceMode.MODE_BINAURAL); - assertThat(mCachedDevice1.getDeviceSide()).isEqualTo( - HearingAidInfo.DeviceSide.SIDE_RIGHT); } /** - * Test initHearingAidDeviceIfNeeded, an invalid HiSyncId will not be assigned + * Test initHearingAidDeviceIfNeeded + * + * Conditions: + * 1) ASHA hearing aid + * 2) Invalid HiSyncId + * Result: + * Do not set hearing aid info to the device. */ @Test - public void initHearingAidDeviceIfNeeded_invalidHiSyncId_notToSetHearingAidInfo() { - when(mHearingAidProfile.getHiSyncId(mDevice1)).thenReturn( - BluetoothHearingAid.HI_SYNC_ID_INVALID); + public void initHearingAidDeviceIfNeeded_asha_invalidHiSyncId_notToSetHearingAidInfo() { + when(mCachedDevice1.getProfiles()).thenReturn(List.of(mHearingAidProfile)); + when(mHearingAidProfile.getHiSyncId(mDevice1)).thenReturn(HI_SYNC_ID_INVALID); mHearingAidDeviceManager.initHearingAidDeviceIfNeeded(mCachedDevice1, null); @@ -175,34 +198,89 @@ public class HearingAidDeviceManagerTest { } /** - * Test initHearingAidDeviceIfNeeded, an invalid HiSyncId and hearing aid scan filter, set an - * empty hearing aid info on the device. + * Test initHearingAidDeviceIfNeeded + * + * Conditions: + * 1) ASHA hearing aid + * 2) Invalid HiSyncId + * 3) ASHA uuid scan filter + * Result: + * Set an empty hearing aid info to the device. */ @Test - public void initHearingAidDeviceIfNeeded_hearingAidScanFilter_setHearingAidInfo() { - when(mHearingAidProfile.getHiSyncId(mDevice1)).thenReturn( - BluetoothHearingAid.HI_SYNC_ID_INVALID); + public void initHearingAidDeviceIfNeeded_asha_scanFilterNotNull_setEmptyHearingAidInfo() { + when(mCachedDevice1.getProfiles()).thenReturn(List.of(mHearingAidProfile)); + when(mHearingAidProfile.getHiSyncId(mDevice1)).thenReturn(HI_SYNC_ID_INVALID); final ScanFilter scanFilter = new ScanFilter.Builder() .setServiceData(BluetoothUuid.HEARING_AID, new byte[]{0}).build(); mHearingAidDeviceManager.initHearingAidDeviceIfNeeded(mCachedDevice1, List.of(scanFilter)); - assertThat(mCachedDevice1.isHearingAidDevice()).isTrue(); + verify(mCachedDevice1).setHearingAidInfo(new HearingAidInfo.Builder().build()); } /** - * Test initHearingAidDeviceIfNeeded, an invalid HiSyncId and random scan filter, not to set - * hearing aid info on the device. + * Test initHearingAidDeviceIfNeeded + * + * Conditions: + * 1) Asha hearing aid + * 2) Invalid HiSyncId + * 3) Random scan filter + * Result: + * Do not set hearing aid info to the device. */ @Test - public void initHearingAidDeviceIfNeeded_randomScanFilter_setHearingAidInfo() { - when(mHearingAidProfile.getHiSyncId(mDevice1)).thenReturn( - BluetoothHearingAid.HI_SYNC_ID_INVALID); + public void initHearingAidDeviceIfNeeded_asha_randomScanFilter_notToSetHearingAidInfo() { + when(mCachedDevice1.getProfiles()).thenReturn(List.of(mHearingAidProfile)); + when(mHearingAidProfile.getHiSyncId(mDevice1)).thenReturn(HI_SYNC_ID_INVALID); final ScanFilter scanFilter = new ScanFilter.Builder().build(); mHearingAidDeviceManager.initHearingAidDeviceIfNeeded(mCachedDevice1, List.of(scanFilter)); - assertThat(mCachedDevice1.isHearingAidDevice()).isFalse(); + verify(mCachedDevice1, never()).setHearingAidInfo(any(HearingAidInfo.class)); + } + + /** + * Test initHearingAidDeviceIfNeeded + * + * Conditions: + * 1) LeAudio hearing aid + * 2) Valid audio location and device type + * Result: + * Set hearing aid info to the device. + */ + @Test + public void initHearingAidDeviceIfNeeded_leAudio_validInfo_setHearingAidInfo() { + when(mCachedDevice1.getProfiles()).thenReturn(List.of(mLeAudioProfile, mHapClientProfile)); + when(mLeAudioProfile.getAudioLocation(mDevice1)).thenReturn(AUDIO_LOCATION_FRONT_LEFT); + when(mHapClientProfile.getHearingAidType(mDevice1)).thenReturn(TYPE_BINAURAL); + + mHearingAidDeviceManager.initHearingAidDeviceIfNeeded(mCachedDevice1, null); + + verify(mCachedDevice1).setHearingAidInfo(any(HearingAidInfo.class)); + assertThat(mCachedDevice1.getDeviceSide()).isEqualTo(HearingAidInfo.DeviceSide.SIDE_LEFT); + assertThat(mCachedDevice1.getDeviceMode()).isEqualTo( + HearingAidInfo.DeviceMode.MODE_BINAURAL); + } + + /** + * Test initHearingAidDeviceIfNeeded + * + * Conditions: + * 1) LeAudio hearing aid + * 2) Invalid audio location and device type + * Result: + * Do not set hearing aid info to the device. + */ + @Test + public void initHearingAidDeviceIfNeeded_leAudio_invalidInfo_notToSetHearingAidInfo() { + when(mCachedDevice1.getProfiles()).thenReturn(List.of(mLeAudioProfile, mHapClientProfile)); + when(mLeAudioProfile.getAudioLocation(mDevice1)).thenReturn(AUDIO_LOCATION_INVALID); + when(mHapClientProfile.getHearingAidType(mDevice1)).thenReturn(TYPE_INVALID); + + mHearingAidDeviceManager.initHearingAidDeviceIfNeeded(mCachedDevice1, null); + + verify(mCachedDevice1, never()).setHearingAidInfo(any(HearingAidInfo.class)); } /** @@ -234,13 +312,20 @@ public class HearingAidDeviceManagerTest { } /** - * Test updateHearingAidsDevices, to link two devices with the same HiSyncId. - * When first paired devices is connected and second paired device is disconnected, first - * paired device would be set as main device and second device will be removed from - * CachedDevices list. + * Test updateHearingAidsDevices + * + * Conditions: + * 1) Two ASHA hearing aids with the same HiSyncId + * 2) First paired devices is connected + * 3) Second paired device is disconnected + * Result: + * First paired device would be set as main device and second paired device will be set + * as sub device and removed from CachedDevices list. */ @Test - public void updateHearingAidsDevices_firstPairedDevicesConnected_verifySubDevice() { + public void updateHearingAidsDevices_asha_firstPairedDevicesConnected_verifySubDevice() { + when(mCachedDevice1.getProfiles()).thenReturn(List.of(mHearingAidProfile)); + when(mCachedDevice2.getProfiles()).thenReturn(List.of(mHearingAidProfile)); when(mHearingAidProfile.getHiSyncId(mDevice1)).thenReturn(HISYNCID1); when(mHearingAidProfile.getHiSyncId(mDevice2)).thenReturn(HISYNCID1); when(mCachedDevice1.isConnected()).thenReturn(true); @@ -257,13 +342,20 @@ public class HearingAidDeviceManagerTest { } /** - * Test updateHearingAidsDevices, to link two devices with the same HiSyncId. - * When second paired devices is connected and first paired device is disconnected, second - * paired device would be set as main device and first device will be removed from - * CachedDevices list. + * Test updateHearingAidsDevices + * + * Conditions: + * 1) Two ASHA hearing aids with the same HiSyncId + * 2) First paired devices is disconnected + * 3) Second paired device is connected + * Result: + * Second paired device would be set as main device and first paired device will be set + * as sub device and removed from CachedDevices list. */ @Test - public void updateHearingAidsDevices_secondPairedDeviceConnected_verifySubDevice() { + public void updateHearingAidsDevices_asha_secondPairedDeviceConnected_verifySubDevice() { + when(mCachedDevice1.getProfiles()).thenReturn(List.of(mHearingAidProfile)); + when(mCachedDevice2.getProfiles()).thenReturn(List.of(mHearingAidProfile)); when(mHearingAidProfile.getHiSyncId(mDevice1)).thenReturn(HISYNCID1); when(mHearingAidProfile.getHiSyncId(mDevice2)).thenReturn(HISYNCID1); when(mCachedDevice1.isConnected()).thenReturn(false); @@ -280,12 +372,20 @@ public class HearingAidDeviceManagerTest { } /** - * Test updateHearingAidsDevices, to link two devices with the same HiSyncId. - * When both devices are connected, to build up main and sub relationship and to remove sub - * device from CachedDevices list. + * Test updateHearingAidsDevices + * + * Conditions: + * 1) Two ASHA hearing aids with the same HiSyncId + * 2) First paired devices is connected + * 3) Second paired device is connected + * Result: + * First paired device would be set as main device and second paired device will be set + * as sub device and removed from CachedDevices list. */ @Test - public void updateHearingAidsDevices_BothConnected_verifySubDevice() { + public void updateHearingAidsDevices_asha_bothConnected_verifySubDevice() { + when(mCachedDevice1.getProfiles()).thenReturn(List.of(mHearingAidProfile)); + when(mCachedDevice2.getProfiles()).thenReturn(List.of(mHearingAidProfile)); when(mHearingAidProfile.getHiSyncId(mDevice1)).thenReturn(HISYNCID1); when(mHearingAidProfile.getHiSyncId(mDevice2)).thenReturn(HISYNCID1); when(mCachedDevice1.isConnected()).thenReturn(true); @@ -302,46 +402,64 @@ public class HearingAidDeviceManagerTest { } /** - * Test updateHearingAidsDevices, dispatch callback + * Test updateHearingAidsDevices + * + * Conditions: + * 1) Two ASHA hearing aids with the same HiSyncId + * Result: + * Dispatch device removed callback */ @Test - public void updateHearingAidsDevices_dispatchDeviceRemovedCallback() { + public void updateHearingAidsDevices_asha_dispatchDeviceRemovedCallback() { + when(mCachedDevice1.getProfiles()).thenReturn(List.of(mHearingAidProfile)); + when(mCachedDevice2.getProfiles()).thenReturn(List.of(mHearingAidProfile)); when(mHearingAidProfile.getHiSyncId(mDevice1)).thenReturn(HISYNCID1); when(mHearingAidProfile.getHiSyncId(mDevice2)).thenReturn(HISYNCID1); mCachedDeviceManager.mCachedDevices.add(mCachedDevice1); mCachedDeviceManager.mCachedDevices.add(mCachedDevice2); + mHearingAidDeviceManager.updateHearingAidsDevices(); verify(mBluetoothEventManager).dispatchDeviceRemoved(mCachedDevice1); } /** - * Test updateHearingAidsDevices, do nothing when HiSyncId is invalid + * Test updateHearingAidsDevices + * + * Conditions: + * 1) Two ASHA hearing aids with invalid HiSyncId + * Result: + * Do nothing */ @Test - public void updateHearingAidsDevices_invalidHiSyncId_doNothing() { - when(mHearingAidProfile.getHiSyncId(mDevice1)). - thenReturn(BluetoothHearingAid.HI_SYNC_ID_INVALID); - when(mHearingAidProfile.getHiSyncId(mDevice2)). - thenReturn(BluetoothHearingAid.HI_SYNC_ID_INVALID); + public void updateHearingAidsDevices_asha_invalidHiSyncId_doNothing() { + when(mCachedDevice1.getProfiles()).thenReturn(List.of(mHearingAidProfile)); + when(mCachedDevice2.getProfiles()).thenReturn(List.of(mHearingAidProfile)); + when(mHearingAidProfile.getHiSyncId(mDevice1)).thenReturn(HI_SYNC_ID_INVALID); + when(mHearingAidProfile.getHiSyncId(mDevice2)).thenReturn(HI_SYNC_ID_INVALID); mCachedDeviceManager.mCachedDevices.add(mCachedDevice1); mCachedDeviceManager.mCachedDevices.add(mCachedDevice2); + mHearingAidDeviceManager.updateHearingAidsDevices(); verify(mHearingAidDeviceManager, never()).onHiSyncIdChanged(anyLong()); } /** - * Test updateHearingAidsDevices, set HearingAid's information, including HiSyncId, deviceSide, - * deviceMode. + * Test updateHearingAidsDevices + * + * Conditions: + * 1) ASHA hearing aids + * 2) Valid HiSync Id + * Result: + * Set hearing aid info to the device. */ @Test - public void updateHearingAidsDevices_validHiSyncId_setHearingAidInfos() { + public void updateHearingAidsDevices_asha_validHiSyncId_setHearingAidInfo() { + when(mCachedDevice1.getProfiles()).thenReturn(List.of(mHearingAidProfile)); when(mHearingAidProfile.getHiSyncId(mDevice1)).thenReturn(HISYNCID1); - when(mHearingAidProfile.getDeviceMode(mDevice1)).thenReturn( - HearingAidProfile.DeviceMode.MODE_BINAURAL); - when(mHearingAidProfile.getDeviceSide(mDevice1)).thenReturn( - HearingAidProfile.DeviceSide.SIDE_RIGHT); + when(mHearingAidProfile.getDeviceMode(mDevice1)).thenReturn(MODE_BINAURAL); + when(mHearingAidProfile.getDeviceSide(mDevice1)).thenReturn(SIDE_RIGHT); mCachedDeviceManager.mCachedDevices.add(mCachedDevice1); mHearingAidDeviceManager.updateHearingAidsDevices(); @@ -355,6 +473,51 @@ public class HearingAidDeviceManagerTest { } /** + * Test updateHearingAidsDevices + * + * Conditions: + * 1) LeAudio hearing aid + * 2) Valid audio location and device type + * Result: + * Set hearing aid info to the device. + */ + @Test + public void updateHearingAidsDevices_leAudio_validInfo_setHearingAidInfo() { + when(mCachedDevice1.getProfiles()).thenReturn(List.of(mLeAudioProfile, mHapClientProfile)); + when(mLeAudioProfile.getAudioLocation(mDevice1)).thenReturn(AUDIO_LOCATION_FRONT_LEFT); + when(mHapClientProfile.getHearingAidType(mDevice1)).thenReturn(TYPE_BINAURAL); + mCachedDeviceManager.mCachedDevices.add(mCachedDevice1); + + mHearingAidDeviceManager.updateHearingAidsDevices(); + + verify(mCachedDevice1).setHearingAidInfo(any(HearingAidInfo.class)); + assertThat(mCachedDevice1.getDeviceSide()).isEqualTo(HearingAidInfo.DeviceSide.SIDE_LEFT); + assertThat(mCachedDevice1.getDeviceMode()).isEqualTo( + HearingAidInfo.DeviceMode.MODE_BINAURAL); + } + + /** + * Test updateHearingAidsDevices + * + * Conditions: + * 1) LeAudio hearing aid + * 2) Invalid audio location and device type + * Result: + * Do not set hearing aid info to the device. + */ + @Test + public void updateHearingAidsDevices_leAudio_invalidInfo_notToSetHearingAidInfo() { + when(mCachedDevice1.getProfiles()).thenReturn(List.of(mLeAudioProfile, mHapClientProfile)); + when(mLeAudioProfile.getAudioLocation(mDevice1)).thenReturn(AUDIO_LOCATION_INVALID); + when(mHapClientProfile.getHearingAidType(mDevice1)).thenReturn(TYPE_INVALID); + mCachedDeviceManager.mCachedDevices.add(mCachedDevice1); + + mHearingAidDeviceManager.updateHearingAidsDevices(); + + verify(mCachedDevice1, never()).setHearingAidInfo(any(HearingAidInfo.class)); + } + + /** * Test onProfileConnectionStateChangedIfProcessed. * When first hearing aid device is connected, to process it same as other generic devices. * No need to process it. |