Bluetooth: Use config value for default max connected audio devices

* The first option in Bluetooth max connected audio devices preference
  should be using system default
* Added template based string array to show system default in the list
  preference and in preference summary when default is chosen
* Reset max connected audio devices property to empty string when
  development setting is disabled or when system default is chosen
* Added instrumentation test to check preference array size and whether
  default value is within range of preference array values
* Modified robolectric tests to verify the updated behaviors

Bug: 64767509
Test: Enable and disable multi-device mode in development settings
      make -j32 RunSettingsRoboTests
      atest SettingsUnitTests:BluetoothMaxConnectedAudioDevicesPreferenceControllerInstrumentationTest
Change-Id: I4915f12df0ac0e6f715e44e0df4a3707dde8d1a4
This commit is contained in:
Jack He
2018-02-27 16:42:57 -08:00
parent 43a2878215
commit 2b0dbf7d24
4 changed files with 182 additions and 73 deletions

View File

@@ -313,7 +313,8 @@
<!-- Bluetooth developer settings: Titles for maximum number of connected audio devices --> <!-- Bluetooth developer settings: Titles for maximum number of connected audio devices -->
<string-array name="bluetooth_max_connected_audio_devices"> <string-array name="bluetooth_max_connected_audio_devices">
<item>1 (Default)</item> <item>Use System Default: <xliff:g id="default_bluetooth_max_connected_audio_devices">%1$d</xliff:g></item>
<item>1</item>
<item>2</item> <item>2</item>
<item>3</item> <item>3</item>
<item>4</item> <item>4</item>
@@ -321,7 +322,8 @@
</string-array> </string-array>
<!-- Bluetooth developer settings: Values for maximum number of connected audio devices --> <!-- Bluetooth developer settings: Values for maximum number of connected audio devices -->
<string-array name="bluetooth_max_connected_audio_devices_values"> <string-array translatable="false" name="bluetooth_max_connected_audio_devices_values">
<item></item>
<item>1</item> <item>1</item>
<item>2</item> <item>2</item>
<item>3</item> <item>3</item>

View File

@@ -22,9 +22,7 @@ import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.ListPreference; import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen; import android.support.v7.preference.PreferenceScreen;
import android.text.TextUtils;
import com.android.settings.R;
import com.android.settings.core.PreferenceControllerMixin; import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.development.DeveloperOptionsPreferenceController; import com.android.settingslib.development.DeveloperOptionsPreferenceController;
@@ -32,58 +30,65 @@ public class BluetoothMaxConnectedAudioDevicesPreferenceController extends
DeveloperOptionsPreferenceController implements Preference.OnPreferenceChangeListener, DeveloperOptionsPreferenceController implements Preference.OnPreferenceChangeListener,
PreferenceControllerMixin { PreferenceControllerMixin {
private static final String BLUETOOTH_MAX_CONNECTED_AUDIO_DEVICES_KEY = private static final String MAX_CONNECTED_AUDIO_DEVICES_PREFERENCE_KEY =
"bluetooth_max_connected_audio_devices"; "bluetooth_max_connected_audio_devices";
@VisibleForTesting @VisibleForTesting
static final String BLUETOOTH_MAX_CONNECTED_AUDIO_DEVICES_PROPERTY = static final String MAX_CONNECTED_AUDIO_DEVICES_PROPERTY =
"persist.bluetooth.maxconnectedaudiodevices"; "persist.bluetooth.maxconnectedaudiodevices";
private final String[] mListValues; private final int mDefaultMaxConnectedAudioDevices;
private final String[] mListSummaries;
private ListPreference mPreference; private ListPreference mPreference;
public BluetoothMaxConnectedAudioDevicesPreferenceController(Context context) { public BluetoothMaxConnectedAudioDevicesPreferenceController(Context context) {
super(context); super(context);
mDefaultMaxConnectedAudioDevices = mContext.getResources().getInteger(
mListValues = context.getResources() com.android.internal.R.integer.config_bluetooth_max_connected_audio_devices);
.getStringArray(R.array.bluetooth_max_connected_audio_devices_values);
mListSummaries = context.getResources()
.getStringArray(R.array.bluetooth_max_connected_audio_devices);
} }
@Override @Override
public String getPreferenceKey() { public String getPreferenceKey() {
return BLUETOOTH_MAX_CONNECTED_AUDIO_DEVICES_KEY; return MAX_CONNECTED_AUDIO_DEVICES_PREFERENCE_KEY;
} }
@Override @Override
public void displayPreference(PreferenceScreen screen) { public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen); super.displayPreference(screen);
mPreference = (ListPreference) screen.findPreference(getPreferenceKey()); mPreference = (ListPreference) screen.findPreference(getPreferenceKey());
final CharSequence[] entries = mPreference.getEntries();
entries[0] = String.format(entries[0].toString(), mDefaultMaxConnectedAudioDevices);
mPreference.setEntries(entries);
} }
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
SystemProperties.set(BLUETOOTH_MAX_CONNECTED_AUDIO_DEVICES_PROPERTY, newValue.toString()); String newValueString = newValue.toString();
final ListPreference listPreference = (ListPreference) preference;
if (listPreference.findIndexOfValue(newValueString) <= 0) {
// Reset property value when default is chosen or when value is illegal
newValueString = "";
}
SystemProperties.set(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY, newValueString);
updateState(preference); updateState(preference);
return true; return true;
} }
@Override @Override
public void updateState(Preference preference) { public void updateState(Preference preference) {
final String currentValue = SystemProperties.get( final ListPreference listPreference = (ListPreference) preference;
BLUETOOTH_MAX_CONNECTED_AUDIO_DEVICES_PROPERTY); final CharSequence[] entries = listPreference.getEntries();
int index = 0; // Defaults to 1 device final String currentValue = SystemProperties.get(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY);
for (int i = 0; i < mListValues.length; i++) { int index = 0;
if (TextUtils.equals(currentValue, mListValues[i])) { if (!currentValue.isEmpty()) {
index = i; index = listPreference.findIndexOfValue(currentValue);
break; if (index < 0) {
// Reset property value when value is illegal
SystemProperties.set(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY, "");
index = 0;
} }
} }
mPreference.setValue(mListValues[index]); listPreference.setValueIndex(index);
mPreference.setSummary(mListSummaries[index]); listPreference.setSummary(entries[index]);
} }
@Override @Override
@@ -95,9 +100,8 @@ public class BluetoothMaxConnectedAudioDevicesPreferenceController extends
@Override @Override
protected void onDeveloperOptionsSwitchDisabled() { protected void onDeveloperOptionsSwitchDisabled() {
mPreference.setEnabled(false); mPreference.setEnabled(false);
SystemProperties.set(BLUETOOTH_MAX_CONNECTED_AUDIO_DEVICES_PROPERTY, mListValues[0]); SystemProperties.set(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY, "");
mPreference.setValue(mListValues[0]); updateState(mPreference);
mPreference.setSummary(mListSummaries[0]);
} }
} }

View File

@@ -17,13 +17,15 @@
package com.android.settings.development; package com.android.settings.development;
import static com.android.settings.development.BluetoothMaxConnectedAudioDevicesPreferenceController import static com.android.settings.development.BluetoothMaxConnectedAudioDevicesPreferenceController
.BLUETOOTH_MAX_CONNECTED_AUDIO_DEVICES_PROPERTY; .MAX_CONNECTED_AUDIO_DEVICES_PROPERTY;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.content.Context; import android.content.Context;
import android.content.res.Resources;
import android.os.SystemProperties; import android.os.SystemProperties;
import android.support.v7.preference.ListPreference; import android.support.v7.preference.ListPreference;
import android.support.v7.preference.PreferenceScreen; import android.support.v7.preference.PreferenceScreen;
@@ -39,6 +41,7 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.robolectric.RuntimeEnvironment; import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
@@ -47,37 +50,41 @@ import org.robolectric.annotation.Config;
sdk = TestConfig.SDK_VERSION, sdk = TestConfig.SDK_VERSION,
shadows = {SettingsShadowSystemProperties.class}) shadows = {SettingsShadowSystemProperties.class})
public class BluetoothMaxConnectedAudioDevicesPreferenceControllerTest { public class BluetoothMaxConnectedAudioDevicesPreferenceControllerTest {
private static final int TEST_MAX_CONNECTED_AUDIO_DEVICES = 3;
@Mock @Mock
private PreferenceScreen mPreferenceScreen; private PreferenceScreen mPreferenceScreen;
@Spy
private Context mSpyContext = RuntimeEnvironment.application;
@Spy
private Resources mSpyResources = RuntimeEnvironment.application.getResources();
private Context mContext;
private ListPreference mPreference; private ListPreference mPreference;
private BluetoothMaxConnectedAudioDevicesPreferenceController mController; private BluetoothMaxConnectedAudioDevicesPreferenceController mController;
/** private CharSequence[] mListValues;
* 0: 1 device maximum (Default) private CharSequence[] mListEntries;
* 1: 2 devices maximum
* 2: 3 devices maximum
* 3: 4 devices maximum
* 4: 5 devices maximum
*/
private String[] mListValues;
private String[] mListSummaries;
@Before @Before
public void setup() { public void setup() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application; doReturn(mSpyResources).when(mSpyContext).getResources();
mPreference = new ListPreference(mContext); // Get XML values without mock
mListValues = mContext.getResources().getStringArray( // Setup test list preference using XML values
R.array.bluetooth_max_connected_audio_devices_values); mPreference = new ListPreference(mSpyContext);
mListSummaries = mContext.getResources().getStringArray( mPreference.setEntries(R.array.bluetooth_max_connected_audio_devices);
R.array.bluetooth_max_connected_audio_devices); mPreference.setEntryValues(R.array.bluetooth_max_connected_audio_devices_values);
mController = new BluetoothMaxConnectedAudioDevicesPreferenceController(mContext); // Stub default max connected audio devices to a test controlled value
doReturn(TEST_MAX_CONNECTED_AUDIO_DEVICES).when(mSpyResources).getInteger(
com.android.internal.R.integer.config_bluetooth_max_connected_audio_devices);
// Init the actual controller
mController = new BluetoothMaxConnectedAudioDevicesPreferenceController(mSpyContext);
// Construct preference in the controller via a mocked preference screen object
when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn( when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
mPreference); mPreference);
mController.displayPreference(mPreferenceScreen); mController.displayPreference(mPreferenceScreen);
mListValues = mPreference.getEntryValues();
mListEntries = mPreference.getEntries();
} }
@After @After
@@ -85,40 +92,68 @@ public class BluetoothMaxConnectedAudioDevicesPreferenceControllerTest {
SettingsShadowSystemProperties.clear(); SettingsShadowSystemProperties.clear();
} }
@Test
public void verifyResourceSizeAndRange() {
// Verify normal list entries and default preference entries have the same size
assertThat(mListEntries.length).isEqualTo(mListValues.length);
// Verify that list entries are formatted correctly
final String defaultEntry = String.format(mListEntries[0].toString(),
TEST_MAX_CONNECTED_AUDIO_DEVICES);
assertThat(mListEntries[0]).isEqualTo(defaultEntry);
// Update the preference
mController.updateState(mPreference);
// Verify default preference value, entry and summary
assertThat(mPreference.getValue()).isEqualTo(mListValues[0]);
assertThat(mPreference.getEntry()).isEqualTo(mListEntries[0]);
assertThat(mPreference.getSummary()).isEqualTo(mListEntries[0]);
// Verify that default system property is empty
assertThat(SystemProperties.get(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY)).isEmpty();
// Verify default property integer value
assertThat(SystemProperties.getInt(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY,
TEST_MAX_CONNECTED_AUDIO_DEVICES)).isEqualTo(TEST_MAX_CONNECTED_AUDIO_DEVICES);
}
@Test @Test
public void onPreferenceChange_setNumberOfDevices() { public void onPreferenceChange_setNumberOfDevices() {
for (int numberOfDevices = 0; numberOfDevices < mListValues.length; numberOfDevices++) { for (final CharSequence newValue : mListValues) {
mController.onPreferenceChange(mPreference, mListValues[numberOfDevices]); // Change preference using a list value
mController.onPreferenceChange(mPreference, newValue);
final String currentValue = SystemProperties.get( // Verify that value is set on the preference
BLUETOOTH_MAX_CONNECTED_AUDIO_DEVICES_PROPERTY); assertThat(mPreference.getValue()).isEqualTo(newValue);
int index = mPreference.findIndexOfValue(newValue.toString());
assertThat(currentValue).isEqualTo(mListValues[numberOfDevices]); assertThat(mPreference.getEntry()).isEqualTo(mListEntries[index]);
assertThat(mPreference.getValue()).isEqualTo(mListValues[numberOfDevices]); // Verify that system property is set correctly after the change
assertThat(mPreference.getSummary()).isEqualTo(mListSummaries[numberOfDevices]); final String currentValue = SystemProperties.get(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY);
assertThat(currentValue).isEqualTo(mListValues[index]);
} }
} }
@Test @Test
public void updateState_NumberOfDevicesUpdated_shouldSetPreference() { public void updateState_NumberOfDevicesUpdated_shouldSetPreference() {
for (int numberOfDevices = 0; numberOfDevices < mListValues.length; numberOfDevices++) { for (int i = 0; i < mListValues.length; ++i) {
SystemProperties.set(BLUETOOTH_MAX_CONNECTED_AUDIO_DEVICES_PROPERTY, final String propertyValue = mListValues[i].toString();
mListValues[numberOfDevices]); SystemProperties.set(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY, propertyValue);
// Verify that value is set on the preference
mController.updateState(mPreference); mController.updateState(mPreference);
assertThat(mPreference.getValue()).isEqualTo(mListValues[i]);
assertThat(mPreference.getValue()).isEqualTo(mListValues[numberOfDevices]); assertThat(mPreference.getEntry()).isEqualTo(mListEntries[i]);
assertThat(mPreference.getSummary()).isEqualTo(mListSummaries[numberOfDevices]); assertThat(mPreference.getSummary()).isEqualTo(mListEntries[i]);
// Verify that property value remain unchanged
assertThat(SystemProperties.get(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY))
.isEqualTo(propertyValue);
} }
} }
@Test @Test
public void updateState_noValueSet_shouldSetDefaultTo1device() { public void updateState_noValueSet_shouldSetDefaultTo1device() {
SystemProperties.set(BLUETOOTH_MAX_CONNECTED_AUDIO_DEVICES_PROPERTY, "garbage"); SystemProperties.set(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY, "garbage");
mController.updateState(mPreference); mController.updateState(mPreference);
// Verify that preference is reset back to default and property is reset to default
assertThat(mPreference.getValue()).isEqualTo(mListValues[0]); assertThat(mPreference.getValue()).isEqualTo(mListValues[0]);
assertThat(mPreference.getSummary()).isEqualTo(mListSummaries[0]); assertThat(mPreference.getEntry()).isEqualTo(mListEntries[0]);
assertThat(mPreference.getSummary()).isEqualTo(mListEntries[0]);
assertThat(SystemProperties.get(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY)).isEmpty();
} }
@Test @Test
@@ -126,26 +161,30 @@ public class BluetoothMaxConnectedAudioDevicesPreferenceControllerTest {
mController.onDeveloperOptionsSwitchDisabled(); mController.onDeveloperOptionsSwitchDisabled();
assertThat(mPreference.isEnabled()).isFalse(); assertThat(mPreference.isEnabled()).isFalse();
// Verify that preference is reset back to default and property is reset to default
assertThat(mPreference.getValue()).isEqualTo(mListValues[0]); assertThat(mPreference.getValue()).isEqualTo(mListValues[0]);
assertThat(mPreference.getSummary()).isEqualTo(mListSummaries[0]); assertThat(mPreference.getEntry()).isEqualTo(mListEntries[0]);
final String currentValue = SystemProperties.get( assertThat(mPreference.getSummary()).isEqualTo(mListEntries[0]);
BLUETOOTH_MAX_CONNECTED_AUDIO_DEVICES_PROPERTY); assertThat(SystemProperties.get(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY)).isEmpty();
assertThat(currentValue).isEqualTo(mListValues[0]);
} }
@Test @Test
public void onDeveloperOptionsSwitchEnabled_shouldEnablePreference() { public void onDeveloperOptionsSwitchEnabled_shouldEnablePreference() {
for (int numberOfDevices = 0; numberOfDevices < mListValues.length; numberOfDevices++) { for (int i = 0; i < mListValues.length; ++i) {
final String initialValue = mListValues[i].toString();
mController.onDeveloperOptionsSwitchDisabled(); mController.onDeveloperOptionsSwitchDisabled();
assertThat(mPreference.isEnabled()).isFalse(); assertThat(mPreference.isEnabled()).isFalse();
SystemProperties.set(BLUETOOTH_MAX_CONNECTED_AUDIO_DEVICES_PROPERTY, SystemProperties.set(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY, initialValue);
mListValues[numberOfDevices]);
mController.onDeveloperOptionsSwitchEnabled(); mController.onDeveloperOptionsSwitchEnabled();
assertThat(mPreference.isEnabled()).isTrue(); assertThat(mPreference.isEnabled()).isTrue();
assertThat(mPreference.getValue()).isEqualTo(mListValues[numberOfDevices]); assertThat(mPreference.getValue()).isEqualTo(mListValues[i]);
assertThat(mPreference.getSummary()).isEqualTo(mListSummaries[numberOfDevices]); assertThat(mPreference.getEntry()).isEqualTo(mListEntries[i]);
assertThat(mPreference.getSummary()).isEqualTo(mListEntries[i]);
// Verify that property value remain unchanged
assertThat(SystemProperties.get(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY))
.isEqualTo(initialValue);
} }
} }
} }

View File

@@ -0,0 +1,64 @@
/*
* Copyright (C) 2018 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.development;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import com.android.settings.R;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.Arrays;
@RunWith(AndroidJUnit4.class)
@SmallTest
public class BluetoothMaxConnectedAudioDevicesPreferenceControllerInstrumentationTest {
private Context mTargetContext;
private String[] mListValues;
private String[] mListEntries;
private String mDefaultMaxConnectedAudioDevices;
@Before
public void setUp() throws Exception {
mTargetContext = InstrumentationRegistry.getTargetContext();
// Get XML values without mock
mListValues = mTargetContext.getResources()
.getStringArray(R.array.bluetooth_max_connected_audio_devices_values);
mListEntries = mTargetContext.getResources()
.getStringArray(R.array.bluetooth_max_connected_audio_devices);
mDefaultMaxConnectedAudioDevices = String.valueOf(mTargetContext.getResources()
.getInteger(
com.android.internal.R.integer
.config_bluetooth_max_connected_audio_devices));
}
@Test
public void verifyResource() {
// Verify normal list entries and default preference entries have the same size
Assert.assertEquals(mListEntries.length, mListValues.length);
Assert.assertThat(Arrays.asList(mListValues),
CoreMatchers.hasItem(mDefaultMaxConnectedAudioDevices));
}
}