From 2028535d06ebc341ef2fdf3be482629835fc03a7 Mon Sep 17 00:00:00 2001 From: Srinivas Patibandla Date: Tue, 30 Apr 2024 23:35:03 +0000 Subject: [PATCH] [Hide DCK Device] Update unit tests per change in exclusive manager verification Test: atest: com.android.settings.bluetooth.ConnectedBluetoothDeviceUpdaterTest Test: atest: com.android.settings.bluetooth.SavedBluetoothDeviceUpdaterTest Bug: 322285078 Bug: 324475542 Flag: ACONFIG com.android.settingslib.flags.enable_hide_exclusively_managed_bluetooth_device NEXTFOOD Change-Id: I5a4f9eccc461033aeca79ea657af61958af0c660 --- .../ConnectedBluetoothDeviceUpdaterTest.java | 87 +++++++------------ .../SavedBluetoothDeviceUpdaterTest.java | 83 ++++++++---------- 2 files changed, 68 insertions(+), 102 deletions(-) diff --git a/tests/robotests/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdaterTest.java b/tests/robotests/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdaterTest.java index ee000686b2a..b2449dab39e 100644 --- a/tests/robotests/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdaterTest.java +++ b/tests/robotests/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdaterTest.java @@ -30,7 +30,7 @@ import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothProfile; import android.content.Context; -import android.content.pm.PackageInfo; +import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.graphics.drawable.Drawable; import android.media.AudioManager; @@ -44,7 +44,6 @@ import com.android.settings.dashboard.DashboardFragment; import com.android.settings.testutils.shadow.ShadowAudioManager; import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; import com.android.settings.testutils.shadow.ShadowCachedBluetoothDeviceManager; -import com.android.settingslib.bluetooth.BluetoothUtils; import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.flags.Flags; @@ -68,7 +67,7 @@ import java.util.Collection; public class ConnectedBluetoothDeviceUpdaterTest { private static final String MAC_ADDRESS = "04:52:C7:0B:D8:3C"; - private static final String FAKE_EXCLUSIVE_MANAGER_NAME = "com.fake.name"; + private static final String TEST_EXCLUSIVE_MANAGER = "com.test.manager"; @Rule public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule(); @@ -355,13 +354,16 @@ public class ConnectedBluetoothDeviceUpdaterTest { @Test @RequiresFlagsEnabled(Flags.FLAG_ENABLE_HIDE_EXCLUSIVELY_MANAGED_BLUETOOTH_DEVICE) - public void update_notAllowedExclusiveManagedDevice_addDevice() { + public void update_exclusivelyManagedDevice_packageNotInstalled_addDevice() + throws Exception { mAudioManager.setMode(AudioManager.MODE_NORMAL); when(mBluetoothDeviceUpdater .isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_EXCLUSIVE_MANAGER)).thenReturn( - FAKE_EXCLUSIVE_MANAGER_NAME.getBytes()); + TEST_EXCLUSIVE_MANAGER.getBytes()); + doThrow(new PackageManager.NameNotFoundException()).when(mPackageManager) + .getApplicationInfo(TEST_EXCLUSIVE_MANAGER, 0); mBluetoothDeviceUpdater.update(mCachedBluetoothDevice); @@ -370,64 +372,39 @@ public class ConnectedBluetoothDeviceUpdaterTest { @Test @RequiresFlagsEnabled(Flags.FLAG_ENABLE_HIDE_EXCLUSIVELY_MANAGED_BLUETOOTH_DEVICE) - public void update_existingExclusivelyManagedDeviceWithPackageInstalled_removePreference() + public void update_exclusivelyManagedDevice_packageNotEnabled_addDevice() throws Exception { - final String exclusiveManagerName = - BluetoothUtils.getExclusiveManagers().stream().findAny().orElse( - FAKE_EXCLUSIVE_MANAGER_NAME); + ApplicationInfo appInfo = new ApplicationInfo(); + appInfo.enabled = false; mAudioManager.setMode(AudioManager.MODE_NORMAL); when(mBluetoothDeviceUpdater - .isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); + .isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_EXCLUSIVE_MANAGER)).thenReturn( - exclusiveManagerName.getBytes()); - doReturn(new PackageInfo()).when(mPackageManager).getPackageInfo(exclusiveManagerName, 0); - - mBluetoothDeviceUpdater.update(mCachedBluetoothDevice); - - verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); - verify(mBluetoothDeviceUpdater, never()).addPreference(mCachedBluetoothDevice); - } - - @Test - @RequiresFlagsEnabled(Flags.FLAG_ENABLE_HIDE_EXCLUSIVELY_MANAGED_BLUETOOTH_DEVICE) - public void update_newExclusivelyManagedDeviceWithPackageInstalled_doNotAddPreference() - throws Exception { - final String exclusiveManagerName = - BluetoothUtils.getExclusiveManagers().stream().findAny().orElse( - FAKE_EXCLUSIVE_MANAGER_NAME); - mAudioManager.setMode(AudioManager.MODE_NORMAL); - when(mBluetoothDeviceUpdater - .isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); - when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); - when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_EXCLUSIVE_MANAGER)).thenReturn( - exclusiveManagerName.getBytes()); - doReturn(new PackageInfo()).when(mPackageManager).getPackageInfo(exclusiveManagerName, 0); - - mBluetoothDeviceUpdater.update(mCachedBluetoothDevice); - - verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); - verify(mBluetoothDeviceUpdater, never()).addPreference(mCachedBluetoothDevice); - } - - @Test - @RequiresFlagsEnabled(Flags.FLAG_ENABLE_HIDE_EXCLUSIVELY_MANAGED_BLUETOOTH_DEVICE) - public void update_exclusivelyManagedDeviceWithoutPackageInstalled_addDevice() - throws Exception { - final String exclusiveManagerName = - BluetoothUtils.getExclusiveManagers().stream().findAny().orElse( - FAKE_EXCLUSIVE_MANAGER_NAME); - mAudioManager.setMode(AudioManager.MODE_NORMAL); - when(mBluetoothDeviceUpdater - .isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); - when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); - when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_EXCLUSIVE_MANAGER)).thenReturn( - exclusiveManagerName.getBytes()); - doThrow(new PackageManager.NameNotFoundException()).when(mPackageManager).getPackageInfo( - exclusiveManagerName, 0); + TEST_EXCLUSIVE_MANAGER.getBytes()); + doReturn(appInfo).when(mPackageManager).getApplicationInfo(TEST_EXCLUSIVE_MANAGER, 0); mBluetoothDeviceUpdater.update(mCachedBluetoothDevice); verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice); } + + @Test + @RequiresFlagsEnabled(Flags.FLAG_ENABLE_HIDE_EXCLUSIVELY_MANAGED_BLUETOOTH_DEVICE) + public void update_exclusivelyManagedDevice_packageInstalledAndEnabled_removePreference() + throws Exception { + mAudioManager.setMode(AudioManager.MODE_NORMAL); + when(mBluetoothDeviceUpdater + .isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); + when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); + when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_EXCLUSIVE_MANAGER)).thenReturn( + TEST_EXCLUSIVE_MANAGER.getBytes()); + doReturn(new ApplicationInfo()).when(mPackageManager).getApplicationInfo( + TEST_EXCLUSIVE_MANAGER, 0); + + mBluetoothDeviceUpdater.update(mCachedBluetoothDevice); + + verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); + verify(mBluetoothDeviceUpdater, never()).addPreference(mCachedBluetoothDevice); + } } diff --git a/tests/robotests/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdaterTest.java b/tests/robotests/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdaterTest.java index 796120d43f5..e2cf14810c4 100644 --- a/tests/robotests/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdaterTest.java +++ b/tests/robotests/src/com/android/settings/bluetooth/SavedBluetoothDeviceUpdaterTest.java @@ -29,7 +29,7 @@ import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothProfile; import android.content.Context; -import android.content.pm.PackageInfo; +import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.graphics.drawable.Drawable; import android.platform.test.annotations.RequiresFlagsDisabled; @@ -41,7 +41,6 @@ import android.util.Pair; import com.android.settings.connecteddevice.DevicePreferenceCallback; import com.android.settings.dashboard.DashboardFragment; import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; -import com.android.settingslib.bluetooth.BluetoothUtils; import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager; import com.android.settingslib.bluetooth.LocalBluetoothManager; @@ -66,7 +65,7 @@ import java.util.List; public class SavedBluetoothDeviceUpdaterTest { private static final String MAC_ADDRESS = "04:52:C7:0B:D8:3C"; - private static final String FAKE_EXCLUSIVE_MANAGER_NAME = "com.fake.name"; + private static final String TEST_EXCLUSIVE_MANAGER = "com.test.manager"; @Rule public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule(); @@ -339,42 +338,18 @@ public class SavedBluetoothDeviceUpdaterTest { @Test @RequiresFlagsEnabled(Flags.FLAG_ENABLE_HIDE_EXCLUSIVELY_MANAGED_BLUETOOTH_DEVICE) - public void update_notAllowedExclusivelyManagedDevice_addDevice() { - final Collection cachedDevices = new ArrayList<>(); - cachedDevices.add(mCachedBluetoothDevice); - - when(mBluetoothAdapter.isEnabled()).thenReturn(true); - when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mDeviceManager); - when(mDeviceManager.getCachedDevicesCopy()).thenReturn(cachedDevices); - when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); - when(mBluetoothDevice.isConnected()).thenReturn(false); - when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_EXCLUSIVE_MANAGER)).thenReturn( - FAKE_EXCLUSIVE_MANAGER_NAME.getBytes()); - - mBluetoothDeviceUpdater.update(mCachedBluetoothDevice); - - verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice, - BluetoothDevicePreference.SortType.TYPE_NO_SORT); - } - - @Test - @RequiresFlagsEnabled(Flags.FLAG_ENABLE_HIDE_EXCLUSIVELY_MANAGED_BLUETOOTH_DEVICE) - public void update_existingExclusivelyManagedDeviceWithPackageInstalled_removePreference() + public void update_existingExclusivelyManagedDevice_packageEnabled_removePreference() throws Exception { final Collection cachedDevices = new ArrayList<>(); - final String exclusiveManagerName = - BluetoothUtils.getExclusiveManagers().stream().findAny().orElse( - FAKE_EXCLUSIVE_MANAGER_NAME); - when(mBluetoothAdapter.isEnabled()).thenReturn(true); when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mDeviceManager); when(mDeviceManager.getCachedDevicesCopy()).thenReturn(cachedDevices); when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); when(mBluetoothDevice.isConnected()).thenReturn(false); when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_EXCLUSIVE_MANAGER)).thenReturn( - exclusiveManagerName.getBytes()); - - doReturn(new PackageInfo()).when(mPackageManager).getPackageInfo(exclusiveManagerName, 0); + TEST_EXCLUSIVE_MANAGER.getBytes()); + doReturn(new ApplicationInfo()).when(mPackageManager).getApplicationInfo( + TEST_EXCLUSIVE_MANAGER, 0); mBluetoothDeviceUpdater.mPreferenceMap.put(mBluetoothDevice, mPreference); mBluetoothDeviceUpdater.update(mCachedBluetoothDevice); @@ -386,23 +361,19 @@ public class SavedBluetoothDeviceUpdaterTest { @Test @RequiresFlagsEnabled(Flags.FLAG_ENABLE_HIDE_EXCLUSIVELY_MANAGED_BLUETOOTH_DEVICE) - public void update_newExclusivelyManagedDeviceWithPackageInstalled_doNotAddPreference() + public void update_newExclusivelyManagedDevice_packageEnabled_doNotAddPreference() throws Exception { final Collection cachedDevices = new ArrayList<>(); - final String exclusiveManagerName = - BluetoothUtils.getExclusiveManagers().stream().findAny().orElse( - FAKE_EXCLUSIVE_MANAGER_NAME); cachedDevices.add(mCachedBluetoothDevice); - when(mBluetoothAdapter.isEnabled()).thenReturn(true); when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mDeviceManager); when(mDeviceManager.getCachedDevicesCopy()).thenReturn(cachedDevices); when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); when(mBluetoothDevice.isConnected()).thenReturn(false); when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_EXCLUSIVE_MANAGER)).thenReturn( - exclusiveManagerName.getBytes()); - - doReturn(new PackageInfo()).when(mPackageManager).getPackageInfo(exclusiveManagerName, 0); + TEST_EXCLUSIVE_MANAGER.getBytes()); + doReturn(new ApplicationInfo()).when(mPackageManager).getApplicationInfo( + TEST_EXCLUSIVE_MANAGER, 0); mBluetoothDeviceUpdater.update(mCachedBluetoothDevice); @@ -413,24 +384,42 @@ public class SavedBluetoothDeviceUpdaterTest { @Test @RequiresFlagsEnabled(Flags.FLAG_ENABLE_HIDE_EXCLUSIVELY_MANAGED_BLUETOOTH_DEVICE) - public void update_exclusivelyManagedDeviceWithoutPackageInstalled_addDevice() + public void update_exclusivelyManagedDevice_packageNotInstalled_addDevice() throws Exception { final Collection cachedDevices = new ArrayList<>(); - final String exclusiveManagerName = - BluetoothUtils.getExclusiveManagers().stream().findAny().orElse( - FAKE_EXCLUSIVE_MANAGER_NAME); cachedDevices.add(mCachedBluetoothDevice); - when(mBluetoothAdapter.isEnabled()).thenReturn(true); when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mDeviceManager); when(mDeviceManager.getCachedDevicesCopy()).thenReturn(cachedDevices); when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); when(mBluetoothDevice.isConnected()).thenReturn(false); when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_EXCLUSIVE_MANAGER)).thenReturn( - exclusiveManagerName.getBytes()); + TEST_EXCLUSIVE_MANAGER.getBytes()); + doThrow(new PackageManager.NameNotFoundException()).when(mPackageManager) + .getApplicationInfo(TEST_EXCLUSIVE_MANAGER, 0); - doThrow(new PackageManager.NameNotFoundException()).when(mPackageManager).getPackageInfo( - exclusiveManagerName, 0); + mBluetoothDeviceUpdater.update(mCachedBluetoothDevice); + + verify(mBluetoothDeviceUpdater).addPreference(mCachedBluetoothDevice, + BluetoothDevicePreference.SortType.TYPE_NO_SORT); + } + + @Test + @RequiresFlagsEnabled(Flags.FLAG_ENABLE_HIDE_EXCLUSIVELY_MANAGED_BLUETOOTH_DEVICE) + public void update_exclusivelyManagedDevice_packageNotEnabled_addDevice() + throws Exception { + final Collection cachedDevices = new ArrayList<>(); + cachedDevices.add(mCachedBluetoothDevice); + ApplicationInfo appInfo = new ApplicationInfo(); + appInfo.enabled = false; + when(mBluetoothAdapter.isEnabled()).thenReturn(true); + when(mBluetoothManager.getCachedDeviceManager()).thenReturn(mDeviceManager); + when(mDeviceManager.getCachedDevicesCopy()).thenReturn(cachedDevices); + when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); + when(mBluetoothDevice.isConnected()).thenReturn(false); + when(mBluetoothDevice.getMetadata(BluetoothDevice.METADATA_EXCLUSIVE_MANAGER)).thenReturn( + TEST_EXCLUSIVE_MANAGER.getBytes()); + doReturn(appInfo).when(mPackageManager).getApplicationInfo(TEST_EXCLUSIVE_MANAGER, 0); mBluetoothDeviceUpdater.update(mCachedBluetoothDevice);