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; |
| 19 | import static org.mockito.Matchers.any; |
| 20 | import static org.mockito.Matchers.eq; |
| 21 | import static org.mockito.Mockito.doAnswer; |
| 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; |
| 26 | import android.bluetooth.BluetoothCodecConfig; |
| 27 | import android.bluetooth.BluetoothCodecStatus; |
| 28 | import android.bluetooth.BluetoothDevice; |
| 29 | import android.bluetooth.BluetoothProfile; |
| 30 | import android.content.Context; |
Justin Klaassen | 9acc02e | 2017-09-04 17:00:16 -0700 | [diff] [blame] | 31 | import android.content.res.Resources; |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 32 | |
| 33 | import com.android.settingslib.R; |
Doris Ling | f16ea68 | 2017-09-15 14:44:34 -0700 | [diff] [blame] | 34 | import com.android.settingslib.wrapper.BluetoothA2dpWrapper; |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 35 | |
| 36 | import org.junit.Before; |
| 37 | import org.junit.Test; |
| 38 | import org.junit.runner.RunWith; |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 39 | import org.mockito.Mock; |
| 40 | import org.mockito.MockitoAnnotations; |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 41 | import org.robolectric.RobolectricTestRunner; |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 42 | |
| 43 | @RunWith(RobolectricTestRunner.class) |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 44 | public 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 Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 70 | mServiceListener.onServiceConnected(BluetoothProfile.A2DP, mBluetoothA2dp); |
Doris Ling | f16ea68 | 2017-09-15 14:44:34 -0700 | [diff] [blame] | 71 | mProfile.setBluetoothA2dpWrapper(mBluetoothA2dpWrapper); |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 72 | } |
| 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 Radoslavov | 502af21 | 2018-01-03 19:38:39 -0800 | [diff] [blame] | 119 | when(mBluetoothA2dpWrapper.getCodecStatus(mDevice)).thenReturn(status); |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 120 | 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 Klaassen | 9acc02e | 2017-09-04 17:00:16 -0700 | [diff] [blame] | 131 | private static String[] CODEC_NAMES = |
| 132 | new String[] { "Default", "SBC", "AAC", "aptX", "aptX HD", "LDAC" }; |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 133 | |
| 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 Klaassen | 9acc02e | 2017-09-04 17:00:16 -0700 | [diff] [blame] | 147 | 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 Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 152 | // 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 Radoslavov | 502af21 | 2018-01-03 19:38:39 -0800 | [diff] [blame] | 182 | when(mBluetoothA2dpWrapper.getCodecStatus(mDevice)).thenReturn(status); |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 183 | 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 Radoslavov | 502af21 | 2018-01-03 19:38:39 -0800 | [diff] [blame] | 197 | when(mBluetoothA2dpWrapper.getCodecStatus(mDevice)).thenReturn(status); |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 198 | when(status.getCodecsSelectableCapabilities()).thenReturn(configs); |
| 199 | |
| 200 | when(config.isMandatoryCodec()).thenReturn(false); |
Justin Klaassen | 9acc02e | 2017-09-04 17:00:16 -0700 | [diff] [blame] | 201 | when(config.getCodecType()).thenReturn(4); |
| 202 | when(config.getCodecName()).thenReturn("LDAC"); |
Antony Sargent | 374d259 | 2017-04-20 11:23:34 -0700 | [diff] [blame] | 203 | assertThat(mProfile.getHighQualityAudioOptionLabel(mDevice)).isEqualTo( |
| 204 | String.format(KNOWN_CODEC_LABEL, config.getCodecName())); |
| 205 | } |
| 206 | } |