Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package com.android.settingslib.bluetooth; |
| 17 | |
James Lemieux | 5c50dc1 | 2018-02-12 01:30:32 -0800 | [diff] [blame] | 18 | import static com.google.common.truth.Truth.assertThat; |
Fan Zhang | b5bf739 | 2018-07-25 13:36:33 -0700 | [diff] [blame] | 19 | |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 20 | import static org.mockito.ArgumentMatchers.any; |
| 21 | import static org.mockito.ArgumentMatchers.eq; |
James Lemieux | 5c50dc1 | 2018-02-12 01:30:32 -0800 | [diff] [blame] | 22 | import static org.mockito.Mockito.mock; |
| 23 | import static org.mockito.Mockito.when; |
| 24 | |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 25 | import android.bluetooth.BluetoothA2dp; |
timhypeng | 45a477e | 2018-07-31 15:32:59 +0800 | [diff] [blame] | 26 | import android.bluetooth.BluetoothAdapter; |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 27 | import android.bluetooth.BluetoothCodecConfig; |
| 28 | import android.bluetooth.BluetoothCodecStatus; |
| 29 | import android.bluetooth.BluetoothDevice; |
| 30 | import android.bluetooth.BluetoothProfile; |
| 31 | import android.content.Context; |
Justin Klaassen | 9acc02e | 2017-09-04 17:00:16 -0700 | [diff] [blame] | 32 | import android.content.res.Resources; |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 33 | |
| 34 | import com.android.settingslib.R; |
timhypeng | 45a477e | 2018-07-31 15:32:59 +0800 | [diff] [blame] | 35 | import com.android.settingslib.testutils.shadow.ShadowBluetoothAdapter; |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 36 | |
| 37 | import org.junit.Before; |
| 38 | import org.junit.Test; |
| 39 | import org.junit.runner.RunWith; |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 40 | import org.mockito.Mock; |
| 41 | import org.mockito.MockitoAnnotations; |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 42 | import org.robolectric.RobolectricTestRunner; |
timhypeng | 45a477e | 2018-07-31 15:32:59 +0800 | [diff] [blame] | 43 | import org.robolectric.annotation.Config; |
| 44 | import org.robolectric.shadow.api.Shadow; |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 45 | |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 46 | @RunWith(RobolectricTestRunner.class) |
timhypeng | 45a477e | 2018-07-31 15:32:59 +0800 | [diff] [blame] | 47 | @Config(shadows = {ShadowBluetoothAdapter.class}) |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 48 | public class A2dpProfileTest { |
| 49 | |
Fan Zhang | b5bf739 | 2018-07-25 13:36:33 -0700 | [diff] [blame] | 50 | @Mock |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 51 | private Context mContext; |
Fan Zhang | b5bf739 | 2018-07-25 13:36:33 -0700 | [diff] [blame] | 52 | @Mock |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 53 | private CachedBluetoothDeviceManager mDeviceManager; |
Fan Zhang | b5bf739 | 2018-07-25 13:36:33 -0700 | [diff] [blame] | 54 | @Mock |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 55 | private LocalBluetoothProfileManager mProfileManager; |
Fan Zhang | b5bf739 | 2018-07-25 13:36:33 -0700 | [diff] [blame] | 56 | @Mock |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 57 | private BluetoothDevice mDevice; |
Fan Zhang | b5bf739 | 2018-07-25 13:36:33 -0700 | [diff] [blame] | 58 | @Mock |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 59 | private BluetoothA2dp mBluetoothA2dp; |
| 60 | private BluetoothProfile.ServiceListener mServiceListener; |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 61 | |
James Lemieux | ec3ad9e | 2018-11-28 17:49:14 -0800 | [diff] [blame] | 62 | private A2dpProfile mProfile; |
timhypeng | 45a477e | 2018-07-31 15:32:59 +0800 | [diff] [blame] | 63 | private ShadowBluetoothAdapter mShadowBluetoothAdapter; |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 64 | |
| 65 | @Before |
| 66 | public void setUp() { |
| 67 | MockitoAnnotations.initMocks(this); |
timhypeng | 45a477e | 2018-07-31 15:32:59 +0800 | [diff] [blame] | 68 | mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter()); |
| 69 | mProfile = new A2dpProfile(mContext, mDeviceManager, mProfileManager); |
| 70 | mServiceListener = mShadowBluetoothAdapter.getServiceListener(); |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 71 | mServiceListener.onServiceConnected(BluetoothProfile.A2DP, mBluetoothA2dp); |
Rahul Sabnis | 35bc5b5 | 2020-02-25 22:28:00 +0000 | [diff] [blame] | 72 | when(mBluetoothA2dp.getActiveDevice()).thenReturn(mDevice); |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Test |
| 76 | public void supportsHighQualityAudio() { |
Rahul Sabnis | 35bc5b5 | 2020-02-25 22:28:00 +0000 | [diff] [blame] | 77 | when(mBluetoothA2dp.isOptionalCodecsSupported(mDevice)).thenReturn( |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 78 | BluetoothA2dp.OPTIONAL_CODECS_SUPPORTED); |
| 79 | assertThat(mProfile.supportsHighQualityAudio(mDevice)).isTrue(); |
| 80 | |
Rahul Sabnis | 35bc5b5 | 2020-02-25 22:28:00 +0000 | [diff] [blame] | 81 | when(mBluetoothA2dp.isOptionalCodecsSupported(mDevice)).thenReturn( |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 82 | BluetoothA2dp.OPTIONAL_CODECS_NOT_SUPPORTED); |
| 83 | assertThat(mProfile.supportsHighQualityAudio(mDevice)).isFalse(); |
| 84 | |
Rahul Sabnis | 35bc5b5 | 2020-02-25 22:28:00 +0000 | [diff] [blame] | 85 | when(mBluetoothA2dp.isOptionalCodecsSupported(mDevice)).thenReturn( |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 86 | BluetoothA2dp.OPTIONAL_CODECS_SUPPORT_UNKNOWN); |
| 87 | assertThat(mProfile.supportsHighQualityAudio(mDevice)).isFalse(); |
| 88 | } |
| 89 | |
| 90 | @Test |
| 91 | public void isHighQualityAudioEnabled() { |
Rahul Sabnis | 35bc5b5 | 2020-02-25 22:28:00 +0000 | [diff] [blame] | 92 | when(mBluetoothA2dp.isOptionalCodecsEnabled(mDevice)).thenReturn( |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 93 | BluetoothA2dp.OPTIONAL_CODECS_PREF_ENABLED); |
| 94 | assertThat(mProfile.isHighQualityAudioEnabled(mDevice)).isTrue(); |
| 95 | |
Rahul Sabnis | 35bc5b5 | 2020-02-25 22:28:00 +0000 | [diff] [blame] | 96 | when(mBluetoothA2dp.isOptionalCodecsEnabled(mDevice)).thenReturn( |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 97 | 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 Sabnis | 35bc5b5 | 2020-02-25 22:28:00 +0000 | [diff] [blame] | 104 | when(mBluetoothA2dp.isOptionalCodecsEnabled(mDevice)).thenReturn( |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 105 | BluetoothA2dp.OPTIONAL_CODECS_PREF_UNKNOWN); |
| 106 | when(mBluetoothA2dp.getConnectionState(any())).thenReturn( |
| 107 | BluetoothProfile.STATE_DISCONNECTED); |
| 108 | |
Rahul Sabnis | 35bc5b5 | 2020-02-25 22:28:00 +0000 | [diff] [blame] | 109 | when(mBluetoothA2dp.isOptionalCodecsSupported(mDevice)).thenReturn( |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 110 | BluetoothA2dp.OPTIONAL_CODECS_NOT_SUPPORTED); |
| 111 | assertThat(mProfile.isHighQualityAudioEnabled(mDevice)).isFalse(); |
| 112 | |
Rahul Sabnis | 35bc5b5 | 2020-02-25 22:28:00 +0000 | [diff] [blame] | 113 | when(mBluetoothA2dp.isOptionalCodecsSupported(mDevice)).thenReturn( |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 114 | 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); |
hjchangliao | bd8c8fb | 2018-04-26 15:12:11 +0800 | [diff] [blame] | 120 | when(mBluetoothA2dp.getCodecStatus(mDevice)).thenReturn(status); |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 121 | 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 Klaassen | 9acc02e | 2017-09-04 17:00:16 -0700 | [diff] [blame] | 132 | private static String[] CODEC_NAMES = |
Fan Zhang | b5bf739 | 2018-07-25 13:36:33 -0700 | [diff] [blame] | 133 | new String[]{"Default", "SBC", "AAC", "aptX", "aptX HD", "LDAC"}; |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 134 | |
| 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 Klaassen | 9acc02e | 2017-09-04 17:00:16 -0700 | [diff] [blame] | 148 | 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 Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 153 | // Most tests want to simulate optional codecs being supported by the device, so do that |
| 154 | // by default here. |
Rahul Sabnis | 35bc5b5 | 2020-02-25 22:28:00 +0000 | [diff] [blame] | 155 | when(mBluetoothA2dp.isOptionalCodecsSupported(any())).thenReturn( |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 156 | BluetoothA2dp.OPTIONAL_CODECS_SUPPORTED); |
| 157 | } |
| 158 | |
| 159 | @Test |
| 160 | public void getLableCodecsNotSupported() { |
| 161 | setupLabelTest(); |
Rahul Sabnis | 35bc5b5 | 2020-02-25 22:28:00 +0000 | [diff] [blame] | 162 | when(mBluetoothA2dp.isOptionalCodecsSupported(any())).thenReturn( |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 163 | 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}; |
hjchangliao | bd8c8fb | 2018-04-26 15:12:11 +0800 | [diff] [blame] | 183 | when(mBluetoothA2dp.getCodecStatus(mDevice)).thenReturn(status); |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 184 | 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}; |
hjchangliao | bd8c8fb | 2018-04-26 15:12:11 +0800 | [diff] [blame] | 198 | when(mBluetoothA2dp.getCodecStatus(mDevice)).thenReturn(status); |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 199 | when(status.getCodecsSelectableCapabilities()).thenReturn(configs); |
| 200 | |
| 201 | when(config.isMandatoryCodec()).thenReturn(false); |
Justin Klaassen | 9acc02e | 2017-09-04 17:00:16 -0700 | [diff] [blame] | 202 | when(config.getCodecType()).thenReturn(4); |
| 203 | when(config.getCodecName()).thenReturn("LDAC"); |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 204 | assertThat(mProfile.getHighQualityAudioOptionLabel(mDevice)).isEqualTo( |
| 205 | String.format(KNOWN_CODEC_LABEL, config.getCodecName())); |
| 206 | } |
hughchen | 531382f | 2020-02-27 11:32:39 +0800 | [diff] [blame] | 207 | |
| 208 | @Test |
| 209 | public void setActiveDevice_returnTrue() { |
| 210 | assertThat(mProfile.setActiveDevice(null)).isTrue(); |
| 211 | assertThat(mProfile.setActiveDevice(mDevice)).isTrue(); |
| 212 | } |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 213 | } |