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