2 * Copyright (C) 2018 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.sound;
20 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
21 import static com.android.settings.core.BasePreferenceController.DISABLED_UNSUPPORTED;
23 import static com.google.common.truth.Truth.assertThat;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.spy;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
31 import android.bluetooth.BluetoothAdapter;
32 import android.bluetooth.BluetoothDevice;
33 import android.bluetooth.BluetoothManager;
34 import android.content.BroadcastReceiver;
35 import android.content.Context;
36 import android.content.IntentFilter;
37 import android.support.v7.preference.ListPreference;
38 import android.support.v7.preference.PreferenceManager;
39 import android.support.v7.preference.PreferenceScreen;
40 import android.util.FeatureFlagUtils;
42 import com.android.settings.R;
43 import com.android.settings.core.FeatureFlags;
44 import com.android.settings.testutils.SettingsRobolectricTestRunner;
45 import com.android.settings.testutils.shadow.ShadowAudioManager;
46 import com.android.settings.testutils.shadow.ShadowBluetoothUtils;
47 import com.android.settings.testutils.shadow.ShadowMediaRouter;
48 import com.android.settingslib.bluetooth.A2dpProfile;
49 import com.android.settingslib.bluetooth.BluetoothCallback;
50 import com.android.settingslib.bluetooth.BluetoothEventManager;
51 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
52 import com.android.settingslib.bluetooth.LocalBluetoothManager;
53 import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
55 import org.junit.After;
56 import org.junit.Before;
57 import org.junit.Test;
58 import org.junit.runner.RunWith;
59 import org.mockito.Mock;
60 import org.mockito.MockitoAnnotations;
61 import org.robolectric.RuntimeEnvironment;
62 import org.robolectric.Shadows;
63 import org.robolectric.annotation.Config;
64 import org.robolectric.shadows.ShadowBluetoothDevice;
66 import java.util.ArrayList;
67 import java.util.List;
69 @RunWith(SettingsRobolectricTestRunner.class)
71 ShadowAudioManager.class,
72 ShadowMediaRouter.class,
73 ShadowBluetoothUtils.class,
74 ShadowBluetoothDevice.class}
76 public class AudioOutputSwitchPreferenceControllerTest {
77 private static final String TEST_KEY = "Test_Key";
78 private static final String TEST_DEVICE_NAME_1 = "Test_A2DP_BT_Device_NAME_1";
79 private static final String TEST_DEVICE_NAME_2 = "Test_A2DP_BT_Device_NAME_2";
80 private static final String TEST_DEVICE_ADDRESS_1 = "00:07:80:78:A4:69";
81 private static final String TEST_DEVICE_ADDRESS_2 = "00:00:00:00:00:00";
84 private LocalBluetoothManager mLocalManager;
86 private BluetoothEventManager mBluetoothEventManager;
88 private CachedBluetoothDevice mCachedBluetoothDevice;
90 private LocalBluetoothProfileManager mLocalBluetoothProfileManager;
92 private A2dpProfile mA2dpProfile;
94 private Context mContext;
95 private PreferenceScreen mScreen;
96 private ListPreference mPreference;
97 private ShadowAudioManager mShadowAudioManager;
98 private ShadowMediaRouter mShadowMediaRouter;
99 private BluetoothManager mBluetoothManager;
100 private BluetoothAdapter mBluetoothAdapter;
101 private BluetoothDevice mBluetoothDevice;
102 private ShadowBluetoothDevice mShadowBluetoothDevice;
103 private LocalBluetoothManager mLocalBluetoothManager;
104 private AudioSwitchPreferenceController mController;
105 private List<BluetoothDevice> mConnectedDevices;
108 public void setUp() {
109 MockitoAnnotations.initMocks(this);
110 mContext = spy(RuntimeEnvironment.application);
112 mShadowAudioManager = ShadowAudioManager.getShadow();
113 mShadowMediaRouter = ShadowMediaRouter.getShadow();
115 ShadowBluetoothUtils.sLocalBluetoothManager = mLocalManager;
116 mLocalBluetoothManager = ShadowBluetoothUtils.getLocalBtManager(mContext);
118 when(mLocalBluetoothManager.getEventManager()).thenReturn(mBluetoothEventManager);
119 when(mLocalBluetoothManager.getProfileManager()).thenReturn(mLocalBluetoothProfileManager);
120 when(mLocalBluetoothProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile);
122 mBluetoothManager = new BluetoothManager(mContext);
123 mBluetoothAdapter = mBluetoothManager.getAdapter();
125 mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS_1);
126 mShadowBluetoothDevice = Shadows.shadowOf(mBluetoothDevice);
127 mShadowBluetoothDevice.setName(TEST_DEVICE_NAME_1);
128 when(mCachedBluetoothDevice.getDevice()).thenReturn(mBluetoothDevice);
130 mController = new AudioSwitchPreferenceControllerTestable(mContext, TEST_KEY);
131 mScreen = spy(new PreferenceScreen(mContext, null));
132 mPreference = new ListPreference(mContext);
133 mConnectedDevices = new ArrayList<>(1);
134 mConnectedDevices.add(mBluetoothDevice);
136 when(mScreen.getPreferenceManager()).thenReturn(mock(PreferenceManager.class));
137 when(mScreen.getContext()).thenReturn(mContext);
138 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
139 mScreen.addPreference(mPreference);
140 mController.displayPreference(mScreen);
144 public void tearDown() {
145 mShadowAudioManager.reset();
146 mShadowMediaRouter.reset();
147 ShadowBluetoothUtils.reset();
151 public void getAvailabilityStatus_byDefault_isAvailable() {
152 assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
156 public void getAvailabilityStatus_whenNotVisible_isDisable() {
157 FeatureFlagUtils.setEnabled(mContext, FeatureFlags.AUDIO_SWITCHER_SETTINGS, false);
158 assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_UNSUPPORTED);
162 public void onStart_shouldRegisterCallbackAndRegisterReceiver() {
163 mController.onStart();
165 verify(mLocalBluetoothManager.getEventManager()).registerCallback(
166 any(BluetoothCallback.class));
167 verify(mContext).registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class));
171 public void onStop_shouldUnregisterCallbackAndUnregisterReceiver() {
172 mController.onStart();
173 mController.onStop();
175 verify(mLocalBluetoothManager.getEventManager()).unregisterCallback(
176 any(BluetoothCallback.class));
177 verify(mContext).unregisterReceiver(any(BroadcastReceiver.class));
181 public void onPreferenceChange_toThisDevice_shouldSetDefaultSummary() {
182 mController.mConnectedDevices = mConnectedDevices;
184 mController.onPreferenceChange(mPreference,
185 mContext.getText(R.string.media_output_default_summary));
187 assertThat(mPreference.getSummary()).isEqualTo(
188 mContext.getText(R.string.media_output_default_summary));
192 * One Bluetooth devices are available, and select the device.
193 * Preference summary should be device name.
196 public void onPreferenceChange_toBtDevice_shouldSetBtDeviceName() {
197 mController.mConnectedDevices = mConnectedDevices;
199 mController.onPreferenceChange(mPreference, TEST_DEVICE_ADDRESS_1);
201 assertThat(mPreference.getSummary()).isEqualTo(mBluetoothDevice.getName());
205 * More than one Bluetooth devices are available, and select second device.
206 * Preference summary should be second device name.
209 public void onPreferenceChange_toBtDevices_shouldSetSecondBtDeviceName() {
210 ShadowBluetoothDevice shadowBluetoothDevice;
211 BluetoothDevice secondBluetoothDevice;
212 secondBluetoothDevice = mBluetoothAdapter.getRemoteDevice(TEST_DEVICE_ADDRESS_2);
213 shadowBluetoothDevice = Shadows.shadowOf(secondBluetoothDevice);
214 shadowBluetoothDevice.setName(TEST_DEVICE_NAME_2);
215 List<BluetoothDevice> connectedDevices = new ArrayList<>(2);
216 connectedDevices.add(mBluetoothDevice);
217 connectedDevices.add(secondBluetoothDevice);
218 mController.mConnectedDevices = connectedDevices;
220 mController.onPreferenceChange(mPreference, TEST_DEVICE_ADDRESS_2);
222 assertThat(mPreference.getSummary()).isEqualTo(secondBluetoothDevice.getName());
226 * mConnectedDevices is Null.
227 * onPreferenceChange should return false.
230 public void onPreferenceChange_connectedDeviceIsNull_shouldReturnFalse() {
231 mController.mConnectedDevices = null;
233 assertThat(mController.onPreferenceChange(mPreference, TEST_DEVICE_ADDRESS_1)).isFalse();
236 private class AudioSwitchPreferenceControllerTestable extends
237 AudioSwitchPreferenceController {
238 AudioSwitchPreferenceControllerTestable(Context context, String key) {
243 public void setActiveBluetoothDevice(BluetoothDevice device) {
247 public String getPreferenceKey() {