Make HearingAid code more generic

-handle UI updating when sub device connection state changes
-add test case

Bug: 112735753
Test: make -j42 RunSettingsRoboTests
Change-Id: Ie2643657c47a0956aac3f8cac4bfdbdea0399ce8
This commit is contained in:
timhypeng
2018-10-09 11:19:23 +08:00
committed by Hugh Chen
parent 2f5200ea5a
commit c95056d7b5
5 changed files with 36 additions and 7 deletions

View File

@@ -65,12 +65,10 @@ public class BluetoothDetailsHeaderController extends BluetoothDetailsController
.getBtClassDrawableWithDescription(mContext, mCachedDevice, .getBtClassDrawableWithDescription(mContext, mCachedDevice,
mContext.getResources().getFraction(R.fraction.bt_battery_scale_fraction, 1, 1)); mContext.getResources().getFraction(R.fraction.bt_battery_scale_fraction, 1, 1));
String summaryText = mCachedDevice.getConnectionSummary(); String summaryText = mCachedDevice.getConnectionSummary();
// If both the hearing aids are connected, two battery status should be shown. // If both the hearing aids are connected, two device status should be shown.
final String pairDeviceSummary = mDeviceManager // If Second Summary is unavailable, to set it to null.
.getHearingAidPairDeviceSummary(mCachedDevice); mHeaderController.setSecondSummary(
if (pairDeviceSummary != null) { mDeviceManager.getSubDeviceSummary(mCachedDevice));
mHeaderController.setSecondSummary(pairDeviceSummary);
}
mHeaderController.setLabel(mCachedDevice.getName()); mHeaderController.setLabel(mCachedDevice.getName());
mHeaderController.setIcon(pair.first); mHeaderController.setIcon(pair.first);
mHeaderController.setIconContentDescription(pair.second); mHeaderController.setIconContentDescription(pair.second);

View File

@@ -51,6 +51,8 @@ public class BluetoothDetailsMacAddressController extends BluetoothDetailsContro
@Override @Override
protected void refresh() { protected void refresh() {
mFooterPreference.setTitle(mContext.getString(
R.string.bluetooth_device_mac_address, mCachedDevice.getAddress()));
} }
@Override @Override

View File

@@ -230,9 +230,19 @@ public abstract class BluetoothDeviceUpdater implements BluetoothCallback,
*/ */
protected void removePreference(CachedBluetoothDevice cachedDevice) { protected void removePreference(CachedBluetoothDevice cachedDevice) {
final BluetoothDevice device = cachedDevice.getDevice(); final BluetoothDevice device = cachedDevice.getDevice();
final CachedBluetoothDevice subCachedDevice = cachedDevice.getSubDevice();
if (mPreferenceMap.containsKey(device)) { if (mPreferenceMap.containsKey(device)) {
mDevicePreferenceCallback.onDeviceRemoved(mPreferenceMap.get(device)); mDevicePreferenceCallback.onDeviceRemoved(mPreferenceMap.get(device));
mPreferenceMap.remove(device); mPreferenceMap.remove(device);
} else if (subCachedDevice != null) {
// When doing remove, to check if preference maps to sub device.
// This would happen when connection state is changed in detail page that there is no
// callback from SettingsLib.
final BluetoothDevice subDevice = subCachedDevice.getDevice();
if (mPreferenceMap.containsKey(subDevice)) {
mDevicePreferenceCallback.onDeviceRemoved(mPreferenceMap.get(subDevice));
mPreferenceMap.remove(subDevice);
}
} }
} }

View File

@@ -72,7 +72,7 @@ public class BluetoothDetailsHeaderControllerTest extends BluetoothDetailsContro
FakeFeatureFactory.setupForTest(); FakeFeatureFactory.setupForTest();
ShadowEntityHeaderController.setUseMock(mHeaderController); ShadowEntityHeaderController.setUseMock(mHeaderController);
when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager); when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager);
when(mCachedDeviceManager.getHearingAidPairDeviceSummary(mCachedDevice)).thenReturn("abc"); when(mCachedDeviceManager.getSubDeviceSummary(mCachedDevice)).thenReturn("abc");
mController = mController =
new BluetoothDetailsHeaderController(mContext, mFragment, mCachedDevice, mLifecycle, new BluetoothDetailsHeaderController(mContext, mFragment, mCachedDevice, mLifecycle,
mBluetoothManager); mBluetoothManager);

View File

@@ -63,8 +63,12 @@ public class BluetoothDeviceUpdaterTest {
@Mock @Mock
private CachedBluetoothDevice mCachedBluetoothDevice; private CachedBluetoothDevice mCachedBluetoothDevice;
@Mock @Mock
private CachedBluetoothDevice mSubCachedBluetoothDevice;
@Mock
private BluetoothDevice mBluetoothDevice; private BluetoothDevice mBluetoothDevice;
@Mock @Mock
private BluetoothDevice mSubBluetoothDevice;
@Mock
private SettingsActivity mSettingsActivity; private SettingsActivity mSettingsActivity;
@Mock @Mock
private LocalBluetoothManager mLocalManager; private LocalBluetoothManager mLocalManager;
@@ -86,6 +90,7 @@ public class BluetoothDeviceUpdaterTest {
mCachedDevices.add(mCachedBluetoothDevice); mCachedDevices.add(mCachedBluetoothDevice);
doReturn(mContext).when(mDashboardFragment).getContext(); doReturn(mContext).when(mDashboardFragment).getContext();
when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
when(mSubCachedBluetoothDevice.getDevice()).thenReturn(mSubBluetoothDevice);
when(mLocalManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager); when(mLocalManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager);
when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(mCachedDevices); when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(mCachedDevices);
@@ -146,6 +151,20 @@ public class BluetoothDeviceUpdaterTest {
verify(mDevicePreferenceCallback, never()).onDeviceRemoved(any(Preference.class)); verify(mDevicePreferenceCallback, never()).onDeviceRemoved(any(Preference.class));
} }
@Test
public void testRemovePreference_subDeviceExist_removePreference() {
when(mCachedBluetoothDevice.getSubDevice()).thenReturn(mSubCachedBluetoothDevice);
mBluetoothDeviceUpdater.mPreferenceMap.put(mSubBluetoothDevice, mPreference);
assertThat(mBluetoothDeviceUpdater.mPreferenceMap.
containsKey(mSubBluetoothDevice)).isTrue();
mBluetoothDeviceUpdater.removePreference(mCachedBluetoothDevice);
verify(mDevicePreferenceCallback).onDeviceRemoved(mPreference);
assertThat(mBluetoothDeviceUpdater.mPreferenceMap.
containsKey(mSubBluetoothDevice)).isFalse();
}
@Test @Test
public void testDeviceProfilesListener_click_startBluetoothDeviceDetailPage() { public void testDeviceProfilesListener_click_startBluetoothDeviceDetailPage() {
doReturn(mSettingsActivity).when(mDashboardFragment).getContext(); doReturn(mSettingsActivity).when(mDashboardFragment).getContext();