[Ambient Volume] UI of volume sliders in Settings
Collapse/expand the controls when clicking on the hearder with arrow. Flag: com.android.settingslib.flags.hearing_devices_ambient_volume_control Bug: 357878944 Test: atest AmbientVolumePreferenceTest Test: atest BluetoothDetailsAmbientVolumePreferenceControllerTest Test: atest BluetoothDetailsHearingDeviceControllerTest Change-Id: I845a4397601e563ed027d7d2a0a13651e95de708
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
/*
|
||||
* 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.AmbientVolumePreference.ROTATION_COLLAPSED;
|
||||
import static com.android.settings.bluetooth.AmbientVolumePreference.ROTATION_EXPANDED;
|
||||
import static com.android.settings.bluetooth.AmbientVolumePreference.SIDE_UNIFIED;
|
||||
import static com.android.settings.bluetooth.BluetoothDetailsAmbientVolumePreferenceController.KEY_AMBIENT_VOLUME;
|
||||
import static com.android.settings.bluetooth.BluetoothDetailsAmbientVolumePreferenceController.KEY_AMBIENT_VOLUME_SLIDER;
|
||||
import static com.android.settingslib.bluetooth.HearingAidInfo.DeviceSide.SIDE_LEFT;
|
||||
import static com.android.settingslib.bluetooth.HearingAidInfo.DeviceSide.SIDE_RIGHT;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.ArrayMap;
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import androidx.preference.PreferenceManager;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
import androidx.preference.PreferenceViewHolder;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.widget.SeekBarPreference;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Spy;
|
||||
import org.mockito.junit.MockitoJUnit;
|
||||
import org.mockito.junit.MockitoRule;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/** Tests for {@link AmbientVolumePreference}. */
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class AmbientVolumePreferenceTest {
|
||||
|
||||
private static final String KEY_UNIFIED_SLIDER = KEY_AMBIENT_VOLUME_SLIDER + "_" + SIDE_UNIFIED;
|
||||
private static final String KEY_LEFT_SLIDER = KEY_AMBIENT_VOLUME_SLIDER + "_" + SIDE_LEFT;
|
||||
private static final String KEY_RIGHT_SLIDER = KEY_AMBIENT_VOLUME_SLIDER + "_" + SIDE_RIGHT;
|
||||
|
||||
@Rule
|
||||
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
|
||||
@Spy
|
||||
private Context mContext = ApplicationProvider.getApplicationContext();
|
||||
@Mock
|
||||
private AmbientVolumePreference.OnIconClickListener mListener;
|
||||
@Mock
|
||||
private View mItemView;
|
||||
|
||||
private AmbientVolumePreference mPreference;
|
||||
private ImageView mExpandIcon;
|
||||
private final Map<Integer, SeekBarPreference> mSideToSlidersMap = new ArrayMap<>();
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
PreferenceManager preferenceManager = new PreferenceManager(mContext);
|
||||
PreferenceScreen preferenceScreen = preferenceManager.createPreferenceScreen(mContext);
|
||||
mPreference = new AmbientVolumePreference(mContext);
|
||||
mPreference.setKey(KEY_AMBIENT_VOLUME);
|
||||
mPreference.setOnIconClickListener(mListener);
|
||||
mPreference.setExpandable(true);
|
||||
preferenceScreen.addPreference(mPreference);
|
||||
|
||||
prepareSliders();
|
||||
mPreference.setSliders(mSideToSlidersMap);
|
||||
|
||||
mExpandIcon = new ImageView(mContext);
|
||||
when(mItemView.requireViewById(R.id.expand_icon)).thenReturn(mExpandIcon);
|
||||
|
||||
PreferenceViewHolder preferenceViewHolder = PreferenceViewHolder.createInstanceForTests(
|
||||
mItemView);
|
||||
mPreference.onBindViewHolder(preferenceViewHolder);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setExpandable_expandable_expandIconVisible() {
|
||||
mPreference.setExpandable(true);
|
||||
|
||||
assertThat(mExpandIcon.getVisibility()).isEqualTo(View.VISIBLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setExpandable_notExpandable_expandIconGone() {
|
||||
mPreference.setExpandable(false);
|
||||
|
||||
assertThat(mExpandIcon.getVisibility()).isEqualTo(View.GONE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setExpanded_expanded_assertControlUiCorrect() {
|
||||
mPreference.setExpanded(true);
|
||||
|
||||
assertControlUiCorrect();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setExpanded_notExpanded_assertControlUiCorrect() {
|
||||
mPreference.setExpanded(false);
|
||||
|
||||
assertControlUiCorrect();
|
||||
}
|
||||
|
||||
private void assertControlUiCorrect() {
|
||||
final boolean expanded = mPreference.isExpanded();
|
||||
assertThat(mSideToSlidersMap.get(SIDE_UNIFIED).isVisible()).isEqualTo(!expanded);
|
||||
assertThat(mSideToSlidersMap.get(SIDE_LEFT).isVisible()).isEqualTo(expanded);
|
||||
assertThat(mSideToSlidersMap.get(SIDE_RIGHT).isVisible()).isEqualTo(expanded);
|
||||
final float rotation = expanded ? ROTATION_EXPANDED : ROTATION_COLLAPSED;
|
||||
assertThat(mExpandIcon.getRotation()).isEqualTo(rotation);
|
||||
}
|
||||
|
||||
private void prepareSliders() {
|
||||
prepareSlider(SIDE_UNIFIED);
|
||||
prepareSlider(SIDE_LEFT);
|
||||
prepareSlider(SIDE_RIGHT);
|
||||
}
|
||||
|
||||
private void prepareSlider(int side) {
|
||||
SeekBarPreference slider = new SeekBarPreference(mContext);
|
||||
if (side == SIDE_LEFT) {
|
||||
slider.setKey(KEY_LEFT_SLIDER);
|
||||
} else if (side == SIDE_RIGHT) {
|
||||
slider.setKey(KEY_RIGHT_SLIDER);
|
||||
} else {
|
||||
slider.setKey(KEY_UNIFIED_SLIDER);
|
||||
}
|
||||
mSideToSlidersMap.put(side, slider);
|
||||
}
|
||||
}
|
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* 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.BluetoothDetailsAmbientVolumePreferenceController.KEY_AMBIENT_VOLUME;
|
||||
import static com.android.settings.bluetooth.BluetoothDetailsAmbientVolumePreferenceController.KEY_AMBIENT_VOLUME_SLIDER;
|
||||
import static com.android.settings.bluetooth.BluetoothDetailsHearingDeviceController.KEY_HEARING_DEVICE_GROUP;
|
||||
import static com.android.settingslib.bluetooth.HearingAidInfo.DeviceSide.SIDE_LEFT;
|
||||
import static com.android.settingslib.bluetooth.HearingAidInfo.DeviceSide.SIDE_RIGHT;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.robolectric.Shadows.shadowOf;
|
||||
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.os.Looper;
|
||||
|
||||
import androidx.preference.PreferenceCategory;
|
||||
|
||||
import com.android.settings.testutils.shadow.ShadowThreadUtils;
|
||||
import com.android.settings.widget.SeekBarPreference;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||
|
||||
import org.junit.Before;
|
||||
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 org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/** Tests for {@link BluetoothDetailsAmbientVolumePreferenceController}. */
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(shadows = {
|
||||
ShadowThreadUtils.class
|
||||
})
|
||||
public class BluetoothDetailsAmbientVolumePreferenceControllerTest extends
|
||||
BluetoothDetailsControllerTestBase {
|
||||
|
||||
@Rule
|
||||
public final MockitoRule mMockitoRule = MockitoJUnit.rule();
|
||||
|
||||
private static final String LEFT_CONTROL_KEY = KEY_AMBIENT_VOLUME_SLIDER + "_" + SIDE_LEFT;
|
||||
private static final String RIGHT_CONTROL_KEY = KEY_AMBIENT_VOLUME_SLIDER + "_" + SIDE_RIGHT;
|
||||
private static final String TEST_ADDRESS = "00:00:00:00:11";
|
||||
private static final String TEST_MEMBER_ADDRESS = "00:00:00:00:22";
|
||||
|
||||
@Mock
|
||||
private CachedBluetoothDevice mCachedMemberDevice;
|
||||
@Mock
|
||||
private BluetoothDevice mDevice;
|
||||
@Mock
|
||||
private BluetoothDevice mMemberDevice;
|
||||
|
||||
private BluetoothDetailsAmbientVolumePreferenceController mController;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
super.setUp();
|
||||
|
||||
PreferenceCategory deviceControls = new PreferenceCategory(mContext);
|
||||
deviceControls.setKey(KEY_HEARING_DEVICE_GROUP);
|
||||
mScreen.addPreference(deviceControls);
|
||||
mController = new BluetoothDetailsAmbientVolumePreferenceController(mContext, mFragment,
|
||||
mCachedDevice, mLifecycle);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void init_deviceWithoutMember_controlNotExpandable() {
|
||||
prepareDevice(/* hasMember= */ false);
|
||||
|
||||
mController.init(mScreen);
|
||||
|
||||
AmbientVolumePreference preference = mScreen.findPreference(KEY_AMBIENT_VOLUME);
|
||||
assertThat(preference).isNotNull();
|
||||
assertThat(preference.isExpandable()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void init_deviceWithMember_controlExpandable() {
|
||||
prepareDevice(/* hasMember= */ true);
|
||||
|
||||
mController.init(mScreen);
|
||||
|
||||
AmbientVolumePreference preference = mScreen.findPreference(KEY_AMBIENT_VOLUME);
|
||||
assertThat(preference).isNotNull();
|
||||
assertThat(preference.isExpandable()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onDeviceAttributesChanged_newDevice_newPreference() {
|
||||
prepareDevice(/* hasMember= */ false);
|
||||
|
||||
mController.init(mScreen);
|
||||
|
||||
// check the right control is null before onDeviceAttributesChanged()
|
||||
SeekBarPreference leftControl = mScreen.findPreference(LEFT_CONTROL_KEY);
|
||||
SeekBarPreference rightControl = mScreen.findPreference(RIGHT_CONTROL_KEY);
|
||||
assertThat(leftControl).isNotNull();
|
||||
assertThat(rightControl).isNull();
|
||||
|
||||
prepareDevice(/* hasMember= */ true);
|
||||
mController.onDeviceAttributesChanged();
|
||||
shadowOf(Looper.getMainLooper()).idle();
|
||||
|
||||
// check the right control is created after onDeviceAttributesChanged()
|
||||
SeekBarPreference updatedLeftControl = mScreen.findPreference(LEFT_CONTROL_KEY);
|
||||
SeekBarPreference updatedRightControl = mScreen.findPreference(RIGHT_CONTROL_KEY);
|
||||
assertThat(updatedLeftControl).isEqualTo(leftControl);
|
||||
assertThat(updatedRightControl).isNotNull();
|
||||
}
|
||||
|
||||
private void prepareDevice(boolean hasMember) {
|
||||
when(mCachedDevice.getDeviceSide()).thenReturn(SIDE_LEFT);
|
||||
when(mCachedDevice.getDevice()).thenReturn(mDevice);
|
||||
when(mDevice.getAddress()).thenReturn(TEST_ADDRESS);
|
||||
if (hasMember) {
|
||||
when(mCachedDevice.getMemberDevice()).thenReturn(Set.of(mCachedMemberDevice));
|
||||
when(mCachedMemberDevice.getDeviceSide()).thenReturn(SIDE_RIGHT);
|
||||
when(mCachedMemberDevice.getDevice()).thenReturn(mMemberDevice);
|
||||
when(mMemberDevice.getAddress()).thenReturn(TEST_MEMBER_ADDRESS);
|
||||
}
|
||||
}
|
||||
}
|
@@ -53,12 +53,12 @@ public class BluetoothDetailsHearingDeviceControllerTest extends
|
||||
@Mock
|
||||
private LocalBluetoothProfileManager mProfileManager;
|
||||
@Mock
|
||||
private BluetoothDetailsHearingDeviceController mHearingDeviceController;
|
||||
@Mock
|
||||
private BluetoothDetailsHearingAidsPresetsController mPresetsController;
|
||||
@Mock
|
||||
private BluetoothDetailsHearingDeviceSettingsController mHearingDeviceSettingsController;
|
||||
|
||||
private BluetoothDetailsHearingDeviceController mHearingDeviceController;
|
||||
|
||||
@Override
|
||||
public void setUp() {
|
||||
super.setUp();
|
||||
@@ -126,4 +126,24 @@ public class BluetoothDetailsHearingDeviceControllerTest extends
|
||||
assertThat(mHearingDeviceController.getSubControllers().stream().anyMatch(
|
||||
c -> c instanceof BluetoothDetailsHearingAidsPresetsController)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@RequiresFlagsEnabled(
|
||||
com.android.settingslib.flags.Flags.FLAG_HEARING_DEVICES_AMBIENT_VOLUME_CONTROL)
|
||||
public void initSubControllers_flagEnabled_ambientVolumeControllerExist() {
|
||||
mHearingDeviceController.initSubControllers(false);
|
||||
|
||||
assertThat(mHearingDeviceController.getSubControllers().stream().anyMatch(
|
||||
c -> c instanceof BluetoothDetailsAmbientVolumePreferenceController)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@RequiresFlagsDisabled(
|
||||
com.android.settingslib.flags.Flags.FLAG_HEARING_DEVICES_AMBIENT_VOLUME_CONTROL)
|
||||
public void initSubControllers_flagDisabled_ambientVolumeControllerNotExist() {
|
||||
mHearingDeviceController.initSubControllers(false);
|
||||
|
||||
assertThat(mHearingDeviceController.getSubControllers().stream().anyMatch(
|
||||
c -> c instanceof BluetoothDetailsAmbientVolumePreferenceController)).isFalse();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user