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