Selects presets in device details page (1/2)

Enables users to select their presets in Bluetooth device details page if the device supports HAP.

This CL only contains the UI elements. The full functionality will be introduce in the next CL.

Bug: 300015207
Test: atest BluetoothDetailsHearingDeviceControllerTest
Test: atest BluetoothDetailsHearingAidsPresetsControllerTest
Change-Id: I1ab4781191b0c9e1033a29c30ca61671878bb7e1
This commit is contained in:
Angela Wang
2024-02-27 13:41:56 +00:00
parent 4af270b231
commit 82e4ed3bd1
7 changed files with 403 additions and 6 deletions

View File

@@ -0,0 +1,129 @@
/*
* Copyright (C) 2024 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.bluetooth;
import static com.android.settings.bluetooth.BluetoothDetailsHearingDeviceController.KEY_HEARING_DEVICE_GROUP;
import static com.android.settings.bluetooth.BluetoothDetailsHearingAidsPresetsController.KEY_HEARING_AIDS_PRESETS;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothHapClient;
import androidx.preference.ListPreference;
import androidx.preference.PreferenceCategory;
import com.android.settingslib.bluetooth.HapClientProfile;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import java.util.List;
import java.util.concurrent.Executor;
/** Tests for {@link BluetoothDetailsHearingAidsPresetsController}. */
@RunWith(RobolectricTestRunner.class)
public class BluetoothDetailsHearingAidsPresetsControllerTest extends
BluetoothDetailsControllerTestBase {
private static final int TEST_PRESET_INDEX = 1;
private static final String TEST_PRESET_NAME = "test_preset";
@Rule
public final MockitoRule mockito = MockitoJUnit.rule();
@Mock
private LocalBluetoothManager mLocalManager;
@Mock
private LocalBluetoothProfileManager mProfileManager;
@Mock
private HapClientProfile mHapClientProfile;
private BluetoothDetailsHearingAidsPresetsController mController;
@Override
public void setUp() {
super.setUp();
when(mLocalManager.getProfileManager()).thenReturn(mProfileManager);
when(mProfileManager.getHapClientProfile()).thenReturn(mHapClientProfile);
when(mCachedDevice.getProfiles()).thenReturn(List.of(mHapClientProfile));
PreferenceCategory deviceControls = new PreferenceCategory(mContext);
deviceControls.setKey(KEY_HEARING_DEVICE_GROUP);
mScreen.addPreference(deviceControls);
mController = new BluetoothDetailsHearingAidsPresetsController(mContext, mFragment,
mLocalManager, mCachedDevice, mLifecycle);
mController.init(mScreen);
}
@Test
public void onResume_registerCallback() {
mController.onResume();
verify(mHapClientProfile).registerCallback(any(Executor.class),
any(BluetoothHapClient.Callback.class));
}
@Test
public void onPause_unregisterCallback() {
mController.onPause();
verify(mHapClientProfile).unregisterCallback(any(BluetoothHapClient.Callback.class));
}
@Test
public void onPreferenceChange_keyMatched_verifyStatusUpdated() {
final ListPreference presetPreference = getTestPresetPreference(KEY_HEARING_AIDS_PRESETS);
boolean handled = mController.onPreferenceChange(presetPreference,
String.valueOf(TEST_PRESET_INDEX));
assertThat(handled).isTrue();
}
@Test
public void onPreferenceChange_keyNotMatched_doNothing() {
final ListPreference presetPreference = getTestPresetPreference("wrong_key");
boolean handled = mController.onPreferenceChange(
presetPreference, String.valueOf(TEST_PRESET_INDEX));
assertThat(handled).isFalse();
}
private ListPreference getTestPresetPreference(String key) {
final ListPreference presetPreference = spy(new ListPreference(mContext));
when(presetPreference.findIndexOfValue(String.valueOf(TEST_PRESET_INDEX))).thenReturn(0);
when(presetPreference.getEntries()).thenReturn(new CharSequence[]{TEST_PRESET_NAME});
when(presetPreference.getEntryValues()).thenReturn(
new CharSequence[]{String.valueOf(TEST_PRESET_INDEX)});
presetPreference.setKey(key);
return presetPreference;
}
}

View File

@@ -20,6 +20,15 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
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 com.android.settings.accessibility.Flags;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -33,11 +42,20 @@ import org.robolectric.RobolectricTestRunner;
public class BluetoothDetailsHearingDeviceControllerTest extends
BluetoothDetailsControllerTestBase {
@Rule
public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
@Rule
public final MockitoRule mockito = MockitoJUnit.rule();
@Mock
private LocalBluetoothManager mLocalManager;
@Mock
private LocalBluetoothProfileManager mProfileManager;
@Mock
private BluetoothDetailsHearingDeviceController mHearingDeviceController;
@Mock
private BluetoothDetailsHearingAidsPresetsController mPresetsController;
@Mock
private BluetoothDetailsHearingDeviceSettingsController mHearingDeviceSettingsController;
@@ -45,9 +63,11 @@ public class BluetoothDetailsHearingDeviceControllerTest extends
public void setUp() {
super.setUp();
when(mLocalManager.getProfileManager()).thenReturn(mProfileManager);
mHearingDeviceController = new BluetoothDetailsHearingDeviceController(mContext,
mFragment, mCachedDevice, mLifecycle);
mHearingDeviceController.setSubControllers(mHearingDeviceSettingsController);
mFragment, mLocalManager, mCachedDevice, mLifecycle);
mHearingDeviceController.setSubControllers(mHearingDeviceSettingsController,
mPresetsController);
}
@Test
@@ -57,9 +77,17 @@ public class BluetoothDetailsHearingDeviceControllerTest extends
assertThat(mHearingDeviceController.isAvailable()).isTrue();
}
@Test
public void isAvailable_presetsControlsAvailable_returnTrue() {
when(mPresetsController.isAvailable()).thenReturn(true);
assertThat(mHearingDeviceController.isAvailable()).isTrue();
}
@Test
public void isAvailable_noControllersAvailable_returnFalse() {
when(mHearingDeviceSettingsController.isAvailable()).thenReturn(false);
when(mPresetsController.isAvailable()).thenReturn(false);
assertThat(mHearingDeviceController.isAvailable()).isFalse();
}
@@ -80,4 +108,22 @@ public class BluetoothDetailsHearingDeviceControllerTest extends
assertThat(mHearingDeviceController.getSubControllers().stream().anyMatch(
c -> c instanceof BluetoothDetailsHearingDeviceSettingsController)).isTrue();
}
@Test
@RequiresFlagsEnabled(Flags.FLAG_ENABLE_HEARING_AID_PRESET_CONTROL)
public void initSubControllers_flagEnabled_presetControllerExist() {
mHearingDeviceController.initSubControllers(false);
assertThat(mHearingDeviceController.getSubControllers().stream().anyMatch(
c -> c instanceof BluetoothDetailsHearingAidsPresetsController)).isTrue();
}
@Test
@RequiresFlagsDisabled(Flags.FLAG_ENABLE_HEARING_AID_PRESET_CONTROL)
public void initSubControllers_flagDisabled_presetControllerNotExist() {
mHearingDeviceController.initSubControllers(false);
assertThat(mHearingDeviceController.getSubControllers().stream().anyMatch(
c -> c instanceof BluetoothDetailsHearingAidsPresetsController)).isFalse();
}
}