blob: ece0d51e2b66447fcea9a2cc935a70e96a557290 [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
18import android.bluetooth.BluetoothA2dp;
19import android.bluetooth.BluetoothCodecConfig;
20import android.bluetooth.BluetoothCodecStatus;
21import android.bluetooth.BluetoothDevice;
22import android.bluetooth.BluetoothProfile;
23import android.content.Context;
Justin Klaassen9acc02e2017-09-04 17:00:16 -070024import android.content.res.Resources;
Antony Sargent374d2592017-04-20 11:23:34 -070025
26import com.android.settingslib.R;
27import com.android.settingslib.TestConfig;
Doris Lingf16ea682017-09-15 14:44:34 -070028import com.android.settingslib.wrapper.BluetoothA2dpWrapper;
Antony Sargent374d2592017-04-20 11:23:34 -070029
30import org.junit.Before;
31import org.junit.Test;
32import org.junit.runner.RunWith;
33
34import org.mockito.Mock;
35import org.mockito.MockitoAnnotations;
36import org.mockito.invocation.InvocationOnMock;
37import org.mockito.stubbing.Answer;
38import org.robolectric.RobolectricTestRunner;
39import org.robolectric.annotation.Config;
40
41import static com.google.common.truth.Truth.assertThat;
42
43import static org.mockito.Matchers.any;
44import static org.mockito.Matchers.eq;
45import static org.mockito.Mockito.doAnswer;
46import static org.mockito.Mockito.mock;
47import static org.mockito.Mockito.when;
48
49@RunWith(RobolectricTestRunner.class)
50@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
51public class A2dpProfileTest {
52
53 @Mock Context mContext;
54 @Mock LocalBluetoothAdapter mAdapter;
55 @Mock CachedBluetoothDeviceManager mDeviceManager;
56 @Mock LocalBluetoothProfileManager mProfileManager;
57 @Mock BluetoothDevice mDevice;
58 @Mock BluetoothA2dp mBluetoothA2dp;
59 @Mock BluetoothA2dpWrapper mBluetoothA2dpWrapper;
60 BluetoothProfile.ServiceListener mServiceListener;
61
62 A2dpProfile mProfile;
63
64 @Before
65 public void setUp() {
66 MockitoAnnotations.initMocks(this);
67
68 // Capture the A2dpServiceListener our A2dpProfile will pass during its constructor, so that
69 // we can call its onServiceConnected method and get it to use our mock BluetoothA2dp
70 // object.
71 doAnswer((invocation) -> {
72 mServiceListener = (BluetoothProfile.ServiceListener) invocation.getArguments()[1];
73 return null;
74 }).when(mAdapter).getProfileProxy(any(Context.class), any(), eq(BluetoothProfile.A2DP));
75
76 mProfile = new A2dpProfile(mContext, mAdapter, mDeviceManager, mProfileManager);
Antony Sargent374d2592017-04-20 11:23:34 -070077 mServiceListener.onServiceConnected(BluetoothProfile.A2DP, mBluetoothA2dp);
Doris Lingf16ea682017-09-15 14:44:34 -070078 mProfile.setBluetoothA2dpWrapper(mBluetoothA2dpWrapper);
Antony Sargent374d2592017-04-20 11:23:34 -070079 }
80
81 @Test
82 public void supportsHighQualityAudio() {
83 when(mBluetoothA2dpWrapper.supportsOptionalCodecs(any())).thenReturn(
84 BluetoothA2dp.OPTIONAL_CODECS_SUPPORTED);
85 assertThat(mProfile.supportsHighQualityAudio(mDevice)).isTrue();
86
87 when(mBluetoothA2dpWrapper.supportsOptionalCodecs(any())).thenReturn(
88 BluetoothA2dp.OPTIONAL_CODECS_NOT_SUPPORTED);
89 assertThat(mProfile.supportsHighQualityAudio(mDevice)).isFalse();
90
91 when(mBluetoothA2dpWrapper.supportsOptionalCodecs(any())).thenReturn(
92 BluetoothA2dp.OPTIONAL_CODECS_SUPPORT_UNKNOWN);
93 assertThat(mProfile.supportsHighQualityAudio(mDevice)).isFalse();
94 }
95
96 @Test
97 public void isHighQualityAudioEnabled() {
98 when(mBluetoothA2dpWrapper.getOptionalCodecsEnabled(any())).thenReturn(
99 BluetoothA2dp.OPTIONAL_CODECS_PREF_ENABLED);
100 assertThat(mProfile.isHighQualityAudioEnabled(mDevice)).isTrue();
101
102 when(mBluetoothA2dpWrapper.getOptionalCodecsEnabled(any())).thenReturn(
103 BluetoothA2dp.OPTIONAL_CODECS_PREF_DISABLED);
104 assertThat(mProfile.isHighQualityAudioEnabled(mDevice)).isFalse();
105
106 // If we don't have a stored pref for whether optional codecs should be enabled or not,
107 // then isHighQualityAudioEnabled() should return true or false based on whether optional
108 // codecs are supported. If the device is connected then we should ask it directly, but if
109 // the device isn't connected then rely on the stored pref about such support.
110 when(mBluetoothA2dpWrapper.getOptionalCodecsEnabled(any())).thenReturn(
111 BluetoothA2dp.OPTIONAL_CODECS_PREF_UNKNOWN);
112 when(mBluetoothA2dp.getConnectionState(any())).thenReturn(
113 BluetoothProfile.STATE_DISCONNECTED);
114
115 when(mBluetoothA2dpWrapper.supportsOptionalCodecs(any())).thenReturn(
116 BluetoothA2dp.OPTIONAL_CODECS_NOT_SUPPORTED);
117 assertThat(mProfile.isHighQualityAudioEnabled(mDevice)).isFalse();
118
119 when(mBluetoothA2dpWrapper.supportsOptionalCodecs(any())).thenReturn(
120 BluetoothA2dp.OPTIONAL_CODECS_SUPPORTED);
121 assertThat(mProfile.isHighQualityAudioEnabled(mDevice)).isTrue();
122
123 when(mBluetoothA2dp.getConnectionState(any())).thenReturn(
124 BluetoothProfile.STATE_CONNECTED);
125 BluetoothCodecStatus status = mock(BluetoothCodecStatus.class);
126 when(mBluetoothA2dpWrapper.getCodecStatus()).thenReturn(status);
127 BluetoothCodecConfig config = mock(BluetoothCodecConfig.class);
128 when(status.getCodecConfig()).thenReturn(config);
129 when(config.isMandatoryCodec()).thenReturn(false);
130 assertThat(mProfile.isHighQualityAudioEnabled(mDevice)).isTrue();
131 when(config.isMandatoryCodec()).thenReturn(true);
132 assertThat(mProfile.isHighQualityAudioEnabled(mDevice)).isFalse();
133 }
134
135 // Strings to use in fake resource lookups.
136 private static String KNOWN_CODEC_LABEL = "Use high quality audio: %1$s";
137 private static String UNKNOWN_CODEC_LABEL = "Use high quality audio";
Justin Klaassen9acc02e2017-09-04 17:00:16 -0700138 private static String[] CODEC_NAMES =
139 new String[] { "Default", "SBC", "AAC", "aptX", "aptX HD", "LDAC" };
Antony Sargent374d2592017-04-20 11:23:34 -0700140
141 /**
142 * Helper for setting up several tests of getHighQualityAudioOptionLabel
143 */
144 private void setupLabelTest() {
145 // SettingsLib doesn't have string resource lookup working for robotests, so fake our own
146 // string loading.
147 when(mContext.getString(eq(R.string.bluetooth_profile_a2dp_high_quality),
148 any(String.class))).thenAnswer((invocation) -> {
149 return String.format(KNOWN_CODEC_LABEL, invocation.getArguments()[1]);
150 });
151 when(mContext.getString(eq(R.string.bluetooth_profile_a2dp_high_quality_unknown_codec)))
152 .thenReturn(UNKNOWN_CODEC_LABEL);
153
Justin Klaassen9acc02e2017-09-04 17:00:16 -0700154 final Resources res = mock(Resources.class);
155 when(mContext.getResources()).thenReturn(res);
156 when(res.getStringArray(eq(R.array.bluetooth_a2dp_codec_titles)))
157 .thenReturn(CODEC_NAMES);
158
Antony Sargent374d2592017-04-20 11:23:34 -0700159 // Most tests want to simulate optional codecs being supported by the device, so do that
160 // by default here.
161 when(mBluetoothA2dpWrapper.supportsOptionalCodecs(any())).thenReturn(
162 BluetoothA2dp.OPTIONAL_CODECS_SUPPORTED);
163 }
164
165 @Test
166 public void getLableCodecsNotSupported() {
167 setupLabelTest();
168 when(mBluetoothA2dpWrapper.supportsOptionalCodecs(any())).thenReturn(
169 BluetoothA2dp.OPTIONAL_CODECS_NOT_SUPPORTED);
170 assertThat(mProfile.getHighQualityAudioOptionLabel(mDevice)).isEqualTo(UNKNOWN_CODEC_LABEL);
171 }
172
173 @Test
174 public void getLabelDeviceDisconnected() {
175 setupLabelTest();
176 when(mBluetoothA2dp.getConnectionState(any())).thenReturn(
177 BluetoothProfile.STATE_DISCONNECTED);
178 assertThat(mProfile.getHighQualityAudioOptionLabel(mDevice)).isEqualTo(UNKNOWN_CODEC_LABEL);
179 }
180
181 @Test
182 public void getLabelDeviceConnectedButNotHighQualityCodec() {
183 setupLabelTest();
184 when(mBluetoothA2dp.getConnectionState(any())).thenReturn(
185 BluetoothProfile.STATE_CONNECTED);
186 BluetoothCodecStatus status = mock(BluetoothCodecStatus.class);
187 BluetoothCodecConfig config = mock(BluetoothCodecConfig.class);
188 BluetoothCodecConfig[] configs = {config};
189 when(mBluetoothA2dpWrapper.getCodecStatus()).thenReturn(status);
190 when(status.getCodecsSelectableCapabilities()).thenReturn(configs);
191
192 when(config.isMandatoryCodec()).thenReturn(true);
193 assertThat(mProfile.getHighQualityAudioOptionLabel(mDevice)).isEqualTo(UNKNOWN_CODEC_LABEL);
194 }
195
196 @Test
197 public void getLabelDeviceConnectedWithHighQualityCodec() {
198 setupLabelTest();
199 when(mBluetoothA2dp.getConnectionState(any())).thenReturn(
200 BluetoothProfile.STATE_CONNECTED);
201 BluetoothCodecStatus status = mock(BluetoothCodecStatus.class);
202 BluetoothCodecConfig config = mock(BluetoothCodecConfig.class);
203 BluetoothCodecConfig[] configs = {config};
204 when(mBluetoothA2dpWrapper.getCodecStatus()).thenReturn(status);
205 when(status.getCodecsSelectableCapabilities()).thenReturn(configs);
206
207 when(config.isMandatoryCodec()).thenReturn(false);
Justin Klaassen9acc02e2017-09-04 17:00:16 -0700208 when(config.getCodecType()).thenReturn(4);
209 when(config.getCodecName()).thenReturn("LDAC");
Antony Sargent374d2592017-04-20 11:23:34 -0700210 assertThat(mProfile.getHighQualityAudioOptionLabel(mDevice)).isEqualTo(
211 String.format(KNOWN_CODEC_LABEL, config.getCodecName()));
212 }
213}