Merge changes from topics "ha-aics", "ha-aics-mute", "ha-local-data" into main
* changes: [Ambient Volume] Ambient volume icon [Ambient Volume] Show value with remote data [Ambient Volume] Show value with local data [Ambient Volume] UI of volume sliders in Settings
This commit is contained in:
@@ -0,0 +1,238 @@
|
||||
/*
|
||||
* 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 int TEST_LEFT_VOLUME_LEVEL = 1;
|
||||
private static final int TEST_RIGHT_VOLUME_LEVEL = 2;
|
||||
private static final int TEST_UNIFIED_VOLUME_LEVEL = 3;
|
||||
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 ImageView mVolumeIcon;
|
||||
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);
|
||||
mPreference.setMutable(true);
|
||||
preferenceScreen.addPreference(mPreference);
|
||||
|
||||
prepareSliders();
|
||||
mPreference.setSliders(mSideToSlidersMap);
|
||||
|
||||
mExpandIcon = new ImageView(mContext);
|
||||
mVolumeIcon = new ImageView(mContext);
|
||||
mVolumeIcon.setImageResource(com.android.settingslib.R.drawable.ic_ambient_volume);
|
||||
mVolumeIcon.setImageLevel(0);
|
||||
when(mItemView.requireViewById(R.id.expand_icon)).thenReturn(mExpandIcon);
|
||||
when(mItemView.requireViewById(com.android.internal.R.id.icon)).thenReturn(mVolumeIcon);
|
||||
when(mItemView.requireViewById(R.id.icon_frame)).thenReturn(mVolumeIcon);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setMutable_mutable_clickOnMuteIconChangeMuteState() {
|
||||
mPreference.setMutable(true);
|
||||
mPreference.setMuted(false);
|
||||
|
||||
mVolumeIcon.callOnClick();
|
||||
|
||||
assertThat(mPreference.isMuted()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setMutable_notMutable_clickOnMuteIconWontChangeMuteState() {
|
||||
mPreference.setMutable(false);
|
||||
mPreference.setMuted(false);
|
||||
|
||||
mVolumeIcon.callOnClick();
|
||||
|
||||
assertThat(mPreference.isMuted()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateLayout_mute_volumeIconIsCorrect() {
|
||||
mPreference.setMuted(true);
|
||||
mPreference.updateLayout();
|
||||
|
||||
assertThat(mVolumeIcon.getDrawable().getLevel()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateLayout_unmuteAndExpanded_volumeIconIsCorrect() {
|
||||
mPreference.setMuted(false);
|
||||
mPreference.setExpanded(true);
|
||||
mPreference.updateLayout();
|
||||
|
||||
int expectedLevel = calculateVolumeLevel(TEST_LEFT_VOLUME_LEVEL, TEST_RIGHT_VOLUME_LEVEL);
|
||||
assertThat(mVolumeIcon.getDrawable().getLevel()).isEqualTo(expectedLevel);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateLayout_unmuteAndNotExpanded_volumeIconIsCorrect() {
|
||||
mPreference.setMuted(false);
|
||||
mPreference.setExpanded(false);
|
||||
mPreference.updateLayout();
|
||||
|
||||
int expectedLevel = calculateVolumeLevel(TEST_UNIFIED_VOLUME_LEVEL,
|
||||
TEST_UNIFIED_VOLUME_LEVEL);
|
||||
assertThat(mVolumeIcon.getDrawable().getLevel()).isEqualTo(expectedLevel);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setSliderEnabled_expandedAndLeftIsDisabled_volumeIconIcCorrect() {
|
||||
mPreference.setExpanded(true);
|
||||
mPreference.setSliderEnabled(SIDE_LEFT, false);
|
||||
|
||||
int expectedLevel = calculateVolumeLevel(0, TEST_RIGHT_VOLUME_LEVEL);
|
||||
assertThat(mVolumeIcon.getDrawable().getLevel()).isEqualTo(expectedLevel);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setSliderValue_expandedAndLeftValueChanged_volumeIconIcCorrect() {
|
||||
mPreference.setExpanded(true);
|
||||
mPreference.setSliderValue(SIDE_LEFT, 4);
|
||||
|
||||
int expectedLevel = calculateVolumeLevel(4, TEST_RIGHT_VOLUME_LEVEL);
|
||||
assertThat(mVolumeIcon.getDrawable().getLevel()).isEqualTo(expectedLevel);
|
||||
}
|
||||
|
||||
private int calculateVolumeLevel(int left, int right) {
|
||||
return left * 5 + right;
|
||||
}
|
||||
|
||||
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);
|
||||
slider.setMin(0);
|
||||
slider.setMax(4);
|
||||
if (side == SIDE_LEFT) {
|
||||
slider.setKey(KEY_LEFT_SLIDER);
|
||||
slider.setProgress(TEST_LEFT_VOLUME_LEVEL);
|
||||
} else if (side == SIDE_RIGHT) {
|
||||
slider.setKey(KEY_RIGHT_SLIDER);
|
||||
slider.setProgress(TEST_RIGHT_VOLUME_LEVEL);
|
||||
} else {
|
||||
slider.setKey(KEY_UNIFIED_SLIDER);
|
||||
slider.setProgress(TEST_UNIFIED_VOLUME_LEVEL);
|
||||
}
|
||||
mSideToSlidersMap.put(side, slider);
|
||||
}
|
||||
}
|
@@ -0,0 +1,434 @@
|
||||
/*
|
||||
* 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 android.bluetooth.AudioInputControl.MUTE_DISABLED;
|
||||
import static android.bluetooth.AudioInputControl.MUTE_NOT_MUTED;
|
||||
import static android.bluetooth.AudioInputControl.MUTE_MUTED;
|
||||
import static android.bluetooth.BluetoothDevice.BOND_BONDED;
|
||||
|
||||
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.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.atLeastOnce;
|
||||
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 static org.robolectric.Shadows.shadowOf;
|
||||
|
||||
import android.bluetooth.BluetoothDevice;
|
||||
import android.bluetooth.BluetoothProfile;
|
||||
import android.content.ContentResolver;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.provider.Settings;
|
||||
|
||||
import androidx.preference.PreferenceCategory;
|
||||
|
||||
import com.android.settings.testutils.shadow.ShadowThreadUtils;
|
||||
import com.android.settings.widget.SeekBarPreference;
|
||||
import com.android.settingslib.bluetooth.AmbientVolumeController;
|
||||
import com.android.settingslib.bluetooth.BluetoothEventManager;
|
||||
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
|
||||
import com.android.settingslib.bluetooth.HearingDeviceLocalDataManager;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||
import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
|
||||
import com.android.settingslib.bluetooth.VolumeControlProfile;
|
||||
|
||||
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 org.robolectric.annotation.Implementation;
|
||||
import org.robolectric.annotation.Implements;
|
||||
import org.robolectric.shadows.ShadowSettings;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
/** Tests for {@link BluetoothDetailsAmbientVolumePreferenceController}. */
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(shadows = {
|
||||
BluetoothDetailsAmbientVolumePreferenceControllerTest.ShadowGlobal.class,
|
||||
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;
|
||||
@Mock
|
||||
private HearingDeviceLocalDataManager mLocalDataManager;
|
||||
@Mock
|
||||
private LocalBluetoothManager mBluetoothManager;
|
||||
@Mock
|
||||
private BluetoothEventManager mEventManager;
|
||||
@Mock
|
||||
private LocalBluetoothProfileManager mProfileManager;
|
||||
@Mock
|
||||
private VolumeControlProfile mVolumeControlProfile;
|
||||
@Mock
|
||||
private AmbientVolumeController mVolumeController;
|
||||
@Mock
|
||||
private Handler mTestHandler;
|
||||
|
||||
private BluetoothDetailsAmbientVolumePreferenceController mController;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
super.setUp();
|
||||
|
||||
mContext = spy(mContext);
|
||||
PreferenceCategory deviceControls = new PreferenceCategory(mContext);
|
||||
deviceControls.setKey(KEY_HEARING_DEVICE_GROUP);
|
||||
mScreen.addPreference(deviceControls);
|
||||
mController = spy(
|
||||
new BluetoothDetailsAmbientVolumePreferenceController(mContext, mBluetoothManager,
|
||||
mFragment, mCachedDevice, mLifecycle, mLocalDataManager,
|
||||
mVolumeController));
|
||||
|
||||
when(mBluetoothManager.getEventManager()).thenReturn(mEventManager);
|
||||
when(mBluetoothManager.getProfileManager()).thenReturn(mProfileManager);
|
||||
when(mProfileManager.getVolumeControlProfile()).thenReturn(mVolumeControlProfile);
|
||||
when(mVolumeControlProfile.getConnectionStatus(mDevice)).thenReturn(
|
||||
BluetoothProfile.STATE_CONNECTED);
|
||||
when(mVolumeControlProfile.getConnectionStatus(mMemberDevice)).thenReturn(
|
||||
BluetoothProfile.STATE_CONNECTED);
|
||||
when(mCachedDevice.getProfiles()).thenReturn(List.of(mVolumeControlProfile));
|
||||
when(mLocalDataManager.get(any(BluetoothDevice.class))).thenReturn(
|
||||
new HearingDeviceLocalDataManager.Data.Builder().build());
|
||||
|
||||
when(mContext.getMainThreadHandler()).thenReturn(mTestHandler);
|
||||
when(mTestHandler.postDelayed(any(Runnable.class), anyLong())).thenAnswer(
|
||||
invocationOnMock -> {
|
||||
invocationOnMock.getArgument(0, Runnable.class).run();
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
@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 onDeviceLocalDataChange_noMemberAndExpanded_uiCorrectAndDataUpdated() {
|
||||
prepareDevice(/* hasMember= */ false);
|
||||
mController.init(mScreen);
|
||||
HearingDeviceLocalDataManager.Data data = new HearingDeviceLocalDataManager.Data.Builder()
|
||||
.ambient(0).groupAmbient(0).ambientControlExpanded(true).build();
|
||||
when(mLocalDataManager.get(mDevice)).thenReturn(data);
|
||||
|
||||
mController.onDeviceLocalDataChange(TEST_ADDRESS, data);
|
||||
shadowOf(Looper.getMainLooper()).idle();
|
||||
|
||||
AmbientVolumePreference preference = mScreen.findPreference(KEY_AMBIENT_VOLUME);
|
||||
assertThat(preference).isNotNull();
|
||||
assertThat(preference.isExpanded()).isFalse();
|
||||
verifyDeviceDataUpdated(mDevice);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onDeviceLocalDataChange_noMemberAndCollapsed_uiCorrectAndDataUpdated() {
|
||||
prepareDevice(/* hasMember= */ false);
|
||||
mController.init(mScreen);
|
||||
HearingDeviceLocalDataManager.Data data = new HearingDeviceLocalDataManager.Data.Builder()
|
||||
.ambient(0).groupAmbient(0).ambientControlExpanded(false).build();
|
||||
when(mLocalDataManager.get(mDevice)).thenReturn(data);
|
||||
|
||||
mController.onDeviceLocalDataChange(TEST_ADDRESS, data);
|
||||
shadowOf(Looper.getMainLooper()).idle();
|
||||
|
||||
AmbientVolumePreference preference = mScreen.findPreference(KEY_AMBIENT_VOLUME);
|
||||
assertThat(preference).isNotNull();
|
||||
assertThat(preference.isExpanded()).isFalse();
|
||||
verifyDeviceDataUpdated(mDevice);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onDeviceLocalDataChange_hasMemberAndExpanded_uiCorrectAndDataUpdated() {
|
||||
prepareDevice(/* hasMember= */ true);
|
||||
mController.init(mScreen);
|
||||
HearingDeviceLocalDataManager.Data data = new HearingDeviceLocalDataManager.Data.Builder()
|
||||
.ambient(0).groupAmbient(0).ambientControlExpanded(true).build();
|
||||
when(mLocalDataManager.get(mDevice)).thenReturn(data);
|
||||
|
||||
mController.onDeviceLocalDataChange(TEST_ADDRESS, data);
|
||||
shadowOf(Looper.getMainLooper()).idle();
|
||||
|
||||
AmbientVolumePreference preference = mScreen.findPreference(KEY_AMBIENT_VOLUME);
|
||||
assertThat(preference).isNotNull();
|
||||
assertThat(preference.isExpanded()).isTrue();
|
||||
verifyDeviceDataUpdated(mDevice);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onDeviceLocalDataChange_hasMemberAndCollapsed_uiCorrectAndDataUpdated() {
|
||||
prepareDevice(/* hasMember= */ true);
|
||||
mController.init(mScreen);
|
||||
HearingDeviceLocalDataManager.Data data = new HearingDeviceLocalDataManager.Data.Builder()
|
||||
.ambient(0).groupAmbient(0).ambientControlExpanded(false).build();
|
||||
when(mLocalDataManager.get(mDevice)).thenReturn(data);
|
||||
|
||||
mController.onDeviceLocalDataChange(TEST_ADDRESS, data);
|
||||
shadowOf(Looper.getMainLooper()).idle();
|
||||
|
||||
AmbientVolumePreference preference = mScreen.findPreference(KEY_AMBIENT_VOLUME);
|
||||
assertThat(preference).isNotNull();
|
||||
assertThat(preference.isExpanded()).isFalse();
|
||||
verifyDeviceDataUpdated(mDevice);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onStart_localDataManagerStartAndCallbackRegistered() {
|
||||
prepareDevice(/* hasMember= */ true);
|
||||
mController.init(mScreen);
|
||||
|
||||
mController.onStart();
|
||||
|
||||
verify(mLocalDataManager, atLeastOnce()).start();
|
||||
verify(mVolumeController).registerCallback(any(Executor.class), eq(mDevice));
|
||||
verify(mVolumeController).registerCallback(any(Executor.class), eq(mMemberDevice));
|
||||
verify(mCachedDevice).registerCallback(any(Executor.class),
|
||||
any(CachedBluetoothDevice.Callback.class));
|
||||
verify(mCachedMemberDevice).registerCallback(any(Executor.class),
|
||||
any(CachedBluetoothDevice.Callback.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onStop_localDataManagerStopAndCallbackUnregistered() {
|
||||
prepareDevice(/* hasMember= */ true);
|
||||
mController.init(mScreen);
|
||||
|
||||
mController.onStop();
|
||||
|
||||
verify(mLocalDataManager).stop();
|
||||
verify(mVolumeController).unregisterCallback(mDevice);
|
||||
verify(mVolumeController).unregisterCallback(mMemberDevice);
|
||||
verify(mCachedDevice).unregisterCallback(any(CachedBluetoothDevice.Callback.class));
|
||||
verify(mCachedMemberDevice).unregisterCallback(any(CachedBluetoothDevice.Callback.class));
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onAmbientChanged_refreshWhenNotInitiateFromUi() {
|
||||
prepareDevice(/* hasMember= */ false);
|
||||
mController.init(mScreen);
|
||||
final int testAmbient = 10;
|
||||
HearingDeviceLocalDataManager.Data data = new HearingDeviceLocalDataManager.Data.Builder()
|
||||
.ambient(testAmbient)
|
||||
.groupAmbient(testAmbient)
|
||||
.ambientControlExpanded(false)
|
||||
.build();
|
||||
when(mLocalDataManager.get(mDevice)).thenReturn(data);
|
||||
getPreference().setExpanded(true);
|
||||
|
||||
mController.onAmbientChanged(mDevice, testAmbient);
|
||||
verify(mController, never()).refresh();
|
||||
|
||||
final int updatedTestAmbient = 20;
|
||||
mController.onAmbientChanged(mDevice, updatedTestAmbient);
|
||||
verify(mController).refresh();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onMuteChanged_refreshWhenNotInitiateFromUi() {
|
||||
prepareDevice(/* hasMember= */ false);
|
||||
mController.init(mScreen);
|
||||
final int testMute = MUTE_NOT_MUTED;
|
||||
AmbientVolumeController.RemoteAmbientState state =
|
||||
new AmbientVolumeController.RemoteAmbientState(testMute, 0);
|
||||
when(mVolumeController.refreshAmbientState(mDevice)).thenReturn(state);
|
||||
getPreference().setMuted(false);
|
||||
|
||||
mController.onMuteChanged(mDevice, testMute);
|
||||
verify(mController, never()).refresh();
|
||||
|
||||
final int updatedTestMute = MUTE_MUTED;
|
||||
mController.onMuteChanged(mDevice, updatedTestMute);
|
||||
verify(mController).refresh();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void refresh_leftAndRightDifferentGainSetting_expandControl() {
|
||||
prepareDevice(/* hasMember= */ true);
|
||||
mController.init(mScreen);
|
||||
prepareRemoteData(mDevice, 10, MUTE_NOT_MUTED);
|
||||
prepareRemoteData(mMemberDevice, 20, MUTE_NOT_MUTED);
|
||||
getPreference().setExpanded(false);
|
||||
|
||||
mController.refresh();
|
||||
|
||||
assertThat(getPreference().isExpanded()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void refresh_oneSideNotMutable_controlNotMutableAndNotMuted() {
|
||||
prepareDevice(/* hasMember= */ true);
|
||||
mController.init(mScreen);
|
||||
prepareRemoteData(mDevice, 10, MUTE_DISABLED);
|
||||
prepareRemoteData(mMemberDevice, 20, MUTE_NOT_MUTED);
|
||||
getPreference().setMutable(true);
|
||||
getPreference().setMuted(true);
|
||||
|
||||
mController.refresh();
|
||||
|
||||
assertThat(getPreference().isMutable()).isFalse();
|
||||
assertThat(getPreference().isMuted()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void refresh_oneSideNotMuted_controlNotMutedAndSyncToRemote() {
|
||||
prepareDevice(/* hasMember= */ true);
|
||||
mController.init(mScreen);
|
||||
prepareRemoteData(mDevice, 10, MUTE_MUTED);
|
||||
prepareRemoteData(mMemberDevice, 20, MUTE_NOT_MUTED);
|
||||
getPreference().setMutable(true);
|
||||
getPreference().setMuted(true);
|
||||
|
||||
mController.refresh();
|
||||
|
||||
assertThat(getPreference().isMutable()).isTrue();
|
||||
assertThat(getPreference().isMuted()).isFalse();
|
||||
verify(mVolumeController).setMuted(mDevice, false);
|
||||
}
|
||||
|
||||
private void prepareDevice(boolean hasMember) {
|
||||
when(mCachedDevice.getDeviceSide()).thenReturn(SIDE_LEFT);
|
||||
when(mCachedDevice.getDevice()).thenReturn(mDevice);
|
||||
when(mCachedDevice.getBondState()).thenReturn(BOND_BONDED);
|
||||
when(mDevice.getAddress()).thenReturn(TEST_ADDRESS);
|
||||
when(mDevice.getAnonymizedAddress()).thenReturn(TEST_ADDRESS);
|
||||
when(mDevice.isConnected()).thenReturn(true);
|
||||
if (hasMember) {
|
||||
when(mCachedDevice.getMemberDevice()).thenReturn(Set.of(mCachedMemberDevice));
|
||||
when(mCachedMemberDevice.getDeviceSide()).thenReturn(SIDE_RIGHT);
|
||||
when(mCachedMemberDevice.getDevice()).thenReturn(mMemberDevice);
|
||||
when(mCachedMemberDevice.getBondState()).thenReturn(BOND_BONDED);
|
||||
when(mMemberDevice.getAddress()).thenReturn(TEST_MEMBER_ADDRESS);
|
||||
when(mMemberDevice.getAnonymizedAddress()).thenReturn(TEST_MEMBER_ADDRESS);
|
||||
when(mMemberDevice.isConnected()).thenReturn(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void prepareRemoteData(BluetoothDevice device, int gainSetting, int mute) {
|
||||
when(mVolumeController.isAmbientControlAvailable(device)).thenReturn(true);
|
||||
when(mVolumeController.refreshAmbientState(device)).thenReturn(
|
||||
new AmbientVolumeController.RemoteAmbientState(gainSetting, mute));
|
||||
}
|
||||
|
||||
private void verifyDeviceDataUpdated(BluetoothDevice device) {
|
||||
verify(mLocalDataManager, atLeastOnce()).updateAmbient(eq(device), anyInt());
|
||||
verify(mLocalDataManager, atLeastOnce()).updateGroupAmbient(eq(device), anyInt());
|
||||
verify(mLocalDataManager, atLeastOnce()).updateAmbientControlExpanded(eq(device),
|
||||
anyBoolean());
|
||||
}
|
||||
|
||||
private AmbientVolumePreference getPreference() {
|
||||
return mScreen.findPreference(KEY_AMBIENT_VOLUME);
|
||||
}
|
||||
|
||||
@Implements(value = Settings.Global.class)
|
||||
public static class ShadowGlobal extends ShadowSettings.ShadowGlobal {
|
||||
private static final Map<ContentResolver, Map<String, String>> sDataMap = new HashMap<>();
|
||||
|
||||
@Implementation
|
||||
protected static boolean putStringForUser(
|
||||
ContentResolver cr, String name, String value, int userHandle) {
|
||||
get(cr).put(name, value);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Implementation
|
||||
protected static String getStringForUser(ContentResolver cr, String name, int userHandle) {
|
||||
return get(cr).get(name);
|
||||
}
|
||||
|
||||
private static Map<String, String> get(ContentResolver cr) {
|
||||
return sDataMap.computeIfAbsent(cr, k -> new HashMap<>());
|
||||
}
|
||||
}
|
||||
}
|
@@ -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