From 220312eab7b1cb8f19afb4e9646f211a55b73eff Mon Sep 17 00:00:00 2001 From: Ze Li Date: Thu, 13 Feb 2025 15:26:20 +0800 Subject: [PATCH] [Temp bonding] Clean up old guest device logic as requirements changed Test: manual test Bug: 362859132 Flag: com.android.settingslib.flags.enable_temporary_bond_devices_ui Change-Id: If4a471e2058873be5cdb0ee43371333ebdc9a38a --- res/values/strings.xml | 2 - res/xml/connected_devices.xml | 5 - .../AvailableMediaBluetoothDeviceUpdater.java | 9 - .../ConnectedBluetoothDeviceUpdater.java | 8 - .../ConnectedDeviceDashboardFragment.java | 5 - .../AudioSharingBluetoothDeviceUpdater.java | 9 - ...oSharingCallAudioPreferenceController.java | 11 - .../TemporaryBondDeviceGroupController.java | 170 ------------ .../TemporaryBondDeviceGroupUpdater.java | 74 ------ .../ConnectedBluetoothDeviceUpdaterTest.java | 19 -- .../ConnectedDeviceDashboardFragmentTest.java | 4 +- ...ringCallAudioPreferenceControllerTest.java | 21 -- ...emporaryBondDeviceGroupControllerTest.java | 243 ------------------ .../TemporaryBondDeviceGroupUpdaterTest.java | 139 ---------- 14 files changed, 1 insertion(+), 718 deletions(-) delete mode 100644 src/com/android/settings/connecteddevice/audiosharing/TemporaryBondDeviceGroupController.java delete mode 100644 src/com/android/settings/connecteddevice/audiosharing/TemporaryBondDeviceGroupUpdater.java delete mode 100644 tests/robotests/src/com/android/settings/connecteddevice/audiosharing/TemporaryBondDeviceGroupControllerTest.java delete mode 100644 tests/robotests/src/com/android/settings/connecteddevice/audiosharing/TemporaryBondDeviceGroupUpdaterTest.java diff --git a/res/values/strings.xml b/res/values/strings.xml index 089003006cf..c6b82106107 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -305,8 +305,6 @@ Media devices - - Guest devices Call devices diff --git a/res/xml/connected_devices.xml b/res/xml/connected_devices.xml index 9642236ad03..06fb5a7941d 100644 --- a/res/xml/connected_devices.xml +++ b/res/xml/connected_devices.xml @@ -40,11 +40,6 @@ settings:controller="com.android.settings.connecteddevice.audiosharing.AudioSharingDevicePreferenceController"> - - !anyTemporaryBondDevice(entry.getValue())) - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); - } mDeviceItemsInSharingSession = AudioSharingUtils.buildOrderedConnectedLeadAudioSharingDeviceItem( mBtManager, mGroupedConnectedDevices, /* filterByInSharing= */ true); } - private boolean anyTemporaryBondDevice(List connectedDevices) { - return connectedDevices.stream().anyMatch(BluetoothUtils::isTemporaryBondDevice); - } - @Nullable private Pair getActiveItemWithIndex() { List deviceItems = new ArrayList<>(mDeviceItemsInSharingSession); diff --git a/src/com/android/settings/connecteddevice/audiosharing/TemporaryBondDeviceGroupController.java b/src/com/android/settings/connecteddevice/audiosharing/TemporaryBondDeviceGroupController.java deleted file mode 100644 index ff3aa2c12e0..00000000000 --- a/src/com/android/settings/connecteddevice/audiosharing/TemporaryBondDeviceGroupController.java +++ /dev/null @@ -1,170 +0,0 @@ -/* - * Copyright (C) 2025 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings.connecteddevice.audiosharing; - -import android.content.Context; -import android.util.Log; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.annotation.VisibleForTesting; -import androidx.lifecycle.DefaultLifecycleObserver; -import androidx.lifecycle.LifecycleOwner; -import androidx.preference.Preference; -import androidx.preference.PreferenceGroup; -import androidx.preference.PreferenceScreen; - -import com.android.settings.bluetooth.BluetoothDeviceUpdater; -import com.android.settings.bluetooth.Utils; -import com.android.settings.connecteddevice.DevicePreferenceCallback; -import com.android.settings.core.BasePreferenceController; -import com.android.settings.dashboard.DashboardFragment; -import com.android.settingslib.bluetooth.BluetoothCallback; -import com.android.settingslib.bluetooth.BluetoothEventManager; -import com.android.settingslib.bluetooth.BluetoothUtils; -import com.android.settingslib.bluetooth.LocalBluetoothManager; -import com.android.settingslib.flags.Flags; -import com.android.settingslib.utils.ThreadUtils; - -/** - * Controller to maintain the {@link androidx.preference.PreferenceGroup} for all connected - * temporary bond devices. It uses {@link DevicePreferenceCallback} to add/remove - * {@link Preference} - */ -public class TemporaryBondDeviceGroupController extends BasePreferenceController implements - DefaultLifecycleObserver, DevicePreferenceCallback, BluetoothCallback { - private static final String TAG = "TemporaryBondDeviceGroupController"; - private static final String KEY = "temp_bond_device_list"; - - @Nullable - private final BluetoothEventManager mEventManager; - @Nullable - private PreferenceGroup mPreferenceGroup; - @Nullable - private BluetoothDeviceUpdater mBluetoothDeviceUpdater; - - - public TemporaryBondDeviceGroupController(@NonNull Context context) { - super(context, KEY); - LocalBluetoothManager btManager = Utils.getLocalBtManager(mContext); - mEventManager = btManager == null ? null : btManager.getEventManager(); - } - - @Override - public void onStart(@NonNull LifecycleOwner owner) { - if (!isAvailable()) { - Log.d(TAG, "Skip onStart(), feature is not supported."); - return; - } - if (mEventManager == null) { - Log.d(TAG, "onStart() Bluetooth is not supported on this device"); - return; - } - var unused = ThreadUtils.postOnBackgroundThread(() -> { - mEventManager.registerCallback(this); - if (mBluetoothDeviceUpdater != null) { - mBluetoothDeviceUpdater.registerCallback(); - mBluetoothDeviceUpdater.refreshPreference(); - } - }); - } - - @Override - public void onStop(@NonNull LifecycleOwner owner) { - var unused = ThreadUtils.postOnBackgroundThread(() -> { - if (mBluetoothDeviceUpdater != null) { - mBluetoothDeviceUpdater.unregisterCallback(); - } - if (mEventManager != null) { - mEventManager.unregisterCallback(this); - return; - } - Log.d(TAG, "onStop() Bluetooth is not supported on this device"); - }); - } - - @Override - public void displayPreference(@NonNull PreferenceScreen screen) { - super.displayPreference(screen); - mPreferenceGroup = screen.findPreference(KEY); - if (mPreferenceGroup != null) { - mPreferenceGroup.setVisible(false); - } - - if (isAvailable() && mBluetoothDeviceUpdater != null) { - mBluetoothDeviceUpdater.setPrefContext(screen.getContext()); - mBluetoothDeviceUpdater.forceUpdate(); - } - } - - @Override - public void onDeviceAdded(@NonNull Preference preference) { - if (mPreferenceGroup != null) { - mPreferenceGroup.addPreference(preference); - Log.d(TAG, "Temporary bond device added"); - if (mPreferenceGroup.getPreferenceCount() == 1) { - mPreferenceGroup.setVisible(true); - } - } - } - - @Override - public void onDeviceRemoved(@NonNull Preference preference) { - if (mPreferenceGroup != null) { - mPreferenceGroup.removePreference(preference); - Log.d(TAG, "Temporary bond device removed"); - if (mPreferenceGroup.getPreferenceCount() == 0) { - mPreferenceGroup.setVisible(false); - } - } - } - - @Override - public int getAvailabilityStatus() { - return (BluetoothUtils.isAudioSharingUIAvailable(mContext) - && mBluetoothDeviceUpdater != null && Flags.enableTemporaryBondDevicesUi()) - ? AVAILABLE_UNSEARCHABLE - : UNSUPPORTED_ON_DEVICE; - } - - @Override - public String getPreferenceKey() { - return KEY; - } - - /** - * Initialize the controller. - * - * @param fragment The fragment to provide the context and metrics category for {@link - * TemporaryBondDeviceGroupUpdater} and provide the host for dialogs. - */ - public void init(@NonNull DashboardFragment fragment) { - mBluetoothDeviceUpdater = new TemporaryBondDeviceGroupUpdater(fragment.getContext(), - TemporaryBondDeviceGroupController.this, - fragment.getMetricsCategory()); - } - - @VisibleForTesting - void setBluetoothDeviceUpdater(@Nullable BluetoothDeviceUpdater bluetoothDeviceUpdater) { - mBluetoothDeviceUpdater = bluetoothDeviceUpdater; - } - - @VisibleForTesting - void setPreferenceGroup(@Nullable PreferenceGroup preferenceGroup) { - mPreferenceGroup = preferenceGroup; - } -} diff --git a/src/com/android/settings/connecteddevice/audiosharing/TemporaryBondDeviceGroupUpdater.java b/src/com/android/settings/connecteddevice/audiosharing/TemporaryBondDeviceGroupUpdater.java deleted file mode 100644 index 59e37c51c08..00000000000 --- a/src/com/android/settings/connecteddevice/audiosharing/TemporaryBondDeviceGroupUpdater.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (C) 2025 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings.connecteddevice.audiosharing; - -import android.content.Context; -import android.util.Log; - -import androidx.annotation.NonNull; - -import com.android.settings.bluetooth.BluetoothDeviceUpdater; -import com.android.settings.connecteddevice.DevicePreferenceCallback; -import com.android.settingslib.bluetooth.BluetoothUtils; -import com.android.settingslib.bluetooth.CachedBluetoothDevice; -import com.android.settingslib.flags.Flags; - -/** Maintain and update connected temporary bond bluetooth devices */ -public class TemporaryBondDeviceGroupUpdater extends BluetoothDeviceUpdater { - private static final String TAG = "TemporaryBondDeviceGroupUpdater"; - private static final String PREF_KEY_PREFIX = "temp_bond_bt_"; - - public TemporaryBondDeviceGroupUpdater( - @NonNull Context context, - @NonNull DevicePreferenceCallback devicePreferenceCallback, - int metricsCategory) { - super(context, devicePreferenceCallback, metricsCategory); - } - - @Override - public boolean isFilterMatched(@NonNull CachedBluetoothDevice cachedDevice) { - // Only connected temporary bond device should be shown in this section when Audio - // sharing UI is available. - boolean isFilterMatched = Flags.enableTemporaryBondDevicesUi() - && BluetoothUtils.isTemporaryBondDevice(cachedDevice.getDevice()) - && isDeviceConnected(cachedDevice) && isDeviceInCachedDevicesList(cachedDevice) - && BluetoothUtils.isAudioSharingUIAvailable(mContext); - Log.d( - TAG, - "isFilterMatched() device : " - + cachedDevice.getName() - + ", isFilterMatched : " - + isFilterMatched); - return isFilterMatched; - } - - @Override - protected String getPreferenceKeyPrefix() { - return PREF_KEY_PREFIX; - } - - @Override - protected String getLogTag() { - return TAG; - } - - @Override - protected void update(CachedBluetoothDevice cachedBluetoothDevice) { - super.update(cachedBluetoothDevice); - Log.d(TAG, "Map : " + mPreferenceMap); - } -} diff --git a/tests/robotests/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdaterTest.java b/tests/robotests/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdaterTest.java index ad155ffd743..f68a8d4cf6a 100644 --- a/tests/robotests/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdaterTest.java +++ b/tests/robotests/src/com/android/settings/bluetooth/ConnectedBluetoothDeviceUpdaterTest.java @@ -68,9 +68,6 @@ public class ConnectedBluetoothDeviceUpdaterTest { private static final String MAC_ADDRESS = "04:52:C7:0B:D8:3C"; private static final String TEST_EXCLUSIVE_MANAGER = "com.test.manager"; - private static final String TEMP_BOND_METADATA = - "le_audio_sharing"; - private static final int METADATA_FAST_PAIR_CUSTOMIZED_FIELDS = 25; @Rule public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule(); @@ -408,22 +405,6 @@ public class ConnectedBluetoothDeviceUpdaterTest { verify(mBluetoothDeviceUpdater, never()).addPreference(mCachedBluetoothDevice); } - @Test - @RequiresFlagsEnabled(Flags.FLAG_ENABLE_TEMPORARY_BOND_DEVICES_UI) - public void update_temporaryBondDevice_removePreference() { - setUpDeviceUpdaterWithAudioMode(AudioManager.MODE_NORMAL); - when(mBluetoothDeviceUpdater - .isDeviceConnected(any(CachedBluetoothDevice.class))).thenReturn(true); - when(mCachedBluetoothDevice.isConnectedHfpDevice()).thenReturn(true); - when(mBluetoothDevice.getMetadata(METADATA_FAST_PAIR_CUSTOMIZED_FIELDS)) - .thenReturn(TEMP_BOND_METADATA.getBytes()); - - mBluetoothDeviceUpdater.update(mCachedBluetoothDevice); - - verify(mBluetoothDeviceUpdater).removePreference(mCachedBluetoothDevice); - verify(mBluetoothDeviceUpdater, never()).addPreference(mCachedBluetoothDevice); - } - private void setUpDeviceUpdaterWithAudioMode(int audioMode) { mAudioManager.setMode(audioMode); mBluetoothDeviceUpdater = spy(new ConnectedBluetoothDeviceUpdater(mContext, diff --git a/tests/robotests/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragmentTest.java b/tests/robotests/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragmentTest.java index a1cc1d80acb..ecf6d003cef 100644 --- a/tests/robotests/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragmentTest.java +++ b/tests/robotests/src/com/android/settings/connecteddevice/ConnectedDeviceDashboardFragmentTest.java @@ -75,7 +75,6 @@ public class ConnectedDeviceDashboardFragmentTest { private static final String KEY_AUDIO_SHARING_SETTINGS = "connected_device_audio_sharing_settings"; private static final String KEY_ADD_BT_DEVICES = "add_bt_devices"; - private static final String KEY_TEMPORARY_BOND_DEVICES = "temp_bond_device_list"; private static final String SETTINGS_PACKAGE_NAME = "com.android.settings"; private static final String SYSTEMUI_PACKAGE_NAME = "com.android.systemui"; private static final String SLICE_ACTION = "com.android.settings.SEARCH_RESULT_TRAMPOLINE"; @@ -130,8 +129,7 @@ public class ConnectedDeviceDashboardFragmentTest { KEY_SAVED_DEVICE_SEE_ALL, KEY_FAST_PAIR_DEVICE_SEE_ALL, KEY_AUDIO_SHARING_DEVICES, - KEY_AUDIO_SHARING_SETTINGS, - KEY_TEMPORARY_BOND_DEVICES); + KEY_AUDIO_SHARING_SETTINGS); } @Test diff --git a/tests/robotests/src/com/android/settings/connecteddevice/audiosharing/AudioSharingCallAudioPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/connecteddevice/audiosharing/AudioSharingCallAudioPreferenceControllerTest.java index a2ac0cc99c9..4f6fed7b732 100644 --- a/tests/robotests/src/com/android/settings/connecteddevice/audiosharing/AudioSharingCallAudioPreferenceControllerTest.java +++ b/tests/robotests/src/com/android/settings/connecteddevice/audiosharing/AudioSharingCallAudioPreferenceControllerTest.java @@ -43,7 +43,6 @@ import android.content.ContentResolver; import android.content.Context; import android.database.ContentObserver; import android.os.Looper; -import android.platform.test.annotations.EnableFlags; import android.platform.test.flag.junit.SetFlagsRule; import android.provider.Settings; import android.view.View; @@ -111,11 +110,8 @@ public class AudioSharingCallAudioPreferenceControllerTest { private static final String PREF_KEY = "calls_and_alarms"; private static final String TEST_DEVICE_NAME1 = "test1"; private static final String TEST_DEVICE_NAME2 = "test2"; - private static final String TEMP_BOND_METADATA = - "le_audio_sharing"; private static final int TEST_DEVICE_GROUP_ID1 = 1; private static final int TEST_DEVICE_GROUP_ID2 = 2; - private static final int METADATA_FAST_PAIR_CUSTOMIZED_FIELDS = 25; private static final String TEST_SETTINGS_KEY = "bluetooth_le_broadcast_fallback_active_group_id"; @@ -447,23 +443,6 @@ public class AudioSharingCallAudioPreferenceControllerTest { assertThat(mPreference.getSummary().toString()).isEmpty(); } - @Test - @EnableFlags(Flags.FLAG_ENABLE_TEMPORARY_BOND_DEVICES_UI) - public void displayPreference_hasTemporaryBondDevice_doNotShow() { - Settings.Secure.putInt(mContentResolver, TEST_SETTINGS_KEY, TEST_DEVICE_GROUP_ID1); - when(mCachedDevice1.isActiveDevice(BluetoothProfile.LE_AUDIO)).thenReturn(true); - when(mBroadcast.isEnabled(any())).thenReturn(true); - when(mAssistant.getAllConnectedDevices()).thenReturn(ImmutableList.of(mDevice1, mDevice2)); - when(mAssistant.getAllSources(any())).thenReturn(ImmutableList.of(mState)); - when(mDevice2.getMetadata(METADATA_FAST_PAIR_CUSTOMIZED_FIELDS)).thenReturn( - TEMP_BOND_METADATA.getBytes()); - - mController.displayPreference(mScreen); - shadowOf(Looper.getMainLooper()).idle(); - - assertThat(mController.mGroupedConnectedDevices).hasSize(0); - } - @Test public void displayPreference_clickToShowCorrectDialog() { AlertDialog latestAlertDialog = ShadowAlertDialogCompat.getLatestAlertDialog(); diff --git a/tests/robotests/src/com/android/settings/connecteddevice/audiosharing/TemporaryBondDeviceGroupControllerTest.java b/tests/robotests/src/com/android/settings/connecteddevice/audiosharing/TemporaryBondDeviceGroupControllerTest.java deleted file mode 100644 index a85555d36d5..00000000000 --- a/tests/robotests/src/com/android/settings/connecteddevice/audiosharing/TemporaryBondDeviceGroupControllerTest.java +++ /dev/null @@ -1,243 +0,0 @@ -/* - * Copyright 2025 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings.connecteddevice.audiosharing; - -import static com.android.settings.core.BasePreferenceController.AVAILABLE_UNSEARCHABLE; - -import static com.google.common.truth.Truth.assertThat; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import android.bluetooth.BluetoothAdapter; -import android.bluetooth.BluetoothStatusCodes; -import android.content.Context; -import android.platform.test.annotations.RequiresFlagsDisabled; -import android.platform.test.annotations.RequiresFlagsEnabled; -import android.platform.test.flag.junit.CheckFlagsRule; -import android.platform.test.flag.junit.DeviceFlagsValueProvider; - -import androidx.lifecycle.LifecycleOwner; -import androidx.preference.Preference; -import androidx.preference.PreferenceCategory; -import androidx.preference.PreferenceGroup; -import androidx.preference.PreferenceManager; -import androidx.preference.PreferenceScreen; -import androidx.test.core.app.ApplicationProvider; - -import com.android.settings.bluetooth.Utils; -import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; -import com.android.settings.testutils.shadow.ShadowBluetoothUtils; -import com.android.settingslib.bluetooth.BluetoothCallback; -import com.android.settingslib.bluetooth.BluetoothEventManager; -import com.android.settingslib.bluetooth.LocalBluetoothManager; -import com.android.settingslib.core.lifecycle.Lifecycle; -import com.android.settingslib.flags.Flags; - -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Answers; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.annotation.Config; -import org.robolectric.shadow.api.Shadow; - -/** Tests for {@link TemporaryBondDeviceGroupController}. */ -@RunWith(RobolectricTestRunner.class) -@Config( - shadows = { - ShadowBluetoothAdapter.class, - ShadowBluetoothUtils.class - }) -public class TemporaryBondDeviceGroupControllerTest { - private static final String KEY = "temp_bond_device_list"; - private static final String PREFERENCE_KEY_1 = "pref_key_1"; - - @Rule - public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule(); - - @Mock - private TemporaryBondDeviceGroupUpdater mBluetoothDeviceUpdater; - @Mock(answer = Answers.RETURNS_DEEP_STUBS) - private PreferenceManager mPreferenceManager; - @Mock - private LocalBluetoothManager mLocalBtManager; - @Mock - private BluetoothEventManager mEventManager; - @Mock private PreferenceScreen mScreen; - - - private PreferenceGroup mPreferenceGroup; - private Context mContext; - private Preference mPreference; - private TemporaryBondDeviceGroupController mTemporaryBondDeviceGroupController; - private LifecycleOwner mLifecycleOwner; - private Lifecycle mLifecycle; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - - mContext = ApplicationProvider.getApplicationContext(); - mPreference = new Preference(mContext); - mPreference.setKey(PREFERENCE_KEY_1); - mPreferenceGroup = spy(new PreferenceCategory(mContext)); - when(mPreferenceGroup.getPreferenceManager()).thenReturn(mPreferenceManager); - mLifecycleOwner = () -> mLifecycle; - mLifecycle = new Lifecycle(mLifecycleOwner); - when(mScreen.getContext()).thenReturn(mContext); - when(mScreen.findPreference(KEY)).thenReturn(mPreferenceGroup); - - ShadowBluetoothUtils.sLocalBluetoothManager = mLocalBtManager; - mLocalBtManager = Utils.getLocalBtManager(mContext); - when(mLocalBtManager.getEventManager()).thenReturn(mEventManager); - ShadowBluetoothAdapter shadowBluetoothAdapter = Shadow.extract( - BluetoothAdapter.getDefaultAdapter()); - shadowBluetoothAdapter.setEnabled(true); - shadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported( - BluetoothStatusCodes.FEATURE_SUPPORTED); - shadowBluetoothAdapter.setIsLeAudioBroadcastAssistantSupported( - BluetoothStatusCodes.FEATURE_SUPPORTED); - - mTemporaryBondDeviceGroupController = spy(new TemporaryBondDeviceGroupController(mContext)); - mTemporaryBondDeviceGroupController.setBluetoothDeviceUpdater(mBluetoothDeviceUpdater); - mTemporaryBondDeviceGroupController.setPreferenceGroup(mPreferenceGroup); - } - - @Test - @RequiresFlagsDisabled(Flags.FLAG_ENABLE_TEMPORARY_BOND_DEVICES_UI) - @RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) - public void onStart_flagOff_doNothing() { - mTemporaryBondDeviceGroupController.onStart(mLifecycleOwner); - - verify(mEventManager, never()).registerCallback(any(BluetoothCallback.class)); - verify(mBluetoothDeviceUpdater, never()).registerCallback(); - verify(mBluetoothDeviceUpdater, never()).refreshPreference(); - } - - @Test - @RequiresFlagsDisabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) - @RequiresFlagsEnabled(Flags.FLAG_ENABLE_TEMPORARY_BOND_DEVICES_UI) - public void onStart_audioSharingUINotAvailable_doNothing() { - mTemporaryBondDeviceGroupController.onStart(mLifecycleOwner); - - verify(mEventManager, never()).registerCallback(any(BluetoothCallback.class)); - verify(mBluetoothDeviceUpdater, never()).registerCallback(); - verify(mBluetoothDeviceUpdater, never()).refreshPreference(); - } - - @Test - @RequiresFlagsEnabled({Flags.FLAG_ENABLE_TEMPORARY_BOND_DEVICES_UI, - Flags.FLAG_ENABLE_LE_AUDIO_SHARING}) - public void onStart_registerCallbacks() { - mTemporaryBondDeviceGroupController.onStart(mLifecycleOwner); - - verify(mEventManager).registerCallback(any(BluetoothCallback.class)); - verify(mBluetoothDeviceUpdater).registerCallback(); - verify(mBluetoothDeviceUpdater).refreshPreference(); - } - - @Test - @RequiresFlagsEnabled({Flags.FLAG_ENABLE_TEMPORARY_BOND_DEVICES_UI, - Flags.FLAG_ENABLE_LE_AUDIO_SHARING}) - public void onStop_unregisterCallbacks() { - mTemporaryBondDeviceGroupController.onStop(mLifecycleOwner); - - verify(mEventManager).unregisterCallback(any(BluetoothCallback.class)); - verify(mBluetoothDeviceUpdater).unregisterCallback(); - } - - @Test - @RequiresFlagsDisabled(Flags.FLAG_ENABLE_TEMPORARY_BOND_DEVICES_UI) - @RequiresFlagsEnabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) - public void displayPreference_flagOff_doNothing() { - mTemporaryBondDeviceGroupController.displayPreference(mScreen); - - assertThat(mPreferenceGroup.isVisible()).isFalse(); - verify(mBluetoothDeviceUpdater, never()).forceUpdate(); - } - - @Test - @RequiresFlagsDisabled(Flags.FLAG_ENABLE_LE_AUDIO_SHARING) - @RequiresFlagsEnabled(Flags.FLAG_ENABLE_TEMPORARY_BOND_DEVICES_UI) - public void displayPreference_audioSharingUINotAvailable_doNothing() { - mTemporaryBondDeviceGroupController.displayPreference(mScreen); - - assertThat(mPreferenceGroup.isVisible()).isFalse(); - verify(mBluetoothDeviceUpdater, never()).forceUpdate(); - } - - @Test - @RequiresFlagsEnabled({Flags.FLAG_ENABLE_TEMPORARY_BOND_DEVICES_UI, - Flags.FLAG_ENABLE_LE_AUDIO_SHARING}) - public void displayPreference_updateDeviceList() { - mTemporaryBondDeviceGroupController.displayPreference(mScreen); - - assertThat(mPreferenceGroup.isVisible()).isFalse(); - verify(mBluetoothDeviceUpdater).setPrefContext(mContext); - verify(mBluetoothDeviceUpdater).forceUpdate(); - } - - @Test - public void onDeviceAdded_firstAdd_becomeVisibleAndPreferenceAdded() { - mTemporaryBondDeviceGroupController.onDeviceAdded(mPreference); - - assertThat(mPreferenceGroup.isVisible()).isTrue(); - assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(1); - } - - @Test - public void onDeviceRemoved_lastRemove_becomeInvisibleAndPreferenceRemoved() { - mPreferenceGroup.addPreference(mPreference); - - mTemporaryBondDeviceGroupController.onDeviceRemoved(mPreference); - - assertThat(mPreferenceGroup.isVisible()).isFalse(); - assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(0); - } - - @Test - public void onDeviceRemoved_notLastRemove_stillVisible() { - mPreferenceGroup.setVisible(true); - mPreferenceGroup.addPreference(mPreference); - mPreferenceGroup.addPreference(new Preference(mContext)); - - mTemporaryBondDeviceGroupController.onDeviceRemoved(mPreference); - - assertThat(mPreferenceGroup.isVisible()).isTrue(); - assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(1); - } - - @Test - public void getPreferenceKey_returnsCorrectKey() { - assertThat(mTemporaryBondDeviceGroupController.getPreferenceKey()).isEqualTo(KEY); - } - - @Test - @RequiresFlagsEnabled({Flags.FLAG_ENABLE_TEMPORARY_BOND_DEVICES_UI, - Flags.FLAG_ENABLE_LE_AUDIO_SHARING}) - public void getAvailabilityStatus_returnsAvailable() { - assertThat(mTemporaryBondDeviceGroupController.getAvailabilityStatus()).isEqualTo( - AVAILABLE_UNSEARCHABLE); - } -} diff --git a/tests/robotests/src/com/android/settings/connecteddevice/audiosharing/TemporaryBondDeviceGroupUpdaterTest.java b/tests/robotests/src/com/android/settings/connecteddevice/audiosharing/TemporaryBondDeviceGroupUpdaterTest.java deleted file mode 100644 index 0b34c101719..00000000000 --- a/tests/robotests/src/com/android/settings/connecteddevice/audiosharing/TemporaryBondDeviceGroupUpdaterTest.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Copyright (C) 2025 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings.connecteddevice.audiosharing; - -import static com.google.common.truth.Truth.assertThat; - -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.when; - -import android.bluetooth.BluetoothAdapter; -import android.bluetooth.BluetoothDevice; -import android.bluetooth.BluetoothStatusCodes; -import android.content.Context; -import android.platform.test.annotations.RequiresFlagsEnabled; -import android.platform.test.flag.junit.CheckFlagsRule; -import android.platform.test.flag.junit.DeviceFlagsValueProvider; - -import androidx.test.core.app.ApplicationProvider; - -import com.android.settings.bluetooth.Utils; -import com.android.settings.connecteddevice.DevicePreferenceCallback; -import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; -import com.android.settings.testutils.shadow.ShadowBluetoothUtils; -import com.android.settingslib.bluetooth.CachedBluetoothDevice; -import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager; -import com.android.settingslib.bluetooth.LocalBluetoothManager; -import com.android.settingslib.flags.Flags; - -import org.junit.After; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.annotation.Config; -import org.robolectric.shadow.api.Shadow; - -import java.util.ArrayList; -import java.util.Collection; - -/** Tests for {@link TemporaryBondDeviceGroupUpdater}. */ -@RunWith(RobolectricTestRunner.class) -@Config( - shadows = { - ShadowBluetoothAdapter.class, - ShadowBluetoothUtils.class - }) -public class TemporaryBondDeviceGroupUpdaterTest { - private static final String TAG = "TemporaryBondDeviceGroupUpdater"; - private static final String PREF_KEY_PREFIX = "temp_bond_bt_"; - private static final String TEMP_BOND_METADATA = - "le_audio_sharing"; - private static final int METADATA_FAST_PAIR_CUSTOMIZED_FIELDS = 25; - - @Rule - public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule(); - - @Mock - private DevicePreferenceCallback mDevicePreferenceCallback; - @Mock - private CachedBluetoothDevice mCachedBluetoothDevice; - @Mock - private BluetoothDevice mBluetoothDevice; - @Mock - private LocalBluetoothManager mLocalBtManager; - @Mock - private CachedBluetoothDeviceManager mCachedDeviceManager; - - private TemporaryBondDeviceGroupUpdater mDeviceUpdater; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - - ShadowBluetoothAdapter shadowBluetoothAdapter = Shadow.extract( - BluetoothAdapter.getDefaultAdapter()); - shadowBluetoothAdapter.setEnabled(true); - shadowBluetoothAdapter.setIsLeAudioBroadcastSourceSupported( - BluetoothStatusCodes.FEATURE_SUPPORTED); - shadowBluetoothAdapter.setIsLeAudioBroadcastAssistantSupported( - BluetoothStatusCodes.FEATURE_SUPPORTED); - Context context = ApplicationProvider.getApplicationContext(); - ShadowBluetoothUtils.sLocalBluetoothManager = mLocalBtManager; - mLocalBtManager = Utils.getLocalBtManager(context); - when(mLocalBtManager.getCachedDeviceManager()).thenReturn(mCachedDeviceManager); - when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice); - Collection cachedDevices = new ArrayList<>(); - cachedDevices.add(mCachedBluetoothDevice); - when(mCachedDeviceManager.getCachedDevicesCopy()).thenReturn(cachedDevices); - mDeviceUpdater = - spy( - new TemporaryBondDeviceGroupUpdater( - context, mDevicePreferenceCallback, /* metricsCategory= */ 0)); - mDeviceUpdater.setPrefContext(context); - } - - @After - public void tearDown() { - ShadowBluetoothUtils.reset(); - } - - @Test - @RequiresFlagsEnabled({Flags.FLAG_ENABLE_TEMPORARY_BOND_DEVICES_UI, - Flags.FLAG_ENABLE_LE_AUDIO_SHARING}) - public void isFilterMatched_isTemporaryBondDevice_returnsTrue() { - when(mBluetoothDevice.isConnected()).thenReturn(true); - when(mBluetoothDevice.getBondState()).thenReturn(BluetoothDevice.BOND_BONDED); - when(mBluetoothDevice.getMetadata(METADATA_FAST_PAIR_CUSTOMIZED_FIELDS)) - .thenReturn(TEMP_BOND_METADATA.getBytes()); - - assertThat(mDeviceUpdater.isFilterMatched(mCachedBluetoothDevice)).isTrue(); - } - - @Test - public void getLogTag_returnsCorrectTag() { - assertThat(mDeviceUpdater.getLogTag()).isEqualTo(TAG); - } - - @Test - public void getPreferenceKey_returnsCorrectKey() { - assertThat(mDeviceUpdater.getPreferenceKeyPrefix()).isEqualTo(PREF_KEY_PREFIX); - } -}