1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.development; 18 19 import static com.android.settings.development.BluetoothMaxConnectedAudioDevicesPreferenceController.MAX_CONNECTED_AUDIO_DEVICES_PROPERTY; 20 import static com.google.common.truth.Truth.assertThat; 21 import static org.mockito.Mockito.doReturn; 22 import static org.mockito.Mockito.when; 23 24 import android.content.Context; 25 import android.content.res.Resources; 26 import android.os.SystemProperties; 27 import android.support.v7.preference.ListPreference; 28 import android.support.v7.preference.PreferenceScreen; 29 30 import com.android.settings.R; 31 import com.android.settings.testutils.SettingsRobolectricTestRunner; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.mockito.Mock; 37 import org.mockito.MockitoAnnotations; 38 import org.mockito.Spy; 39 import org.robolectric.RuntimeEnvironment; 40 41 @RunWith(SettingsRobolectricTestRunner.class) 42 public class BluetoothMaxConnectedAudioDevicesPreferenceControllerTest { 43 44 private static final int TEST_MAX_CONNECTED_AUDIO_DEVICES = 3; 45 46 @Mock 47 private PreferenceScreen mPreferenceScreen; 48 @Spy 49 private Context mSpyContext = RuntimeEnvironment.application; 50 @Spy 51 private Resources mSpyResources = RuntimeEnvironment.application.getResources(); 52 53 private ListPreference mPreference; 54 private BluetoothMaxConnectedAudioDevicesPreferenceController mController; 55 56 private CharSequence[] mListValues; 57 private CharSequence[] mListEntries; 58 59 @Before setup()60 public void setup() { 61 MockitoAnnotations.initMocks(this); 62 doReturn(mSpyResources).when(mSpyContext).getResources(); 63 // Get XML values without mock 64 // Setup test list preference using XML values 65 mPreference = new ListPreference(mSpyContext); 66 mPreference.setEntries(R.array.bluetooth_max_connected_audio_devices); 67 mPreference.setEntryValues(R.array.bluetooth_max_connected_audio_devices_values); 68 // Stub default max connected audio devices to a test controlled value 69 doReturn(TEST_MAX_CONNECTED_AUDIO_DEVICES).when(mSpyResources).getInteger( 70 com.android.internal.R.integer.config_bluetooth_max_connected_audio_devices); 71 // Init the actual controller 72 mController = new BluetoothMaxConnectedAudioDevicesPreferenceController(mSpyContext); 73 // Construct preference in the controller via a mocked preference screen object 74 when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn( 75 mPreference); 76 mController.displayPreference(mPreferenceScreen); 77 mListValues = mPreference.getEntryValues(); 78 mListEntries = mPreference.getEntries(); 79 } 80 81 @Test verifyResourceSizeAndRange()82 public void verifyResourceSizeAndRange() { 83 // Verify normal list entries and default preference entries have the same size 84 assertThat(mListEntries.length).isEqualTo(mListValues.length); 85 // Verify that list entries are formatted correctly 86 final String defaultEntry = String.format(mListEntries[0].toString(), 87 TEST_MAX_CONNECTED_AUDIO_DEVICES); 88 assertThat(mListEntries[0]).isEqualTo(defaultEntry); 89 // Update the preference 90 mController.updateState(mPreference); 91 // Verify default preference value, entry and summary 92 assertThat(mPreference.getValue()).isEqualTo(mListValues[0]); 93 assertThat(mPreference.getEntry()).isEqualTo(mListEntries[0]); 94 assertThat(mPreference.getSummary()).isEqualTo(mListEntries[0]); 95 // Verify that default system property is empty 96 assertThat(SystemProperties.get(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY)).isEmpty(); 97 // Verify default property integer value 98 assertThat(SystemProperties.getInt(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY, 99 TEST_MAX_CONNECTED_AUDIO_DEVICES)).isEqualTo(TEST_MAX_CONNECTED_AUDIO_DEVICES); 100 } 101 102 @Test onPreferenceChange_setNumberOfDevices()103 public void onPreferenceChange_setNumberOfDevices() { 104 for (final CharSequence newValue : mListValues) { 105 // Change preference using a list value 106 mController.onPreferenceChange(mPreference, newValue); 107 // Verify that value is set on the preference 108 assertThat(mPreference.getValue()).isEqualTo(newValue); 109 int index = mPreference.findIndexOfValue(newValue.toString()); 110 assertThat(mPreference.getEntry()).isEqualTo(mListEntries[index]); 111 // Verify that system property is set correctly after the change 112 final String currentValue = SystemProperties.get(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY); 113 assertThat(currentValue).isEqualTo(mListValues[index]); 114 } 115 } 116 117 @Test updateState_NumberOfDevicesUpdated_shouldSetPreference()118 public void updateState_NumberOfDevicesUpdated_shouldSetPreference() { 119 for (int i = 0; i < mListValues.length; ++i) { 120 final String propertyValue = mListValues[i].toString(); 121 SystemProperties.set(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY, propertyValue); 122 // Verify that value is set on the preference 123 mController.updateState(mPreference); 124 assertThat(mPreference.getValue()).isEqualTo(mListValues[i]); 125 assertThat(mPreference.getEntry()).isEqualTo(mListEntries[i]); 126 assertThat(mPreference.getSummary()).isEqualTo(mListEntries[i]); 127 // Verify that property value remain unchanged 128 assertThat(SystemProperties.get(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY)) 129 .isEqualTo(propertyValue); 130 } 131 } 132 133 @Test updateState_noValueSet_shouldSetDefaultTo1device()134 public void updateState_noValueSet_shouldSetDefaultTo1device() { 135 SystemProperties.set(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY, "garbage"); 136 mController.updateState(mPreference); 137 138 // Verify that preference is reset back to default and property is reset to default 139 assertThat(mPreference.getValue()).isEqualTo(mListValues[0]); 140 assertThat(mPreference.getEntry()).isEqualTo(mListEntries[0]); 141 assertThat(mPreference.getSummary()).isEqualTo(mListEntries[0]); 142 assertThat(SystemProperties.get(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY)).isEmpty(); 143 } 144 145 @Test onDeveloperOptionsSwitchDisabled_shouldDisablePreference()146 public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() { 147 mController.onDeveloperOptionsSwitchDisabled(); 148 149 assertThat(mPreference.isEnabled()).isFalse(); 150 // Verify that preference is reset back to default and property is reset to default 151 assertThat(mPreference.getValue()).isEqualTo(mListValues[0]); 152 assertThat(mPreference.getEntry()).isEqualTo(mListEntries[0]); 153 assertThat(mPreference.getSummary()).isEqualTo(mListEntries[0]); 154 assertThat(SystemProperties.get(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY)).isEmpty(); 155 } 156 157 @Test onDeveloperOptionsSwitchEnabled_shouldEnablePreference()158 public void onDeveloperOptionsSwitchEnabled_shouldEnablePreference() { 159 for (int i = 0; i < mListValues.length; ++i) { 160 final String initialValue = mListValues[i].toString(); 161 mController.onDeveloperOptionsSwitchDisabled(); 162 assertThat(mPreference.isEnabled()).isFalse(); 163 164 SystemProperties.set(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY, initialValue); 165 mController.onDeveloperOptionsSwitchEnabled(); 166 167 assertThat(mPreference.isEnabled()).isTrue(); 168 assertThat(mPreference.getValue()).isEqualTo(mListValues[i]); 169 assertThat(mPreference.getEntry()).isEqualTo(mListEntries[i]); 170 assertThat(mPreference.getSummary()).isEqualTo(mListEntries[i]); 171 // Verify that property value remain unchanged 172 assertThat(SystemProperties.get(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY)) 173 .isEqualTo(initialValue); 174 } 175 } 176 } 177