blob: 9afdd43ce73c17a2391f8874ad0e40e90e257115 [file] [log] [blame]
Antony Sargent374d2592017-04-20 11:23:34 -07001/*
2 * Copyright (C) 2017 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 */
16package com.android.settingslib.bluetooth;
17
James Lemieux5c50dc12018-02-12 01:30:32 -080018import static com.google.common.truth.Truth.assertThat;
Fan Zhangb5bf7392018-07-25 13:36:33 -070019
James Lemieuxec3ad9e2018-11-28 17:49:14 -080020import static org.mockito.ArgumentMatchers.any;
21import static org.mockito.ArgumentMatchers.eq;
James Lemieux5c50dc12018-02-12 01:30:32 -080022import static org.mockito.Mockito.mock;
23import static org.mockito.Mockito.when;
24
Antony Sargent374d2592017-04-20 11:23:34 -070025import android.bluetooth.BluetoothA2dp;
timhypeng45a477e2018-07-31 15:32:59 +080026import android.bluetooth.BluetoothAdapter;
Antony Sargent374d2592017-04-20 11:23:34 -070027import android.bluetooth.BluetoothCodecConfig;
28import android.bluetooth.BluetoothCodecStatus;
29import android.bluetooth.BluetoothDevice;
30import android.bluetooth.BluetoothProfile;
31import android.content.Context;
Justin Klaassen9acc02e2017-09-04 17:00:16 -070032import android.content.res.Resources;
Antony Sargent374d2592017-04-20 11:23:34 -070033
34import com.android.settingslib.R;
timhypeng45a477e2018-07-31 15:32:59 +080035import com.android.settingslib.testutils.shadow.ShadowBluetoothAdapter;
Antony Sargent374d2592017-04-20 11:23:34 -070036
37import org.junit.Before;
38import org.junit.Test;
39import org.junit.runner.RunWith;
Antony Sargent374d2592017-04-20 11:23:34 -070040import org.mockito.Mock;
41import org.mockito.MockitoAnnotations;
James Lemieuxec3ad9e2018-11-28 17:49:14 -080042import org.robolectric.RobolectricTestRunner;
timhypeng45a477e2018-07-31 15:32:59 +080043import org.robolectric.annotation.Config;
44import org.robolectric.shadow.api.Shadow;
Antony Sargent374d2592017-04-20 11:23:34 -070045
James Lemieuxec3ad9e2018-11-28 17:49:14 -080046@RunWith(RobolectricTestRunner.class)
timhypeng45a477e2018-07-31 15:32:59 +080047@Config(shadows = {ShadowBluetoothAdapter.class})
Antony Sargent374d2592017-04-20 11:23:34 -070048public class A2dpProfileTest {
49
Fan Zhangb5bf7392018-07-25 13:36:33 -070050 @Mock
James Lemieuxec3ad9e2018-11-28 17:49:14 -080051 private Context mContext;
Fan Zhangb5bf7392018-07-25 13:36:33 -070052 @Mock
James Lemieuxec3ad9e2018-11-28 17:49:14 -080053 private CachedBluetoothDeviceManager mDeviceManager;
Fan Zhangb5bf7392018-07-25 13:36:33 -070054 @Mock
James Lemieuxec3ad9e2018-11-28 17:49:14 -080055 private LocalBluetoothProfileManager mProfileManager;
Fan Zhangb5bf7392018-07-25 13:36:33 -070056 @Mock
James Lemieuxec3ad9e2018-11-28 17:49:14 -080057 private BluetoothDevice mDevice;
Fan Zhangb5bf7392018-07-25 13:36:33 -070058 @Mock
James Lemieuxec3ad9e2018-11-28 17:49:14 -080059 private BluetoothA2dp mBluetoothA2dp;
60 private BluetoothProfile.ServiceListener mServiceListener;
Antony Sargent374d2592017-04-20 11:23:34 -070061
James Lemieuxec3ad9e2018-11-28 17:49:14 -080062 private A2dpProfile mProfile;
timhypeng45a477e2018-07-31 15:32:59 +080063 private ShadowBluetoothAdapter mShadowBluetoothAdapter;
Antony Sargent374d2592017-04-20 11:23:34 -070064
65 @Before
66 public void setUp() {
67 MockitoAnnotations.initMocks(this);
timhypeng45a477e2018-07-31 15:32:59 +080068 mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
69 mProfile = new A2dpProfile(mContext, mDeviceManager, mProfileManager);
70 mServiceListener = mShadowBluetoothAdapter.getServiceListener();
Antony Sargent374d2592017-04-20 11:23:34 -070071 mServiceListener.onServiceConnected(BluetoothProfile.A2DP, mBluetoothA2dp);
Rahul Sabnis35bc5b52020-02-25 22:28:00 +000072 when(mBluetoothA2dp.getActiveDevice()).thenReturn(mDevice);
Antony Sargent374d2592017-04-20 11:23:34 -070073 }
74
75 @Test
76 public void supportsHighQualityAudio() {
Rahul Sabnis35bc5b52020-02-25 22:28:00 +000077 when(mBluetoothA2dp.isOptionalCodecsSupported(mDevice)).thenReturn(
Antony Sargent374d2592017-04-20 11:23:34 -070078 BluetoothA2dp.OPTIONAL_CODECS_SUPPORTED);
79 assertThat(mProfile.supportsHighQualityAudio(mDevice)).isTrue();
80
Rahul Sabnis35bc5b52020-02-25 22:28:00 +000081 when(mBluetoothA2dp.isOptionalCodecsSupported(mDevice)).thenReturn(
Antony Sargent374d2592017-04-20 11:23:34 -070082 BluetoothA2dp.OPTIONAL_CODECS_NOT_SUPPORTED);
83 assertThat(mProfile.supportsHighQualityAudio(mDevice)).isFalse();
84
Rahul Sabnis35bc5b52020-02-25 22:28:00 +000085 when(mBluetoothA2dp.isOptionalCodecsSupported(mDevice)).thenReturn(
Antony Sargent374d2592017-04-20 11:23:34 -070086 BluetoothA2dp.OPTIONAL_CODECS_SUPPORT_UNKNOWN);
87 assertThat(mProfile.supportsHighQualityAudio(mDevice)).isFalse();
88 }
89
90 @Test
91 public void isHighQualityAudioEnabled() {
Rahul Sabnis35bc5b52020-02-25 22:28:00 +000092 when(mBluetoothA2dp.isOptionalCodecsEnabled(mDevice)).thenReturn(
Antony Sargent374d2592017-04-20 11:23:34 -070093 BluetoothA2dp.OPTIONAL_CODECS_PREF_ENABLED);
94 assertThat(mProfile.isHighQualityAudioEnabled(mDevice)).isTrue();
95
Rahul Sabnis35bc5b52020-02-25 22:28:00 +000096 when(mBluetoothA2dp.isOptionalCodecsEnabled(mDevice)).thenReturn(
Antony Sargent374d2592017-04-20 11:23:34 -070097 BluetoothA2dp.OPTIONAL_CODECS_PREF_DISABLED);
98 assertThat(mProfile.isHighQualityAudioEnabled(mDevice)).isFalse();
99
100 // If we don't have a stored pref for whether optional codecs should be enabled or not,
101 // then isHighQualityAudioEnabled() should return true or false based on whether optional
102 // codecs are supported. If the device is connected then we should ask it directly, but if
103 // the device isn't connected then rely on the stored pref about such support.
Rahul Sabnis35bc5b52020-02-25 22:28:00 +0000104 when(mBluetoothA2dp.isOptionalCodecsEnabled(mDevice)).thenReturn(
Antony Sargent374d2592017-04-20 11:23:34 -0700105 BluetoothA2dp.OPTIONAL_CODECS_PREF_UNKNOWN);
106 when(mBluetoothA2dp.getConnectionState(any())).thenReturn(
107 BluetoothProfile.STATE_DISCONNECTED);
108
Rahul Sabnis35bc5b52020-02-25 22:28:00 +0000109 when(mBluetoothA2dp.isOptionalCodecsSupported(mDevice)).thenReturn(
Antony Sargent374d2592017-04-20 11:23:34 -0700110 BluetoothA2dp.OPTIONAL_CODECS_NOT_SUPPORTED);
111 assertThat(mProfile.isHighQualityAudioEnabled(mDevice)).isFalse();
112
Rahul Sabnis35bc5b52020-02-25 22:28:00 +0000113 when(mBluetoothA2dp.isOptionalCodecsSupported(mDevice)).thenReturn(
Antony Sargent374d2592017-04-20 11:23:34 -0700114 BluetoothA2dp.OPTIONAL_CODECS_SUPPORTED);
115 assertThat(mProfile.isHighQualityAudioEnabled(mDevice)).isTrue();
116
117 when(mBluetoothA2dp.getConnectionState(any())).thenReturn(
118 BluetoothProfile.STATE_CONNECTED);
119 BluetoothCodecStatus status = mock(BluetoothCodecStatus.class);
hjchangliaobd8c8fb2018-04-26 15:12:11 +0800120 when(mBluetoothA2dp.getCodecStatus(mDevice)).thenReturn(status);
Antony Sargent374d2592017-04-20 11:23:34 -0700121 BluetoothCodecConfig config = mock(BluetoothCodecConfig.class);
122 when(status.getCodecConfig()).thenReturn(config);
123 when(config.isMandatoryCodec()).thenReturn(false);
124 assertThat(mProfile.isHighQualityAudioEnabled(mDevice)).isTrue();
125 when(config.isMandatoryCodec()).thenReturn(true);
126 assertThat(mProfile.isHighQualityAudioEnabled(mDevice)).isFalse();
127 }
128
129 // Strings to use in fake resource lookups.
130 private static String KNOWN_CODEC_LABEL = "Use high quality audio: %1$s";
131 private static String UNKNOWN_CODEC_LABEL = "Use high quality audio";
Justin Klaassen9acc02e2017-09-04 17:00:16 -0700132 private static String[] CODEC_NAMES =
Fan Zhangb5bf7392018-07-25 13:36:33 -0700133 new String[]{"Default", "SBC", "AAC", "aptX", "aptX HD", "LDAC"};
Antony Sargent374d2592017-04-20 11:23:34 -0700134
135 /**
136 * Helper for setting up several tests of getHighQualityAudioOptionLabel
137 */
138 private void setupLabelTest() {
139 // SettingsLib doesn't have string resource lookup working for robotests, so fake our own
140 // string loading.
141 when(mContext.getString(eq(R.string.bluetooth_profile_a2dp_high_quality),
142 any(String.class))).thenAnswer((invocation) -> {
143 return String.format(KNOWN_CODEC_LABEL, invocation.getArguments()[1]);
144 });
145 when(mContext.getString(eq(R.string.bluetooth_profile_a2dp_high_quality_unknown_codec)))
146 .thenReturn(UNKNOWN_CODEC_LABEL);
147
Justin Klaassen9acc02e2017-09-04 17:00:16 -0700148 final Resources res = mock(Resources.class);
149 when(mContext.getResources()).thenReturn(res);
150 when(res.getStringArray(eq(R.array.bluetooth_a2dp_codec_titles)))
151 .thenReturn(CODEC_NAMES);
152
Antony Sargent374d2592017-04-20 11:23:34 -0700153 // Most tests want to simulate optional codecs being supported by the device, so do that
154 // by default here.
Rahul Sabnis35bc5b52020-02-25 22:28:00 +0000155 when(mBluetoothA2dp.isOptionalCodecsSupported(any())).thenReturn(
Antony Sargent374d2592017-04-20 11:23:34 -0700156 BluetoothA2dp.OPTIONAL_CODECS_SUPPORTED);
157 }
158
159 @Test
160 public void getLableCodecsNotSupported() {
161 setupLabelTest();
Rahul Sabnis35bc5b52020-02-25 22:28:00 +0000162 when(mBluetoothA2dp.isOptionalCodecsSupported(any())).thenReturn(
Antony Sargent374d2592017-04-20 11:23:34 -0700163 BluetoothA2dp.OPTIONAL_CODECS_NOT_SUPPORTED);
164 assertThat(mProfile.getHighQualityAudioOptionLabel(mDevice)).isEqualTo(UNKNOWN_CODEC_LABEL);
165 }
166
167 @Test
168 public void getLabelDeviceDisconnected() {
169 setupLabelTest();
170 when(mBluetoothA2dp.getConnectionState(any())).thenReturn(
171 BluetoothProfile.STATE_DISCONNECTED);
172 assertThat(mProfile.getHighQualityAudioOptionLabel(mDevice)).isEqualTo(UNKNOWN_CODEC_LABEL);
173 }
174
175 @Test
176 public void getLabelDeviceConnectedButNotHighQualityCodec() {
177 setupLabelTest();
178 when(mBluetoothA2dp.getConnectionState(any())).thenReturn(
179 BluetoothProfile.STATE_CONNECTED);
180 BluetoothCodecStatus status = mock(BluetoothCodecStatus.class);
181 BluetoothCodecConfig config = mock(BluetoothCodecConfig.class);
182 BluetoothCodecConfig[] configs = {config};
hjchangliaobd8c8fb2018-04-26 15:12:11 +0800183 when(mBluetoothA2dp.getCodecStatus(mDevice)).thenReturn(status);
Antony Sargent374d2592017-04-20 11:23:34 -0700184 when(status.getCodecsSelectableCapabilities()).thenReturn(configs);
185
186 when(config.isMandatoryCodec()).thenReturn(true);
187 assertThat(mProfile.getHighQualityAudioOptionLabel(mDevice)).isEqualTo(UNKNOWN_CODEC_LABEL);
188 }
189
190 @Test
191 public void getLabelDeviceConnectedWithHighQualityCodec() {
192 setupLabelTest();
193 when(mBluetoothA2dp.getConnectionState(any())).thenReturn(
194 BluetoothProfile.STATE_CONNECTED);
195 BluetoothCodecStatus status = mock(BluetoothCodecStatus.class);
196 BluetoothCodecConfig config = mock(BluetoothCodecConfig.class);
197 BluetoothCodecConfig[] configs = {config};
hjchangliaobd8c8fb2018-04-26 15:12:11 +0800198 when(mBluetoothA2dp.getCodecStatus(mDevice)).thenReturn(status);
Antony Sargent374d2592017-04-20 11:23:34 -0700199 when(status.getCodecsSelectableCapabilities()).thenReturn(configs);
200
201 when(config.isMandatoryCodec()).thenReturn(false);
Justin Klaassen9acc02e2017-09-04 17:00:16 -0700202 when(config.getCodecType()).thenReturn(4);
203 when(config.getCodecName()).thenReturn("LDAC");
Antony Sargent374d2592017-04-20 11:23:34 -0700204 assertThat(mProfile.getHighQualityAudioOptionLabel(mDevice)).isEqualTo(
205 String.format(KNOWN_CODEC_LABEL, config.getCodecName()));
206 }
hughchen531382f2020-02-27 11:32:39 +0800207
208 @Test
209 public void setActiveDevice_returnTrue() {
210 assertThat(mProfile.setActiveDevice(null)).isTrue();
211 assertThat(mProfile.setActiveDevice(mDevice)).isTrue();
212 }
Antony Sargent374d2592017-04-20 11:23:34 -0700213}