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 20 .BluetoothMaxConnectedAudioDevicesPreferenceController.MAX_CONNECTED_AUDIO_DEVICES_PROPERTY; 21 22 import static com.google.common.truth.Truth.assertThat; 23 24 import static org.mockito.Mockito.doReturn; 25 import static org.mockito.Mockito.when; 26 27 import android.content.Context; 28 import android.content.res.Resources; 29 import android.os.SystemProperties; 30 31 import androidx.preference.ListPreference; 32 import androidx.preference.PreferenceScreen; 33 34 import com.android.settings.R; 35 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.mockito.Mock; 40 import org.mockito.MockitoAnnotations; 41 import org.mockito.Spy; 42 import org.robolectric.RobolectricTestRunner; 43 import org.robolectric.RuntimeEnvironment; 44 45 @RunWith(RobolectricTestRunner.class) 46 public class BluetoothMaxConnectedAudioDevicesPreferenceControllerTest { 47 48 private static final int TEST_MAX_CONNECTED_AUDIO_DEVICES = 3; 49 50 @Mock 51 private PreferenceScreen mPreferenceScreen; 52 @Spy 53 private Context mSpyContext = RuntimeEnvironment.application; 54 @Spy 55 private Resources mSpyResources = RuntimeEnvironment.application.getResources(); 56 57 private ListPreference mPreference; 58 private BluetoothMaxConnectedAudioDevicesPreferenceController mController; 59 60 private CharSequence[] mListValues; 61 private CharSequence[] mListEntries; 62 63 @Before setup()64 public void setup() { 65 MockitoAnnotations.initMocks(this); 66 doReturn(mSpyResources).when(mSpyContext).getResources(); 67 // Get XML values without mock 68 // Setup test list preference using XML values 69 mPreference = new ListPreference(mSpyContext); 70 mPreference.setEntries(R.array.bluetooth_max_connected_audio_devices); 71 mPreference.setEntryValues(R.array.bluetooth_max_connected_audio_devices_values); 72 // Stub default max connected audio devices to a test controlled value 73 doReturn(TEST_MAX_CONNECTED_AUDIO_DEVICES).when(mSpyResources).getInteger( 74 com.android.internal.R.integer.config_bluetooth_max_connected_audio_devices); 75 // Init the actual controller 76 mController = new BluetoothMaxConnectedAudioDevicesPreferenceController(mSpyContext); 77 // Construct preference in the controller via a mocked preference screen object 78 when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn( 79 mPreference); 80 mController.displayPreference(mPreferenceScreen); 81 mListValues = mPreference.getEntryValues(); 82 mListEntries = mPreference.getEntries(); 83 } 84 85 @Test verifyResourceSizeAndRange()86 public void verifyResourceSizeAndRange() { 87 // Verify normal list entries and default preference entries have the same size 88 assertThat(mListEntries.length).isEqualTo(mListValues.length); 89 // Verify that list entries are formatted correctly 90 final String defaultEntry = String.format(mListEntries[0].toString(), 91 TEST_MAX_CONNECTED_AUDIO_DEVICES); 92 assertThat(mListEntries[0]).isEqualTo(defaultEntry); 93 // Update the preference 94 mController.updateState(mPreference); 95 // Verify default preference value, entry and summary 96 assertThat(mPreference.getValue()).isEqualTo(mListValues[0]); 97 assertThat(mPreference.getEntry()).isEqualTo(mListEntries[0]); 98 assertThat(mPreference.getSummary()).isEqualTo(mListEntries[0]); 99 // Verify that default system property is empty 100 assertThat(SystemProperties.get(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY)).isEmpty(); 101 // Verify default property integer value 102 assertThat(SystemProperties.getInt(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY, 103 TEST_MAX_CONNECTED_AUDIO_DEVICES)).isEqualTo(TEST_MAX_CONNECTED_AUDIO_DEVICES); 104 } 105 106 @Test onPreferenceChange_setNumberOfDevices()107 public void onPreferenceChange_setNumberOfDevices() { 108 for (final CharSequence newValue : mListValues) { 109 // Change preference using a list value 110 mController.onPreferenceChange(mPreference, newValue); 111 // Verify that value is set on the preference 112 assertThat(mPreference.getValue()).isEqualTo(newValue); 113 int index = mPreference.findIndexOfValue(newValue.toString()); 114 assertThat(mPreference.getEntry()).isEqualTo(mListEntries[index]); 115 // Verify that system property is set correctly after the change 116 final String currentValue = SystemProperties.get(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY); 117 assertThat(currentValue).isEqualTo(mListValues[index]); 118 } 119 } 120 121 @Test updateState_NumberOfDevicesUpdated_shouldSetPreference()122 public void updateState_NumberOfDevicesUpdated_shouldSetPreference() { 123 for (int i = 0; i < mListValues.length; ++i) { 124 final String propertyValue = mListValues[i].toString(); 125 SystemProperties.set(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY, propertyValue); 126 // Verify that value is set on the preference 127 mController.updateState(mPreference); 128 assertThat(mPreference.getValue()).isEqualTo(mListValues[i]); 129 assertThat(mPreference.getEntry()).isEqualTo(mListEntries[i]); 130 assertThat(mPreference.getSummary()).isEqualTo(mListEntries[i]); 131 // Verify that property value remain unchanged 132 assertThat(SystemProperties.get(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY)) 133 .isEqualTo(propertyValue); 134 } 135 } 136 137 @Test updateState_noValueSet_shouldSetDefaultTo1device()138 public void updateState_noValueSet_shouldSetDefaultTo1device() { 139 SystemProperties.set(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY, "garbage"); 140 mController.updateState(mPreference); 141 142 // Verify that preference is reset back to default and property is reset to default 143 assertThat(mPreference.getValue()).isEqualTo(mListValues[0]); 144 assertThat(mPreference.getEntry()).isEqualTo(mListEntries[0]); 145 assertThat(mPreference.getSummary()).isEqualTo(mListEntries[0]); 146 assertThat(SystemProperties.get(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY)).isEmpty(); 147 } 148 149 @Test onDeveloperOptionsSwitchDisabled_shouldDisablePreference()150 public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() { 151 mController.onDeveloperOptionsSwitchDisabled(); 152 153 assertThat(mPreference.isEnabled()).isFalse(); 154 // Verify that preference is reset back to default and property is reset to default 155 assertThat(mPreference.getValue()).isEqualTo(mListValues[0]); 156 assertThat(mPreference.getEntry()).isEqualTo(mListEntries[0]); 157 assertThat(mPreference.getSummary()).isEqualTo(mListEntries[0]); 158 assertThat(SystemProperties.get(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY)).isEmpty(); 159 } 160 161 @Test onDeveloperOptionsSwitchEnabled_shouldEnablePreference()162 public void onDeveloperOptionsSwitchEnabled_shouldEnablePreference() { 163 for (int i = 0; i < mListValues.length; ++i) { 164 final String initialValue = mListValues[i].toString(); 165 mController.onDeveloperOptionsSwitchDisabled(); 166 assertThat(mPreference.isEnabled()).isFalse(); 167 168 SystemProperties.set(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY, initialValue); 169 mController.onDeveloperOptionsSwitchEnabled(); 170 171 assertThat(mPreference.isEnabled()).isTrue(); 172 assertThat(mPreference.getValue()).isEqualTo(mListValues[i]); 173 assertThat(mPreference.getEntry()).isEqualTo(mListEntries[i]); 174 assertThat(mPreference.getSummary()).isEqualTo(mListEntries[i]); 175 // Verify that property value remain unchanged 176 assertThat(SystemProperties.get(MAX_CONNECTED_AUDIO_DEVICES_PROPERTY)) 177 .isEqualTo(initialValue); 178 } 179 } 180 } 181