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

@@ -17,13 +17,15 @@
package com.android.settings.development;
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 org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.res.Resources;
import android.os.SystemProperties;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.PreferenceScreen;
@@ -39,6 +41,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@@ -47,37 +50,41 @@ import org.robolectric.annotation.Config;
sdk = TestConfig.SDK_VERSION,
shadows = {SettingsShadowSystemProperties.class})
public class BluetoothMaxConnectedAudioDevicesPreferenceControllerTest {
private static final int TEST_MAX_CONNECTED_AUDIO_DEVICES = 3;
@Mock
private PreferenceScreen mPreferenceScreen;
@Spy
private Context mSpyContext = RuntimeEnvironment.application;
@Spy
private Resources mSpyResources = RuntimeEnvironment.application.getResources();
private Context mContext;
private ListPreference mPreference;
private BluetoothMaxConnectedAudioDevicesPreferenceController mController;
/**
* 0: 1 device maximum (Default)
* 1: 2 devices maximum
* 2: 3 devices maximum
* 3: 4 devices maximum
* 4: 5 devices maximum
*/
private String[] mListValues;
private String[] mListSummaries;
private CharSequence[] mListValues;
private CharSequence[] mListEntries;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mPreference = new ListPreference(mContext);
mListValues = mContext.getResources().getStringArray(
R.array.bluetooth_max_connected_audio_devices_values);
mListSummaries = mContext.getResources().getStringArray(
R.array.bluetooth_max_connected_audio_devices);
mController = new BluetoothMaxConnectedAudioDevicesPreferenceController(mContext);
doReturn(mSpyResources).when(mSpyContext).getResources();
// Get XML values without mock
// Setup test list preference using XML values
mPreference = new ListPreference(mSpyContext);
mPreference.setEntries(R.array.bluetooth_max_connected_audio_devices);
mPreference.setEntryValues(R.array.bluetooth_max_connected_audio_devices_values);
// 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(
mPreference);
mController.displayPreference(mPreferenceScreen);
mListValues = mPreference.getEntryValues();
mListEntries = mPreference.getEntries();
}
@After
@@ -85,40 +92,68 @@ public class BluetoothMaxConnectedAudioDevicesPreferenceControllerTest {
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
public void onPreferenceChange_setNumberOfDevices() {
for (int numberOfDevices = 0; numberOfDevices < mListValues.length; numberOfDevices++) {
mController.onPreferenceChange(mPreference, mListValues[numberOfDevices]);
final String currentValue = SystemProperties.get(
BLUETOOTH_MAX_CONNECTED_AUDIO_DEVICES_PROPERTY);
assertThat(currentValue).isEqualTo(mListValues[numberOfDevices]);
assertThat(mPreference.getValue()).isEqualTo(mListValues[numberOfDevices]);
assertThat(mPreference.getSummary()).isEqualTo(mListSummaries[numberOfDevices]);
for (final CharSequence newValue : mListValues) {
// Change preference using a list value
mController.onPreferenceChange(mPreference, newValue);
// Verify that value is set on the preference
assertThat(mPreference.getValue()).isEqualTo(newValue);
int index = mPreference.findIndexOfValue(newValue.toString());
assertThat(mPreference.getEntry()).isEqualTo(mListEntries[index]);
// Verify that system property is set correctly after the change
final String currentValue = SystemProperties.get(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY);
assertThat(currentValue).isEqualTo(mListValues[index]);
}
}
@Test
public void updateState_NumberOfDevicesUpdated_shouldSetPreference() {
for (int numberOfDevices = 0; numberOfDevices < mListValues.length; numberOfDevices++) {
SystemProperties.set(BLUETOOTH_MAX_CONNECTED_AUDIO_DEVICES_PROPERTY,
mListValues[numberOfDevices]);
for (int i = 0; i < mListValues.length; ++i) {
final String propertyValue = mListValues[i].toString();
SystemProperties.set(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY, propertyValue);
// Verify that value is set on the preference
mController.updateState(mPreference);
assertThat(mPreference.getValue()).isEqualTo(mListValues[numberOfDevices]);
assertThat(mPreference.getSummary()).isEqualTo(mListSummaries[numberOfDevices]);
assertThat(mPreference.getValue()).isEqualTo(mListValues[i]);
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(propertyValue);
}
}
@Test
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);
// Verify that preference is reset back to default and property is reset to default
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
@@ -126,26 +161,30 @@ public class BluetoothMaxConnectedAudioDevicesPreferenceControllerTest {
mController.onDeveloperOptionsSwitchDisabled();
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.getSummary()).isEqualTo(mListSummaries[0]);
final String currentValue = SystemProperties.get(
BLUETOOTH_MAX_CONNECTED_AUDIO_DEVICES_PROPERTY);
assertThat(currentValue).isEqualTo(mListValues[0]);
assertThat(mPreference.getEntry()).isEqualTo(mListEntries[0]);
assertThat(mPreference.getSummary()).isEqualTo(mListEntries[0]);
assertThat(SystemProperties.get(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY)).isEmpty();
}
@Test
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();
assertThat(mPreference.isEnabled()).isFalse();
SystemProperties.set(BLUETOOTH_MAX_CONNECTED_AUDIO_DEVICES_PROPERTY,
mListValues[numberOfDevices]);
SystemProperties.set(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY, initialValue);
mController.onDeveloperOptionsSwitchEnabled();
assertThat(mPreference.isEnabled()).isTrue();
assertThat(mPreference.getValue()).isEqualTo(mListValues[numberOfDevices]);
assertThat(mPreference.getSummary()).isEqualTo(mListSummaries[numberOfDevices]);
assertThat(mPreference.getValue()).isEqualTo(mListValues[i]);
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));
}
}