Avoid unintended pref removal after CSIP main/member switch

Test: atest
Bug: 394765052
Flag: EXEMPT small fix
Change-Id: I5b4646ff9ee092b851d3f8d5cc3ac2030f189430
This commit is contained in:
Yiyi Shen
2025-03-13 17:32:38 +08:00
parent c8d9ab2c55
commit efe2738735
2 changed files with 63 additions and 7 deletions

View File

@@ -39,6 +39,8 @@ import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.google.common.collect.ImmutableList;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -99,6 +101,7 @@ public class BluetoothDeviceUpdaterTest {
when(mLocalManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager);
when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(mCachedDevices);
when(mCachedBluetoothDevice.getAddress()).thenReturn(MAC_ADDRESS);
when(mBluetoothDevice.getAddress()).thenReturn(MAC_ADDRESS);
when(mSubBluetoothDevice.getAddress()).thenReturn(SUB_MAC_ADDRESS);
when(mCachedBluetoothDevice.getDrawableWithDescription()).thenReturn(pairs);
@@ -252,7 +255,7 @@ public class BluetoothDeviceUpdaterTest {
}
@Test
public void havePreference_refreshPreference() {
public void refreshPreference_havePreference_refresh() {
mBluetoothDeviceUpdater.mPreferenceMap.put(mBluetoothDevice, mPreference);
mPreference.setTitle("fake_name");
@@ -262,6 +265,45 @@ public class BluetoothDeviceUpdaterTest {
assertThat(mPreference.getTitle()).isEqualTo(TEST_NAME);
}
@Test
public void refreshPreference_staledPreference_remove() {
mBluetoothDeviceUpdater.mPreferenceMap.put(mBluetoothDevice, mPreference);
mPreference.setTitle("fake_name");
when(mCachedBluetoothDevice.getName()).thenReturn(TEST_NAME);
when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(ImmutableList.of());
mBluetoothDeviceUpdater.refreshPreference();
verify(mDevicePreferenceCallback).onDeviceRemoved(mPreference);
assertThat(mBluetoothDeviceUpdater.mPreferenceMap.containsKey(mBluetoothDevice)).isFalse();
}
@Test
public void refreshPreference_inconsistentPreference_doNothing() {
when(mCachedBluetoothDevice.getDevice()).thenReturn(mSubBluetoothDevice);
mBluetoothDeviceUpdater.mPreferenceMap.put(mBluetoothDevice, mPreference);
mBluetoothDeviceUpdater.mPreferenceMap.put(mSubBluetoothDevice, mPreference);
when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(
ImmutableList.of(mSubCachedBluetoothDevice));
mBluetoothDeviceUpdater.refreshPreference();
verify(mDevicePreferenceCallback, never()).onDeviceRemoved(mPreference);
assertThat(mBluetoothDeviceUpdater.mPreferenceMap.containsKey(mBluetoothDevice)).isFalse();
}
@Test
public void refreshPreference_staledInconsistentPreference_remove() {
when(mCachedBluetoothDevice.getDevice()).thenReturn(mSubBluetoothDevice);
mBluetoothDeviceUpdater.mPreferenceMap.put(mBluetoothDevice, mPreference);
when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(ImmutableList.of());
mBluetoothDeviceUpdater.refreshPreference();
verify(mDevicePreferenceCallback).onDeviceRemoved(mPreference);
assertThat(mBluetoothDeviceUpdater.mPreferenceMap.containsKey(mBluetoothDevice)).isFalse();
}
public static class TestBluetoothDeviceUpdater extends BluetoothDeviceUpdater {
public TestBluetoothDeviceUpdater(Context context,
DevicePreferenceCallback devicePreferenceCallback,